#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);
//order_of_key()
#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define fi first
#define se second
#define la "LA"
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define pii pair<int,int>
#define vi vector<int>
int dp[401][401][401][3];
vi a[3];
int R=0, G=0, Y=0;
int br[401], bg[401], by[401];
int n;
int fn(int r, int g, int y, int prev)
{
// cout<<r<<' '<<g<<' '<<y<<endl;
if (r+g+y==n) return 0;
//int x=r+g+y;
if (dp[r][g][y][prev]!=0) return dp[r][g][y][prev];
int c=bigg;
if (r<R && prev!=0) c=min(c,(fn(r+1,g,y,0)+max(0ll,g-bg[a[0][r]])+max(0ll, y-by[a[0][r]])));
if (g<G && prev!=1) c=min(c,(fn(r,g+1,y,1)+max(0ll,r-br[a[1][g]])+max(0ll, y-by[a[1][g]])));
if (y<Y && prev!=2) c=min(c,(fn(r,g,y+1,2)+max(0ll,r-br[a[2][y]])+max(0ll, g-bg[a[2][y]])));
// cout<<no<<r<<' '<<g<<' '<<y<<' '<<c<<endl;
return dp[r][g][y][prev]=c;
}
signed main()
{
// #ifndef LOCAL
// freopen("lifeguards.in","r",stdin);
// freopen("lifeguards.out","w", stdout);
// #endif
fast
// int n;
cin>>n;
string s;
cin>>s;
//R G y
forr(i,0,n)
{
if (s[i]=='R') {R++; a[0].pb(i); br[i]++;}
else if (s[i]=='G') {G++; a[1].pb(i); bg[i]++;}
else {Y++; a[2].pb(i); by[i]++;}
if (i!=0) { br[i]+=br[i-1]; bg[i]+=bg[i-1]; by[i]+=by[i-1];}
//cout<<br[i]<<' '<<bg[i]<<' '<<by[i]<<endl;
}
// cout<<"R"<<endl;
// for(auto y:a[0]) cout<<y<<' ';
// cout<<endl;
// cout<<"G"<<endl;
// for(auto y:a[1]) cout<<y<<' ';
// cout<<endl;
// cout<<"Y"<<endl;
// for(auto y:a[2]) cout<<y<<' ';
// cout<<endl;
int o=min(fn(0,0,0,0),min(fn(0,0,0,1),fn(0,0,0,2)));
if (o==bigg) o=-1;
cout<<o;
// int dp[n][n][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
2144 KB |
Output is correct |
21 |
Correct |
2 ms |
2400 KB |
Output is correct |
22 |
Correct |
1 ms |
1632 KB |
Output is correct |
23 |
Correct |
2 ms |
2148 KB |
Output is correct |
24 |
Correct |
1 ms |
1632 KB |
Output is correct |
25 |
Correct |
1 ms |
864 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
2400 KB |
Output is correct |
28 |
Correct |
2 ms |
2408 KB |
Output is correct |
29 |
Correct |
2 ms |
2404 KB |
Output is correct |
30 |
Correct |
2 ms |
2404 KB |
Output is correct |
31 |
Correct |
2 ms |
2152 KB |
Output is correct |
32 |
Correct |
2 ms |
2404 KB |
Output is correct |
33 |
Correct |
2 ms |
1116 KB |
Output is correct |
34 |
Correct |
1 ms |
1384 KB |
Output is correct |
35 |
Correct |
2 ms |
2664 KB |
Output is correct |
36 |
Correct |
1 ms |
1892 KB |
Output is correct |
37 |
Correct |
1 ms |
1896 KB |
Output is correct |
38 |
Correct |
2 ms |
2552 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
3676 KB |
Output is correct |
3 |
Correct |
5 ms |
3672 KB |
Output is correct |
4 |
Correct |
3 ms |
3676 KB |
Output is correct |
5 |
Correct |
5 ms |
3640 KB |
Output is correct |
6 |
Correct |
4 ms |
3676 KB |
Output is correct |
7 |
Correct |
3 ms |
3676 KB |
Output is correct |
8 |
Correct |
3 ms |
3672 KB |
Output is correct |
9 |
Correct |
4 ms |
3676 KB |
Output is correct |
10 |
Correct |
4 ms |
3504 KB |
Output is correct |
11 |
Correct |
4 ms |
3672 KB |
Output is correct |
12 |
Correct |
2 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2908 KB |
Output is correct |
15 |
Correct |
4 ms |
3676 KB |
Output is correct |
16 |
Correct |
4 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
2144 KB |
Output is correct |
21 |
Correct |
2 ms |
2400 KB |
Output is correct |
22 |
Correct |
1 ms |
1632 KB |
Output is correct |
23 |
Correct |
2 ms |
2148 KB |
Output is correct |
24 |
Correct |
1 ms |
1632 KB |
Output is correct |
25 |
Correct |
1 ms |
864 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
2400 KB |
Output is correct |
28 |
Correct |
2 ms |
2408 KB |
Output is correct |
29 |
Correct |
2 ms |
2404 KB |
Output is correct |
30 |
Correct |
2 ms |
2404 KB |
Output is correct |
31 |
Correct |
2 ms |
2152 KB |
Output is correct |
32 |
Correct |
2 ms |
2404 KB |
Output is correct |
33 |
Correct |
2 ms |
1116 KB |
Output is correct |
34 |
Correct |
1 ms |
1384 KB |
Output is correct |
35 |
Correct |
2 ms |
2664 KB |
Output is correct |
36 |
Correct |
1 ms |
1892 KB |
Output is correct |
37 |
Correct |
1 ms |
1896 KB |
Output is correct |
38 |
Correct |
2 ms |
2552 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
984 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
3676 KB |
Output is correct |
44 |
Correct |
5 ms |
3672 KB |
Output is correct |
45 |
Correct |
3 ms |
3676 KB |
Output is correct |
46 |
Correct |
5 ms |
3640 KB |
Output is correct |
47 |
Correct |
4 ms |
3676 KB |
Output is correct |
48 |
Correct |
3 ms |
3676 KB |
Output is correct |
49 |
Correct |
3 ms |
3672 KB |
Output is correct |
50 |
Correct |
4 ms |
3676 KB |
Output is correct |
51 |
Correct |
4 ms |
3504 KB |
Output is correct |
52 |
Correct |
4 ms |
3672 KB |
Output is correct |
53 |
Correct |
2 ms |
1884 KB |
Output is correct |
54 |
Correct |
3 ms |
2652 KB |
Output is correct |
55 |
Correct |
3 ms |
2908 KB |
Output is correct |
56 |
Correct |
4 ms |
3676 KB |
Output is correct |
57 |
Correct |
4 ms |
3676 KB |
Output is correct |
58 |
Correct |
104 ms |
92244 KB |
Output is correct |
59 |
Correct |
109 ms |
107596 KB |
Output is correct |
60 |
Correct |
109 ms |
102224 KB |
Output is correct |
61 |
Correct |
107 ms |
100432 KB |
Output is correct |
62 |
Correct |
6 ms |
9308 KB |
Output is correct |
63 |
Correct |
9 ms |
16220 KB |
Output is correct |
64 |
Correct |
31 ms |
53404 KB |
Output is correct |
65 |
Correct |
56 ms |
78152 KB |
Output is correct |
66 |
Correct |
129 ms |
100944 KB |
Output is correct |
67 |
Correct |
102 ms |
96360 KB |
Output is correct |
68 |
Correct |
113 ms |
103508 KB |
Output is correct |
69 |
Correct |
115 ms |
101144 KB |
Output is correct |
70 |
Correct |
109 ms |
106064 KB |
Output is correct |
71 |
Correct |
107 ms |
97364 KB |
Output is correct |
72 |
Correct |
8 ms |
11612 KB |
Output is correct |
73 |
Correct |
1 ms |
2648 KB |
Output is correct |