#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(a,b) for(int a = 0; a < (b); ++a)
#define all(t) t.begin(), t.end()
#define pb push_back
const int N = 402, INF = 2e9+54321;
const ll INF_L = (ll)2e18+54321;
int n;
string A;
int B[N];
int S[3][N];
vector<int> V[3];
int dp[N][N][N][3];
void solve()
{
cin >> n >> A;
rep(i,n)
{
if(A[i] == 'R') B[i] = 0;
else if(A[i] == 'Y') B[i] = 1;
else B[i] = 2;
}
rep(i,3)
{
int s = 0;
rep(j,n)
{
S[i][j] = s;
if(B[j] == i) ++s;
}
}
rep(i,n) V[B[i]].pb(i);
rep(i,N) rep(j,N) rep(k,N) rep(f,3) dp[i][j][k][f] = INF;
if((int)V[0].size() > 0) dp[1][1][0][0] = V[0][0];
if((int)V[1].size() > 0) dp[1][0][1][1] = V[1][0];
if((int)V[2].size() > 0) dp[1][0][0][2] = V[2][0];
for(int i = 1; i < n; ++i)
{
for(int A = 0; A <= i; ++A)
{
for(int B = 0; B <= i-A; ++B)
{
int C = i-A-B;
if(A > (int)V[0].size() or B > (int)V[1].size() or C > (int)V[2].size()) continue;
int W[3];
W[0] = A, W[1] = B, W[2] = C;
rep(k,3)
{
rep(f,3)
{
if(k == f or (int)V[f].size() <= W[f]) continue;
int idx = V[f][W[f]], poz = 0;
poz = idx + (max(0,W[0]-S[0][idx])) + (max(0,W[1]-S[1][idx])) + (max(0,W[2]-S[2][idx]));
int H[3];
H[0] = W[0], H[1] = W[1], H[2] = W[2], ++H[f];
int cost = dp[i][W[0]][W[1]][k]+(poz-i);
dp[i+1][H[0]][H[1]][f] = min(dp[i+1][H[0]][H[1]][f],cost);
}
}
}
}
}
int wyn = INF;
rep(i,N) rep(j,N) rep(k,3) wyn = min(wyn,dp[n][i][j][k]);
if(wyn >= INF) cout << "-1" << '\n';
else cout << wyn << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
//cin >> T;
while(T--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
763084 KB |
Output is correct |
2 |
Correct |
264 ms |
763216 KB |
Output is correct |
3 |
Correct |
263 ms |
763216 KB |
Output is correct |
4 |
Correct |
268 ms |
763216 KB |
Output is correct |
5 |
Correct |
296 ms |
763216 KB |
Output is correct |
6 |
Correct |
268 ms |
763220 KB |
Output is correct |
7 |
Correct |
271 ms |
763216 KB |
Output is correct |
8 |
Correct |
266 ms |
763188 KB |
Output is correct |
9 |
Correct |
272 ms |
763472 KB |
Output is correct |
10 |
Correct |
258 ms |
763216 KB |
Output is correct |
11 |
Correct |
277 ms |
763124 KB |
Output is correct |
12 |
Correct |
279 ms |
763216 KB |
Output is correct |
13 |
Correct |
252 ms |
763172 KB |
Output is correct |
14 |
Correct |
259 ms |
763220 KB |
Output is correct |
15 |
Correct |
265 ms |
763220 KB |
Output is correct |
16 |
Correct |
269 ms |
763216 KB |
Output is correct |
17 |
Correct |
250 ms |
762996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
763084 KB |
Output is correct |
2 |
Correct |
264 ms |
763216 KB |
Output is correct |
3 |
Correct |
263 ms |
763216 KB |
Output is correct |
4 |
Correct |
268 ms |
763216 KB |
Output is correct |
5 |
Correct |
296 ms |
763216 KB |
Output is correct |
6 |
Correct |
268 ms |
763220 KB |
Output is correct |
7 |
Correct |
271 ms |
763216 KB |
Output is correct |
8 |
Correct |
266 ms |
763188 KB |
Output is correct |
9 |
Correct |
272 ms |
763472 KB |
Output is correct |
10 |
Correct |
258 ms |
763216 KB |
Output is correct |
11 |
Correct |
277 ms |
763124 KB |
Output is correct |
12 |
Correct |
279 ms |
763216 KB |
Output is correct |
13 |
Correct |
252 ms |
763172 KB |
Output is correct |
14 |
Correct |
259 ms |
763220 KB |
Output is correct |
15 |
Correct |
265 ms |
763220 KB |
Output is correct |
16 |
Correct |
269 ms |
763216 KB |
Output is correct |
17 |
Correct |
250 ms |
762996 KB |
Output is correct |
18 |
Correct |
255 ms |
763220 KB |
Output is correct |
19 |
Correct |
267 ms |
762980 KB |
Output is correct |
20 |
Correct |
261 ms |
763216 KB |
Output is correct |
21 |
Correct |
266 ms |
763172 KB |
Output is correct |
22 |
Correct |
255 ms |
763064 KB |
Output is correct |
23 |
Correct |
255 ms |
763236 KB |
Output is correct |
24 |
Correct |
259 ms |
763216 KB |
Output is correct |
25 |
Correct |
261 ms |
763068 KB |
Output is correct |
26 |
Correct |
252 ms |
763108 KB |
Output is correct |
27 |
Correct |
267 ms |
763220 KB |
Output is correct |
28 |
Correct |
253 ms |
763000 KB |
Output is correct |
29 |
Correct |
272 ms |
763180 KB |
Output is correct |
30 |
Correct |
271 ms |
763220 KB |
Output is correct |
31 |
Correct |
266 ms |
763008 KB |
Output is correct |
32 |
Correct |
257 ms |
763216 KB |
Output is correct |
33 |
Correct |
257 ms |
763220 KB |
Output is correct |
34 |
Correct |
263 ms |
763004 KB |
Output is correct |
35 |
Correct |
321 ms |
763092 KB |
Output is correct |
36 |
Correct |
253 ms |
763216 KB |
Output is correct |
37 |
Correct |
269 ms |
763220 KB |
Output is correct |
38 |
Correct |
264 ms |
763220 KB |
Output is correct |
39 |
Correct |
254 ms |
763224 KB |
Output is correct |
40 |
Correct |
268 ms |
763220 KB |
Output is correct |
41 |
Correct |
253 ms |
763188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
763216 KB |
Output is correct |
2 |
Correct |
277 ms |
763000 KB |
Output is correct |
3 |
Correct |
264 ms |
763220 KB |
Output is correct |
4 |
Correct |
272 ms |
763056 KB |
Output is correct |
5 |
Correct |
271 ms |
763140 KB |
Output is correct |
6 |
Correct |
271 ms |
763164 KB |
Output is correct |
7 |
Correct |
276 ms |
763216 KB |
Output is correct |
8 |
Correct |
273 ms |
763216 KB |
Output is correct |
9 |
Correct |
272 ms |
763216 KB |
Output is correct |
10 |
Correct |
250 ms |
763216 KB |
Output is correct |
11 |
Correct |
266 ms |
763216 KB |
Output is correct |
12 |
Correct |
254 ms |
763240 KB |
Output is correct |
13 |
Correct |
262 ms |
763004 KB |
Output is correct |
14 |
Correct |
261 ms |
763220 KB |
Output is correct |
15 |
Correct |
269 ms |
763212 KB |
Output is correct |
16 |
Correct |
266 ms |
763220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
763084 KB |
Output is correct |
2 |
Correct |
264 ms |
763216 KB |
Output is correct |
3 |
Correct |
263 ms |
763216 KB |
Output is correct |
4 |
Correct |
268 ms |
763216 KB |
Output is correct |
5 |
Correct |
296 ms |
763216 KB |
Output is correct |
6 |
Correct |
268 ms |
763220 KB |
Output is correct |
7 |
Correct |
271 ms |
763216 KB |
Output is correct |
8 |
Correct |
266 ms |
763188 KB |
Output is correct |
9 |
Correct |
272 ms |
763472 KB |
Output is correct |
10 |
Correct |
258 ms |
763216 KB |
Output is correct |
11 |
Correct |
277 ms |
763124 KB |
Output is correct |
12 |
Correct |
279 ms |
763216 KB |
Output is correct |
13 |
Correct |
252 ms |
763172 KB |
Output is correct |
14 |
Correct |
259 ms |
763220 KB |
Output is correct |
15 |
Correct |
265 ms |
763220 KB |
Output is correct |
16 |
Correct |
269 ms |
763216 KB |
Output is correct |
17 |
Correct |
250 ms |
762996 KB |
Output is correct |
18 |
Correct |
255 ms |
763220 KB |
Output is correct |
19 |
Correct |
267 ms |
762980 KB |
Output is correct |
20 |
Correct |
261 ms |
763216 KB |
Output is correct |
21 |
Correct |
266 ms |
763172 KB |
Output is correct |
22 |
Correct |
255 ms |
763064 KB |
Output is correct |
23 |
Correct |
255 ms |
763236 KB |
Output is correct |
24 |
Correct |
259 ms |
763216 KB |
Output is correct |
25 |
Correct |
261 ms |
763068 KB |
Output is correct |
26 |
Correct |
252 ms |
763108 KB |
Output is correct |
27 |
Correct |
267 ms |
763220 KB |
Output is correct |
28 |
Correct |
253 ms |
763000 KB |
Output is correct |
29 |
Correct |
272 ms |
763180 KB |
Output is correct |
30 |
Correct |
271 ms |
763220 KB |
Output is correct |
31 |
Correct |
266 ms |
763008 KB |
Output is correct |
32 |
Correct |
257 ms |
763216 KB |
Output is correct |
33 |
Correct |
257 ms |
763220 KB |
Output is correct |
34 |
Correct |
263 ms |
763004 KB |
Output is correct |
35 |
Correct |
321 ms |
763092 KB |
Output is correct |
36 |
Correct |
253 ms |
763216 KB |
Output is correct |
37 |
Correct |
269 ms |
763220 KB |
Output is correct |
38 |
Correct |
264 ms |
763220 KB |
Output is correct |
39 |
Correct |
254 ms |
763224 KB |
Output is correct |
40 |
Correct |
268 ms |
763220 KB |
Output is correct |
41 |
Correct |
253 ms |
763188 KB |
Output is correct |
42 |
Correct |
265 ms |
763216 KB |
Output is correct |
43 |
Correct |
277 ms |
763000 KB |
Output is correct |
44 |
Correct |
264 ms |
763220 KB |
Output is correct |
45 |
Correct |
272 ms |
763056 KB |
Output is correct |
46 |
Correct |
271 ms |
763140 KB |
Output is correct |
47 |
Correct |
271 ms |
763164 KB |
Output is correct |
48 |
Correct |
276 ms |
763216 KB |
Output is correct |
49 |
Correct |
273 ms |
763216 KB |
Output is correct |
50 |
Correct |
272 ms |
763216 KB |
Output is correct |
51 |
Correct |
250 ms |
763216 KB |
Output is correct |
52 |
Correct |
266 ms |
763216 KB |
Output is correct |
53 |
Correct |
254 ms |
763240 KB |
Output is correct |
54 |
Correct |
262 ms |
763004 KB |
Output is correct |
55 |
Correct |
261 ms |
763220 KB |
Output is correct |
56 |
Correct |
269 ms |
763212 KB |
Output is correct |
57 |
Correct |
266 ms |
763220 KB |
Output is correct |
58 |
Correct |
335 ms |
763220 KB |
Output is correct |
59 |
Correct |
328 ms |
763220 KB |
Output is correct |
60 |
Correct |
328 ms |
763220 KB |
Output is correct |
61 |
Correct |
331 ms |
763100 KB |
Output is correct |
62 |
Correct |
296 ms |
763220 KB |
Output is correct |
63 |
Correct |
277 ms |
763220 KB |
Output is correct |
64 |
Correct |
303 ms |
763188 KB |
Output is correct |
65 |
Correct |
317 ms |
763064 KB |
Output is correct |
66 |
Correct |
328 ms |
763220 KB |
Output is correct |
67 |
Correct |
329 ms |
763216 KB |
Output is correct |
68 |
Correct |
325 ms |
763216 KB |
Output is correct |
69 |
Correct |
352 ms |
763224 KB |
Output is correct |
70 |
Correct |
330 ms |
763220 KB |
Output is correct |
71 |
Correct |
339 ms |
763216 KB |
Output is correct |
72 |
Correct |
276 ms |
763076 KB |
Output is correct |
73 |
Correct |
280 ms |
763216 KB |
Output is correct |