QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#669584 | #7743. Grand Finale | test_algth | WA | 103ms | 53860kb | C++14 | 3.2kb | 2024-10-23 19:05:30 | 2024-10-23 19:05:31 |
Judging History
answer
#include <bits/stdc++.h>
inline int change(char a) {
if (a == 'Q') return 1;
if (a == 'B') return 2;
return 0;
}
inline void update(int& a, int b) {
a = std::min(a, std::max(0, b));
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int testCase = 1;
std::cin >> testCase;
while (testCase--) {
int N, M;
std::cin >> N >> M;
std::string S, T;
std::cin >> S >> T;
S = " " + S;
T = " " + T;
std::vector <int> A(M + 10, 0), prefOne(M + 10, 0), prefTwo(M + 10, 0), prefO(M + 10, 0);
for (int i = 1; i <= M; ++i) {
A[i] = change(T[i]);
prefOne[i] = prefOne[i - 1] + (A[i] == 1);
prefTwo[i] = prefTwo[i - 1] + (A[i] == 2);
prefO[i] = prefO[i - 1] + (A[i] == 0);
}
for (int i = M + 1; i <= M + 2; ++i) {
prefO[i] = prefO[i - 1];
prefOne[i] = prefOne[i - 1];
prefTwo[i] = prefTwo[i - 1];
}
std::array <int, 3> initial = {0, 0, 0};
for (int i = 1; i <= N; ++i) {
++initial[change(S[i])];
}
std::vector <std::vector <int>> dp(M + 10, std::vector <int> (N + M + 50, 1e9)); //dp(i) : i~M, i has not been choosen
for (int i = 0; i <= N + M + 5; ++i) {
dp[M + 1][i] = dp[M + 2][i] = 0;
}
for (int i = M; i; --i) {
for (int j = 0; j <= N + i; ++j) {
if (A[i] == 0) {
update(dp[i][j], dp[i + 1][j] + 1);
if (j) {
update(dp[i][j], dp[i + 2][j - 1]);
}
}
if (A[i] == 1) {
update(dp[i][j], dp[i + 1][j]);
if (j) {
update(dp[i][j], dp[i + 2][j - 1] - 1);
}
}
if (A[i] == 2) {
update(dp[i][j], dp[i + 1][j + 1] + 1);
if (j) {
update(dp[i][j], dp[i + 2][j]);
}
}
}
}
auto calc =[&](int border, int cnts2) {
return border - 2 * (initial[2] + prefTwo[border] - cnts2);
};
auto getOne =[&](int border, int cnts2) {
return initial[1] + prefOne[border] - calc(border, cnts2);
};
std::vector <std::vector <int>> g(M + 10, std::vector <int> (M + N + 10, 0));// g : 1~i
g[0][initial[2]] = 1;
for (int i = 0; i <= M; ++i) {
for (int j = 0; j <= N + i; ++j) {
if (!g[i][j]) continue;
int one = getOne(i, j);
assert(one >= 0);
if (one > 0) {
if (A[i + 1] != 2) {
g[i + 1][j] = 1;
} else {
g[i + 1][j + 1] = 1;
}
}
if (j > 0) {
if (prefTwo[i + 2] - prefTwo[i] == 0) {
g[i + 2][j - 1] = 1;
} else {
g[i + 2][j - 1 + prefTwo[i + 2] - prefTwo[i]] = 1;
}
}
}
}
int best = 1e9;
for (int i = 0; i <= M; ++i) {
for (int j = 0; j <= N + i; ++j) {
if (!g[i][j]) continue;
int one = getOne(i, j);
if (one >= dp[i + 1][j]) {
best = std::min(best, j + one + prefO[i] + initial[0]);
}
}
}
if (best != 1e9)
std::cout << best << '\n';
else
std::cout << "IMPOSSIBLE\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 103ms
memory: 53860kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
184 372 314 161 118 534 IMPOSSIBLE 631 183 275 33 160 643
result:
wrong answer 3rd lines differ - expected: '316', found: '314'