#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<vll> vvll;
#define all(x) x.begin(), x.end()
#define rep(i,a,b) for(int i = a; i < b; i++)
#define relax(a, b) a = min(a,b)
#define posNow(pos,behind,c,i,R,G,Y) pos[c][i]+max(0,R-behind[0][pos[c][i]])+max(0,G-behind[1][pos[c][i]])+max(0,Y-behind[2][pos[c][i]])
int posNow2(vvi &pos, vvi &behind, int c, int i, int R, int G, int Y) {
int p = pos[c][i];
return p+max(0,R-behind[0][p])+max(0,G-behind[1][p])+max(0,Y-behind[2][p]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int NN;
cin >> NN;
const int N = NN;
vi S(N);
string sS;
cin >> sS;
rep(n,0,N) {
switch(sS[n]) {
case 'R': S[n] = 0; break;
case 'G': S[n] = 1; break;
case 'Y': S[n] = 2; break;
}
}
vvi pos(3, vi(0));
rep(n,0,N) pos[S[n]].push_back(n);
const int PS0 = (int)pos[0].size();
const int PS1 = (int)pos[1].size();
const int PS2 = (int)pos[2].size();
vvi behind(3, vi(N+1));
rep(c,0,3) {
int i = 0;
rep(n,0,N+1) {
behind[c][n] = i;
if(i < (int)pos[c].size() && pos[c][i] == n) i++;
}
}
vi steps((PS0+1)*(PS1+1)*(PS2+1)*3+1, 1000000000);
vb vis((PS0+1)*(PS1+1)*(PS2+1)*3+1);
steps[0] = 0;
steps[1] = 0;
steps[2] = 0;
queue<int> q;
q.push(0);
q.push(1);
q.push(2);
while(!q.empty()) {
int front = q.front();
q.pop();
int R = front/3/(PS2+1)/(PS1+1);
int G = (front/3/(PS2+1))%(PS1+1);
int Y = (front/3)%(PS2+1);
int C = front%3;
int here = steps[front];
int used = R+G+Y;
if(here == 1000000000) continue;
if(R < PS0 && !vis[front+(PS1+1)*(PS2+1)*3 -C+0]) {
vis[front+(PS1+1)*(PS2+1)*3 -C+0] = true;
q.push(front+(PS1+1)*(PS2+1)*3 -C+0);
}
if(G < PS1 && !vis[front+(PS2+1)*3 -C+1]) {
vis[front+(PS2+1)*3 -C+1] = true;
q.push(front+(PS2+1)*3 -C+1);
}
if(Y < PS2 && !vis[front+3 -C+2]) {
vis[front+3 -C+2] = true;
q.push(front+3 -C+2);
}
//cerr << "(R,G,Y,C) = ("<<R<<", "<<G<<", "<<Y<<", "<<C<<") "<<here<<"\n";
if(R < PS0 && C != 0) relax(steps[front+(PS1+1)*(PS2+1)*3 -C+0], here+posNow(pos,behind,0,R,R,G,Y)-used);
if(G < PS1 && C != 1) relax(steps[front+ (PS2+1)*3 -C+1], here+posNow(pos,behind,1,G,R,G,Y)-used);
if(Y < PS2 && C != 2) relax(steps[front+ 3 -C+2], here+posNow(pos,behind,2,Y,R,G,Y)-used);
}
int res = 1000000000;
relax(res, steps[PS0*(PS1+1)*(PS2+1)*3+PS1*(PS2+1)*3+PS2*3+0]);//PS0][PS1][PS2][0]);
relax(res, steps[PS0*(PS1+1)*(PS2+1)*3+PS1*(PS2+1)*3+PS2*3+1]);
relax(res, steps[PS0*(PS1+1)*(PS2+1)*3+PS1*(PS2+1)*3+PS2*3+2]);
if(res == 1000000000)
cout << "-1\n";
else
cout << res << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
3 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
3 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
376 KB |
Output is correct |
37 |
Correct |
2 ms |
372 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
39 |
Correct |
3 ms |
508 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
888 KB |
Output is correct |
5 |
Correct |
2 ms |
760 KB |
Output is correct |
6 |
Correct |
2 ms |
760 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
760 KB |
Output is correct |
9 |
Correct |
2 ms |
760 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
760 KB |
Output is correct |
16 |
Correct |
3 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
3 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
3 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
376 KB |
Output is correct |
37 |
Correct |
2 ms |
372 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
39 |
Correct |
3 ms |
508 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
3 ms |
888 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
888 KB |
Output is correct |
46 |
Correct |
2 ms |
760 KB |
Output is correct |
47 |
Correct |
2 ms |
760 KB |
Output is correct |
48 |
Correct |
2 ms |
760 KB |
Output is correct |
49 |
Correct |
2 ms |
760 KB |
Output is correct |
50 |
Correct |
2 ms |
760 KB |
Output is correct |
51 |
Correct |
2 ms |
760 KB |
Output is correct |
52 |
Correct |
3 ms |
760 KB |
Output is correct |
53 |
Correct |
2 ms |
504 KB |
Output is correct |
54 |
Correct |
3 ms |
632 KB |
Output is correct |
55 |
Correct |
2 ms |
632 KB |
Output is correct |
56 |
Correct |
2 ms |
760 KB |
Output is correct |
57 |
Correct |
3 ms |
760 KB |
Output is correct |
58 |
Correct |
187 ms |
29652 KB |
Output is correct |
59 |
Correct |
176 ms |
29616 KB |
Output is correct |
60 |
Correct |
178 ms |
29352 KB |
Output is correct |
61 |
Correct |
191 ms |
29860 KB |
Output is correct |
62 |
Correct |
5 ms |
2808 KB |
Output is correct |
63 |
Correct |
8 ms |
4600 KB |
Output is correct |
64 |
Correct |
61 ms |
14712 KB |
Output is correct |
65 |
Correct |
85 ms |
21112 KB |
Output is correct |
66 |
Correct |
179 ms |
29192 KB |
Output is correct |
67 |
Correct |
180 ms |
29048 KB |
Output is correct |
68 |
Correct |
184 ms |
29708 KB |
Output is correct |
69 |
Correct |
185 ms |
29944 KB |
Output is correct |
70 |
Correct |
183 ms |
29740 KB |
Output is correct |
71 |
Correct |
170 ms |
29120 KB |
Output is correct |
72 |
Correct |
17 ms |
7672 KB |
Output is correct |
73 |
Correct |
4 ms |
2168 KB |
Output is correct |