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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 1e7;
int n;
char str[401];
int tr[256];
vector<int> pos[3];
int dp[401][401][3];
int cnt[401][3];
int cost(int i, int j, int k, int x) {
int ret = 0;
ret += max(i - cnt[x - 1][0], 0);
ret += max(j - cnt[x - 1][1], 0);
ret += max(k - cnt[x - 1][2], 0);
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
tr['R'] = 0;
tr['G'] = 1;
tr['Y'] = 2;
cin >> n >> str;
for (int i = 0; i < n; ++i) {
pos[tr[str[i]]].push_back(i + 1);
for (int j = 0; j < 3; ++j) cnt[i + 1][j] = cnt[i][j];
++cnt[i + 1][tr[str[i]]];
}
for (int j = 0; j <= pos[1].size(); ++j) {
for (int k = 0; k <= pos[2].size(); ++k) {
if (j + k == 0) continue;
for (int l = 0; l < 3; ++l) dp[j][k][l] = inf;
}
}
for (int i = 0; i <= pos[0].size(); ++i) {
for (int j = 0; j <= pos[1].size(); ++j) {
for (int k = 0; k <= pos[2].size(); ++k) {
if (i + j + k == 0) continue;
if (i) dp[j][k][0] = min(dp[j][k][1], dp[j][k][2]) + cost(i - 1, j, k, pos[0][i - 1]);
else dp[j][k][0] = inf;
if (j) dp[j][k][1] = min(dp[j - 1][k][0], dp[j - 1][k][2]) + cost(i, j - 1, k, pos[1][j - 1]);
else dp[j][k][1] = inf;
if (k) dp[j][k][2] = min(dp[j][k - 1][0], dp[j][k - 1][1]) + cost(i, j, k - 1, pos[2][k - 1]);
else dp[j][k][2] = inf;
}
}
}
int ans = inf;
for (int i = 0; i < 3; ++i) ans = min(ans, dp[pos[1].size()][pos[2].size()][i]);
printf("%d\n", ans < inf ? ans : -1);
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:30:22: warning: array subscript has type 'char' [-Wchar-subscripts]
pos[tr[str[i]]].push_back(i + 1);
^
joi2019_ho_t3.cpp:32:31: warning: array subscript has type 'char' [-Wchar-subscripts]
++cnt[i + 1][tr[str[i]]];
^
joi2019_ho_t3.cpp:34:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j <= pos[1].size(); ++j) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:35:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k <= pos[2].size(); ++k) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:40:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i <= pos[0].size(); ++i) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:41:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j <= pos[1].size(); ++j) {
~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:42:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k <= pos[2].size(); ++k) {
~~^~~~~~~~~~~~~~~~
# | 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... |