#include "bits/stdc++.h"
using namespace std;
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void IOIGold2024_InshAllah() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
const ll inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN;
const ll mod = 1e9+7, N = 405;
#define ll int
ll pr[N][3], pos[N][3], dp[N][N][N][3];
void kigash() {
ll n, r = 0, g = 0, y = 0;
string s;
cin>>n>>s;
s = ' '+s;
for(ll i=1; i<=n; i++) {
for(ll j=0; j<3; j++) pr[i][j] = pr[i-1][j];
if(s[i]=='R') pr[i][0]++, pos[++r][0] = i;
if(s[i]=='G') pr[i][1]++, pos[++g][1] = i;
if(s[i]=='Y') pr[i][2]++, pos[++y][2] = i;
}
for(ll i=0; i<=n; i++) {
for(ll j=0; j<=n; j++) {
for(ll k=0; k<=n; k++) {
dp[i][j][k][0] = mod;
dp[i][j][k][1] = mod;
dp[i][j][k][2] = mod;
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(ll i=0; i<=r; i++) {
for(ll j=0; j<=g; j++) {
for(ll k=0; k<=y; k++) {
if(i) {
ll x = max(0, pr[pos[i][0]][1]-j)+max(0, pr[pos[i][0]][2]-k);
dp[i][j][k][0] = min(dp[i][j][k][0], min(dp[i-1][j][k][1], dp[i-1][j][k][2])+x);
}
if(j) {
ll x = max(0, pr[pos[j][1]][0]-i)+max(0, pr[pos[j][1]][2]-k);
dp[i][j][k][1] = min(dp[i][j][k][1], min(dp[i][j-1][k][0], dp[i][j-1][k][2])+x);
}
if(k) {
ll x = max(0, pr[pos[k][2]][0]-i)+max(0, pr[pos[k][2]][1]-j);
dp[i][j][k][2] = min(dp[i][j][k][2], min(dp[i][j][k-1][0], dp[i][j][k-1][1])+x);
}
}
}
}
ll ans = min({dp[r][g][y][0], dp[r][g][y][1], dp[r][g][y][2]});
if(ans>=mod) cout<<"-1\n";
else cout<<ans<<"\n";
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
IOIGold2024_InshAllah();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) {
kigash();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void freopen(std::string)':
joi2019_ho_t3.cpp:17:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:17:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
6 ms |
31268 KB |
Output is correct |
6 |
Correct |
4 ms |
31064 KB |
Output is correct |
7 |
Correct |
3 ms |
31064 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31268 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
3 ms |
29020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
6 ms |
31268 KB |
Output is correct |
6 |
Correct |
4 ms |
31064 KB |
Output is correct |
7 |
Correct |
3 ms |
31064 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31268 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
3 ms |
29020 KB |
Output is correct |
18 |
Correct |
23 ms |
117500 KB |
Output is correct |
19 |
Correct |
12 ms |
117596 KB |
Output is correct |
20 |
Correct |
12 ms |
117596 KB |
Output is correct |
21 |
Correct |
12 ms |
117592 KB |
Output is correct |
22 |
Correct |
12 ms |
117424 KB |
Output is correct |
23 |
Correct |
12 ms |
117392 KB |
Output is correct |
24 |
Correct |
12 ms |
117596 KB |
Output is correct |
25 |
Correct |
13 ms |
117596 KB |
Output is correct |
26 |
Correct |
12 ms |
117596 KB |
Output is correct |
27 |
Correct |
12 ms |
117596 KB |
Output is correct |
28 |
Correct |
12 ms |
117596 KB |
Output is correct |
29 |
Correct |
12 ms |
117520 KB |
Output is correct |
30 |
Correct |
12 ms |
117496 KB |
Output is correct |
31 |
Correct |
13 ms |
117596 KB |
Output is correct |
32 |
Correct |
14 ms |
117596 KB |
Output is correct |
33 |
Correct |
12 ms |
115548 KB |
Output is correct |
34 |
Correct |
13 ms |
115392 KB |
Output is correct |
35 |
Correct |
12 ms |
113696 KB |
Output is correct |
36 |
Correct |
12 ms |
115496 KB |
Output is correct |
37 |
Correct |
11 ms |
111452 KB |
Output is correct |
38 |
Correct |
12 ms |
117592 KB |
Output is correct |
39 |
Correct |
12 ms |
117404 KB |
Output is correct |
40 |
Correct |
13 ms |
115548 KB |
Output is correct |
41 |
Correct |
12 ms |
117596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
213 ms |
774228 KB |
Output is correct |
3 |
Correct |
142 ms |
772012 KB |
Output is correct |
4 |
Correct |
137 ms |
774228 KB |
Output is correct |
5 |
Correct |
148 ms |
774148 KB |
Output is correct |
6 |
Correct |
137 ms |
774216 KB |
Output is correct |
7 |
Correct |
136 ms |
772180 KB |
Output is correct |
8 |
Correct |
141 ms |
772212 KB |
Output is correct |
9 |
Correct |
135 ms |
770236 KB |
Output is correct |
10 |
Correct |
138 ms |
774180 KB |
Output is correct |
11 |
Correct |
141 ms |
774016 KB |
Output is correct |
12 |
Correct |
47 ms |
398676 KB |
Output is correct |
13 |
Correct |
72 ms |
530004 KB |
Output is correct |
14 |
Correct |
96 ms |
638696 KB |
Output is correct |
15 |
Correct |
137 ms |
774216 KB |
Output is correct |
16 |
Correct |
136 ms |
774216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
6 ms |
31268 KB |
Output is correct |
6 |
Correct |
4 ms |
31064 KB |
Output is correct |
7 |
Correct |
3 ms |
31064 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31268 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
3 ms |
29020 KB |
Output is correct |
18 |
Correct |
23 ms |
117500 KB |
Output is correct |
19 |
Correct |
12 ms |
117596 KB |
Output is correct |
20 |
Correct |
12 ms |
117596 KB |
Output is correct |
21 |
Correct |
12 ms |
117592 KB |
Output is correct |
22 |
Correct |
12 ms |
117424 KB |
Output is correct |
23 |
Correct |
12 ms |
117392 KB |
Output is correct |
24 |
Correct |
12 ms |
117596 KB |
Output is correct |
25 |
Correct |
13 ms |
117596 KB |
Output is correct |
26 |
Correct |
12 ms |
117596 KB |
Output is correct |
27 |
Correct |
12 ms |
117596 KB |
Output is correct |
28 |
Correct |
12 ms |
117596 KB |
Output is correct |
29 |
Correct |
12 ms |
117520 KB |
Output is correct |
30 |
Correct |
12 ms |
117496 KB |
Output is correct |
31 |
Correct |
13 ms |
117596 KB |
Output is correct |
32 |
Correct |
14 ms |
117596 KB |
Output is correct |
33 |
Correct |
12 ms |
115548 KB |
Output is correct |
34 |
Correct |
13 ms |
115392 KB |
Output is correct |
35 |
Correct |
12 ms |
113696 KB |
Output is correct |
36 |
Correct |
12 ms |
115496 KB |
Output is correct |
37 |
Correct |
11 ms |
111452 KB |
Output is correct |
38 |
Correct |
12 ms |
117592 KB |
Output is correct |
39 |
Correct |
12 ms |
117404 KB |
Output is correct |
40 |
Correct |
13 ms |
115548 KB |
Output is correct |
41 |
Correct |
12 ms |
117596 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
213 ms |
774228 KB |
Output is correct |
44 |
Correct |
142 ms |
772012 KB |
Output is correct |
45 |
Correct |
137 ms |
774228 KB |
Output is correct |
46 |
Correct |
148 ms |
774148 KB |
Output is correct |
47 |
Correct |
137 ms |
774216 KB |
Output is correct |
48 |
Correct |
136 ms |
772180 KB |
Output is correct |
49 |
Correct |
141 ms |
772212 KB |
Output is correct |
50 |
Correct |
135 ms |
770236 KB |
Output is correct |
51 |
Correct |
138 ms |
774180 KB |
Output is correct |
52 |
Correct |
141 ms |
774016 KB |
Output is correct |
53 |
Correct |
47 ms |
398676 KB |
Output is correct |
54 |
Correct |
72 ms |
530004 KB |
Output is correct |
55 |
Correct |
96 ms |
638696 KB |
Output is correct |
56 |
Correct |
137 ms |
774216 KB |
Output is correct |
57 |
Correct |
136 ms |
774216 KB |
Output is correct |
58 |
Correct |
170 ms |
774132 KB |
Output is correct |
59 |
Correct |
155 ms |
774224 KB |
Output is correct |
60 |
Correct |
156 ms |
770048 KB |
Output is correct |
61 |
Correct |
153 ms |
772112 KB |
Output is correct |
62 |
Correct |
140 ms |
774216 KB |
Output is correct |
63 |
Correct |
144 ms |
774228 KB |
Output is correct |
64 |
Correct |
152 ms |
774228 KB |
Output is correct |
65 |
Correct |
155 ms |
772520 KB |
Output is correct |
66 |
Correct |
162 ms |
767972 KB |
Output is correct |
67 |
Correct |
158 ms |
774160 KB |
Output is correct |
68 |
Correct |
157 ms |
772076 KB |
Output is correct |
69 |
Correct |
156 ms |
774348 KB |
Output is correct |
70 |
Correct |
159 ms |
774224 KB |
Output is correct |
71 |
Correct |
152 ms |
772108 KB |
Output is correct |
72 |
Correct |
144 ms |
771928 KB |
Output is correct |
73 |
Correct |
142 ms |
774032 KB |
Output is correct |