/* In the name of God, aka Allah */
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 405;
int n, dp[N][N][N][3];
string s;
vector<int> vc[3];
vector<pii> vt[3];
void solve() {
cin >> n;
cin >> s;
s = '.'+s;
for (int i = 1; i <= n; i++) {
int c = (s[i] != 'R') + (s[i] == 'Y');
vc[c].push_back(i);
}
int a, b, c;
b = 0, c = 0;
for (int A = 0; A < (int)vc[0].size(); A++) {
while (b < (int)vc[1].size() && vc[1][b] < vc[0][A]) b++;
while (c < (int)vc[2].size() && vc[2][b] < vc[0][A]) c++;
vt[0].push_back({b, c});
}
a = 0, c = 0;
for (int B = 0; B < (int)vc[1].size(); B++) {
while (a < (int)vc[0].size() && vc[0][a] < vc[1][B]) a++;
while (c < (int)vc[2].size() && vc[2][c] < vc[1][B]) c++;
vt[1].push_back({a, c});
}
a = 0, b = 0;
for (int C = 0; C < (int)vc[2].size(); C++) {
while (a < (int)vc[0].size() && vc[0][a] < vc[2][C]) a++;
while (b < (int)vc[1].size() && vc[1][b] < vc[2][C]) b++;
vt[1].push_back({a, b});
}
memset(dp, 64, sizeof dp);
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int sum = 0; sum <= n; sum++) {
for (int A = 0; A <= sum; A++) {
for (int B = 0; A+B <= sum; B++) {
int C = sum-A-B;
if (A < (int)vc[0].size()) {
int b = vt[0][A].F;
int c = vt[0][A].S;
dp[A+1][B][C][0] = min(dp[A+1][B][C][0], min(dp[A][B][C][1], dp[A][B][C][2]) + vc[0][A]+max(B-b, 0)+max(C-c, 0)-(A+B+C+1));
}
if (B < (int)vc[1].size()) {
int a = vt[1][B].F;
int c = vt[1][B].S;
dp[A][B+1][C][1] = min(dp[A][B+1][C][1], min(dp[A][B][C][0], dp[A][B][C][2]) + vc[1][B]+max(A-a, 0)+max(C-c, 0)-(A+B+C+1));
}
if (C < (int)vc[2].size()) {
int a = vt[2][C].F;
int b = vt[2][C].S;
dp[A][B][C+1][2] = min(dp[A][B][C+1][2], min(dp[A][B][C][0], dp[A][B][C][1]) + vc[2][C]+max(A-a,0)+max(B-b, 0)-(A+B+C+1));
}
}
}
}
int ans = 1e9;
ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][0], ans);
ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][1], ans);
ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][2], ans);
cout << (ans >= 1e9? -1 : ans) << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
780220 KB |
Output is correct |
2 |
Execution timed out |
736 ms |
1048576 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
780220 KB |
Output is correct |
2 |
Execution timed out |
736 ms |
1048576 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
780284 KB |
Output is correct |
2 |
Correct |
489 ms |
780432 KB |
Output is correct |
3 |
Correct |
459 ms |
780304 KB |
Output is correct |
4 |
Correct |
447 ms |
780344 KB |
Output is correct |
5 |
Correct |
458 ms |
780424 KB |
Output is correct |
6 |
Correct |
454 ms |
780312 KB |
Output is correct |
7 |
Correct |
465 ms |
780392 KB |
Output is correct |
8 |
Correct |
482 ms |
780228 KB |
Output is correct |
9 |
Correct |
430 ms |
780304 KB |
Output is correct |
10 |
Correct |
456 ms |
780312 KB |
Output is correct |
11 |
Correct |
459 ms |
780232 KB |
Output is correct |
12 |
Correct |
298 ms |
780352 KB |
Output is correct |
13 |
Correct |
324 ms |
780236 KB |
Output is correct |
14 |
Correct |
373 ms |
780324 KB |
Output is correct |
15 |
Correct |
459 ms |
780308 KB |
Output is correct |
16 |
Correct |
461 ms |
780308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
780220 KB |
Output is correct |
2 |
Execution timed out |
736 ms |
1048576 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |