QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#265499#7743. Grand Finaleucup-team1191#WA 77ms18748kbC++206.5kb2023-11-25 18:48:362023-11-25 18:48:37

Judging History

你现在查看的是最新测评结果

  • [2023-11-25 18:48:37]
  • 评测
  • 测评结果:WA
  • 用时:77ms
  • 内存:18748kb
  • [2023-11-25 18:48:36]
  • 提交

answer

/*
    author:  Maksim1744
    created: 25.11.2023 13:13:05
*/

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun)   fun
#define debugv(var)   var
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#define OSTREAM(...)    ;
#define OSTREAM0(...)   ;
#endif

const int N = 2510;

array<array<int, N / 2>, N> dp_full;
array<array<bool, N / 2>, N> dp_can;

const int inf = 1e9;

void test_case(int test) {
    int m, n;
    cin >> m >> n;
    string cur, s;
    cin >> cur >> s;
    // dp_full
    int maxc2 = (n + 1) / 2;
    {
        for (int i = 0; i <= n; ++i)
            for (int j = 0; j <= maxc2; ++j)
                dp_full[i][j] = inf;
        dp_full[0].fill(0);
        dp_full[1].fill(0);
        dp_full[1][0] = 1;
        for (int i = 0; i < n; ++i) {
            for (int c2 = 0; c2 <= maxc2; ++c2) {
                int c1 = dp_full[i][c2];
                if (c1 >= inf / 2) continue;
                // if last used 1
                {
                    bool last1 = (s[n - i - 1] == 'Q');
                    bool last2 = (s[n - i - 1] == 'B');
                    // show(i+1, max(0,c2-last2), max(1, c1 + 1 - last1));
                    dp_full[i + 1][max(0, c2 - last2)] = min(max(1, c1 + 1 - last1), dp_full[i + 1][max(0, c2 - last2)]);
                }
                // if last used 2
                if (i + 2 <= n) {
                    // second to last will be skipped
                    // bool plast1 = (s[n - i - 1] == 'Q');
                    // bool plast2 = (s[n - i - 1] == 'B');
                    bool plast1 = (s[n - i - 2] == 'Q');
                    bool plast2 = (s[n - i - 2] == 'B');
                    // show(i, c2, c1, plast1, plast2);
                    // show(i+2, max(1,c2-plast2), max(0, c1 - plast1));
                    dp_full[i + 2][max(1, c2 + 1 - plast2)] = min(max(0, c1 - plast1), dp_full[i + 2][max(1, c2 + 1 - plast2)]);
                }
            }
            for (int c2 = 1; c2 <= maxc2; ++c2)
                dp_full[i + 1][c2] = min(dp_full[i + 1][c2], dp_full[i + 1][c2 - 1]);
        }
    }
    // dp_can
    {
        for (int i = 0; i <= n; ++i)
            for (int j = 0; j <= maxc2; ++j)
                dp_can[i][j] = false;
        dp_can[0][0] = true;
        int have1 = count(cur.begin(), cur.end(), 'Q');
        int have2 = count(cur.begin(), cur.end(), 'B');
        for (int i = 0; i < n; ++i) {
            for (int c2 = 0; c2 <= maxc2; ++c2) {
                if (!dp_can[i][c2]) continue;
                assert(c2 * 2 <= i);
                int left1 = have1 - (i - c2 * 2);
                assert(left1 >= 0);
                int left2 = have2 - c2;
                assert(left2 >= 0);
                if (left1)
                    dp_can[i + 1][c2] = true;
                if (left2)
                    dp_can[min(n, i + 2)][c2 + 1] = true;
            }
            have1 += (s[i] == 'Q');
            have2 += (s[i] == 'B');
        }
    }

    debug {
        cerr << "dp_full:" << endl;
        for (int i = 0; i <= n; ++i)
            for (int j = 0; j <= maxc2; ++j)
                cerr << dp_full[i][j] << " \n"[j == maxc2];
        cerr << "dp_can:" << endl;
        for (int i = 0; i <= n; ++i)
            for (int j = 0; j <= maxc2; ++j)
                cerr << dp_can[i][j] << " \n"[j == maxc2];
    }

    // calc ans
    int ans = inf;
    {

        int have1 = count(cur.begin(), cur.end(), 'Q');
        int have2 = count(cur.begin(), cur.end(), 'B');
        for (int i = 0; i <= n; ++i) {
            for (int used_c2 = 0; used_c2 <= maxc2; ++used_c2) {
                if (!dp_can[i][used_c2]) continue;
                int need_size = cur.size() + used_c2;
                int left1 = have1;
                int left2 = have2 - used_c2;
                if (left1 >= dp_full[n - i][min(maxc2, left2)]) {
                    ans = min(ans, need_size);
                }
            }
            if (i != n) {
                have1 += (s[i] == 'Q');
                have2 += (s[i] == 'B');
            }
        }
    }
    if (ans == inf)
        cout << "IMPOSSIBLE\n";
    else
        cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int T;
    cin >> T;
    for (int test = 1; test <= T; ++test) {
        test_case(test);
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5428kb

input:

2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB

output:

3
IMPOSSIBLE

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 5500kb

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 77ms
memory: 18748kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

184
114
164
161
118
193
121
204
134
115
33
160
169

result:

wrong answer 2nd lines differ - expected: '372', found: '114'