#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 100000000000000000
#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];
vector<int> g[3];
int cmp[N][3];
int f(int i, int aused, int bused, int prev){
//cout<<i<<" "<<aused<<" "<<bused<<" "<<prev<<endl;
if(i>=n) return 0;
if(dp[i][aused][bused][prev] > -1) return dp[i][aused][bused][prev];
int cnt[] = {aused, bused, i - (aused + bused)};
int tr = INF;
for(int j = 0;j<3;j++){
if(j==prev || g[j].size() <= cnt[j]) continue;
int currind = g[j][cnt[j]];
for(int k = 0;k<3;k++){
if(k==j) continue;
currind += max((int)0, cnt[k] - cmp[g[j][cnt[j]]][k]);
//cout<<"equivalent at index "<<g[j][cnt[j]]<<" for type "<<k<<" is "<<cmp[g[j][cnt[j]]][k]<<endl;
}
cnt[j]++;
//cout<<"putting "<<j<<", cost is "<<(currind - (i+1))<<endl;
tr = min(tr, (currind - (i+1)) + f(i+1, cnt[0], cnt[1], j));
cnt[j]--;
}
if(prev!=4) return dp[i][aused][bused][prev] = tr;
return tr;
}
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] = g[0].size();
cmp[i][1] = g[1].size();
cmp[i][2] = g[2].size();
g[arr[i]].push_back(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] = -1;
int ans = f(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 'long long int f(long long int, long long int, long long int, long long int)':
joi2019_ho_t3.cpp:27:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j==prev || g[j].size() <= cnt[j]) continue;
~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:23:32: warning: array subscript is above array bounds [-Warray-bounds]
if(dp[i][aused][bused][prev] > -1) return dp[i][aused][bused][prev];
~~~~~~~~~~~~~~~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 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 |
1664 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 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 |
1664 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 |
18 ms |
22516 KB |
Output is correct |
19 |
Correct |
15 ms |
22528 KB |
Output is correct |
20 |
Correct |
15 ms |
22528 KB |
Output is correct |
21 |
Correct |
15 ms |
22528 KB |
Output is correct |
22 |
Correct |
15 ms |
22528 KB |
Output is correct |
23 |
Correct |
16 ms |
22656 KB |
Output is correct |
24 |
Correct |
16 ms |
22528 KB |
Output is correct |
25 |
Correct |
16 ms |
22656 KB |
Output is correct |
26 |
Correct |
16 ms |
22528 KB |
Output is correct |
27 |
Correct |
16 ms |
22528 KB |
Output is correct |
28 |
Correct |
16 ms |
22528 KB |
Output is correct |
29 |
Correct |
16 ms |
22632 KB |
Output is correct |
30 |
Correct |
16 ms |
22656 KB |
Output is correct |
31 |
Correct |
16 ms |
22528 KB |
Output is correct |
32 |
Correct |
15 ms |
22528 KB |
Output is correct |
33 |
Correct |
16 ms |
21760 KB |
Output is correct |
34 |
Correct |
15 ms |
21760 KB |
Output is correct |
35 |
Correct |
16 ms |
20992 KB |
Output is correct |
36 |
Correct |
15 ms |
21760 KB |
Output is correct |
37 |
Correct |
14 ms |
20096 KB |
Output is correct |
38 |
Correct |
15 ms |
22528 KB |
Output is correct |
39 |
Correct |
15 ms |
22528 KB |
Output is correct |
40 |
Correct |
16 ms |
21760 KB |
Output is correct |
41 |
Correct |
17 ms |
22656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Runtime error |
633 ms |
1048576 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 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 |
1664 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 |
18 ms |
22516 KB |
Output is correct |
19 |
Correct |
15 ms |
22528 KB |
Output is correct |
20 |
Correct |
15 ms |
22528 KB |
Output is correct |
21 |
Correct |
15 ms |
22528 KB |
Output is correct |
22 |
Correct |
15 ms |
22528 KB |
Output is correct |
23 |
Correct |
16 ms |
22656 KB |
Output is correct |
24 |
Correct |
16 ms |
22528 KB |
Output is correct |
25 |
Correct |
16 ms |
22656 KB |
Output is correct |
26 |
Correct |
16 ms |
22528 KB |
Output is correct |
27 |
Correct |
16 ms |
22528 KB |
Output is correct |
28 |
Correct |
16 ms |
22528 KB |
Output is correct |
29 |
Correct |
16 ms |
22632 KB |
Output is correct |
30 |
Correct |
16 ms |
22656 KB |
Output is correct |
31 |
Correct |
16 ms |
22528 KB |
Output is correct |
32 |
Correct |
15 ms |
22528 KB |
Output is correct |
33 |
Correct |
16 ms |
21760 KB |
Output is correct |
34 |
Correct |
15 ms |
21760 KB |
Output is correct |
35 |
Correct |
16 ms |
20992 KB |
Output is correct |
36 |
Correct |
15 ms |
21760 KB |
Output is correct |
37 |
Correct |
14 ms |
20096 KB |
Output is correct |
38 |
Correct |
15 ms |
22528 KB |
Output is correct |
39 |
Correct |
15 ms |
22528 KB |
Output is correct |
40 |
Correct |
16 ms |
21760 KB |
Output is correct |
41 |
Correct |
17 ms |
22656 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Runtime error |
633 ms |
1048576 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
44 |
Halted |
0 ms |
0 KB |
- |