#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
using namespace std;
namespace __gnu_pbds
{
typedef tree<ll,
null_type,
less_equal<ll>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
}
using namespace __gnu_pbds;
#define fi ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//#define ll double
#define ll1 long long
#define F first
#define S second
#define sz size()
#define all(s) s.begin(),s.end()
#define all1(s) s.rbegin(),s.rend()
int main()
{
//freopen("stdin.in","r",stdin);freopen("stdout.out","w",stdout);
//cin>>T;ll oo=0;
ll n;
cin>>n;
string s;
cin>>s;
map<ll,ll>m;
map<ll,map<ll,ll>>d;
ll arr[n];
ll o[3][n];
o[0][0]=0;
o[1][0]=0;
o[2][0]=0;
for(ll i=0; i<n; i++)
{
ll a=0;
if(s[i]=='R')a=0;
else if(s[i]=='G')a=1;
else a=2;
m[a]++;
d[a][m[a]]=i;
if(i)
{
o[0][i]=o[0][i-1];
o[1][i]=o[1][i-1];
o[2][i]=o[2][i-1];
}
o[a][i]+=1;
}
ll dp[m[0]+5][m[1]+5][m[2]+5][5];
for(ll a=0; a<=m[0]+4; a++)
{
for(ll b=0; b<=m[1]+4; b++)
{
for(ll c=0; c<=m[2]+4; c++)
{
for(ll j=0; j<=4; j++)
dp[a][b][c][j]=1e18;
}
}
}
dp[0][0][0][1]=0;
dp[0][0][0][0]=0;
dp[0][0][0][2]=0;
for(ll c=0; c<=m[2]; c++)
{
for(ll a=0; a<=m[0]; a++)
{
for(ll b=0; b<=m[1]; b++)
{ll i=a+b+c;
if(c<=m[2]&&c>=0)
{
ll xx=0,yy=0,zz=0;
ll mx;
mx=(d[2][c+1]);
xx=max(0ll,c-o[2][mx]);
yy=max(0ll,b-o[1][mx]);
zz=max(0ll,a-o[0][mx]);
dp[a][b][c+1][2]=min(dp[a][b][c+1][2],dp[a][b][c][0]+abs(d[2][c+1]+xx+yy+zz-i));
dp[a][b][c+1][2]=min(dp[a][b][c+1][2],dp[a][b][c][1]+abs(d[2][c+1]+xx+yy+zz-i));
mx=(d[1][b+1]);
xx=max(0ll,c-o[2][mx]);
yy=max(0ll,b-o[1][mx]);
zz=max(0ll,a-o[0][mx]);
dp[a][b+1][c][1]=min(dp[a][b+1][c][1],dp[a][b][c][0]+abs(d[1][b+1]+xx+yy+zz-i));
dp[a][b+1][c][1]=min(dp[a][b+1][c][1],dp[a][b][c][2]+abs(d[1][b+1]+xx+yy+zz-i));
mx=(d[0][a+1]);
xx=max(0ll,c-o[2][mx]);
yy=max(0ll,b-o[1][mx]);
zz=max(0ll,a-o[0][mx]);
dp[a+1][b][c][0]=min(dp[a+1][b][c][0],dp[a][b][c][2]+abs(d[0][a+1]+xx+yy+zz-i));
dp[a+1][b][c][0]=min(dp[a+1][b][c][0],dp[a][b][c][1]+abs(d[0][a+1]+xx+yy+zz-i));
//}
}
}
}
}
for(ll i=0; i<=n; i++)
{
for(ll a=0; a<=m[0]; a++)
{
for(ll b=0; b<=m[1]; b++)
{
ll c=i-a-b;
// if(c<=m[2])cout<<dp[n][m[0]][m[1]][m[2]][0]<<" "<<dp[n][m[0]][m[1]][m[2]][1]<<" "<<dp[n][m[0]][m[1]][m[2]][2]<<"\n";
}
}
}
if(min({dp[m[0]][m[1]][m[2]][0],dp[m[0]][m[1]][m[2]][1],dp[m[0]][m[1]][m[2]][2]})==1e18)cout<<-1;
else
cout<<min({dp[m[0]][m[1]][m[2]][0],dp[m[0]][m[1]][m[2]][1],dp[m[0]][m[1]][m[2]][2]});
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:115:20: warning: unused variable 'c' [-Wunused-variable]
115 | ll c=i-a-b;
| ^
joi2019_ho_t3.cpp:35:8: warning: unused variable 'arr' [-Wunused-variable]
35 | ll arr[n];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
856 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
60 ms |
8660 KB |
Output is correct |
3 |
Correct |
57 ms |
8624 KB |
Output is correct |
4 |
Correct |
74 ms |
8536 KB |
Output is correct |
5 |
Correct |
67 ms |
8664 KB |
Output is correct |
6 |
Correct |
58 ms |
8540 KB |
Output is correct |
7 |
Correct |
55 ms |
8540 KB |
Output is correct |
8 |
Correct |
56 ms |
8540 KB |
Output is correct |
9 |
Correct |
54 ms |
8540 KB |
Output is correct |
10 |
Correct |
57 ms |
8540 KB |
Output is correct |
11 |
Correct |
55 ms |
8540 KB |
Output is correct |
12 |
Correct |
9 ms |
2648 KB |
Output is correct |
13 |
Correct |
21 ms |
4404 KB |
Output is correct |
14 |
Correct |
37 ms |
5980 KB |
Output is correct |
15 |
Correct |
55 ms |
8540 KB |
Output is correct |
16 |
Correct |
56 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
856 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
60 ms |
8660 KB |
Output is correct |
44 |
Correct |
57 ms |
8624 KB |
Output is correct |
45 |
Correct |
74 ms |
8536 KB |
Output is correct |
46 |
Correct |
67 ms |
8664 KB |
Output is correct |
47 |
Correct |
58 ms |
8540 KB |
Output is correct |
48 |
Correct |
55 ms |
8540 KB |
Output is correct |
49 |
Correct |
56 ms |
8540 KB |
Output is correct |
50 |
Correct |
54 ms |
8540 KB |
Output is correct |
51 |
Correct |
57 ms |
8540 KB |
Output is correct |
52 |
Correct |
55 ms |
8540 KB |
Output is correct |
53 |
Correct |
9 ms |
2648 KB |
Output is correct |
54 |
Correct |
21 ms |
4404 KB |
Output is correct |
55 |
Correct |
37 ms |
5980 KB |
Output is correct |
56 |
Correct |
55 ms |
8540 KB |
Output is correct |
57 |
Correct |
56 ms |
8540 KB |
Output is correct |
58 |
Correct |
473 ms |
103076 KB |
Output is correct |
59 |
Correct |
498 ms |
103016 KB |
Output is correct |
60 |
Correct |
474 ms |
102064 KB |
Output is correct |
61 |
Correct |
480 ms |
103504 KB |
Output is correct |
62 |
Correct |
110 ms |
14680 KB |
Output is correct |
63 |
Correct |
114 ms |
20824 KB |
Output is correct |
64 |
Correct |
296 ms |
54352 KB |
Output is correct |
65 |
Correct |
366 ms |
75260 KB |
Output is correct |
66 |
Correct |
469 ms |
101556 KB |
Output is correct |
67 |
Correct |
482 ms |
101972 KB |
Output is correct |
68 |
Correct |
483 ms |
103236 KB |
Output is correct |
69 |
Correct |
485 ms |
103972 KB |
Output is correct |
70 |
Correct |
491 ms |
103652 KB |
Output is correct |
71 |
Correct |
463 ms |
101200 KB |
Output is correct |
72 |
Correct |
123 ms |
29532 KB |
Output is correct |
73 |
Correct |
32 ms |
11864 KB |
Output is correct |