#include <iostream>
#include <vector>
#include <cstring>
#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector")
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 404;
int dp[N][N][N][3], a[N], cnt[N][3];
vi occ[3];
signed main()
{
IO_OP;
int n;
string s;
cin >> n >> s;
for(int i=0;i<n;i++) {
a[i+1] = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
occ[a[i+1]].PB(i+1);
}
for(int i=1;i<=n;i++) {
for(int j=0;j<3;j++) cnt[i][j] = cnt[i-1][j];
cnt[i][a[i]]++;
}
memset(dp, 0x3f, sizeof dp);
for(int i=0;i<=cnt[n][0];i++) {
for(int j=0;j<=cnt[n][1];j++) {
for(int k=0;k<=cnt[n][2];k++) {
if(i == 0 && j == 0 && k == 0) {
for(int l=0;l<3;l++)
dp[i][j][k][l] = 0;
} else {
for(int l=0;l<3;l++) {
int pos, jump, pre_dp;
if(l == 0) {
if(i == 0) continue;
pre_dp = min(dp[i-1][j][k][1], dp[i-1][j][k][2]);
pos = occ[l][i-1];
jump = 0;
jump += max(0, cnt[pos-1][0] - (i-1));
jump += max(0, cnt[pos-1][1] - j);
jump += max(0, cnt[pos-1][2] - k);
} else if(l == 1) {
if(j == 0) continue;
pre_dp = min(dp[i][j-1][k][0], dp[i][j-1][k][2]);
pos = occ[l][j-1];
jump = 0;
jump += max(0, cnt[pos-1][0] - i);
jump += max(0, cnt[pos-1][1] - (j-1));
jump += max(0, cnt[pos-1][2] - k);
} else {
if(k == 0) continue;
pre_dp = min(dp[i][j][k-1][0], dp[i][j][k-1][1]);
pos = occ[l][k-1];
jump = 0;
jump += max(0, cnt[pos-1][0] - i);
jump += max(0, cnt[pos-1][1] - j);
jump += max(0, cnt[pos-1][2] - (k-1));
}
dp[i][j][k][l] = min(dp[i][j][k][l], pre_dp + jump);
}
}
}
}
}
int ans = INF;
for(int i=0;i<3;i++)
ans = min(ans, dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][i]);
if(ans > 1e8) ans = -1;
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
774520 KB |
Output is correct |
2 |
Correct |
404 ms |
774520 KB |
Output is correct |
3 |
Correct |
414 ms |
774520 KB |
Output is correct |
4 |
Correct |
412 ms |
774520 KB |
Output is correct |
5 |
Correct |
418 ms |
774520 KB |
Output is correct |
6 |
Correct |
419 ms |
774524 KB |
Output is correct |
7 |
Correct |
408 ms |
774520 KB |
Output is correct |
8 |
Correct |
416 ms |
774520 KB |
Output is correct |
9 |
Correct |
416 ms |
774524 KB |
Output is correct |
10 |
Correct |
421 ms |
774520 KB |
Output is correct |
11 |
Correct |
415 ms |
774776 KB |
Output is correct |
12 |
Correct |
417 ms |
774616 KB |
Output is correct |
13 |
Correct |
412 ms |
774904 KB |
Output is correct |
14 |
Correct |
423 ms |
774524 KB |
Output is correct |
15 |
Correct |
419 ms |
774572 KB |
Output is correct |
16 |
Correct |
413 ms |
774652 KB |
Output is correct |
17 |
Correct |
421 ms |
774520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
774520 KB |
Output is correct |
2 |
Correct |
404 ms |
774520 KB |
Output is correct |
3 |
Correct |
414 ms |
774520 KB |
Output is correct |
4 |
Correct |
412 ms |
774520 KB |
Output is correct |
5 |
Correct |
418 ms |
774520 KB |
Output is correct |
6 |
Correct |
419 ms |
774524 KB |
Output is correct |
7 |
Correct |
408 ms |
774520 KB |
Output is correct |
8 |
Correct |
416 ms |
774520 KB |
Output is correct |
9 |
Correct |
416 ms |
774524 KB |
Output is correct |
10 |
Correct |
421 ms |
774520 KB |
Output is correct |
11 |
Correct |
415 ms |
774776 KB |
Output is correct |
12 |
Correct |
417 ms |
774616 KB |
Output is correct |
13 |
Correct |
412 ms |
774904 KB |
Output is correct |
14 |
Correct |
423 ms |
774524 KB |
Output is correct |
15 |
Correct |
419 ms |
774572 KB |
Output is correct |
16 |
Correct |
413 ms |
774652 KB |
Output is correct |
17 |
Correct |
421 ms |
774520 KB |
Output is correct |
18 |
Correct |
411 ms |
774648 KB |
Output is correct |
19 |
Correct |
422 ms |
774648 KB |
Output is correct |
20 |
Correct |
417 ms |
774776 KB |
Output is correct |
21 |
Correct |
417 ms |
774520 KB |
Output is correct |
22 |
Correct |
414 ms |
774648 KB |
Output is correct |
23 |
Correct |
416 ms |
774520 KB |
Output is correct |
24 |
Correct |
421 ms |
774520 KB |
Output is correct |
25 |
Correct |
411 ms |
774648 KB |
Output is correct |
26 |
Correct |
421 ms |
774520 KB |
Output is correct |
27 |
Correct |
414 ms |
774520 KB |
Output is correct |
28 |
Correct |
427 ms |
774648 KB |
Output is correct |
29 |
Correct |
417 ms |
774648 KB |
Output is correct |
30 |
Correct |
417 ms |
774648 KB |
Output is correct |
31 |
Correct |
406 ms |
774520 KB |
Output is correct |
32 |
Correct |
412 ms |
774648 KB |
Output is correct |
33 |
Correct |
421 ms |
774520 KB |
Output is correct |
34 |
Correct |
413 ms |
774520 KB |
Output is correct |
35 |
Correct |
415 ms |
774520 KB |
Output is correct |
36 |
Correct |
407 ms |
774648 KB |
Output is correct |
37 |
Correct |
411 ms |
774740 KB |
Output is correct |
38 |
Correct |
421 ms |
774552 KB |
Output is correct |
39 |
Correct |
418 ms |
774840 KB |
Output is correct |
40 |
Correct |
404 ms |
774520 KB |
Output is correct |
41 |
Correct |
422 ms |
774648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
774632 KB |
Output is correct |
2 |
Correct |
425 ms |
774648 KB |
Output is correct |
3 |
Correct |
428 ms |
774524 KB |
Output is correct |
4 |
Correct |
420 ms |
774520 KB |
Output is correct |
5 |
Correct |
428 ms |
774648 KB |
Output is correct |
6 |
Correct |
427 ms |
774520 KB |
Output is correct |
7 |
Correct |
409 ms |
774520 KB |
Output is correct |
8 |
Correct |
407 ms |
774520 KB |
Output is correct |
9 |
Correct |
418 ms |
774648 KB |
Output is correct |
10 |
Correct |
414 ms |
774592 KB |
Output is correct |
11 |
Correct |
411 ms |
774620 KB |
Output is correct |
12 |
Correct |
414 ms |
774520 KB |
Output is correct |
13 |
Correct |
419 ms |
774520 KB |
Output is correct |
14 |
Correct |
415 ms |
774520 KB |
Output is correct |
15 |
Correct |
428 ms |
774520 KB |
Output is correct |
16 |
Correct |
415 ms |
774776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
774520 KB |
Output is correct |
2 |
Correct |
404 ms |
774520 KB |
Output is correct |
3 |
Correct |
414 ms |
774520 KB |
Output is correct |
4 |
Correct |
412 ms |
774520 KB |
Output is correct |
5 |
Correct |
418 ms |
774520 KB |
Output is correct |
6 |
Correct |
419 ms |
774524 KB |
Output is correct |
7 |
Correct |
408 ms |
774520 KB |
Output is correct |
8 |
Correct |
416 ms |
774520 KB |
Output is correct |
9 |
Correct |
416 ms |
774524 KB |
Output is correct |
10 |
Correct |
421 ms |
774520 KB |
Output is correct |
11 |
Correct |
415 ms |
774776 KB |
Output is correct |
12 |
Correct |
417 ms |
774616 KB |
Output is correct |
13 |
Correct |
412 ms |
774904 KB |
Output is correct |
14 |
Correct |
423 ms |
774524 KB |
Output is correct |
15 |
Correct |
419 ms |
774572 KB |
Output is correct |
16 |
Correct |
413 ms |
774652 KB |
Output is correct |
17 |
Correct |
421 ms |
774520 KB |
Output is correct |
18 |
Correct |
411 ms |
774648 KB |
Output is correct |
19 |
Correct |
422 ms |
774648 KB |
Output is correct |
20 |
Correct |
417 ms |
774776 KB |
Output is correct |
21 |
Correct |
417 ms |
774520 KB |
Output is correct |
22 |
Correct |
414 ms |
774648 KB |
Output is correct |
23 |
Correct |
416 ms |
774520 KB |
Output is correct |
24 |
Correct |
421 ms |
774520 KB |
Output is correct |
25 |
Correct |
411 ms |
774648 KB |
Output is correct |
26 |
Correct |
421 ms |
774520 KB |
Output is correct |
27 |
Correct |
414 ms |
774520 KB |
Output is correct |
28 |
Correct |
427 ms |
774648 KB |
Output is correct |
29 |
Correct |
417 ms |
774648 KB |
Output is correct |
30 |
Correct |
417 ms |
774648 KB |
Output is correct |
31 |
Correct |
406 ms |
774520 KB |
Output is correct |
32 |
Correct |
412 ms |
774648 KB |
Output is correct |
33 |
Correct |
421 ms |
774520 KB |
Output is correct |
34 |
Correct |
413 ms |
774520 KB |
Output is correct |
35 |
Correct |
415 ms |
774520 KB |
Output is correct |
36 |
Correct |
407 ms |
774648 KB |
Output is correct |
37 |
Correct |
411 ms |
774740 KB |
Output is correct |
38 |
Correct |
421 ms |
774552 KB |
Output is correct |
39 |
Correct |
418 ms |
774840 KB |
Output is correct |
40 |
Correct |
404 ms |
774520 KB |
Output is correct |
41 |
Correct |
422 ms |
774648 KB |
Output is correct |
42 |
Correct |
415 ms |
774632 KB |
Output is correct |
43 |
Correct |
425 ms |
774648 KB |
Output is correct |
44 |
Correct |
428 ms |
774524 KB |
Output is correct |
45 |
Correct |
420 ms |
774520 KB |
Output is correct |
46 |
Correct |
428 ms |
774648 KB |
Output is correct |
47 |
Correct |
427 ms |
774520 KB |
Output is correct |
48 |
Correct |
409 ms |
774520 KB |
Output is correct |
49 |
Correct |
407 ms |
774520 KB |
Output is correct |
50 |
Correct |
418 ms |
774648 KB |
Output is correct |
51 |
Correct |
414 ms |
774592 KB |
Output is correct |
52 |
Correct |
411 ms |
774620 KB |
Output is correct |
53 |
Correct |
414 ms |
774520 KB |
Output is correct |
54 |
Correct |
419 ms |
774520 KB |
Output is correct |
55 |
Correct |
415 ms |
774520 KB |
Output is correct |
56 |
Correct |
428 ms |
774520 KB |
Output is correct |
57 |
Correct |
415 ms |
774776 KB |
Output is correct |
58 |
Correct |
445 ms |
774532 KB |
Output is correct |
59 |
Correct |
456 ms |
774648 KB |
Output is correct |
60 |
Correct |
444 ms |
774520 KB |
Output is correct |
61 |
Correct |
451 ms |
774524 KB |
Output is correct |
62 |
Correct |
432 ms |
774648 KB |
Output is correct |
63 |
Correct |
434 ms |
774520 KB |
Output is correct |
64 |
Correct |
432 ms |
774520 KB |
Output is correct |
65 |
Correct |
448 ms |
774648 KB |
Output is correct |
66 |
Correct |
452 ms |
774596 KB |
Output is correct |
67 |
Correct |
462 ms |
774644 KB |
Output is correct |
68 |
Correct |
446 ms |
774520 KB |
Output is correct |
69 |
Correct |
441 ms |
774520 KB |
Output is correct |
70 |
Correct |
455 ms |
774520 KB |
Output is correct |
71 |
Correct |
457 ms |
774776 KB |
Output is correct |
72 |
Correct |
428 ms |
774648 KB |
Output is correct |
73 |
Correct |
429 ms |
774520 KB |
Output is correct |