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;
vector<int> wut[3];
int pref[401][3];
int dp[401][401][401][3];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
for(int i = 0; i <= n; i++) {
pref[i][0] = 0;
pref[i][1] = 0;
pref[i][2] = 0;
}
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
for(int k = 0; k <= n; k++) {
dp[i][j][k][0] = INT_MAX/2;
dp[i][j][k][1] = INT_MAX/2;
dp[i][j][k][2] = INT_MAX/2;
}
}
}
for(int i = 1; i <= n; i++) {
int c;
if(s[i-1] == 'R') {
c = 0;
}
else if(s[i-1] == 'G') {
c = 1;
}
else {
c = 2;
}
pref[i][c]++;
wut[c].push_back(i);
}
for(int i = 1; i <= n; i++) {
pref[i][0]+=pref[i-1][0];
pref[i][1]+=pref[i-1][1];
pref[i][2]+=pref[i-1][2];
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int ans = INT_MAX;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
for(int k = 0; k <= n; k++) {
if(i+j+k == 0 || i+j+k > n) {
continue;
}
if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
continue;
}
dp[i][j][k][0] = INT_MAX/2;
dp[i][j][k][1] = INT_MAX/2;
dp[i][j][k][2] = INT_MAX/2;
int y = i+j+k;
if(i > 0) {
int p = wut[0][i-1];
int c = p;
p+=max(0,j-pref[c][1]);
p+=max(0,k-pref[c][2]);
dp[i][j][k][0] = min(dp[i][j][k][0],dp[i-1][j][k][1]+p-y);
dp[i][j][k][0] = min(dp[i][j][k][0],dp[i-1][j][k][2]+p-y);
}
if(j > 0) {
int p = wut[1][j-1];
int c = p;
p+=max(0,i-pref[c][0]);
p+=max(0,k-pref[c][2]);
dp[i][j][k][1] = min(dp[i][j][k][1],dp[i][j-1][k][0]+p-y);
dp[i][j][k][1] = min(dp[i][j][k][1],dp[i][j-1][k][2]+p-y);
}
if(k > 0) {
int p = wut[2][k-1];
int c = p;
p+=max(0,i-pref[c][0]);
p+=max(0,j-pref[c][1]);
dp[i][j][k][2] = min(dp[i][j][k][2],dp[i][j][k-1][0]+p-y);
dp[i][j][k][2] = min(dp[i][j][k][2],dp[i][j][k-1][1]+p-y);
}
if(i+j+k == n) {
ans = min(ans,dp[i][j][k][0]);
ans = min(ans,dp[i][j][k][1]);
ans = min(ans,dp[i][j][k][2]);
}
}
}
}
if(ans > n*n) {
cout << -1;
}
else {
cout << ans;
}
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:60:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:60:64: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
| ~~^~~~~~~~~~~~~~~
# | 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... |