#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
int n,memo[405][405][405][3],cnt[3][405];
char c;
vi v[3];
int dp(int i,int j,int k,int pv){
int p=i+j+k;
if(p==n)return 0;
if(pv!=-1&&memo[i][j][k][pv]!=-1)return memo[i][j][k][pv];
int res=INF;
if(pv!=0&&i<sz(v[0])){
int l=v[0][i];
mnto(res,dp(i+1,j,k,0)+max(0,cnt[1][l]-j)+max(0,cnt[2][l]-k));
}
if(pv!=1&&j<sz(v[1])){
int l=v[1][j];
mnto(res,dp(i,j+1,k,1)+max(0,cnt[0][l]-i)+max(0,cnt[2][l]-k));
}
if(pv!=2&&k<sz(v[2])){
int l=v[2][k];
mnto(res,dp(i,j,k+1,2)+max(0,cnt[0][l]-i)+max(0,cnt[1][l]-j));
}
if(pv==-1)return res;
return memo[i][j][k][pv]=res;
}
int main(){
sf("%d",&n);
for(int i=0;i<n;++i){
sf(" %c",&c);
if(c=='R')v[0].pb(i),++cnt[0][i];
if(c=='G')v[1].pb(i),++cnt[1][i];
if(c=='Y')v[2].pb(i),++cnt[2][i];
if(i!=0){
cnt[0][i]+=cnt[0][i-1];
cnt[1][i]+=cnt[1][i-1];
cnt[2][i]+=cnt[2][i-1];
}
}
memset(memo,-1,sizeof memo);
int ans=dp(0,0,0,-1);
if(ans==INF)ans=-1;
pf("%d\n",ans);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:66:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | sf("%d",&n);
| ^
joi2019_ho_t3.cpp:68:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | sf(" %c",&c);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
780228 KB |
Output is correct |
2 |
Correct |
333 ms |
780256 KB |
Output is correct |
3 |
Correct |
334 ms |
780228 KB |
Output is correct |
4 |
Correct |
331 ms |
780252 KB |
Output is correct |
5 |
Correct |
328 ms |
780168 KB |
Output is correct |
6 |
Correct |
332 ms |
780236 KB |
Output is correct |
7 |
Correct |
328 ms |
780188 KB |
Output is correct |
8 |
Correct |
329 ms |
780208 KB |
Output is correct |
9 |
Correct |
329 ms |
780228 KB |
Output is correct |
10 |
Correct |
334 ms |
780172 KB |
Output is correct |
11 |
Correct |
330 ms |
780228 KB |
Output is correct |
12 |
Correct |
339 ms |
780200 KB |
Output is correct |
13 |
Correct |
336 ms |
780228 KB |
Output is correct |
14 |
Correct |
329 ms |
780228 KB |
Output is correct |
15 |
Correct |
346 ms |
780292 KB |
Output is correct |
16 |
Correct |
338 ms |
780268 KB |
Output is correct |
17 |
Correct |
333 ms |
780228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
780228 KB |
Output is correct |
2 |
Correct |
333 ms |
780256 KB |
Output is correct |
3 |
Correct |
334 ms |
780228 KB |
Output is correct |
4 |
Correct |
331 ms |
780252 KB |
Output is correct |
5 |
Correct |
328 ms |
780168 KB |
Output is correct |
6 |
Correct |
332 ms |
780236 KB |
Output is correct |
7 |
Correct |
328 ms |
780188 KB |
Output is correct |
8 |
Correct |
329 ms |
780208 KB |
Output is correct |
9 |
Correct |
329 ms |
780228 KB |
Output is correct |
10 |
Correct |
334 ms |
780172 KB |
Output is correct |
11 |
Correct |
330 ms |
780228 KB |
Output is correct |
12 |
Correct |
339 ms |
780200 KB |
Output is correct |
13 |
Correct |
336 ms |
780228 KB |
Output is correct |
14 |
Correct |
329 ms |
780228 KB |
Output is correct |
15 |
Correct |
346 ms |
780292 KB |
Output is correct |
16 |
Correct |
338 ms |
780268 KB |
Output is correct |
17 |
Correct |
333 ms |
780228 KB |
Output is correct |
18 |
Correct |
335 ms |
780292 KB |
Output is correct |
19 |
Correct |
329 ms |
780168 KB |
Output is correct |
20 |
Correct |
330 ms |
780352 KB |
Output is correct |
21 |
Correct |
336 ms |
780228 KB |
Output is correct |
22 |
Correct |
336 ms |
780208 KB |
Output is correct |
23 |
Correct |
328 ms |
780228 KB |
Output is correct |
24 |
Correct |
339 ms |
780204 KB |
Output is correct |
25 |
Correct |
349 ms |
780188 KB |
Output is correct |
26 |
Correct |
329 ms |
780276 KB |
Output is correct |
27 |
Correct |
331 ms |
780228 KB |
Output is correct |
28 |
Correct |
344 ms |
780228 KB |
Output is correct |
29 |
Correct |
335 ms |
780272 KB |
Output is correct |
30 |
Correct |
326 ms |
780196 KB |
Output is correct |
31 |
Correct |
332 ms |
780308 KB |
Output is correct |
32 |
Correct |
342 ms |
780168 KB |
Output is correct |
33 |
Correct |
335 ms |
780228 KB |
Output is correct |
34 |
Correct |
331 ms |
780244 KB |
Output is correct |
35 |
Correct |
335 ms |
780228 KB |
Output is correct |
36 |
Correct |
333 ms |
780184 KB |
Output is correct |
37 |
Correct |
328 ms |
780292 KB |
Output is correct |
38 |
Correct |
332 ms |
780404 KB |
Output is correct |
39 |
Correct |
343 ms |
780184 KB |
Output is correct |
40 |
Correct |
332 ms |
780292 KB |
Output is correct |
41 |
Correct |
337 ms |
780228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
780188 KB |
Output is correct |
2 |
Correct |
335 ms |
780312 KB |
Output is correct |
3 |
Correct |
333 ms |
780276 KB |
Output is correct |
4 |
Correct |
334 ms |
780228 KB |
Output is correct |
5 |
Correct |
332 ms |
780240 KB |
Output is correct |
6 |
Correct |
333 ms |
780320 KB |
Output is correct |
7 |
Correct |
332 ms |
780296 KB |
Output is correct |
8 |
Correct |
331 ms |
780268 KB |
Output is correct |
9 |
Correct |
335 ms |
780212 KB |
Output is correct |
10 |
Correct |
340 ms |
780264 KB |
Output is correct |
11 |
Correct |
337 ms |
780344 KB |
Output is correct |
12 |
Correct |
338 ms |
780276 KB |
Output is correct |
13 |
Correct |
331 ms |
780300 KB |
Output is correct |
14 |
Correct |
345 ms |
780220 KB |
Output is correct |
15 |
Correct |
325 ms |
780252 KB |
Output is correct |
16 |
Correct |
335 ms |
780292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
780228 KB |
Output is correct |
2 |
Correct |
333 ms |
780256 KB |
Output is correct |
3 |
Correct |
334 ms |
780228 KB |
Output is correct |
4 |
Correct |
331 ms |
780252 KB |
Output is correct |
5 |
Correct |
328 ms |
780168 KB |
Output is correct |
6 |
Correct |
332 ms |
780236 KB |
Output is correct |
7 |
Correct |
328 ms |
780188 KB |
Output is correct |
8 |
Correct |
329 ms |
780208 KB |
Output is correct |
9 |
Correct |
329 ms |
780228 KB |
Output is correct |
10 |
Correct |
334 ms |
780172 KB |
Output is correct |
11 |
Correct |
330 ms |
780228 KB |
Output is correct |
12 |
Correct |
339 ms |
780200 KB |
Output is correct |
13 |
Correct |
336 ms |
780228 KB |
Output is correct |
14 |
Correct |
329 ms |
780228 KB |
Output is correct |
15 |
Correct |
346 ms |
780292 KB |
Output is correct |
16 |
Correct |
338 ms |
780268 KB |
Output is correct |
17 |
Correct |
333 ms |
780228 KB |
Output is correct |
18 |
Correct |
335 ms |
780292 KB |
Output is correct |
19 |
Correct |
329 ms |
780168 KB |
Output is correct |
20 |
Correct |
330 ms |
780352 KB |
Output is correct |
21 |
Correct |
336 ms |
780228 KB |
Output is correct |
22 |
Correct |
336 ms |
780208 KB |
Output is correct |
23 |
Correct |
328 ms |
780228 KB |
Output is correct |
24 |
Correct |
339 ms |
780204 KB |
Output is correct |
25 |
Correct |
349 ms |
780188 KB |
Output is correct |
26 |
Correct |
329 ms |
780276 KB |
Output is correct |
27 |
Correct |
331 ms |
780228 KB |
Output is correct |
28 |
Correct |
344 ms |
780228 KB |
Output is correct |
29 |
Correct |
335 ms |
780272 KB |
Output is correct |
30 |
Correct |
326 ms |
780196 KB |
Output is correct |
31 |
Correct |
332 ms |
780308 KB |
Output is correct |
32 |
Correct |
342 ms |
780168 KB |
Output is correct |
33 |
Correct |
335 ms |
780228 KB |
Output is correct |
34 |
Correct |
331 ms |
780244 KB |
Output is correct |
35 |
Correct |
335 ms |
780228 KB |
Output is correct |
36 |
Correct |
333 ms |
780184 KB |
Output is correct |
37 |
Correct |
328 ms |
780292 KB |
Output is correct |
38 |
Correct |
332 ms |
780404 KB |
Output is correct |
39 |
Correct |
343 ms |
780184 KB |
Output is correct |
40 |
Correct |
332 ms |
780292 KB |
Output is correct |
41 |
Correct |
337 ms |
780228 KB |
Output is correct |
42 |
Correct |
336 ms |
780188 KB |
Output is correct |
43 |
Correct |
335 ms |
780312 KB |
Output is correct |
44 |
Correct |
333 ms |
780276 KB |
Output is correct |
45 |
Correct |
334 ms |
780228 KB |
Output is correct |
46 |
Correct |
332 ms |
780240 KB |
Output is correct |
47 |
Correct |
333 ms |
780320 KB |
Output is correct |
48 |
Correct |
332 ms |
780296 KB |
Output is correct |
49 |
Correct |
331 ms |
780268 KB |
Output is correct |
50 |
Correct |
335 ms |
780212 KB |
Output is correct |
51 |
Correct |
340 ms |
780264 KB |
Output is correct |
52 |
Correct |
337 ms |
780344 KB |
Output is correct |
53 |
Correct |
338 ms |
780276 KB |
Output is correct |
54 |
Correct |
331 ms |
780300 KB |
Output is correct |
55 |
Correct |
345 ms |
780220 KB |
Output is correct |
56 |
Correct |
325 ms |
780252 KB |
Output is correct |
57 |
Correct |
335 ms |
780292 KB |
Output is correct |
58 |
Correct |
468 ms |
780412 KB |
Output is correct |
59 |
Correct |
447 ms |
780348 KB |
Output is correct |
60 |
Correct |
438 ms |
780200 KB |
Output is correct |
61 |
Correct |
451 ms |
780224 KB |
Output is correct |
62 |
Correct |
336 ms |
780288 KB |
Output is correct |
63 |
Correct |
341 ms |
780272 KB |
Output is correct |
64 |
Correct |
348 ms |
780256 KB |
Output is correct |
65 |
Correct |
371 ms |
780296 KB |
Output is correct |
66 |
Correct |
441 ms |
780308 KB |
Output is correct |
67 |
Correct |
433 ms |
780216 KB |
Output is correct |
68 |
Correct |
448 ms |
780228 KB |
Output is correct |
69 |
Correct |
441 ms |
780308 KB |
Output is correct |
70 |
Correct |
450 ms |
780288 KB |
Output is correct |
71 |
Correct |
437 ms |
780356 KB |
Output is correct |
72 |
Correct |
336 ms |
780228 KB |
Output is correct |
73 |
Correct |
338 ms |
780304 KB |
Output is correct |