//Dost SEFEROĞLU
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int MOD = 1e9+7,inf = 2e7;
const int N = 5001;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vi as,bs,cs;
as.push_back(0),bs.push_back(0),cs.push_back(0);
for (int i=0;i<n;i++) {
if (s[i] == 'R') as.push_back(i+1);
else if (s[i] == 'G') bs.push_back(i+1);
else cs.push_back(i+1);
}
vi pa(n+1,0),pb(n+1,0),pc(n+1,0);
for (int i=1;i<=n;i++) {
pa[i] = pa[i-1],pb[i] = pb[i-1],pc[i] = pc[i-1];
if (s[i-1] == 'R') pa[i]++;
else if (s[i-1] == 'G') pb[i]++;
else pc[i]++;
}
auto cost = [&](int a,int b,int c,int p) {
int pos;
if (p == 0) pos = as[a];
else if (p == 1) pos = bs[b];
else pos = pos = cs[c];
int apos = min(a,pa[pos]),bpos = min(b,pb[pos]),cpos = min(c,pc[pos]);
pos-=apos,pos-=bpos,pos-=cpos;
return pos;
};
int sa = as.size(),sb = bs.size(),sc = cs.size();
int dp[sa+1][sb+1][sc+1][3];
for (int i=0;i<=sa;i++) for (int ii=0;ii<=sb;ii++) for (int iii=0;iii<=sc;iii++) for (int p=0;p<3;p++) dp[i][ii][iii][p] = inf;
dp[1][0][0][0] = as[1]-1;
dp[0][1][0][1] = bs[1]-1;
dp[0][0][1][2] = cs[1]-1;
for (int a=0;a<=sa-1;a++) {
for (int b=0;b<=sb-1;b++) {
for (int c=0;c<=sc-1;c++) {
if ((a+b+c) <= 1) continue;
vi dims{a,b,c};
for (int p=0;p<3;p++) {
dp[a][b][c][p] = inf;
if (!dims[p]) continue;
dims[p]--;
int costy = cost(a,b,c,p);
for (int pp=0;pp<3;pp++) {
if (p == pp) continue;
if (dp[dims[0]][dims[1]][dims[2]][pp] >= inf) continue;
dp[a][b][c][p] = min(dp[a][b][c][p],
dp[dims[0]][dims[1]][dims[2]][pp]+costy);
}
dims[p]++;
}
}
}
}
int ans = min({dp[sa-1][sb-1][sc-1][0],
dp[sa-1][sb-1][sc-1][1],
dp[sa-1][sb-1][sc-1][2]});
if (ans >= inf) cout << -1 << endl;
else cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message
joi2019_ho_t3.cpp: In lambda function:
joi2019_ho_t3.cpp:37:18: warning: operation on 'pos' may be undefined [-Wsequence-point]
37 | else pos = pos = cs[c];
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
3 |
Correct |
2 ms |
1420 KB |
Output is correct |
4 |
Correct |
2 ms |
1424 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
3 ms |
1368 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
2 ms |
1372 KB |
Output is correct |
10 |
Correct |
2 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
968 KB |
Output is correct |
15 |
Correct |
3 ms |
1372 KB |
Output is correct |
16 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1420 KB |
Output is correct |
45 |
Correct |
2 ms |
1424 KB |
Output is correct |
46 |
Correct |
2 ms |
1372 KB |
Output is correct |
47 |
Correct |
2 ms |
1372 KB |
Output is correct |
48 |
Correct |
3 ms |
1368 KB |
Output is correct |
49 |
Correct |
2 ms |
1372 KB |
Output is correct |
50 |
Correct |
2 ms |
1372 KB |
Output is correct |
51 |
Correct |
2 ms |
1372 KB |
Output is correct |
52 |
Correct |
2 ms |
1372 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
2 ms |
968 KB |
Output is correct |
56 |
Correct |
3 ms |
1372 KB |
Output is correct |
57 |
Correct |
4 ms |
1372 KB |
Output is correct |
58 |
Correct |
109 ms |
29212 KB |
Output is correct |
59 |
Correct |
109 ms |
29016 KB |
Output is correct |
60 |
Correct |
103 ms |
29016 KB |
Output is correct |
61 |
Correct |
111 ms |
29320 KB |
Output is correct |
62 |
Correct |
9 ms |
3164 KB |
Output is correct |
63 |
Correct |
15 ms |
5068 KB |
Output is correct |
64 |
Correct |
50 ms |
14896 KB |
Output is correct |
65 |
Correct |
78 ms |
21080 KB |
Output is correct |
66 |
Correct |
104 ms |
28764 KB |
Output is correct |
67 |
Correct |
102 ms |
28764 KB |
Output is correct |
68 |
Correct |
108 ms |
29276 KB |
Output is correct |
69 |
Correct |
107 ms |
29532 KB |
Output is correct |
70 |
Correct |
111 ms |
29272 KB |
Output is correct |
71 |
Correct |
104 ms |
28760 KB |
Output is correct |
72 |
Correct |
31 ms |
7988 KB |
Output is correct |
73 |
Correct |
10 ms |
2652 KB |
Output is correct |