#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
template<class T> bool ckmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}
template<class T> bool ckmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
const int MAX_N = 404;
int n;
int arr[MAX_N];
int pref[MAX_N][3];
vi idxOf[3];
map<pair<pi, vi>, int> dp;
int getcost(int c, vi cnt) { // cost to get the next c
if(cnt[c] >= sz(idxOf[c])) return 1e8; // impossible
int idx = idxOf[c][cnt[c]];
int res = idx;
M00(k, 3) res -= min(cnt[k], pref[idx][k]);
return res;
}
int getdp(int idx, int c, vi cnt) {
pair<pi, vi> tmp = mp(mp(idx, c), cnt);
if(dp.count(tmp)) return dp[tmp];
int cost = getcost(c, cnt);
if(cost == 1e8) {
dp[tmp] = 1e8;
return dp[tmp];
}
if(idx == n-1) {
dp[tmp] = cost;
return dp[tmp];
}
cnt[c]++;
int res = 1e8;
M00(c2, 3) if(c2 != c) {
ckmin(res, cost + getdp(idx+1, c2, cnt));
}
dp[tmp] = res;
return dp[tmp];
}
int main() { FAST
string s;
cin >> n >> s;
M00(i, n) {
if(s[i] == 'R') arr[i] = 0;
else if(s[i] == 'Y') arr[i] = 1;
else arr[i] = 2;
}
M00(c, 3) {
pref[0][c] = (arr[0] == c);
MOO(i, 1, n) pref[i][c] = pref[i-1][c] + (arr[i] == c);
}
M00(i, n) idxOf[arr[i]].pb(i);
int res = 1e8;
vi v(3,0);
M00(i, 3) ckmin(res, getdp(0, i, v));
if(res == 1e8) {
finish(-1);
}
finish(res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
16 ms |
1920 KB |
Output is correct |
19 |
Correct |
16 ms |
1792 KB |
Output is correct |
20 |
Correct |
14 ms |
1792 KB |
Output is correct |
21 |
Correct |
16 ms |
1920 KB |
Output is correct |
22 |
Correct |
11 ms |
1408 KB |
Output is correct |
23 |
Correct |
16 ms |
1920 KB |
Output is correct |
24 |
Correct |
9 ms |
1280 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
9 ms |
1280 KB |
Output is correct |
28 |
Correct |
17 ms |
2048 KB |
Output is correct |
29 |
Correct |
17 ms |
1920 KB |
Output is correct |
30 |
Correct |
15 ms |
1844 KB |
Output is correct |
31 |
Correct |
14 ms |
1792 KB |
Output is correct |
32 |
Correct |
25 ms |
1912 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
12 ms |
1588 KB |
Output is correct |
36 |
Correct |
15 ms |
1792 KB |
Output is correct |
37 |
Correct |
12 ms |
1536 KB |
Output is correct |
38 |
Correct |
12 ms |
1536 KB |
Output is correct |
39 |
Correct |
18 ms |
1920 KB |
Output is correct |
40 |
Correct |
0 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
16 ms |
1920 KB |
Output is correct |
19 |
Correct |
16 ms |
1792 KB |
Output is correct |
20 |
Correct |
14 ms |
1792 KB |
Output is correct |
21 |
Correct |
16 ms |
1920 KB |
Output is correct |
22 |
Correct |
11 ms |
1408 KB |
Output is correct |
23 |
Correct |
16 ms |
1920 KB |
Output is correct |
24 |
Correct |
9 ms |
1280 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
9 ms |
1280 KB |
Output is correct |
28 |
Correct |
17 ms |
2048 KB |
Output is correct |
29 |
Correct |
17 ms |
1920 KB |
Output is correct |
30 |
Correct |
15 ms |
1844 KB |
Output is correct |
31 |
Correct |
14 ms |
1792 KB |
Output is correct |
32 |
Correct |
25 ms |
1912 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
12 ms |
1588 KB |
Output is correct |
36 |
Correct |
15 ms |
1792 KB |
Output is correct |
37 |
Correct |
12 ms |
1536 KB |
Output is correct |
38 |
Correct |
12 ms |
1536 KB |
Output is correct |
39 |
Correct |
18 ms |
1920 KB |
Output is correct |
40 |
Correct |
0 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
512 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
640 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
2 ms |
640 KB |
Output is correct |
46 |
Correct |
1 ms |
640 KB |
Output is correct |
47 |
Correct |
2 ms |
640 KB |
Output is correct |
48 |
Correct |
1 ms |
640 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
2 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
640 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
640 KB |
Output is correct |
57 |
Correct |
1 ms |
512 KB |
Output is correct |
58 |
Execution timed out |
1096 ms |
61192 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |