#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>
#include <bitset>
#include <map>
#include <cassert>
#include <array>
#include <queue>
#include <cstring>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
// #define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll infll = 1e18;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-14;
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("a.in", "r", stdin);
// freopen("outputik.txt", "w", stdout);
}
int dp[401][202][202][3];
array<int, 3> cnt[401];
vi kek[3];
int get(array<int, 3> lol, int a, int b, int c){
int ans = 0;
ans += max(0, lol[0]-a);
ans += max(0, lol[1]-b);
ans += max(0, lol[2]-c);
return ans;
}
void solve(){
int n;
cin >> n;
string s;
cin >> s;
for(auto &i:dp)
for(auto &j:i)
for(auto &l:j)
for(auto &k:l)
k = inf;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(char &c:s){
if(c == 'R')
c = 0;
else if(c == 'G')
c = 1;
else
c = 2;
}
for(int i=1; i<=n; i++){
kek[s[i-1]].pb(i);
}
for(int i=1; i<=n; i++){
cnt[i] = cnt[i-1];
cnt[i][s[i-1]]++;
}
if(max({cnt[n][0], cnt[n][1], cnt[n][2]}) > (n+1)/2){
cout << -1 << endl;
return;
}
for(int i=1; i<=n; i++){
// cur == R
{
for(int r=1; r<=cnt[n][0]; r++){
for(int g=0; g<=cnt[n][1]; g++){
if(r + g > i)
continue;
dp[i][r][g][0] = min(dp[i][r][g][0], dp[i-1][r-1][g][1] + get(cnt[kek[0][r-1]], r, g, i-r-g));
dp[i][r][g][0] = min(dp[i][r][g][0], dp[i-1][r-1][g][2] + get(cnt[kek[0][r-1]], r, g, i-r-g));
}
}
}
{
for(int r=0; r<=cnt[n][0]; r++){
for(int g=1; g<=cnt[n][1]; g++){
if(r + g > i)
continue;
dp[i][r][g][1] = min(dp[i][r][g][1], dp[i-1][r][g-1][0] + get(cnt[kek[1][g-1]], r, g, i-r-g));
dp[i][r][g][1] = min(dp[i][r][g][1], dp[i-1][r][g-1][2] + get(cnt[kek[1][g-1]], r, g, i-r-g));
}
}
}
{
for(int r=0; r<=min(i, cnt[n][0]); r++){
for(int g=0; g<=min(i, cnt[n][1]); g++){
if((i-r-g)<=0 || (i-r-g) > cnt[n][2])
continue;
dp[i][r][g][2] = min(dp[i][r][g][2], dp[i-1][r][g][0] + get(cnt[kek[2][i-r-g-1]], r, g, i-r-g));
dp[i][r][g][2] = min(dp[i][r][g][2], dp[i-1][r][g][1] + get(cnt[kek[2][i-r-g-1]], r, g, i-r-g));
}
}
}
}
cout << *min_element(dp[n][cnt[n][0]][cnt[n][1]], dp[n][cnt[n][0]][cnt[n][1]]+3) << endl;
}
signed main(){
fast_io();
// ;(time(NULL));
cout << fixed << setprecision(10);
int q = 1;
// cin >> q;
while(q--)
solve();
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:85:19: warning: array subscript has type 'char' [-Wchar-subscripts]
85 | kek[s[i-1]].pb(i);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
192432 KB |
Output is correct |
2 |
Correct |
117 ms |
192492 KB |
Output is correct |
3 |
Correct |
117 ms |
192492 KB |
Output is correct |
4 |
Correct |
137 ms |
192492 KB |
Output is correct |
5 |
Correct |
134 ms |
192492 KB |
Output is correct |
6 |
Correct |
117 ms |
192492 KB |
Output is correct |
7 |
Correct |
117 ms |
192640 KB |
Output is correct |
8 |
Correct |
128 ms |
192492 KB |
Output is correct |
9 |
Correct |
135 ms |
192492 KB |
Output is correct |
10 |
Correct |
119 ms |
192492 KB |
Output is correct |
11 |
Correct |
118 ms |
192620 KB |
Output is correct |
12 |
Correct |
131 ms |
192492 KB |
Output is correct |
13 |
Correct |
121 ms |
192492 KB |
Output is correct |
14 |
Correct |
120 ms |
192640 KB |
Output is correct |
15 |
Correct |
118 ms |
192492 KB |
Output is correct |
16 |
Correct |
133 ms |
192492 KB |
Output is correct |
17 |
Correct |
130 ms |
192492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
192432 KB |
Output is correct |
2 |
Correct |
117 ms |
192492 KB |
Output is correct |
3 |
Correct |
117 ms |
192492 KB |
Output is correct |
4 |
Correct |
137 ms |
192492 KB |
Output is correct |
5 |
Correct |
134 ms |
192492 KB |
Output is correct |
6 |
Correct |
117 ms |
192492 KB |
Output is correct |
7 |
Correct |
117 ms |
192640 KB |
Output is correct |
8 |
Correct |
128 ms |
192492 KB |
Output is correct |
9 |
Correct |
135 ms |
192492 KB |
Output is correct |
10 |
Correct |
119 ms |
192492 KB |
Output is correct |
11 |
Correct |
118 ms |
192620 KB |
Output is correct |
12 |
Correct |
131 ms |
192492 KB |
Output is correct |
13 |
Correct |
121 ms |
192492 KB |
Output is correct |
14 |
Correct |
120 ms |
192640 KB |
Output is correct |
15 |
Correct |
118 ms |
192492 KB |
Output is correct |
16 |
Correct |
133 ms |
192492 KB |
Output is correct |
17 |
Correct |
130 ms |
192492 KB |
Output is correct |
18 |
Correct |
118 ms |
192492 KB |
Output is correct |
19 |
Correct |
120 ms |
192512 KB |
Output is correct |
20 |
Correct |
121 ms |
192492 KB |
Output is correct |
21 |
Correct |
118 ms |
192492 KB |
Output is correct |
22 |
Correct |
130 ms |
192620 KB |
Output is correct |
23 |
Correct |
118 ms |
192492 KB |
Output is correct |
24 |
Correct |
123 ms |
192492 KB |
Output is correct |
25 |
Correct |
120 ms |
192480 KB |
Output is correct |
26 |
Correct |
123 ms |
192492 KB |
Output is correct |
27 |
Correct |
120 ms |
192620 KB |
Output is correct |
28 |
Correct |
118 ms |
192456 KB |
Output is correct |
29 |
Correct |
121 ms |
192492 KB |
Output is correct |
30 |
Correct |
121 ms |
192492 KB |
Output is correct |
31 |
Correct |
135 ms |
192492 KB |
Output is correct |
32 |
Correct |
134 ms |
192492 KB |
Output is correct |
33 |
Correct |
120 ms |
192492 KB |
Output is correct |
34 |
Correct |
130 ms |
192492 KB |
Output is correct |
35 |
Correct |
123 ms |
192556 KB |
Output is correct |
36 |
Correct |
119 ms |
192400 KB |
Output is correct |
37 |
Correct |
119 ms |
192492 KB |
Output is correct |
38 |
Correct |
127 ms |
192492 KB |
Output is correct |
39 |
Correct |
118 ms |
192504 KB |
Output is correct |
40 |
Correct |
117 ms |
192492 KB |
Output is correct |
41 |
Correct |
139 ms |
192492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
192492 KB |
Output is correct |
2 |
Correct |
274 ms |
192416 KB |
Output is correct |
3 |
Correct |
324 ms |
192552 KB |
Output is correct |
4 |
Correct |
276 ms |
192620 KB |
Output is correct |
5 |
Correct |
281 ms |
192492 KB |
Output is correct |
6 |
Correct |
298 ms |
192492 KB |
Output is correct |
7 |
Correct |
274 ms |
192620 KB |
Output is correct |
8 |
Correct |
281 ms |
192520 KB |
Output is correct |
9 |
Correct |
271 ms |
192492 KB |
Output is correct |
10 |
Correct |
273 ms |
192492 KB |
Output is correct |
11 |
Correct |
283 ms |
192768 KB |
Output is correct |
12 |
Correct |
143 ms |
192620 KB |
Output is correct |
13 |
Correct |
171 ms |
192492 KB |
Output is correct |
14 |
Correct |
227 ms |
192504 KB |
Output is correct |
15 |
Correct |
120 ms |
192492 KB |
Output is correct |
16 |
Correct |
120 ms |
192492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
192432 KB |
Output is correct |
2 |
Correct |
117 ms |
192492 KB |
Output is correct |
3 |
Correct |
117 ms |
192492 KB |
Output is correct |
4 |
Correct |
137 ms |
192492 KB |
Output is correct |
5 |
Correct |
134 ms |
192492 KB |
Output is correct |
6 |
Correct |
117 ms |
192492 KB |
Output is correct |
7 |
Correct |
117 ms |
192640 KB |
Output is correct |
8 |
Correct |
128 ms |
192492 KB |
Output is correct |
9 |
Correct |
135 ms |
192492 KB |
Output is correct |
10 |
Correct |
119 ms |
192492 KB |
Output is correct |
11 |
Correct |
118 ms |
192620 KB |
Output is correct |
12 |
Correct |
131 ms |
192492 KB |
Output is correct |
13 |
Correct |
121 ms |
192492 KB |
Output is correct |
14 |
Correct |
120 ms |
192640 KB |
Output is correct |
15 |
Correct |
118 ms |
192492 KB |
Output is correct |
16 |
Correct |
133 ms |
192492 KB |
Output is correct |
17 |
Correct |
130 ms |
192492 KB |
Output is correct |
18 |
Correct |
118 ms |
192492 KB |
Output is correct |
19 |
Correct |
120 ms |
192512 KB |
Output is correct |
20 |
Correct |
121 ms |
192492 KB |
Output is correct |
21 |
Correct |
118 ms |
192492 KB |
Output is correct |
22 |
Correct |
130 ms |
192620 KB |
Output is correct |
23 |
Correct |
118 ms |
192492 KB |
Output is correct |
24 |
Correct |
123 ms |
192492 KB |
Output is correct |
25 |
Correct |
120 ms |
192480 KB |
Output is correct |
26 |
Correct |
123 ms |
192492 KB |
Output is correct |
27 |
Correct |
120 ms |
192620 KB |
Output is correct |
28 |
Correct |
118 ms |
192456 KB |
Output is correct |
29 |
Correct |
121 ms |
192492 KB |
Output is correct |
30 |
Correct |
121 ms |
192492 KB |
Output is correct |
31 |
Correct |
135 ms |
192492 KB |
Output is correct |
32 |
Correct |
134 ms |
192492 KB |
Output is correct |
33 |
Correct |
120 ms |
192492 KB |
Output is correct |
34 |
Correct |
130 ms |
192492 KB |
Output is correct |
35 |
Correct |
123 ms |
192556 KB |
Output is correct |
36 |
Correct |
119 ms |
192400 KB |
Output is correct |
37 |
Correct |
119 ms |
192492 KB |
Output is correct |
38 |
Correct |
127 ms |
192492 KB |
Output is correct |
39 |
Correct |
118 ms |
192504 KB |
Output is correct |
40 |
Correct |
117 ms |
192492 KB |
Output is correct |
41 |
Correct |
139 ms |
192492 KB |
Output is correct |
42 |
Correct |
118 ms |
192492 KB |
Output is correct |
43 |
Correct |
274 ms |
192416 KB |
Output is correct |
44 |
Correct |
324 ms |
192552 KB |
Output is correct |
45 |
Correct |
276 ms |
192620 KB |
Output is correct |
46 |
Correct |
281 ms |
192492 KB |
Output is correct |
47 |
Correct |
298 ms |
192492 KB |
Output is correct |
48 |
Correct |
274 ms |
192620 KB |
Output is correct |
49 |
Correct |
281 ms |
192520 KB |
Output is correct |
50 |
Correct |
271 ms |
192492 KB |
Output is correct |
51 |
Correct |
273 ms |
192492 KB |
Output is correct |
52 |
Correct |
283 ms |
192768 KB |
Output is correct |
53 |
Correct |
143 ms |
192620 KB |
Output is correct |
54 |
Correct |
171 ms |
192492 KB |
Output is correct |
55 |
Correct |
227 ms |
192504 KB |
Output is correct |
56 |
Correct |
120 ms |
192492 KB |
Output is correct |
57 |
Correct |
120 ms |
192492 KB |
Output is correct |
58 |
Correct |
231 ms |
192696 KB |
Output is correct |
59 |
Correct |
231 ms |
192492 KB |
Output is correct |
60 |
Correct |
222 ms |
192492 KB |
Output is correct |
61 |
Correct |
221 ms |
192620 KB |
Output is correct |
62 |
Correct |
121 ms |
192492 KB |
Output is correct |
63 |
Correct |
272 ms |
192492 KB |
Output is correct |
64 |
Correct |
265 ms |
192492 KB |
Output is correct |
65 |
Correct |
258 ms |
192492 KB |
Output is correct |
66 |
Correct |
224 ms |
192492 KB |
Output is correct |
67 |
Correct |
215 ms |
192492 KB |
Output is correct |
68 |
Correct |
225 ms |
192492 KB |
Output is correct |
69 |
Correct |
221 ms |
192492 KB |
Output is correct |
70 |
Correct |
233 ms |
192492 KB |
Output is correct |
71 |
Correct |
219 ms |
192492 KB |
Output is correct |
72 |
Correct |
119 ms |
192492 KB |
Output is correct |
73 |
Correct |
118 ms |
192492 KB |
Output is correct |