QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121618 | #4273. Good Game | bashkort# | 3 | 6ms | 3904kb | C++20 | 2.4kb | 2023-07-08 15:46:28 | 2024-07-04 00:31:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
vector dp(n, vector<int>(n));
for (int l = n - 1; l >= 0; --l) {
for (int r = l + 1; r < n; ++r) {
if (r <= l + 2 && s[l] == s[r] && s[l] == s[l + r >> 1]) {
dp[l][r] = -1;
} else {
for (int mid = l + 1, cnt = 0; mid + 1 < r && ++cnt <= 100; ++mid) {
if (dp[l][mid] && dp[mid + 1][r]) {
dp[l][r] = mid;
break;
}
}
if (!dp[l][r] && s[l] == s[r]) {
for (int mid = l + 1, cnt = 0; mid < r && cnt < 100; ++mid) {
if (s[mid] == s[l] && (l + 1 > mid - 1 ? true : dp[l + 1][mid - 1]) && (mid + 1 > r - 1 ? true : dp[mid + 1][r - 1])) {
dp[l][r] = ~mid;
break;
}
cnt += s[mid] == s[l];
}
if (dp[l + 1][r - 1]) {
dp[l][r] = r;
}
}
}
}
}
if (!dp[0][n - 1]) {
cout << "-1\n";
return 0;
}
vector<pair<int, int>> ans;
auto dfs = [&](auto dfs, int l, int r, int del) -> int {
if (l > r) {
return 0;
}
if (l + 2 >= r) {
ans.push_back({l - del, r - l + 1});
return r - l + 1;
}
if (dp[l][r] >= 0) {
int mid = dp[l][r];
if (mid < r) {
del += dfs(dfs, l, mid, del);
dfs(dfs, mid + 1, r, del);
} else {
dfs(dfs, l + 1, r - 1, del);
ans.push_back({l - del, 2});
}
} else {
int mid = ~dp[l][r];
int d = del;
del += dfs(dfs, l + 1, mid - 1, del);
del += dfs(dfs, mid + 1, r - 1, del);
ans.push_back({l - d, 3});
}
return r - l + 1;
};
dfs(dfs, 0, n - 1, 0);
cout << size(ans) << '\n';
for (auto [l, r] : ans) {
cout << l + 1 << " " << r << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 3
Accepted
Test #1:
score: 3
Accepted
time: 0ms
memory: 3424kb
input:
9 ABAABBBAA
output:
4 3 2 2 2 2 2 1 3
result:
ok good solution!
Test #2:
score: 3
Accepted
time: 0ms
memory: 3536kb
input:
13 ABABBABABBABA
output:
6 4 2 3 2 5 2 4 2 2 3 1 2
result:
ok good solution!
Test #3:
score: 3
Accepted
time: 1ms
memory: 3472kb
input:
15 AABABAABABAABAB
output:
7 1 2 4 2 3 2 5 2 4 2 2 3 1 2
result:
ok good solution!
Test #4:
score: 3
Accepted
time: 1ms
memory: 3584kb
input:
15 ABAABBBABAABBAB
output:
7 3 2 2 2 2 2 1 2 4 2 2 3 1 2
result:
ok good solution!
Test #5:
score: 3
Accepted
time: 1ms
memory: 3588kb
input:
1 B
output:
-1
result:
ok no solution
Test #6:
score: 3
Accepted
time: 1ms
memory: 3592kb
input:
2 BB
output:
1 1 2
result:
ok good solution!
Test #7:
score: 3
Accepted
time: 1ms
memory: 3728kb
input:
7 AAAABBB
output:
3 1 2 1 2 1 3
result:
ok good solution!
Test #8:
score: 3
Accepted
time: 0ms
memory: 3476kb
input:
11 BBBBBBAAAAA
output:
5 1 2 1 2 1 2 1 2 1 3
result:
ok good solution!
Test #9:
score: 3
Accepted
time: 0ms
memory: 3476kb
input:
2 AB
output:
-1
result:
ok no solution
Test #10:
score: 3
Accepted
time: 0ms
memory: 3516kb
input:
14 BAAAAAAAAAAAAA
output:
-1
result:
ok no solution
Test #11:
score: 3
Accepted
time: 1ms
memory: 3512kb
input:
14 ABBABAABBABBAB
output:
7 2 2 1 2 2 2 1 2 3 2 2 2 1 2
result:
ok good solution!
Test #12:
score: 3
Accepted
time: 1ms
memory: 3684kb
input:
15 BAABABABBABBAAB
output:
6 2 2 6 2 5 2 4 3 3 3 1 3
result:
ok good solution!
Test #13:
score: 3
Accepted
time: 0ms
memory: 3708kb
input:
14 BABBBBBBBBBBAB
output:
7 3 2 3 2 3 2 3 2 3 2 2 2 1 2
result:
ok good solution!
Test #14:
score: 3
Accepted
time: 1ms
memory: 3596kb
input:
15 BABAAAAAAAAABAB
output:
7 4 2 4 2 4 2 4 3 3 2 2 2 1 2
result:
ok good solution!
Test #15:
score: 3
Accepted
time: 1ms
memory: 3592kb
input:
14 BBBABAAAAAAABA
output:
6 1 3 3 2 3 2 3 3 2 2 1 2
result:
ok good solution!
Test #16:
score: 3
Accepted
time: 0ms
memory: 3788kb
input:
15 AAAABABAAAAABAB
output:
7 1 2 1 2 4 2 4 3 3 2 2 2 1 2
result:
ok good solution!
Test #17:
score: 3
Accepted
time: 1ms
memory: 3472kb
input:
14 BAAABABAAAABAB
output:
6 2 3 5 2 5 2 4 2 3 2 1 3
result:
ok good solution!
Test #18:
score: 3
Accepted
time: 1ms
memory: 3692kb
input:
15 ABAAAABABBBBABA
output:
7 3 2 3 2 5 2 5 2 4 2 2 3 1 2
result:
ok good solution!
Test #19:
score: 3
Accepted
time: 1ms
memory: 3692kb
input:
14 BABAAABAAAABAB
output:
6 4 3 5 2 5 2 3 3 2 2 1 2
result:
ok good solution!
Test #20:
score: 3
Accepted
time: 1ms
memory: 3568kb
input:
15 BABBABABBBBBBAB
output:
7 3 2 2 2 4 2 4 2 4 2 3 2 1 3
result:
ok good solution!
Test #21:
score: 3
Accepted
time: 0ms
memory: 3596kb
input:
14 BABAAAABABBBAB
output:
6 4 2 4 2 3 2 4 3 2 3 1 2
result:
ok good solution!
Test #22:
score: 3
Accepted
time: 1ms
memory: 3592kb
input:
15 BABAAAAAABABAAB
output:
7 4 2 4 2 4 2 3 2 2 2 3 2 1 3
result:
ok good solution!
Test #23:
score: 3
Accepted
time: 1ms
memory: 3568kb
input:
14 BABBBBBABAAAAA
output:
6 3 2 3 3 2 2 1 2 1 2 1 3
result:
ok good solution!
Test #24:
score: 3
Accepted
time: 1ms
memory: 3596kb
input:
15 BABAAAABABAAAAA
output:
7 4 2 4 2 3 2 2 2 1 2 1 2 1 3
result:
ok good solution!
Test #25:
score: 3
Accepted
time: 1ms
memory: 3716kb
input:
15 BAABABAABAABBAA
output:
7 2 2 1 2 3 2 2 2 1 2 2 2 1 3
result:
ok good solution!
Test #26:
score: 3
Accepted
time: 1ms
memory: 3772kb
input:
15 AABAABBAABAABAB
output:
7 1 2 2 2 1 2 5 2 4 2 2 3 1 2
result:
ok good solution!
Test #27:
score: 3
Accepted
time: 1ms
memory: 3536kb
input:
15 AABABBABAABBABB
output:
7 1 2 3 2 2 2 1 2 3 2 1 3 1 2
result:
ok good solution!
Test #28:
score: 3
Accepted
time: 1ms
memory: 3472kb
input:
15 BAABBABBAABABBA
output:
7 2 2 1 2 3 2 2 3 1 2 2 2 1 2
result:
ok good solution!
Test #29:
score: 3
Accepted
time: 1ms
memory: 3552kb
input:
15 BBAABBABABABBAA
output:
-1
result:
ok no solution
Test #30:
score: 3
Accepted
time: 0ms
memory: 3584kb
input:
15 ABABBAABBAABBAB
output:
-1
result:
ok no solution
Test #31:
score: 3
Accepted
time: 0ms
memory: 3476kb
input:
14 AAABAAAAABAAAB
output:
5 1 3 2 2 2 3 3 3 1 3
result:
ok good solution!
Test #32:
score: 3
Accepted
time: 0ms
memory: 3472kb
input:
15 ABBBBABBBBBABBB
output:
6 2 2 2 2 3 2 3 3 1 3 1 3
result:
ok good solution!
Test #33:
score: 3
Accepted
time: 0ms
memory: 3592kb
input:
14 BBBBABBABAAABA
output:
6 1 2 1 2 2 2 4 3 3 2 1 3
result:
ok good solution!
Test #34:
score: 3
Accepted
time: 1ms
memory: 3556kb
input:
15 AAAAABABBBABBAB
output:
6 1 2 1 3 3 3 4 2 2 3 1 2
result:
ok good solution!
Test #35:
score: 3
Accepted
time: 0ms
memory: 3472kb
input:
14 AABABBBBABAAAB
output:
6 1 2 3 2 3 2 2 2 3 3 1 3
result:
ok good solution!
Test #36:
score: 3
Accepted
time: 1ms
memory: 3692kb
input:
15 BAABAAAABABBBBA
output:
7 2 2 3 2 3 2 1 3 2 2 2 2 1 2
result:
ok good solution!
Test #37:
score: 3
Accepted
time: 1ms
memory: 3728kb
input:
14 ABBBABAAABAAAB
output:
5 2 3 1 2 2 3 3 3 1 3
result:
ok good solution!
Test #38:
score: 3
Accepted
time: 1ms
memory: 3536kb
input:
15 BAAABABBBBABAAA
output:
6 2 3 4 2 4 2 3 2 1 3 1 3
result:
ok good solution!
Test #39:
score: 3
Accepted
time: 1ms
memory: 3592kb
input:
14 BABBABBBBABAAA
output:
6 3 2 4 2 4 2 2 3 1 2 1 3
result:
ok good solution!
Test #40:
score: 3
Accepted
time: 0ms
memory: 3536kb
input:
15 ABAAABABBBABBBB
output:
6 3 3 2 2 3 3 1 3 1 2 1 2
result:
ok good solution!
Test #41:
score: 3
Accepted
time: 0ms
memory: 3780kb
input:
14 AABABABBAABBAA
output:
-1
result:
ok no solution
Test #42:
score: 3
Accepted
time: 0ms
memory: 3556kb
input:
15 AABBBABABAABBAA
output:
-1
result:
ok no solution
Test #43:
score: 3
Accepted
time: 0ms
memory: 3588kb
input:
14 AABBAABABABBAA
output:
-1
result:
ok no solution
Test #44:
score: 3
Accepted
time: 0ms
memory: 3540kb
input:
15 BBBAABBAABABABB
output:
-1
result:
ok no solution
Test #45:
score: 3
Accepted
time: 0ms
memory: 3684kb
input:
15 BABABBBBAABBAAA
output:
-1
result:
ok no solution
Test #46:
score: 3
Accepted
time: 0ms
memory: 3772kb
input:
15 BBBABABAABBBAAA
output:
-1
result:
ok no solution
Test #47:
score: 3
Accepted
time: 0ms
memory: 3556kb
input:
15 AAABBBBAABABABB
output:
-1
result:
ok no solution
Test #48:
score: 3
Accepted
time: 0ms
memory: 3468kb
input:
15 AAAAABBAABBABAB
output:
-1
result:
ok no solution
Test #49:
score: 3
Accepted
time: 1ms
memory: 3776kb
input:
15 BAAAABBAABBBABA
output:
-1
result:
ok no solution
Test #50:
score: 3
Accepted
time: 1ms
memory: 3732kb
input:
15 BABAAABBBAAABBA
output:
-1
result:
ok no solution
Subtask #2:
score: 0
Wrong Answer
Test #51:
score: 6
Accepted
time: 2ms
memory: 3704kb
input:
299 ABABABABABABABABABABABABABABABABABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...
output:
-1
result:
ok no solution
Test #52:
score: 6
Accepted
time: 3ms
memory: 3684kb
input:
300 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAB...
output:
-1
result:
ok no solution
Test #53:
score: 6
Accepted
time: 6ms
memory: 3844kb
input:
299 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
149 96 2 96 2 96 2 96 2 95 2 94 2 93 2 92 2 91 2 90 2 89 2 88 2 87 2 86 2 85 2 84 2 83 2 82 2 81 2 80 2 79 2 78 2 77 2 76 2 75 2 74 2 73 2 72 2 71 2 70 2 69 2 68 2 67 2 66 2 65 2 64 2 63 2 62 2 61 2 60 2 59 2 58 2 57 2 56 2 55 2 54 2 53 2 52 2 51 2 50 2 49 2 97 2 97 2 97 2 96 2 95 2 94 2 93 2 92 2 9...
result:
ok good solution!
Test #54:
score: 6
Accepted
time: 5ms
memory: 3904kb
input:
299 BBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
148 1 2 1 2 1 2 96 3 95 2 94 2 93 2 92 2 91 2 90 2 89 2 88 2 87 2 86 2 85 2 84 2 83 2 82 2 81 2 80 2 79 2 78 2 77 2 76 2 75 2 74 2 73 2 72 2 71 2 70 2 69 2 68 2 67 2 66 2 65 2 64 2 63 2 62 2 61 2 60 2 59 2 58 2 57 2 56 2 55 2 54 2 53 2 52 2 51 2 50 2 49 2 97 2 97 3 96 2 95 2 94 2 93 2 92 2 91 2 90 2...
result:
ok good solution!
Test #55:
score: 0
Wrong Answer
time: 5ms
memory: 3616kb
input:
297 BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAABBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBAABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA
output:
-1
result:
wrong answer you didn't find a solution but jury did
Subtask #3:
score: 0
Time Limit Exceeded
Test #102:
score: 0
Time Limit Exceeded
input:
5998 BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #153:
score: 0
Time Limit Exceeded
input:
999997 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...