//khodaya khodet komak kon
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 400 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;
int n, dp[N][N][N][3], koj[3][N], a[N], ps[3][N], num[3];
string s;
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
for (int i = 1; i <= n; i++){
if (s[i - 1] == 'R') a[i] = 0;
else if(s[i - 1] == 'G') a[i] = 1;
else a[i] = 2;
}
for (int i = 0; i < 3; i++){
int cnt = 0;
for (int j = 1; j <= n; j++) if (a[j] == i) koj[i][++cnt] = j, ps[i][j] ++;
for (int j = 1; j <= n; j++) ps[i][j] += ps[i][j - 1];
num[i] = cnt;
}
memset(dp, 31, sizeof dp);
dp[1][0][0][0] = dp[0][1][0][1] = dp[0][0][1][2] = 0;
for (int i = 0; i <= num[0]; i++)for (int j = 0; j <= num[1]; j++)for (int k = 0; k <= num[2]; k++){
if (i + j + k <= 1) continue;
int x = koj[0][i], y = koj[1][j], z = koj[2][k];
int mv = 0;
if (i > 0){
if (y > x) mv += ps[1][y] - ps[1][x];
if (z > x) mv += ps[2][z] - ps[2][x];
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + mv;
}
mv = 0;
if (j > 0){
if (x > y) mv += ps[0][x] - ps[0][y];
if (z > y) mv += ps[2][z] - ps[2][y];
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + mv;
}
mv = 0;
if (k > 0){
if (x > z) mv += ps[0][x] - ps[0][z];
if (y > z) mv += ps[1][y] - ps[1][z];
dp[i][j][k][2] = min(dp[i][j][k - 1][1], dp[i][j][k - 1][0]) + mv;
}
// cout << i << ' ' << j << ' ' << ' ' << k << ' ' << dp[i][j][k][0] << ' ' << dp[i][j][k][1] << ' ' << dp[i][j][k][2] << '\n';
}
int res = min({dp[num[0]][num[1]][num[2]][0], dp[num[0]][num[1]][num[2]][1], dp[num[0]][num[1]][num[2]][2]});
cout << (res > n * n?-1:res);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
414 ms |
809616 KB |
Output is correct |
2 |
Correct |
405 ms |
809592 KB |
Output is correct |
3 |
Correct |
412 ms |
809640 KB |
Output is correct |
4 |
Correct |
419 ms |
809592 KB |
Output is correct |
5 |
Correct |
410 ms |
809628 KB |
Output is correct |
6 |
Correct |
413 ms |
809720 KB |
Output is correct |
7 |
Correct |
398 ms |
809592 KB |
Output is correct |
8 |
Correct |
406 ms |
809760 KB |
Output is correct |
9 |
Correct |
398 ms |
809668 KB |
Output is correct |
10 |
Correct |
401 ms |
809564 KB |
Output is correct |
11 |
Correct |
402 ms |
809592 KB |
Output is correct |
12 |
Correct |
411 ms |
809720 KB |
Output is correct |
13 |
Correct |
412 ms |
809592 KB |
Output is correct |
14 |
Correct |
419 ms |
809592 KB |
Output is correct |
15 |
Correct |
423 ms |
809592 KB |
Output is correct |
16 |
Correct |
421 ms |
809720 KB |
Output is correct |
17 |
Correct |
422 ms |
809584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
414 ms |
809616 KB |
Output is correct |
2 |
Correct |
405 ms |
809592 KB |
Output is correct |
3 |
Correct |
412 ms |
809640 KB |
Output is correct |
4 |
Correct |
419 ms |
809592 KB |
Output is correct |
5 |
Correct |
410 ms |
809628 KB |
Output is correct |
6 |
Correct |
413 ms |
809720 KB |
Output is correct |
7 |
Correct |
398 ms |
809592 KB |
Output is correct |
8 |
Correct |
406 ms |
809760 KB |
Output is correct |
9 |
Correct |
398 ms |
809668 KB |
Output is correct |
10 |
Correct |
401 ms |
809564 KB |
Output is correct |
11 |
Correct |
402 ms |
809592 KB |
Output is correct |
12 |
Correct |
411 ms |
809720 KB |
Output is correct |
13 |
Correct |
412 ms |
809592 KB |
Output is correct |
14 |
Correct |
419 ms |
809592 KB |
Output is correct |
15 |
Correct |
423 ms |
809592 KB |
Output is correct |
16 |
Correct |
421 ms |
809720 KB |
Output is correct |
17 |
Correct |
422 ms |
809584 KB |
Output is correct |
18 |
Correct |
416 ms |
809592 KB |
Output is correct |
19 |
Correct |
438 ms |
809720 KB |
Output is correct |
20 |
Correct |
431 ms |
809724 KB |
Output is correct |
21 |
Correct |
424 ms |
809592 KB |
Output is correct |
22 |
Correct |
425 ms |
809592 KB |
Output is correct |
23 |
Correct |
428 ms |
809620 KB |
Output is correct |
24 |
Correct |
422 ms |
809592 KB |
Output is correct |
25 |
Correct |
446 ms |
809720 KB |
Output is correct |
26 |
Correct |
442 ms |
809728 KB |
Output is correct |
27 |
Correct |
424 ms |
809612 KB |
Output is correct |
28 |
Correct |
426 ms |
809592 KB |
Output is correct |
29 |
Correct |
429 ms |
809848 KB |
Output is correct |
30 |
Correct |
420 ms |
809592 KB |
Output is correct |
31 |
Correct |
451 ms |
809568 KB |
Output is correct |
32 |
Correct |
435 ms |
809544 KB |
Output is correct |
33 |
Correct |
423 ms |
809848 KB |
Output is correct |
34 |
Correct |
418 ms |
809464 KB |
Output is correct |
35 |
Correct |
430 ms |
809608 KB |
Output is correct |
36 |
Correct |
460 ms |
809592 KB |
Output is correct |
37 |
Correct |
423 ms |
809592 KB |
Output is correct |
38 |
Correct |
461 ms |
809592 KB |
Output is correct |
39 |
Correct |
456 ms |
809592 KB |
Output is correct |
40 |
Correct |
425 ms |
809684 KB |
Output is correct |
41 |
Correct |
443 ms |
809584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
420 ms |
809784 KB |
Output is correct |
2 |
Correct |
429 ms |
809720 KB |
Output is correct |
3 |
Correct |
430 ms |
809720 KB |
Output is correct |
4 |
Correct |
425 ms |
809592 KB |
Output is correct |
5 |
Correct |
412 ms |
809592 KB |
Output is correct |
6 |
Correct |
424 ms |
809720 KB |
Output is correct |
7 |
Correct |
447 ms |
809592 KB |
Output is correct |
8 |
Correct |
450 ms |
809768 KB |
Output is correct |
9 |
Correct |
448 ms |
809720 KB |
Output is correct |
10 |
Correct |
432 ms |
809636 KB |
Output is correct |
11 |
Correct |
434 ms |
809592 KB |
Output is correct |
12 |
Correct |
444 ms |
809592 KB |
Output is correct |
13 |
Correct |
434 ms |
809592 KB |
Output is correct |
14 |
Correct |
429 ms |
809592 KB |
Output is correct |
15 |
Correct |
434 ms |
809720 KB |
Output is correct |
16 |
Correct |
416 ms |
809560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
414 ms |
809616 KB |
Output is correct |
2 |
Correct |
405 ms |
809592 KB |
Output is correct |
3 |
Correct |
412 ms |
809640 KB |
Output is correct |
4 |
Correct |
419 ms |
809592 KB |
Output is correct |
5 |
Correct |
410 ms |
809628 KB |
Output is correct |
6 |
Correct |
413 ms |
809720 KB |
Output is correct |
7 |
Correct |
398 ms |
809592 KB |
Output is correct |
8 |
Correct |
406 ms |
809760 KB |
Output is correct |
9 |
Correct |
398 ms |
809668 KB |
Output is correct |
10 |
Correct |
401 ms |
809564 KB |
Output is correct |
11 |
Correct |
402 ms |
809592 KB |
Output is correct |
12 |
Correct |
411 ms |
809720 KB |
Output is correct |
13 |
Correct |
412 ms |
809592 KB |
Output is correct |
14 |
Correct |
419 ms |
809592 KB |
Output is correct |
15 |
Correct |
423 ms |
809592 KB |
Output is correct |
16 |
Correct |
421 ms |
809720 KB |
Output is correct |
17 |
Correct |
422 ms |
809584 KB |
Output is correct |
18 |
Correct |
416 ms |
809592 KB |
Output is correct |
19 |
Correct |
438 ms |
809720 KB |
Output is correct |
20 |
Correct |
431 ms |
809724 KB |
Output is correct |
21 |
Correct |
424 ms |
809592 KB |
Output is correct |
22 |
Correct |
425 ms |
809592 KB |
Output is correct |
23 |
Correct |
428 ms |
809620 KB |
Output is correct |
24 |
Correct |
422 ms |
809592 KB |
Output is correct |
25 |
Correct |
446 ms |
809720 KB |
Output is correct |
26 |
Correct |
442 ms |
809728 KB |
Output is correct |
27 |
Correct |
424 ms |
809612 KB |
Output is correct |
28 |
Correct |
426 ms |
809592 KB |
Output is correct |
29 |
Correct |
429 ms |
809848 KB |
Output is correct |
30 |
Correct |
420 ms |
809592 KB |
Output is correct |
31 |
Correct |
451 ms |
809568 KB |
Output is correct |
32 |
Correct |
435 ms |
809544 KB |
Output is correct |
33 |
Correct |
423 ms |
809848 KB |
Output is correct |
34 |
Correct |
418 ms |
809464 KB |
Output is correct |
35 |
Correct |
430 ms |
809608 KB |
Output is correct |
36 |
Correct |
460 ms |
809592 KB |
Output is correct |
37 |
Correct |
423 ms |
809592 KB |
Output is correct |
38 |
Correct |
461 ms |
809592 KB |
Output is correct |
39 |
Correct |
456 ms |
809592 KB |
Output is correct |
40 |
Correct |
425 ms |
809684 KB |
Output is correct |
41 |
Correct |
443 ms |
809584 KB |
Output is correct |
42 |
Correct |
420 ms |
809784 KB |
Output is correct |
43 |
Correct |
429 ms |
809720 KB |
Output is correct |
44 |
Correct |
430 ms |
809720 KB |
Output is correct |
45 |
Correct |
425 ms |
809592 KB |
Output is correct |
46 |
Correct |
412 ms |
809592 KB |
Output is correct |
47 |
Correct |
424 ms |
809720 KB |
Output is correct |
48 |
Correct |
447 ms |
809592 KB |
Output is correct |
49 |
Correct |
450 ms |
809768 KB |
Output is correct |
50 |
Correct |
448 ms |
809720 KB |
Output is correct |
51 |
Correct |
432 ms |
809636 KB |
Output is correct |
52 |
Correct |
434 ms |
809592 KB |
Output is correct |
53 |
Correct |
444 ms |
809592 KB |
Output is correct |
54 |
Correct |
434 ms |
809592 KB |
Output is correct |
55 |
Correct |
429 ms |
809592 KB |
Output is correct |
56 |
Correct |
434 ms |
809720 KB |
Output is correct |
57 |
Correct |
416 ms |
809560 KB |
Output is correct |
58 |
Correct |
451 ms |
809592 KB |
Output is correct |
59 |
Correct |
445 ms |
809592 KB |
Output is correct |
60 |
Correct |
453 ms |
809720 KB |
Output is correct |
61 |
Correct |
454 ms |
809640 KB |
Output is correct |
62 |
Correct |
430 ms |
809820 KB |
Output is correct |
63 |
Correct |
450 ms |
809592 KB |
Output is correct |
64 |
Correct |
460 ms |
809724 KB |
Output is correct |
65 |
Correct |
452 ms |
809848 KB |
Output is correct |
66 |
Correct |
463 ms |
809592 KB |
Output is correct |
67 |
Correct |
469 ms |
809720 KB |
Output is correct |
68 |
Correct |
449 ms |
809592 KB |
Output is correct |
69 |
Correct |
457 ms |
809720 KB |
Output is correct |
70 |
Correct |
451 ms |
809848 KB |
Output is correct |
71 |
Correct |
453 ms |
809660 KB |
Output is correct |
72 |
Correct |
452 ms |
809636 KB |
Output is correct |
73 |
Correct |
448 ms |
809752 KB |
Output is correct |