#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,n) for(int i=1;i<=n;++i)
#define SZ(i) int(i.size())
#define eb emplace_back
#define ALL(i) i.begin(),i.end()
#define X first
#define Y second
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"#%d: %s = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
template<typename It> ostream& _printRng(ostream &os,It bg,It ed)
{
os<<"{";
for(It it=bg;it!=ed;it++) {
os<<(it==bg?"":",")<<*it;
}
os<<"}";
return os;
}
template<typename T> ostream &operator << (ostream &os,vector<T> &v){return _printRng(os,v.begin(), v.end());}
template<typename T> void pary(T bg, T ed){_printRng(cerr,bg,ed);cerr<<endl;}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#define pary(...)
#endif
const int MAXN = 402;
const int MOD = 0x3f3f3f3f;
int n;
int dp[2][MAXN][MAXN][3];
int g[MAXN];
string str;
int cnt[3];
int pre[MAXN][3];
vector<int> app[3];
int shf (int x, int y) {
return x + y >= 3 ? x + y - 3 : x + y;
}
/*********************GoodLuck***********************/
int main () {
IOS();
cin >> n;
cin >> str;
REP (i, 3) {
app[i].eb(-1);
}
REP (i, SZ(str)) {
if (str[i] == 'R') {
g[i] = 0;
} else if (str[i] == 'G') {
g[i] = 1;
} else {
g[i] = 2;
}
app[g[i]].eb(i);
cnt[g[i]]++;
for (int j=0; j<3; j++) {
pre[i][j] = cnt[j];
}
}
#define cdp dp[i&1]
#define pdp dp[(i&1)^1]
memset(dp, 0x3f, sizeof(dp));
int ans = MOD;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i=1; i<=n; i++) {
for (int lst=0; lst<3; lst++) {
for (int r=0; r<=i; r++) {
for (int g=0; g<=i; g++) {
cdp[r][g][lst] = MOD;
int y = i - r - g;
if (y < 0) {
break;
}
if (r > cnt[0] || g > cnt[1] || y > cnt[2]) {
continue;
}
int rr = r - (lst == 0);
int gg = g - (lst == 1);
int yy = y - (lst == 2);
if (min({rr,gg,yy}) < 0) {
continue;
}
int prv = min(pdp[rr][gg][shf(lst,1)], pdp[rr][gg][shf(lst,2)]);
// debug(pdp[rr][gg][shf(lst,1)], pdp[rr][gg][shf(lst,2)]);
int trans = 0;
int cur[3];
cur[0] = r;
cur[1] = g;
cur[2] = y;
int pos = app[lst][cur[lst]];
trans = 0;
for (int x=0; x<3; x++) {
if (x != lst) {
if (app[x][cur[x]] < pos) {
trans += pre[pos][x] - cur[x];
}
}
}
// debug(i, r, g, y, lst,trans, prv);
if (prv != MOD) {
cdp[r][g][lst] = prv + trans;
debug(i,r,g,y,lst,prv,trans);
if (i == n) {
assert(r == cnt[0] && g == cnt[1] && y == cnt[2]);
ans = min(ans, cdp[r][g][lst]);
}
}
}
}
}
}
cout << (ans == MOD ? -1 : ans) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4088 KB |
Output is correct |
2 |
Correct |
6 ms |
4088 KB |
Output is correct |
3 |
Correct |
6 ms |
4088 KB |
Output is correct |
4 |
Correct |
7 ms |
4088 KB |
Output is correct |
5 |
Correct |
7 ms |
4088 KB |
Output is correct |
6 |
Correct |
7 ms |
4088 KB |
Output is correct |
7 |
Correct |
6 ms |
4092 KB |
Output is correct |
8 |
Correct |
6 ms |
4088 KB |
Output is correct |
9 |
Correct |
6 ms |
4088 KB |
Output is correct |
10 |
Correct |
6 ms |
4088 KB |
Output is correct |
11 |
Correct |
6 ms |
4088 KB |
Output is correct |
12 |
Correct |
8 ms |
4088 KB |
Output is correct |
13 |
Correct |
6 ms |
4088 KB |
Output is correct |
14 |
Correct |
6 ms |
4088 KB |
Output is correct |
15 |
Correct |
6 ms |
4088 KB |
Output is correct |
16 |
Correct |
7 ms |
4088 KB |
Output is correct |
17 |
Correct |
6 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4088 KB |
Output is correct |
2 |
Correct |
6 ms |
4088 KB |
Output is correct |
3 |
Correct |
6 ms |
4088 KB |
Output is correct |
4 |
Correct |
7 ms |
4088 KB |
Output is correct |
5 |
Correct |
7 ms |
4088 KB |
Output is correct |
6 |
Correct |
7 ms |
4088 KB |
Output is correct |
7 |
Correct |
6 ms |
4092 KB |
Output is correct |
8 |
Correct |
6 ms |
4088 KB |
Output is correct |
9 |
Correct |
6 ms |
4088 KB |
Output is correct |
10 |
Correct |
6 ms |
4088 KB |
Output is correct |
11 |
Correct |
6 ms |
4088 KB |
Output is correct |
12 |
Correct |
8 ms |
4088 KB |
Output is correct |
13 |
Correct |
6 ms |
4088 KB |
Output is correct |
14 |
Correct |
6 ms |
4088 KB |
Output is correct |
15 |
Correct |
6 ms |
4088 KB |
Output is correct |
16 |
Correct |
7 ms |
4088 KB |
Output is correct |
17 |
Correct |
6 ms |
4088 KB |
Output is correct |
18 |
Correct |
7 ms |
4192 KB |
Output is correct |
19 |
Correct |
7 ms |
4088 KB |
Output is correct |
20 |
Correct |
7 ms |
4088 KB |
Output is correct |
21 |
Correct |
7 ms |
4088 KB |
Output is correct |
22 |
Correct |
7 ms |
4216 KB |
Output is correct |
23 |
Correct |
7 ms |
4088 KB |
Output is correct |
24 |
Correct |
7 ms |
4216 KB |
Output is correct |
25 |
Correct |
7 ms |
4088 KB |
Output is correct |
26 |
Correct |
7 ms |
4088 KB |
Output is correct |
27 |
Correct |
7 ms |
4088 KB |
Output is correct |
28 |
Correct |
7 ms |
4088 KB |
Output is correct |
29 |
Correct |
7 ms |
4088 KB |
Output is correct |
30 |
Correct |
7 ms |
4088 KB |
Output is correct |
31 |
Correct |
7 ms |
4088 KB |
Output is correct |
32 |
Correct |
7 ms |
4088 KB |
Output is correct |
33 |
Correct |
7 ms |
4216 KB |
Output is correct |
34 |
Correct |
7 ms |
4088 KB |
Output is correct |
35 |
Correct |
8 ms |
4088 KB |
Output is correct |
36 |
Correct |
7 ms |
4088 KB |
Output is correct |
37 |
Correct |
7 ms |
4088 KB |
Output is correct |
38 |
Correct |
8 ms |
4088 KB |
Output is correct |
39 |
Correct |
7 ms |
4088 KB |
Output is correct |
40 |
Correct |
6 ms |
4088 KB |
Output is correct |
41 |
Correct |
7 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4088 KB |
Output is correct |
2 |
Correct |
75 ms |
4088 KB |
Output is correct |
3 |
Correct |
72 ms |
4216 KB |
Output is correct |
4 |
Correct |
72 ms |
4084 KB |
Output is correct |
5 |
Correct |
73 ms |
4196 KB |
Output is correct |
6 |
Correct |
75 ms |
4216 KB |
Output is correct |
7 |
Correct |
72 ms |
4216 KB |
Output is correct |
8 |
Correct |
73 ms |
4088 KB |
Output is correct |
9 |
Correct |
74 ms |
4088 KB |
Output is correct |
10 |
Correct |
73 ms |
4088 KB |
Output is correct |
11 |
Correct |
73 ms |
4088 KB |
Output is correct |
12 |
Correct |
20 ms |
4216 KB |
Output is correct |
13 |
Correct |
36 ms |
4088 KB |
Output is correct |
14 |
Correct |
44 ms |
4088 KB |
Output is correct |
15 |
Correct |
72 ms |
4088 KB |
Output is correct |
16 |
Correct |
70 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4088 KB |
Output is correct |
2 |
Correct |
6 ms |
4088 KB |
Output is correct |
3 |
Correct |
6 ms |
4088 KB |
Output is correct |
4 |
Correct |
7 ms |
4088 KB |
Output is correct |
5 |
Correct |
7 ms |
4088 KB |
Output is correct |
6 |
Correct |
7 ms |
4088 KB |
Output is correct |
7 |
Correct |
6 ms |
4092 KB |
Output is correct |
8 |
Correct |
6 ms |
4088 KB |
Output is correct |
9 |
Correct |
6 ms |
4088 KB |
Output is correct |
10 |
Correct |
6 ms |
4088 KB |
Output is correct |
11 |
Correct |
6 ms |
4088 KB |
Output is correct |
12 |
Correct |
8 ms |
4088 KB |
Output is correct |
13 |
Correct |
6 ms |
4088 KB |
Output is correct |
14 |
Correct |
6 ms |
4088 KB |
Output is correct |
15 |
Correct |
6 ms |
4088 KB |
Output is correct |
16 |
Correct |
7 ms |
4088 KB |
Output is correct |
17 |
Correct |
6 ms |
4088 KB |
Output is correct |
18 |
Correct |
7 ms |
4192 KB |
Output is correct |
19 |
Correct |
7 ms |
4088 KB |
Output is correct |
20 |
Correct |
7 ms |
4088 KB |
Output is correct |
21 |
Correct |
7 ms |
4088 KB |
Output is correct |
22 |
Correct |
7 ms |
4216 KB |
Output is correct |
23 |
Correct |
7 ms |
4088 KB |
Output is correct |
24 |
Correct |
7 ms |
4216 KB |
Output is correct |
25 |
Correct |
7 ms |
4088 KB |
Output is correct |
26 |
Correct |
7 ms |
4088 KB |
Output is correct |
27 |
Correct |
7 ms |
4088 KB |
Output is correct |
28 |
Correct |
7 ms |
4088 KB |
Output is correct |
29 |
Correct |
7 ms |
4088 KB |
Output is correct |
30 |
Correct |
7 ms |
4088 KB |
Output is correct |
31 |
Correct |
7 ms |
4088 KB |
Output is correct |
32 |
Correct |
7 ms |
4088 KB |
Output is correct |
33 |
Correct |
7 ms |
4216 KB |
Output is correct |
34 |
Correct |
7 ms |
4088 KB |
Output is correct |
35 |
Correct |
8 ms |
4088 KB |
Output is correct |
36 |
Correct |
7 ms |
4088 KB |
Output is correct |
37 |
Correct |
7 ms |
4088 KB |
Output is correct |
38 |
Correct |
8 ms |
4088 KB |
Output is correct |
39 |
Correct |
7 ms |
4088 KB |
Output is correct |
40 |
Correct |
6 ms |
4088 KB |
Output is correct |
41 |
Correct |
7 ms |
4088 KB |
Output is correct |
42 |
Correct |
6 ms |
4088 KB |
Output is correct |
43 |
Correct |
75 ms |
4088 KB |
Output is correct |
44 |
Correct |
72 ms |
4216 KB |
Output is correct |
45 |
Correct |
72 ms |
4084 KB |
Output is correct |
46 |
Correct |
73 ms |
4196 KB |
Output is correct |
47 |
Correct |
75 ms |
4216 KB |
Output is correct |
48 |
Correct |
72 ms |
4216 KB |
Output is correct |
49 |
Correct |
73 ms |
4088 KB |
Output is correct |
50 |
Correct |
74 ms |
4088 KB |
Output is correct |
51 |
Correct |
73 ms |
4088 KB |
Output is correct |
52 |
Correct |
73 ms |
4088 KB |
Output is correct |
53 |
Correct |
20 ms |
4216 KB |
Output is correct |
54 |
Correct |
36 ms |
4088 KB |
Output is correct |
55 |
Correct |
44 ms |
4088 KB |
Output is correct |
56 |
Correct |
72 ms |
4088 KB |
Output is correct |
57 |
Correct |
70 ms |
4088 KB |
Output is correct |
58 |
Correct |
133 ms |
4088 KB |
Output is correct |
59 |
Correct |
138 ms |
4088 KB |
Output is correct |
60 |
Correct |
131 ms |
4120 KB |
Output is correct |
61 |
Correct |
134 ms |
4088 KB |
Output is correct |
62 |
Correct |
80 ms |
4088 KB |
Output is correct |
63 |
Correct |
81 ms |
4220 KB |
Output is correct |
64 |
Correct |
101 ms |
4088 KB |
Output is correct |
65 |
Correct |
116 ms |
4088 KB |
Output is correct |
66 |
Correct |
127 ms |
4088 KB |
Output is correct |
67 |
Correct |
147 ms |
4220 KB |
Output is correct |
68 |
Correct |
135 ms |
4088 KB |
Output is correct |
69 |
Correct |
131 ms |
4216 KB |
Output is correct |
70 |
Correct |
133 ms |
4088 KB |
Output is correct |
71 |
Correct |
132 ms |
4320 KB |
Output is correct |
72 |
Correct |
88 ms |
4216 KB |
Output is correct |
73 |
Correct |
57 ms |
4088 KB |
Output is correct |