#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 100000000
//#define int long long
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
#define ordered_set tree<pair<int, int>, null_type,less<pair<int, int>>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
int n;
const int N = 405;
int dp[N][N][N][4];
int arr[N];
int g[3][N];
int cc[] = {0, 0, 0};
int cmp[N][3];
int getans(int i, int aused, int bused, int prev){
int cused = i - (aused + bused);
int ause = INF, buse = INF, cuse = INF;
int currind;
//calculate use[0]
if(cc[0]) {
currind = g[0][0];
ause = (currind - (i + 1)) + ((i == (n - 1)) ? 0 : dp[i + 1][aused + 1][bused][0]);
}
//calculate buse
if(cc[1]) {
currind = g[1][0];
buse = (currind - (i + 1)) + ((i == (n - 1)) ? 0 : dp[i + 1][aused][bused + 1][1]);
}
//calculate cuse
if(cc[2]) {
currind = g[2][0];
cuse = (currind - (i + 1)) + ((i == (n - 1)) ? 0 : dp[i + 1][aused][bused][2]);
}
return min(ause, min(buse, cuse));
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
string s;
cin>>s;
for(int i = 1;i<=n;i++){
if(s[i-1]=='R') arr[i] = 0;
else if(s[i-1]=='G') arr[i] = 1;
else arr[i] = 2;
cmp[i][0] = cc[0];
cmp[i][1] = cc[1];
cmp[i][2] = cc[2];
g[arr[i]][cc[arr[i]]++] = i;
}
for(int i = 0;i<=n;i++) for(int j = 0;j<=n;j++) for(int k = 0;k<=n;k++) for(int x = 0;x<3;x++) dp[i][j][k][x] = INF;
for(int i = n-1;i>=1;i--){
for(int aused = n;aused>=0;aused--){
for(int bused = n;bused >= 0 ;bused--){
int cused = i - (aused + bused);
int ause = INF, buse = INF, cuse = INF;
if(aused>cc[0] || bused>cc[1] || cused>cc[2]) continue;
if(cused<0) continue;
int currind;
//calculate use[0]
if(cc[0] > aused) {
currind = g[0][aused] + max((int) 0, bused - cmp[g[0][aused]][1]) +
max((int) 0, cused - cmp[g[0][aused]][2]);
ause = (currind - (i + 1)) + ((i == (n - 1)) ? 0 : dp[i + 1][aused + 1][bused][0]);
}
//calculate buse
if(cc[1] > bused) {
currind = g[1][bused] + max((int) 0, aused - cmp[g[1][bused]][0]) +
max((int) 0, cused - cmp[g[1][bused]][2]);
buse = (currind - (i + 1)) + ((i == (n - 1)) ? 0 : dp[i + 1][aused][bused + 1][1]);
}
//calculate cuse
if(cc[2] > cused) {
currind = g[2][cused] + max((int) 0, aused - cmp[g[2][cused]][0]) +
max((int) 0, bused - cmp[g[2][cused]][1]);
cuse = (currind - (i + 1)) + ((i == (n - 1)) ? 0 : dp[i + 1][aused][bused][2]);
}
dp[i][aused][bused][0] = min(buse, cuse);
dp[i][aused][bused][1] = min(ause, cuse);
dp[i][aused][bused][2] = min(buse, ause);
}
}
}
int ans = getans(0, 0, 0, 4);
if(ans>=INF) ans = -1;
cout<<ans<<"\n";
}
//2^(2k - 1) - 2^(k-1)
/*
4 4
RGWR
GRRG
WGGW
WWWR
5 5
RGRGW
GRRGW
WGGWR
RWRGW
RGWGW
20
YYGYYYGGGGRGYYGRGRYG
*/
Compilation message
joi2019_ho_t3.cpp: In function 'int getans(int, int, int, int)':
joi2019_ho_t3.cpp:22:9: warning: unused variable 'cused' [-Wunused-variable]
int cused = i - (aused + bused);
^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
896 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
5 ms |
1536 KB |
Output is correct |
7 |
Correct |
5 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1536 KB |
Output is correct |
9 |
Correct |
5 ms |
1536 KB |
Output is correct |
10 |
Correct |
5 ms |
1536 KB |
Output is correct |
11 |
Correct |
5 ms |
1536 KB |
Output is correct |
12 |
Correct |
5 ms |
1536 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
1536 KB |
Output is correct |
16 |
Correct |
5 ms |
1536 KB |
Output is correct |
17 |
Correct |
5 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
896 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
5 ms |
1536 KB |
Output is correct |
7 |
Correct |
5 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1536 KB |
Output is correct |
9 |
Correct |
5 ms |
1536 KB |
Output is correct |
10 |
Correct |
5 ms |
1536 KB |
Output is correct |
11 |
Correct |
5 ms |
1536 KB |
Output is correct |
12 |
Correct |
5 ms |
1536 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
1536 KB |
Output is correct |
16 |
Correct |
5 ms |
1536 KB |
Output is correct |
17 |
Correct |
5 ms |
1408 KB |
Output is correct |
18 |
Correct |
14 ms |
19072 KB |
Output is correct |
19 |
Correct |
14 ms |
19072 KB |
Output is correct |
20 |
Correct |
18 ms |
19072 KB |
Output is correct |
21 |
Correct |
15 ms |
19072 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
23 |
Correct |
14 ms |
19072 KB |
Output is correct |
24 |
Correct |
13 ms |
19072 KB |
Output is correct |
25 |
Correct |
13 ms |
19072 KB |
Output is correct |
26 |
Correct |
13 ms |
19072 KB |
Output is correct |
27 |
Correct |
15 ms |
19072 KB |
Output is correct |
28 |
Correct |
14 ms |
19072 KB |
Output is correct |
29 |
Correct |
14 ms |
19072 KB |
Output is correct |
30 |
Correct |
15 ms |
19072 KB |
Output is correct |
31 |
Correct |
13 ms |
19072 KB |
Output is correct |
32 |
Correct |
13 ms |
19072 KB |
Output is correct |
33 |
Correct |
13 ms |
18432 KB |
Output is correct |
34 |
Correct |
15 ms |
18304 KB |
Output is correct |
35 |
Correct |
13 ms |
17792 KB |
Output is correct |
36 |
Correct |
13 ms |
18304 KB |
Output is correct |
37 |
Correct |
14 ms |
17024 KB |
Output is correct |
38 |
Correct |
15 ms |
19072 KB |
Output is correct |
39 |
Correct |
15 ms |
19072 KB |
Output is correct |
40 |
Correct |
13 ms |
18432 KB |
Output is correct |
41 |
Correct |
14 ms |
19072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Execution timed out |
631 ms |
1021524 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
896 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
5 ms |
1536 KB |
Output is correct |
7 |
Correct |
5 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1536 KB |
Output is correct |
9 |
Correct |
5 ms |
1536 KB |
Output is correct |
10 |
Correct |
5 ms |
1536 KB |
Output is correct |
11 |
Correct |
5 ms |
1536 KB |
Output is correct |
12 |
Correct |
5 ms |
1536 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
1536 KB |
Output is correct |
16 |
Correct |
5 ms |
1536 KB |
Output is correct |
17 |
Correct |
5 ms |
1408 KB |
Output is correct |
18 |
Correct |
14 ms |
19072 KB |
Output is correct |
19 |
Correct |
14 ms |
19072 KB |
Output is correct |
20 |
Correct |
18 ms |
19072 KB |
Output is correct |
21 |
Correct |
15 ms |
19072 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
23 |
Correct |
14 ms |
19072 KB |
Output is correct |
24 |
Correct |
13 ms |
19072 KB |
Output is correct |
25 |
Correct |
13 ms |
19072 KB |
Output is correct |
26 |
Correct |
13 ms |
19072 KB |
Output is correct |
27 |
Correct |
15 ms |
19072 KB |
Output is correct |
28 |
Correct |
14 ms |
19072 KB |
Output is correct |
29 |
Correct |
14 ms |
19072 KB |
Output is correct |
30 |
Correct |
15 ms |
19072 KB |
Output is correct |
31 |
Correct |
13 ms |
19072 KB |
Output is correct |
32 |
Correct |
13 ms |
19072 KB |
Output is correct |
33 |
Correct |
13 ms |
18432 KB |
Output is correct |
34 |
Correct |
15 ms |
18304 KB |
Output is correct |
35 |
Correct |
13 ms |
17792 KB |
Output is correct |
36 |
Correct |
13 ms |
18304 KB |
Output is correct |
37 |
Correct |
14 ms |
17024 KB |
Output is correct |
38 |
Correct |
15 ms |
19072 KB |
Output is correct |
39 |
Correct |
15 ms |
19072 KB |
Output is correct |
40 |
Correct |
13 ms |
18432 KB |
Output is correct |
41 |
Correct |
14 ms |
19072 KB |
Output is correct |
42 |
Correct |
4 ms |
512 KB |
Output is correct |
43 |
Execution timed out |
631 ms |
1021524 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |