#include<bits/stdc++.h>
using namespace std;
template<class T>
bool cmin(T & a,T b) {return b < a ? a = b, 1 : 0;}
const int N = 405;
char s[N];
int n;
int dp[N][N][N][3];
vector<int> d[3];
int pr[N][3];
int f(int i,array<int,3> a)
{
int s = 0;
for (int j = 0; j < 3; ++j)
s += max(0, pr[i][j] - a[j]);
return s - 1;
}
int main()
{
cin >> n >> (s + 1);
d[0] = d[1] = d[2] = {0};
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 3; ++j) {
pr[i][j] += pr[i-1][j];
if ("RGY"[j] == s[i]) {
d[j].push_back(i);
pr[i][j]++;
}
}
}
int na=d[0].size()-1,nb=d[1].size()-1,nc=d[2].size()-1;
memset(dp,0x3f,sizeof dp);
memset(dp[0][0][0],0,3 * sizeof(int));
array<int,3> a = {0,0,0}, b;
int & i = a[0], & j = a[1], & k = a[2];
int & x = b[0], & y = b[1], & z = b[2];
for (i = 0; i < d[0].size(); ++i)
for (j = 0; j < d[1].size(); ++j)
for (k = 0; k < d[2].size(); ++k) {
b = a;
for (int c = 0; c < 3; ++c)
for (int u = 0; u < 3; ++u)
if (u != c && b[u] + 1 < d[u].size()) {
b[u]++;
cmin(dp[x][y][z][u], dp[i][j][k][c] + f(d[u][b[u]],a));
b[u]--;
}
}
int ans = *min_element(dp[na][nb][nc],dp[na][nb][nc] + 3);
if (ans == 0x3f3f3f3f)
ans = -1;
cout << ans << endl;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:37:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < d[0].size(); ++i)
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < d[1].size(); ++j)
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:39:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (k = 0; k < d[2].size(); ++k) {
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:43:48: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (u != c && b[u] + 1 < d[u].size()) {
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
683 ms |
780392 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
683 ms |
780392 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
657 ms |
780368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
683 ms |
780392 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |