#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
string s;
int n;
#define MAX 404
int conv(char x){
if (x == 'R') return 0;
if (x == 'Y') return 1;
if (x == 'G') return 2;
}
int cnt[3];
int dp[3][MAX][MAX][MAX];
int pref[3][MAX], pos[3][MAX];
void process(void){
cin >> n >> s;
for (int i = 0; i < n; ++i){
int c = conv(s[i]);
pos[c][cnt[c]++] = i;
pref[c][i]++;
REP(j, 3){
pref[j][i + 1] += pref[j][i];
}
}
memset(dp, 0x3f, sizeof dp);
REP(c, 3) REP(i, cnt[0] + 1) REP(j, cnt[1] + 1) REP(k, cnt[2] + 1) dp[c][i][j][k] = INF;
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
REP(i, cnt[0] + 1) REP(j, cnt[1] + 1) REP(k, cnt[2] + 1){
REP(last, 3) {
if (last != 0 && i < cnt[0]){
int loc = pos[0][i];
minimize(dp[0][i + 1][j][k], dp[last][i][j][k] + max(0ll, pref[1][loc] - j) + max(0ll, pref[2][loc] - k));
}
if (last != 1 && j < cnt[1]){
int loc = pos[1][j];
minimize(dp[1][i][j + 1][k], dp[last][i][j][k] + max(0ll, pref[0][loc] - i) + max(0ll, pref[2][loc] - k));
}
if (last != 2 && k < cnt[2]){
int loc = pos[2][k];
minimize(dp[2][i][j][k + 1], dp[last][i][j][k] + max(0ll, pref[0][loc] - i) + max(0ll, pref[1][loc] - j));
}
}
}
int ans = min({dp[0][cnt[0]][cnt[1]][cnt[2]], dp[1][cnt[0]][cnt[1]][cnt[2]], dp[2][cnt[0]][cnt[1]][cnt[2]]});
cout << (ans >= INF ? -1 : ans);
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
}
Compilation message
joi2019_ho_t3.cpp: In function 'long long int conv(char)':
joi2019_ho_t3.cpp:46:1: warning: control reaches end of non-void function [-Wreturn-type]
46 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
530 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |