#pragma GCC optimization "Ofast"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 400 + 6;
const ll Log2 = 19;
const ll inf = 1e9 + 7;
typedef pair<ll,ll> LL;
ll n,cntR,cntG,cntY,posR[N],posG[N],posY[N];
string s;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "tst"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
}
cin>>n>>s; s = " " + s;
for (ll i = 1;i <= n;i++){
if (s[i] == 'R') cntR++,posR[cntR] = i;
if (s[i] == 'G') cntG++,posG[cntG] = i;
if (s[i] == 'Y') cntY++,posY[cntY] = i;
}
ll lim = ceil((ld)n/2);
if (cntR > lim||cntG > lim||cntY > lim){
cout<<-1; return 0;
}
ll dp[cntR + 6][cntG + 6][cntY + 6][3],ans;
dp[cntR][cntG][cntY][0] = dp[cntR][cntG][cntY][1] = dp[cntR][cntG][cntY][2] = 0;
for (ll i = cntR;i >= 0;i--){
for (ll j = cntG;j >= 0;j--){
for (ll l = cntY;l >= 0;l--){
if (i == cntR && j == cntG && l == cntY) continue;
ll now = i + j + l + 1;
for (ll k = 0;k < 3;k++){
ll p = inf;
if (k == 0 && i){
if (j + 1 <= cntG) p = min(p,dp[i][j + 1][l][1] + max(0,posG[j + 1] - now));
if (l + 1 <= cntY) p = min(p,dp[i][j][l + 1][2] + max(0,posY[l + 1] - now));
}
if (k == 1 && j){
if (i + 1 <= cntR) p = min(p,dp[i + 1][j][l][0] + max(0,posR[i + 1] - now));
if (l + 1 <= cntY) p = min(p,dp[i][j][l + 1][2] + max(0,posY[l + 1] - now));
}
if (k == 2 && l){
if (i + 1 <= cntR) p = min(p,dp[i + 1][j][l][0] + max(0,posR[i + 1] - now));
if (j + 1 <= cntG) p = min(p,dp[i][j + 1][l][1] + max(0,posG[j + 1] - now));
}
dp[i][j][l][k] = p;
}
}
}
}
//cout<<cntR; return 0;
//cout<<dp[1][1][0][1]; return 0;
ans = inf;
if (cntR) ans = min(ans,dp[1][0][0][0] + posR[1] - 1);
if (cntG) ans = min(ans,dp[0][1][0][1] + posG[1] - 1);
if (cntY) ans = min(ans,dp[0][0][1][2] + posY[1] - 1);
cout<<ans;
}
Compilation message
joi2019_ho_t3.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
1 | #pragma GCC optimization "Ofast"
|
joi2019_ho_t3.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
24 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
3308 KB |
Output is correct |
3 |
Correct |
3 ms |
3180 KB |
Output is correct |
4 |
Correct |
3 ms |
3308 KB |
Output is correct |
5 |
Correct |
4 ms |
3308 KB |
Output is correct |
6 |
Correct |
3 ms |
3308 KB |
Output is correct |
7 |
Correct |
3 ms |
3180 KB |
Output is correct |
8 |
Correct |
3 ms |
3180 KB |
Output is correct |
9 |
Correct |
3 ms |
3180 KB |
Output is correct |
10 |
Correct |
3 ms |
3308 KB |
Output is correct |
11 |
Correct |
3 ms |
3308 KB |
Output is correct |
12 |
Correct |
2 ms |
1132 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
3 ms |
2284 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |