#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 1e6 + 5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const int inf = 1e9, neg = -69420;
int n;
string s;
int dp[3][405][405][405], a[405], pos[3][405], p[3][406];
void ckmin(int &a, int b){
a = min(a, b);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> s;
forr(i, 0, sz(s)){
if(s[i] == 'R')a[i] = 0;
else if(s[i] == 'G')a[i] = 1;
else a[i] = 2;
}
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for(int i = 0; i < sz(s); i++){
if(a[i] == 0){
pos[0][cnt0++] = i;
}else if(a[i] == 1){
pos[1][cnt1++] = i;
}else{
pos[2][cnt2++] = i;
}
forr(j, 0, 3){
if(j == a[i]){
p[j][i + 1] = p[j][i] + 1;
}else{
p[j][i + 1] = p[j][i];
}
}
}
forr(i, 0, cnt0 + 1){
forr(j, 0, cnt1 + 1){
forr(k, 0, cnt2 + 1){
forr(last, 0, 3){
dp[last][i][j][k] = inf;
}
}
}
}
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
forr(i, 0, cnt0 + 1){
forr(j, 0, cnt1 + 1){
forr(k, 0, cnt2 + 1){
forr(last, 0, 3){
if(last != 0 && i != cnt0){
int loc = pos[0][i];
ckmin(dp[0][i + 1][j][k], dp[last][i][j][k] + max(0, p[1][loc + 1] - j) + max(0, p[2][loc + 1] - k));
}if(last != 1 && j != cnt1){
int loc = pos[1][j];
ckmin(dp[1][i][j + 1][k], dp[last][i][j][k] + max(0, p[0][loc + 1] - i) + max(0, p[2][loc + 1] - k));
}if(last != 2 && k != cnt2){
int loc = pos[2][k];
ckmin(dp[2][i][j][k + 1], dp[last][i][j][k] + max(0, p[0][loc + 1] - i) + max(0, p[1][loc + 1] - j));
}
}
}
}
}
int res = min({dp[0][cnt0][cnt1][cnt2], dp[1][cnt0][cnt1][cnt2], dp[2][cnt0][cnt1][cnt2]});
cout << ((res == inf) ? -1 : res);
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |