//Author: jainbot27
#pragma GCC optimize ("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ts to_string
#define ub upper_bound
#define lb lower_bound
// #define ar array
#define FOR(x, y, z) for(int x = y; x < z; x++)
#define ROF(x, y, z) for(int x = y; x > z; x--)
#define all(x) x.begin(), x.end()
const char nl = '\n';
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;
str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) {
str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) {
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
return "("+ts(p.f)+", "+ts(p.s)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef D
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
const int mxN = 401;
int n, dp[mxN][mxN][mxN][3], vis[mxN][mxN][mxN][3], p[3][mxN];
vi pos[3];
string c;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> c;
for(int i = 0; i < n; i++){
if(c[i] == 'R'){
p[0][i]++;
pos[0].pb(i);
}
else if(c[i] == 'G'){
p[1][i]++;
pos[1].pb(i);
}
else{
p[2][i]++;
pos[2].pb(i);
}
if(i > 0){
for(int j = 0; j < 3; j++){
p[j][i] += p[j][i-1];
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
vis[0][0][0][0] = 1;
vis[0][0][0][1] = 1;
vis[0][0][0][2] = 1;
for(int i =0; i < n; i++){
for(int j = 0; j <= i; j++){
for(int k = 0; k <= i; k++){
for(int l = 0; l < 3; l++){
if(!vis[i][j][k][l])
continue;
int ar = j, ag = k, ay = i - j - k;
if(ar < (int)pos[0].size() && l != 0){
int pr = pos[0][ar];
if(ag > 0 && pos[1][ag-1] > pr){
pr += p[1][pos[1][ag-1]] - p[1][pr];
}
if(ay > 0 && pos[2][ay-1] > pr){
pr += p[2][pos[2][ay-1]] - p[2][pr];
}
if(!vis[i+1][j+1][k][0]|| dp[i+1][j+1][k][0] > dp[i][j][k][l] + max(pr - i, 0)){
dp[i+1][j+1][k][0] = dp[i][j][k][l] + max(pr - i, 0);
vis[i+1][j+1][k][0] = 1;
}
}
if(ag < (int)pos[1].size() && l != 1){
int pg = pos[1][ag];
if(ar > 0 && pos[0][ar-1] > pg){
pg += p[0][pos[0][ar-1]] - p[0][pg];
}
if(ay > 0 && pos[2][ay-1] > pg){
pg += p[2][pos[2][ay-1]] - p[2][pg];
}
if(!vis[i+1][j][k+1][1]|| dp[i+1][j][k+1][1] > dp[i][j][k][l] + max(pg - i, 0)){
dp[i+1][j][k+1][1] = dp[i][j][k][l] + max(pg - i, 0);
vis[i+1][j][k+1][1] = 1;
}
}
if(ay < (int)pos[2].size() && l != 2){
int py = pos[2][ay];
if(ar > 0 && pos[0][ar-1] > py){
py += p[0][pos[0][ar-1]] - p[0][py];
}
if(ag > 0 && pos[1][ag-1] > py){
py += p[1][pos[1][ag-1]] - p[1][py];
}
if(!vis[i+1][j][k][2]|| dp[i+1][j][k][2] > dp[i][j][k][l] + max(py - i, 0)){
dp[i+1][j][k][2] = dp[i][j][k][l] + max(py - i, 0);
vis[i+1][j][k][2] = 1;
}
}
}
}
}
}
int ans = 2e9;
for(int i=0 ; i < 3; i++){
if(vis[n][(int)pos[0].size()][(int)pos[1].size()][i]){
ans = min(dp[n][(int)pos[0].size()][(int)pos[1].size()][i], ans);
}
}
cout << (ans==(int)2e9?-1:ans) << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
6144 KB |
Output is correct |
19 |
Correct |
5 ms |
6144 KB |
Output is correct |
20 |
Correct |
5 ms |
6016 KB |
Output is correct |
21 |
Correct |
5 ms |
6272 KB |
Output is correct |
22 |
Correct |
5 ms |
5376 KB |
Output is correct |
23 |
Correct |
5 ms |
6144 KB |
Output is correct |
24 |
Correct |
4 ms |
4992 KB |
Output is correct |
25 |
Correct |
3 ms |
1664 KB |
Output is correct |
26 |
Correct |
3 ms |
1920 KB |
Output is correct |
27 |
Correct |
4 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
6272 KB |
Output is correct |
29 |
Correct |
6 ms |
6272 KB |
Output is correct |
30 |
Correct |
5 ms |
6016 KB |
Output is correct |
31 |
Correct |
5 ms |
5888 KB |
Output is correct |
32 |
Correct |
5 ms |
6144 KB |
Output is correct |
33 |
Correct |
3 ms |
2176 KB |
Output is correct |
34 |
Correct |
4 ms |
2816 KB |
Output is correct |
35 |
Correct |
5 ms |
5504 KB |
Output is correct |
36 |
Correct |
5 ms |
5888 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
39 |
Correct |
5 ms |
6272 KB |
Output is correct |
40 |
Correct |
2 ms |
1024 KB |
Output is correct |
41 |
Correct |
3 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
146 ms |
8184 KB |
Output is correct |
3 |
Correct |
143 ms |
8184 KB |
Output is correct |
4 |
Correct |
157 ms |
8056 KB |
Output is correct |
5 |
Correct |
143 ms |
8056 KB |
Output is correct |
6 |
Correct |
147 ms |
8056 KB |
Output is correct |
7 |
Correct |
143 ms |
8184 KB |
Output is correct |
8 |
Correct |
139 ms |
8060 KB |
Output is correct |
9 |
Correct |
135 ms |
8016 KB |
Output is correct |
10 |
Correct |
134 ms |
8056 KB |
Output is correct |
11 |
Correct |
140 ms |
8056 KB |
Output is correct |
12 |
Correct |
27 ms |
4352 KB |
Output is correct |
13 |
Correct |
56 ms |
5628 KB |
Output is correct |
14 |
Correct |
84 ms |
6776 KB |
Output is correct |
15 |
Correct |
150 ms |
8160 KB |
Output is correct |
16 |
Correct |
138 ms |
8052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
6144 KB |
Output is correct |
19 |
Correct |
5 ms |
6144 KB |
Output is correct |
20 |
Correct |
5 ms |
6016 KB |
Output is correct |
21 |
Correct |
5 ms |
6272 KB |
Output is correct |
22 |
Correct |
5 ms |
5376 KB |
Output is correct |
23 |
Correct |
5 ms |
6144 KB |
Output is correct |
24 |
Correct |
4 ms |
4992 KB |
Output is correct |
25 |
Correct |
3 ms |
1664 KB |
Output is correct |
26 |
Correct |
3 ms |
1920 KB |
Output is correct |
27 |
Correct |
4 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
6272 KB |
Output is correct |
29 |
Correct |
6 ms |
6272 KB |
Output is correct |
30 |
Correct |
5 ms |
6016 KB |
Output is correct |
31 |
Correct |
5 ms |
5888 KB |
Output is correct |
32 |
Correct |
5 ms |
6144 KB |
Output is correct |
33 |
Correct |
3 ms |
2176 KB |
Output is correct |
34 |
Correct |
4 ms |
2816 KB |
Output is correct |
35 |
Correct |
5 ms |
5504 KB |
Output is correct |
36 |
Correct |
5 ms |
5888 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
39 |
Correct |
5 ms |
6272 KB |
Output is correct |
40 |
Correct |
2 ms |
1024 KB |
Output is correct |
41 |
Correct |
3 ms |
1920 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
146 ms |
8184 KB |
Output is correct |
44 |
Correct |
143 ms |
8184 KB |
Output is correct |
45 |
Correct |
157 ms |
8056 KB |
Output is correct |
46 |
Correct |
143 ms |
8056 KB |
Output is correct |
47 |
Correct |
147 ms |
8056 KB |
Output is correct |
48 |
Correct |
143 ms |
8184 KB |
Output is correct |
49 |
Correct |
139 ms |
8060 KB |
Output is correct |
50 |
Correct |
135 ms |
8016 KB |
Output is correct |
51 |
Correct |
134 ms |
8056 KB |
Output is correct |
52 |
Correct |
140 ms |
8056 KB |
Output is correct |
53 |
Correct |
27 ms |
4352 KB |
Output is correct |
54 |
Correct |
56 ms |
5628 KB |
Output is correct |
55 |
Correct |
84 ms |
6776 KB |
Output is correct |
56 |
Correct |
150 ms |
8160 KB |
Output is correct |
57 |
Correct |
138 ms |
8052 KB |
Output is correct |
58 |
Correct |
300 ms |
240044 KB |
Output is correct |
59 |
Correct |
303 ms |
240632 KB |
Output is correct |
60 |
Correct |
280 ms |
240436 KB |
Output is correct |
61 |
Correct |
279 ms |
242680 KB |
Output is correct |
62 |
Correct |
157 ms |
19580 KB |
Output is correct |
63 |
Correct |
148 ms |
33276 KB |
Output is correct |
64 |
Correct |
189 ms |
107256 KB |
Output is correct |
65 |
Correct |
241 ms |
159484 KB |
Output is correct |
66 |
Correct |
273 ms |
240120 KB |
Output is correct |
67 |
Correct |
269 ms |
236280 KB |
Output is correct |
68 |
Correct |
277 ms |
242552 KB |
Output is correct |
69 |
Correct |
296 ms |
243988 KB |
Output is correct |
70 |
Correct |
289 ms |
242680 KB |
Output is correct |
71 |
Correct |
275 ms |
236536 KB |
Output is correct |
72 |
Correct |
159 ms |
36600 KB |
Output is correct |
73 |
Correct |
148 ms |
11256 KB |
Output is correct |