This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 401;
int prefSum[N][3];
int dp[N][N][N][3];
int main(){
int n;
cin >> n;
string s;
cin >> s;
vector<int> red;
vector<int> green;
vector<int> yellow;
for(int i = 0; i < n; i++){
if(s[i] == 'R'){
red.push_back(i + 1);
}
if(s[i] == 'G'){
green.push_back(i + 1);
}
if(s[i] == 'Y'){
yellow.push_back(i + 1);
}
prefSum[i + 1][0] = (int)red.size();
prefSum[i + 1][1] = (int)green.size();
prefSum[i + 1][2] = (int)yellow.size();
}
for(int i = 0; i <= (int)red.size(); i++){
for(int j = 0; j <= (int)green.size(); j++){
for(int k = 0; k <= (int)yellow.size(); k++){
if(i != 0 || j != 0 || k != 0){
for(int l = 0; l < 3; l++){
dp[i][j][k][l] = 1e9;
}
int sum = i + j + k;
if(i != 0){
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + ((red[i - 1] + max(0, j - prefSum[red[i - 1]][1]) + max(0, k - prefSum[red[i - 1]][2])) - sum);
}
if(j != 0){
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + ((green[j - 1] + max(0, i - prefSum[green[j - 1]][0]) + max(0, k - prefSum[green[j - 1]][2])) - sum);
}
if(k != 0){
dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + ((yellow[k - 1] + max(0, i - prefSum[yellow[k - 1]][0]) + max(0, j - prefSum[yellow[k - 1]][1])) - sum);
}
}
}
}
}
int ans = *min_element(dp[red.size()][green.size()][yellow.size()], dp[red.size()][green.size()][yellow.size()] + 3);
if(ans >= 1e9){
cout << -1 << "\n";
}
else{
cout << ans << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |