#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=(j);i<=(k);i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
//#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=400+5;
const ll offset=1e18;
const ll block_sz=317;
const ll inf=1e9;
const ll mod=1e9+7;
int n,a[maxn],id[maxn],dp[maxn][maxn][maxn][3],pre[maxn][3];
string s;
vector<int> L1,L2,L0;
int cost(int j0,int j1,int j2,int add)
{
if (add==0)
{
return max(pre[L0[j0]][1] - pre[L1[j1]][1],0) +
max(pre[L0[j0]][2] - pre[L2[j2]][2],0);
}
else if (add==1)
{
return max(pre[L1[j1]][0] - pre[L0[j0]][0],0) +
max(pre[L1[j1]][2] - pre[L2[j2]][2],0);
}
else
{
return max(pre[L2[j2]][0] - pre[L0[j0]][0],0) +
max(pre[L2[j2]][1] - pre[L1[j1]][1],0);
}
}
void sol()
{
id['R']=0;
id['G']=1;
id['Y']=2;
cin >> n>> s;
int n0=0,n1=0,n2=0;
L1.pb(0);
L2.pb(0);
L0.pb(0);
for1(i,1,n)
{
a[i]=id[s[i-1]];
if(a[i]==0)
{
n0++;
L0.pb(i);
}
if(a[i]==1)
{
n1++;
L1.pb(i);
}
if(a[i]==2)
{
n2++;
L2.pb(i);
}
}
for1(j,0,2)
{
for1(i,1,n)
{
pre[i][j]=pre[i-1][j]+(a[i]==j);
}
}
for1(i,0,n) for1(j0,0,n) for1(j1,0,n) for1(k,0,2) dp[i][j0][j1][k]=inf;
dp[0][0][0][0]=0;
dp[0][0][0][1]=0;
dp[0][0][0][2]=0;
for1(i,1,n)
{
for1(j0,0,int(min(n0,i)))
{
for1(j1,0,int(min(n1,i-j0)))
{
int j2=i-j0-j1;
if (j2>n2) continue;
if (j0>0) dp[i][j0][j1][0]=min(dp[i][j0][j1][0],min(dp[i-1][j0-1][j1][1],dp[i-1][j0-1][j1][2])+cost(j0,j1,j2,0));
if (j1>0) dp[i][j0][j1][1]=min(dp[i][j0][j1][1],min(dp[i-1][j0][j1-1][2],dp[i-1][j0][j1-1][0])+cost(j0,j1,j2,1));
if (j2>0)
{
dp[i][j0][j1][2]=min(dp[i][j0][j1][2],min(dp[i-1][j0][j1][1],dp[i-1][j0][j1][0])+cost(j0,j1,j2,2));
// if (i==1 && j0==0 && j1==0) cerr<< "awdawd\n";
}
// cerr<<i<<' ' << j0<<' '<<j1<<' '<<dp[n][n0][n1][0]<< ' '<<dp[n][n0][n1][1]<<' '<<dp[n][n0][n1][2]<<'\n';
}
}
}
// cerr<< cost(0,0,1,2)<<" wtf\n";
int ans=min(dp[n][n0][n1][0],min(dp[n][n0][n1][1],dp[n][n0][n1][2]));
if (ans>=inf) cout << "-1";
else cout << ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;//cin >> t;
while (t--)
{
sol();
}
}
/*
3 5 2 4 1
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void sol()':
joi2019_ho_t3.cpp:58:23: warning: array subscript has type 'char' [-Wchar-subscripts]
58 | a[i]=id[s[i-1]];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
31064 KB |
Output is correct |
6 |
Correct |
3 ms |
31068 KB |
Output is correct |
7 |
Correct |
4 ms |
31068 KB |
Output is correct |
8 |
Correct |
4 ms |
31064 KB |
Output is correct |
9 |
Correct |
3 ms |
31064 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
3 ms |
31064 KB |
Output is correct |
12 |
Correct |
5 ms |
31064 KB |
Output is correct |
13 |
Correct |
4 ms |
31064 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
4 ms |
31064 KB |
Output is correct |
16 |
Correct |
4 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
29016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
31064 KB |
Output is correct |
6 |
Correct |
3 ms |
31068 KB |
Output is correct |
7 |
Correct |
4 ms |
31068 KB |
Output is correct |
8 |
Correct |
4 ms |
31064 KB |
Output is correct |
9 |
Correct |
3 ms |
31064 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
3 ms |
31064 KB |
Output is correct |
12 |
Correct |
5 ms |
31064 KB |
Output is correct |
13 |
Correct |
4 ms |
31064 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
4 ms |
31064 KB |
Output is correct |
16 |
Correct |
4 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
29016 KB |
Output is correct |
18 |
Correct |
12 ms |
117592 KB |
Output is correct |
19 |
Correct |
12 ms |
117512 KB |
Output is correct |
20 |
Correct |
12 ms |
117596 KB |
Output is correct |
21 |
Correct |
13 ms |
117636 KB |
Output is correct |
22 |
Correct |
12 ms |
117516 KB |
Output is correct |
23 |
Correct |
13 ms |
117596 KB |
Output is correct |
24 |
Correct |
12 ms |
117848 KB |
Output is correct |
25 |
Correct |
13 ms |
117596 KB |
Output is correct |
26 |
Correct |
12 ms |
117596 KB |
Output is correct |
27 |
Correct |
12 ms |
117596 KB |
Output is correct |
28 |
Correct |
11 ms |
117592 KB |
Output is correct |
29 |
Correct |
12 ms |
117592 KB |
Output is correct |
30 |
Correct |
12 ms |
117592 KB |
Output is correct |
31 |
Correct |
12 ms |
117592 KB |
Output is correct |
32 |
Correct |
12 ms |
117472 KB |
Output is correct |
33 |
Correct |
12 ms |
115544 KB |
Output is correct |
34 |
Correct |
12 ms |
115412 KB |
Output is correct |
35 |
Correct |
13 ms |
113240 KB |
Output is correct |
36 |
Correct |
13 ms |
115288 KB |
Output is correct |
37 |
Correct |
11 ms |
111192 KB |
Output is correct |
38 |
Correct |
11 ms |
117596 KB |
Output is correct |
39 |
Correct |
13 ms |
117592 KB |
Output is correct |
40 |
Correct |
11 ms |
115548 KB |
Output is correct |
41 |
Correct |
13 ms |
117492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
248 ms |
774200 KB |
Output is correct |
3 |
Correct |
158 ms |
772012 KB |
Output is correct |
4 |
Correct |
145 ms |
774068 KB |
Output is correct |
5 |
Correct |
141 ms |
774224 KB |
Output is correct |
6 |
Correct |
140 ms |
774096 KB |
Output is correct |
7 |
Correct |
141 ms |
772160 KB |
Output is correct |
8 |
Correct |
142 ms |
771988 KB |
Output is correct |
9 |
Correct |
142 ms |
770128 KB |
Output is correct |
10 |
Correct |
145 ms |
774112 KB |
Output is correct |
11 |
Correct |
148 ms |
774224 KB |
Output is correct |
12 |
Correct |
48 ms |
398588 KB |
Output is correct |
13 |
Correct |
73 ms |
529952 KB |
Output is correct |
14 |
Correct |
100 ms |
638676 KB |
Output is correct |
15 |
Correct |
143 ms |
774228 KB |
Output is correct |
16 |
Correct |
144 ms |
774168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
31064 KB |
Output is correct |
6 |
Correct |
3 ms |
31068 KB |
Output is correct |
7 |
Correct |
4 ms |
31068 KB |
Output is correct |
8 |
Correct |
4 ms |
31064 KB |
Output is correct |
9 |
Correct |
3 ms |
31064 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
3 ms |
31064 KB |
Output is correct |
12 |
Correct |
5 ms |
31064 KB |
Output is correct |
13 |
Correct |
4 ms |
31064 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
4 ms |
31064 KB |
Output is correct |
16 |
Correct |
4 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
29016 KB |
Output is correct |
18 |
Correct |
12 ms |
117592 KB |
Output is correct |
19 |
Correct |
12 ms |
117512 KB |
Output is correct |
20 |
Correct |
12 ms |
117596 KB |
Output is correct |
21 |
Correct |
13 ms |
117636 KB |
Output is correct |
22 |
Correct |
12 ms |
117516 KB |
Output is correct |
23 |
Correct |
13 ms |
117596 KB |
Output is correct |
24 |
Correct |
12 ms |
117848 KB |
Output is correct |
25 |
Correct |
13 ms |
117596 KB |
Output is correct |
26 |
Correct |
12 ms |
117596 KB |
Output is correct |
27 |
Correct |
12 ms |
117596 KB |
Output is correct |
28 |
Correct |
11 ms |
117592 KB |
Output is correct |
29 |
Correct |
12 ms |
117592 KB |
Output is correct |
30 |
Correct |
12 ms |
117592 KB |
Output is correct |
31 |
Correct |
12 ms |
117592 KB |
Output is correct |
32 |
Correct |
12 ms |
117472 KB |
Output is correct |
33 |
Correct |
12 ms |
115544 KB |
Output is correct |
34 |
Correct |
12 ms |
115412 KB |
Output is correct |
35 |
Correct |
13 ms |
113240 KB |
Output is correct |
36 |
Correct |
13 ms |
115288 KB |
Output is correct |
37 |
Correct |
11 ms |
111192 KB |
Output is correct |
38 |
Correct |
11 ms |
117596 KB |
Output is correct |
39 |
Correct |
13 ms |
117592 KB |
Output is correct |
40 |
Correct |
11 ms |
115548 KB |
Output is correct |
41 |
Correct |
13 ms |
117492 KB |
Output is correct |
42 |
Correct |
1 ms |
6488 KB |
Output is correct |
43 |
Correct |
248 ms |
774200 KB |
Output is correct |
44 |
Correct |
158 ms |
772012 KB |
Output is correct |
45 |
Correct |
145 ms |
774068 KB |
Output is correct |
46 |
Correct |
141 ms |
774224 KB |
Output is correct |
47 |
Correct |
140 ms |
774096 KB |
Output is correct |
48 |
Correct |
141 ms |
772160 KB |
Output is correct |
49 |
Correct |
142 ms |
771988 KB |
Output is correct |
50 |
Correct |
142 ms |
770128 KB |
Output is correct |
51 |
Correct |
145 ms |
774112 KB |
Output is correct |
52 |
Correct |
148 ms |
774224 KB |
Output is correct |
53 |
Correct |
48 ms |
398588 KB |
Output is correct |
54 |
Correct |
73 ms |
529952 KB |
Output is correct |
55 |
Correct |
100 ms |
638676 KB |
Output is correct |
56 |
Correct |
143 ms |
774228 KB |
Output is correct |
57 |
Correct |
144 ms |
774168 KB |
Output is correct |
58 |
Correct |
173 ms |
774224 KB |
Output is correct |
59 |
Correct |
173 ms |
773992 KB |
Output is correct |
60 |
Correct |
172 ms |
770080 KB |
Output is correct |
61 |
Correct |
174 ms |
772176 KB |
Output is correct |
62 |
Correct |
146 ms |
774172 KB |
Output is correct |
63 |
Correct |
165 ms |
774228 KB |
Output is correct |
64 |
Correct |
161 ms |
774224 KB |
Output is correct |
65 |
Correct |
176 ms |
772108 KB |
Output is correct |
66 |
Correct |
165 ms |
767828 KB |
Output is correct |
67 |
Correct |
176 ms |
774224 KB |
Output is correct |
68 |
Correct |
173 ms |
772244 KB |
Output is correct |
69 |
Correct |
173 ms |
774024 KB |
Output is correct |
70 |
Correct |
170 ms |
774224 KB |
Output is correct |
71 |
Correct |
168 ms |
771924 KB |
Output is correct |
72 |
Correct |
144 ms |
772168 KB |
Output is correct |
73 |
Correct |
138 ms |
774224 KB |
Output is correct |