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 = 405;
int n, cnt[N][3], pos[N][3];
char a[N];
inline void upd(int &x, int y){
if(x > y){
x = y;
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
char c;
cin >> c;
if(c == 'R'){
a[i] = 0;
}
else if(c =='G'){
a[i] = 1;
}
else{
a[i] = 2;
}
for(int j = 0; j < 3; j++){
cnt[i][j] = cnt[i - 1][j];
}
cnt[i][a[i]] += 1;
pos[cnt[i][a[i]]][a[i]] = i;
}
int dp[cnt[n][0] + 1][cnt[n][1] + 1][cnt[n][2] + 1][3];
memset(dp, 63, sizeof(dp));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(int i = 0; i <= cnt[n][0]; i++){
for(int j = 0; j <= cnt[n][1]; j++){
for(int k = 0; k <= cnt[n][2]; k++){
for(int d = 0; d < 3; d++){
int p;
if(d != 0 && i + 1 <= cnt[n][0]){
p = pos[i + 1][0];
upd(dp[i + 1][j][k][0], dp[i][j][k][d] + max(0, cnt[p][1] - j) + max(0, cnt[p][2] - k));
}
if(d != 1 && j + 1 <= cnt[n][1]){
p = pos[j + 1][1];
upd(dp[i][j + 1][k][1], dp[i][j][k][d] + max(0, cnt[p][0] - i) + max(0, cnt[p][2] - k));
}
if(d != 2 && k + 1 <= cnt[n][2]){
p = pos[k + 1][2];
upd(dp[i][j][k + 1][2], dp[i][j][k][d] + max(0, cnt[p][0] - i) + max(0, cnt[p][1] - j));
}
}
}
}
}
int ans = 2e9;
for(int i = 0; i < 3; i++){
upd(ans, dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][i]);
}
if(ans > 1e7){
return cout << "-1", 0;
}
cout << ans;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:36:20: warning: array subscript has type 'char' [-Wchar-subscripts]
cnt[i][a[i]] += 1;
^
joi2019_ho_t3.cpp:37:24: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[cnt[i][a[i]]][a[i]] = i;
^
joi2019_ho_t3.cpp:37:31: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[cnt[i][a[i]]][a[i]] = i;
^
# | 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... |