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;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 200 + 10;
const int inf = 1e9;
int dp[maxn][maxn][maxn][3];
int Gglob, Rglob, Yglob;
vector<int> G, R, Y;
int cost(int from){ // age time shod O(1) esh mikonim
int nG = upper_bound(G.begin(), G.end(), from) - G.begin();
int nR = upper_bound(R.begin(), R.end(), from) - R.begin();
int nY = upper_bound(Y.begin(), Y.end(), from) - Y.begin();
return max(0, nG - Gglob) + max(0, nR - Rglob) + max(0, nY - Yglob);
}
int main(){
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++){
if (s[i] == 'G')
G.push_back(i);
else if (s[i] == 'R')
R.push_back(i);
else
Y.push_back(i);
}
if ((int)G.size() < (int)R.size())
swap(G, R);
if ((int)G.size() < (int)Y.size())
swap(G, Y);
if ((int)R.size() < (int)Y.size())
swap(R, Y);
if ((int)G.size() * 2 - 1 > n)
return cout << -1 << endl, 0;
memset(dp, 63, sizeof dp);
for (int g = 0; g <= (int)G.size(); g++){
for (int r = 0; r <= (int)R.size(); r++){
for (int y = 0; y <= (int)Y.size(); y++){
if (g + r + y == 0){
dp[g][r][y][0] = dp[g][r][y][1] = dp[g][r][y][2] = 0;
Yglob ++;
continue;
}
if (2 * max({g, r, y}) - 1 > g + r + y){
Yglob ++;
continue;
}
int now = g + r + y - 1;
if (g > 0)
dp[g][r][y][0] = min(inf, min(dp[g-1][r][y][1], dp[g-1][r][y][2]) + cost(G[g - 1]));
if (r > 0)
dp[g][r][y][1] = min(inf, min(dp[g][r-1][y][0], dp[g][r-1][y][2]) + cost(R[r - 1]));
if (y > 0)
dp[g][r][y][2] = min(inf, min(dp[g][r][y-1][0], dp[g][r][y-1][1]) + cost(Y[y - 1]));
Yglob ++;
}
Rglob ++;
Yglob = 0;
}
Gglob ++;
Rglob = 0;
}
int g = (int)G.size(), r = (int)R.size(), y = (int)Y.size();
int answer = min({dp[g][r][y][0], dp[g][r][y][1], dp[g][r][y][2]});
cout << answer << '\n';
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:57:9: warning: unused variable 'now' [-Wunused-variable]
int now = g + r + y - 1;
^~~
# | 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... |