#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define bas(x) #x << ": " << x << " "
#define prarr(x, n) cout << #x << ": "; for (int qsd = 0; qsd < n; qsd++) cout << x[qsd] << " "; cout << endl;
#define prarrv(x) cout << #x << ": "; for (int qsd = 0; qsd < (int)x.size(); qsd++) cout << x[qsd] << " "; cout << endl;
#define ppair(x) "(" << x.first << ", " << x.second << ")"
#define inside sl<=l&&r<=sr
#define outside sr<l||r<sl
typedef long long ll;
string s;
int dist(string str){
string ana = s;
ll ans = 0;
int say = 0;
for (int i = str.size()-1; i >= 0; i--){
for (int j = ana.size()-1; j >= 0; j--){
if (ana[j] == str[i]){
ana[j] = 'x';
say++;
break;
} else if (ana[j] != 'x') ans++;
}
}
if (say != str.size()) return 1000000000;
return ans;
}
int dp[404][404][404][4];
vector<int> rler, gler, yler;
int sufsumg[404];
int sufsumy[404];
int sufsumr[404];
int f(int r, int g, int y, int last){
//cout << "f(" << r << ", " << g << ", " << y << ", " << last << ")" << endl;
if (dp[r][g][y][last] != -1) return dp[r][g][y][last];
int minn = 400*400+2;
if ((r > 0) && (last != 0)){
int ret = f(r-1, g, y, 0);
int ind = rler[rler.size()-r];
ret += max(sufsumr[ind]-1, r-1) + max(sufsumg[ind], g) + max(sufsumy[ind], y) - (g+y+r-1);
minn = min(minn, ret);
}
if (g > 0 && last != 1){
int ret = f(r, g-1, y, 1);
int ind = gler[gler.size()-g];
ret += max(sufsumr[ind], r) + max(sufsumg[ind]-1, g-1) + max(sufsumy[ind], y) - (g+y+r-1);
minn = min(minn, ret);
}
if (y > 0 && last != 2){
int ret = f(r, g, y-1, 2);
int ind = yler[yler.size()-y];
ret += max(sufsumr[ind], r) + max(sufsumg[ind], g) + max(sufsumy[ind]-1, y-1) - (g+y+r-1);
minn = min(minn, ret);
}
if (g == 0 && r == 0 && y == 0){
return dp[r][g][y][last] = 0;
}
return dp[r][g][y][last] = minn;
}
int main(){
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
clock_t start = clock();
int n;
cin >> n;
cin >> s;
n = s.size();
int r = 0, g = 0, y = 0;
for (int i = 0; i < n; i++){
r += s[i] == 'R';
g += s[i] == 'G';
y += s[i] == 'Y';
if (s[i] == 'R') rler.pb(i);
else if (s[i] == 'G') gler.pb(i);
else yler.pb(i);
}
for (int i1 = 0; i1 <= r; i1++)
for (int i2 = 0; i2 <= g; i2++)
for (int i3 = 0; i3 <= y; i3++)
for (int i4 = 0; i4 < 4; i4++)
dp[i1][i2][i3][i4] = -1;
int sufr = 0, sufg = 0, sufy = 0;
for (int i = n-1; i >= 0; i--){
if (s[i] == 'R') sufr++;
if (s[i] == 'G') sufg++;
if (s[i] == 'Y') sufy++;
sufsumr[i] = sufr;
sufsumg[i] = sufg;
sufsumy[i] = sufy;
}
//cout << f(1, 0, 0, 1) << endl;
int ans = f(r, g, y, 3);
//int ans = 0;
if (ans >= 400*400+2) cout << -1 << endl;
else cout << ans << endl;
//cout << "bitti" << endl;
//cout << 1000.0*(clock()-start)/(double)CLOCKS_PER_SEC << "ms" << endl;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int dist(std::__cxx11::string)':
joi2019_ho_t3.cpp:31:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (say != str.size()) return 1000000000;
~~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:73:10: warning: unused variable 'start' [-Wunused-variable]
clock_t start = clock();
^~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
3 ms |
2012 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
11 ms |
4216 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
5 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2300 KB |
Output is correct |
31 |
Correct |
3 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2484 KB |
Output is correct |
33 |
Correct |
5 ms |
3832 KB |
Output is correct |
34 |
Correct |
4 ms |
3576 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
3 ms |
2040 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
3 ms |
2300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
125 ms |
162924 KB |
Output is correct |
3 |
Correct |
123 ms |
162296 KB |
Output is correct |
4 |
Correct |
185 ms |
162936 KB |
Output is correct |
5 |
Correct |
157 ms |
162936 KB |
Output is correct |
6 |
Correct |
122 ms |
162912 KB |
Output is correct |
7 |
Correct |
125 ms |
162168 KB |
Output is correct |
8 |
Correct |
124 ms |
162220 KB |
Output is correct |
9 |
Correct |
124 ms |
161276 KB |
Output is correct |
10 |
Correct |
125 ms |
162936 KB |
Output is correct |
11 |
Correct |
125 ms |
163068 KB |
Output is correct |
12 |
Correct |
35 ms |
44280 KB |
Output is correct |
13 |
Correct |
60 ms |
77176 KB |
Output is correct |
14 |
Correct |
87 ms |
111460 KB |
Output is correct |
15 |
Correct |
126 ms |
162940 KB |
Output is correct |
16 |
Correct |
126 ms |
162944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
3 ms |
2012 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
11 ms |
4216 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
5 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2300 KB |
Output is correct |
31 |
Correct |
3 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2484 KB |
Output is correct |
33 |
Correct |
5 ms |
3832 KB |
Output is correct |
34 |
Correct |
4 ms |
3576 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
3 ms |
2040 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
3 ms |
2300 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
125 ms |
162924 KB |
Output is correct |
44 |
Correct |
123 ms |
162296 KB |
Output is correct |
45 |
Correct |
185 ms |
162936 KB |
Output is correct |
46 |
Correct |
157 ms |
162936 KB |
Output is correct |
47 |
Correct |
122 ms |
162912 KB |
Output is correct |
48 |
Correct |
125 ms |
162168 KB |
Output is correct |
49 |
Correct |
124 ms |
162220 KB |
Output is correct |
50 |
Correct |
124 ms |
161276 KB |
Output is correct |
51 |
Correct |
125 ms |
162936 KB |
Output is correct |
52 |
Correct |
125 ms |
163068 KB |
Output is correct |
53 |
Correct |
35 ms |
44280 KB |
Output is correct |
54 |
Correct |
60 ms |
77176 KB |
Output is correct |
55 |
Correct |
87 ms |
111460 KB |
Output is correct |
56 |
Correct |
126 ms |
162940 KB |
Output is correct |
57 |
Correct |
126 ms |
162944 KB |
Output is correct |
58 |
Correct |
215 ms |
101240 KB |
Output is correct |
59 |
Correct |
230 ms |
119544 KB |
Output is correct |
60 |
Correct |
231 ms |
110968 KB |
Output is correct |
61 |
Correct |
255 ms |
109580 KB |
Output is correct |
62 |
Correct |
151 ms |
161272 KB |
Output is correct |
63 |
Correct |
165 ms |
161556 KB |
Output is correct |
64 |
Correct |
153 ms |
155896 KB |
Output is correct |
65 |
Correct |
163 ms |
144760 KB |
Output is correct |
66 |
Correct |
222 ms |
109816 KB |
Output is correct |
67 |
Correct |
218 ms |
106872 KB |
Output is correct |
68 |
Correct |
226 ms |
111992 KB |
Output is correct |
69 |
Correct |
226 ms |
110200 KB |
Output is correct |
70 |
Correct |
231 ms |
115676 KB |
Output is correct |
71 |
Correct |
216 ms |
108280 KB |
Output is correct |
72 |
Correct |
42 ms |
45052 KB |
Output is correct |
73 |
Correct |
5 ms |
3832 KB |
Output is correct |