#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
#define __builtin_popcount __builtin_popcountll
using ll=long long; using ld=long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi=acos(0)*2;
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
#define taskname "Vegetable3"
int n;
string s;
int cnt[256];
int x[3][401][3];
int f[401][201][134][3];
bool done[401][201][134][3];
int F(int c0, int c1, int c2, int l){
if(done[c0][c1][c2][l]) return f[c0][c1][c2][l];
done[c0][c1][c2][l]=1;
if(c0+c1+c2==0) return f[c0][c1][c2][l]=0;
int res=1e9;
if((l!=0)&&c0) res=min(res, max(0, c1-x[0][c0][1])+max(0, c2-x[0][c0][2])+F(c0-1, c1, c2, 0));
if((l!=1)&&c1) res=min(res, max(0, c0-x[1][c1][0])+max(0, c2-x[1][c1][2])+F(c0, c1-1, c2, 1));
if((l!=2)&&c2) res=min(res, max(0, c0-x[2][c2][0])+max(0, c1-x[2][c2][1])+F(c0, c1, c2-1, 2));
return f[c0][c1][c2][l]=res;
}
int main(){
#ifdef Aria
if(fopen(taskname".in", "r"))
freopen(taskname".in", "r", stdin);
#endif // Aria
cin>>n>>s;
string all="RGY";
for(char c: s) cnt[c]++;
sort(all.begin(), all.end(), [](char A, char B){
return cnt[A]>cnt[B];
});
for(char &c: s) c=all.find(c);
// reverse(s.begin(), s.end());
for(char c: s){
cnt[c]++;
FFOR(i, 0, 3) x[c][cnt[c]][i]=cnt[i];
}
assert(cnt[2]<=133);
assert(cnt[1]<=200);
int ans=1e9;
FFOR(i, 0, 3) ans=min(ans, F(cnt[0], cnt[1], cnt[2], i));
if(ans==1e9) ans=-1;
writeln(ans);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:43:25: warning: array subscript has type 'char' [-Wchar-subscripts]
for(char c: s) cnt[c]++;
^
joi2019_ho_t3.cpp: In lambda function:
joi2019_ho_t3.cpp:45:21: warning: array subscript has type 'char' [-Wchar-subscripts]
return cnt[A]>cnt[B];
^
joi2019_ho_t3.cpp:45:28: warning: array subscript has type 'char' [-Wchar-subscripts]
return cnt[A]>cnt[B];
^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:50:14: warning: array subscript has type 'char' [-Wchar-subscripts]
cnt[c]++;
^
joi2019_ho_t3.cpp:51:26: warning: array subscript has type 'char' [-Wchar-subscripts]
FFOR(i, 0, 3) x[c][cnt[c]][i]=cnt[i];
^
joi2019_ho_t3.cpp:51:33: warning: array subscript has type 'char' [-Wchar-subscripts]
FFOR(i, 0, 3) x[c][cnt[c]][i]=cnt[i];
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
432 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
432 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
1408 KB |
Output is correct |
19 |
Correct |
3 ms |
1408 KB |
Output is correct |
20 |
Correct |
3 ms |
1536 KB |
Output is correct |
21 |
Correct |
3 ms |
1408 KB |
Output is correct |
22 |
Correct |
2 ms |
1408 KB |
Output is correct |
23 |
Correct |
3 ms |
1536 KB |
Output is correct |
24 |
Correct |
3 ms |
1408 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
1408 KB |
Output is correct |
28 |
Correct |
3 ms |
1408 KB |
Output is correct |
29 |
Correct |
3 ms |
1408 KB |
Output is correct |
30 |
Correct |
3 ms |
1408 KB |
Output is correct |
31 |
Correct |
3 ms |
1508 KB |
Output is correct |
32 |
Correct |
3 ms |
1452 KB |
Output is correct |
33 |
Correct |
2 ms |
896 KB |
Output is correct |
34 |
Correct |
2 ms |
1024 KB |
Output is correct |
35 |
Correct |
3 ms |
1408 KB |
Output is correct |
36 |
Correct |
3 ms |
1408 KB |
Output is correct |
37 |
Correct |
3 ms |
1452 KB |
Output is correct |
38 |
Correct |
3 ms |
1536 KB |
Output is correct |
39 |
Correct |
3 ms |
1380 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
2944 KB |
Output is correct |
3 |
Correct |
5 ms |
2916 KB |
Output is correct |
4 |
Correct |
4 ms |
2944 KB |
Output is correct |
5 |
Correct |
4 ms |
2944 KB |
Output is correct |
6 |
Correct |
4 ms |
2916 KB |
Output is correct |
7 |
Correct |
4 ms |
2944 KB |
Output is correct |
8 |
Correct |
4 ms |
2944 KB |
Output is correct |
9 |
Correct |
4 ms |
2860 KB |
Output is correct |
10 |
Correct |
4 ms |
2944 KB |
Output is correct |
11 |
Correct |
4 ms |
2988 KB |
Output is correct |
12 |
Correct |
3 ms |
1664 KB |
Output is correct |
13 |
Correct |
3 ms |
2048 KB |
Output is correct |
14 |
Correct |
3 ms |
2432 KB |
Output is correct |
15 |
Correct |
4 ms |
2944 KB |
Output is correct |
16 |
Correct |
4 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
2 ms |
432 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
1408 KB |
Output is correct |
19 |
Correct |
3 ms |
1408 KB |
Output is correct |
20 |
Correct |
3 ms |
1536 KB |
Output is correct |
21 |
Correct |
3 ms |
1408 KB |
Output is correct |
22 |
Correct |
2 ms |
1408 KB |
Output is correct |
23 |
Correct |
3 ms |
1536 KB |
Output is correct |
24 |
Correct |
3 ms |
1408 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
1408 KB |
Output is correct |
28 |
Correct |
3 ms |
1408 KB |
Output is correct |
29 |
Correct |
3 ms |
1408 KB |
Output is correct |
30 |
Correct |
3 ms |
1408 KB |
Output is correct |
31 |
Correct |
3 ms |
1508 KB |
Output is correct |
32 |
Correct |
3 ms |
1452 KB |
Output is correct |
33 |
Correct |
2 ms |
896 KB |
Output is correct |
34 |
Correct |
2 ms |
1024 KB |
Output is correct |
35 |
Correct |
3 ms |
1408 KB |
Output is correct |
36 |
Correct |
3 ms |
1408 KB |
Output is correct |
37 |
Correct |
3 ms |
1452 KB |
Output is correct |
38 |
Correct |
3 ms |
1536 KB |
Output is correct |
39 |
Correct |
3 ms |
1380 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
2944 KB |
Output is correct |
44 |
Correct |
5 ms |
2916 KB |
Output is correct |
45 |
Correct |
4 ms |
2944 KB |
Output is correct |
46 |
Correct |
4 ms |
2944 KB |
Output is correct |
47 |
Correct |
4 ms |
2916 KB |
Output is correct |
48 |
Correct |
4 ms |
2944 KB |
Output is correct |
49 |
Correct |
4 ms |
2944 KB |
Output is correct |
50 |
Correct |
4 ms |
2860 KB |
Output is correct |
51 |
Correct |
4 ms |
2944 KB |
Output is correct |
52 |
Correct |
4 ms |
2988 KB |
Output is correct |
53 |
Correct |
3 ms |
1664 KB |
Output is correct |
54 |
Correct |
3 ms |
2048 KB |
Output is correct |
55 |
Correct |
3 ms |
2432 KB |
Output is correct |
56 |
Correct |
4 ms |
2944 KB |
Output is correct |
57 |
Correct |
4 ms |
2944 KB |
Output is correct |
58 |
Correct |
117 ms |
38744 KB |
Output is correct |
59 |
Correct |
115 ms |
39988 KB |
Output is correct |
60 |
Correct |
118 ms |
37732 KB |
Output is correct |
61 |
Correct |
120 ms |
37224 KB |
Output is correct |
62 |
Correct |
7 ms |
5732 KB |
Output is correct |
63 |
Correct |
9 ms |
8960 KB |
Output is correct |
64 |
Correct |
31 ms |
24824 KB |
Output is correct |
65 |
Correct |
57 ms |
33916 KB |
Output is correct |
66 |
Correct |
115 ms |
36948 KB |
Output is correct |
67 |
Correct |
126 ms |
40592 KB |
Output is correct |
68 |
Correct |
118 ms |
37732 KB |
Output is correct |
69 |
Correct |
117 ms |
37900 KB |
Output is correct |
70 |
Correct |
122 ms |
39028 KB |
Output is correct |
71 |
Correct |
117 ms |
40056 KB |
Output is correct |
72 |
Correct |
12 ms |
8320 KB |
Output is correct |
73 |
Correct |
5 ms |
3456 KB |
Output is correct |