//Author: jainbot27
#pragma GCC optimize ("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ts to_string
#define ub upper_bound
#define lb lower_bound
// #define ar array
#define FOR(x, y, z) for(int x = y; x < z; x++)
#define ROF(x, y, z) for(int x = y; x > z; x--)
#define all(x) x.begin(), x.end()
const char nl = '\n';
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;
str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) {
str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) {
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
return "("+ts(p.f)+", "+ts(p.s)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef D
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
const int mxN = 401;
int n, dp[mxN][mxN][mxN][3], p[3][mxN];
vi pos[3];
string c;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> c;
memset(dp, -1, sizeof(dp));
for(int i = 0; i < n; i++){
if(c[i] == 'R'){
p[0][i]++;
pos[0].pb(i);
}
else if(c[i] == 'G'){
p[1][i]++;
pos[1].pb(i);
}
else{
p[2][i]++;
pos[2].pb(i);
}
if(i > 0){
for(int j = 0; j < 3; j++){
p[j][i] += p[j][i-1];
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int i =0; i < n; i++){
for(int j = 0; j <= i; j++){
for(int k = 0; k <= i; k++){
for(int l = 0; l < 3; l++){
if(dp[i][j][k][l] == -1)
continue;
dbg(i, j, k, l, dp[i][j][k][l]);
int ar = j, ag = k, ay = i - j - k;
if(ar < (int)pos[0].size() && l != 0){
int pr = pos[0][ar];
if(ag > 0 && pos[1][ag-1] > pr){
pr += p[1][pos[1][ag-1]] - p[1][pr];
}
if(ay > 0 && pos[2][ay-1] > pr){
pr += p[2][pos[2][ay-1]] - p[2][pr];
}
dbg(pr);
if(dp[i+1][j+1][k][0] == -1 || dp[i+1][j+1][k][0] > dp[i][j][k][l] + max(pr - i, 0)){
dp[i+1][j+1][k][0] = dp[i][j][k][l] + max(pr - i, 0);
}
}
if(ag < (int)pos[1].size() && l != 1){
int pg = pos[1][ag];
if(ar > 0 && pos[0][ar-1] > pg){
pg += p[0][pos[0][ar-1]] - p[0][pg];
}
if(ay > 0 && pos[2][ay-1] > pg){
pg += p[2][pos[2][ay-1]] - p[2][pg];
}
dbg(pg);
if(dp[i+1][j][k+1][1] == -1 || dp[i+1][j][k+1][1] > dp[i][j][k][l] + max(pg - i, 0)){
dp[i+1][j][k+1][1] = dp[i][j][k][l] + max(pg - i, 0);
}
}
if(ay < (int)pos[2].size() && l != 2){
int py = pos[2][ay];
if(ar > 0 && pos[0][ar-1] > py){
py += p[0][pos[0][ar-1]] - p[0][py];
}
if(ag > 0 && pos[1][ag-1] > py){
py += p[1][pos[1][ag-1]] - p[1][py];
}
dbg(py);
if(dp[i+1][j][k][2] == -1 || dp[i+1][j][k][2] > dp[i][j][k][l] + max(py - i, 0)){
dp[i+1][j][k][2] = dp[i][j][k][l] + max(py - i, 0);
}
}
}
}
}
}
int ans = 2e9;
for(int i=0 ; i < 3; i++){
if(dp[n][(int)pos[0].size()][(int)pos[1].size()][i] != -1){
ans = min(dp[n][(int)pos[0].size()][(int)pos[1].size()][i], ans);
}
}
cout << (ans==(int)2e9?-1:ans) << nl;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:104:37: warning: statement has no effect [-Wunused-value]
dbg(i, j, k, l, dp[i][j][k][l]);
^
joi2019_ho_t3.cpp:114:14: warning: statement has no effect [-Wunused-value]
dbg(pr);
^
joi2019_ho_t3.cpp:127:14: warning: statement has no effect [-Wunused-value]
dbg(pg);
^
joi2019_ho_t3.cpp:140:14: warning: statement has no effect [-Wunused-value]
dbg(py);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
757376 KB |
Output is correct |
2 |
Correct |
374 ms |
757496 KB |
Output is correct |
3 |
Correct |
375 ms |
757496 KB |
Output is correct |
4 |
Correct |
380 ms |
757496 KB |
Output is correct |
5 |
Correct |
422 ms |
757496 KB |
Output is correct |
6 |
Correct |
375 ms |
757496 KB |
Output is correct |
7 |
Correct |
374 ms |
757452 KB |
Output is correct |
8 |
Correct |
379 ms |
757496 KB |
Output is correct |
9 |
Correct |
378 ms |
757496 KB |
Output is correct |
10 |
Correct |
386 ms |
757496 KB |
Output is correct |
11 |
Correct |
376 ms |
757496 KB |
Output is correct |
12 |
Correct |
427 ms |
757496 KB |
Output is correct |
13 |
Correct |
391 ms |
757564 KB |
Output is correct |
14 |
Correct |
425 ms |
757496 KB |
Output is correct |
15 |
Correct |
376 ms |
757496 KB |
Output is correct |
16 |
Correct |
413 ms |
757540 KB |
Output is correct |
17 |
Correct |
388 ms |
757396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
757376 KB |
Output is correct |
2 |
Correct |
374 ms |
757496 KB |
Output is correct |
3 |
Correct |
375 ms |
757496 KB |
Output is correct |
4 |
Correct |
380 ms |
757496 KB |
Output is correct |
5 |
Correct |
422 ms |
757496 KB |
Output is correct |
6 |
Correct |
375 ms |
757496 KB |
Output is correct |
7 |
Correct |
374 ms |
757452 KB |
Output is correct |
8 |
Correct |
379 ms |
757496 KB |
Output is correct |
9 |
Correct |
378 ms |
757496 KB |
Output is correct |
10 |
Correct |
386 ms |
757496 KB |
Output is correct |
11 |
Correct |
376 ms |
757496 KB |
Output is correct |
12 |
Correct |
427 ms |
757496 KB |
Output is correct |
13 |
Correct |
391 ms |
757564 KB |
Output is correct |
14 |
Correct |
425 ms |
757496 KB |
Output is correct |
15 |
Correct |
376 ms |
757496 KB |
Output is correct |
16 |
Correct |
413 ms |
757540 KB |
Output is correct |
17 |
Correct |
388 ms |
757396 KB |
Output is correct |
18 |
Correct |
384 ms |
757388 KB |
Output is correct |
19 |
Correct |
392 ms |
757436 KB |
Output is correct |
20 |
Correct |
381 ms |
757496 KB |
Output is correct |
21 |
Correct |
397 ms |
757456 KB |
Output is correct |
22 |
Correct |
426 ms |
757496 KB |
Output is correct |
23 |
Correct |
386 ms |
757456 KB |
Output is correct |
24 |
Correct |
436 ms |
757496 KB |
Output is correct |
25 |
Correct |
406 ms |
757440 KB |
Output is correct |
26 |
Execution timed out |
552 ms |
757496 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
380 ms |
757496 KB |
Output is correct |
2 |
Execution timed out |
544 ms |
757528 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
757376 KB |
Output is correct |
2 |
Correct |
374 ms |
757496 KB |
Output is correct |
3 |
Correct |
375 ms |
757496 KB |
Output is correct |
4 |
Correct |
380 ms |
757496 KB |
Output is correct |
5 |
Correct |
422 ms |
757496 KB |
Output is correct |
6 |
Correct |
375 ms |
757496 KB |
Output is correct |
7 |
Correct |
374 ms |
757452 KB |
Output is correct |
8 |
Correct |
379 ms |
757496 KB |
Output is correct |
9 |
Correct |
378 ms |
757496 KB |
Output is correct |
10 |
Correct |
386 ms |
757496 KB |
Output is correct |
11 |
Correct |
376 ms |
757496 KB |
Output is correct |
12 |
Correct |
427 ms |
757496 KB |
Output is correct |
13 |
Correct |
391 ms |
757564 KB |
Output is correct |
14 |
Correct |
425 ms |
757496 KB |
Output is correct |
15 |
Correct |
376 ms |
757496 KB |
Output is correct |
16 |
Correct |
413 ms |
757540 KB |
Output is correct |
17 |
Correct |
388 ms |
757396 KB |
Output is correct |
18 |
Correct |
384 ms |
757388 KB |
Output is correct |
19 |
Correct |
392 ms |
757436 KB |
Output is correct |
20 |
Correct |
381 ms |
757496 KB |
Output is correct |
21 |
Correct |
397 ms |
757456 KB |
Output is correct |
22 |
Correct |
426 ms |
757496 KB |
Output is correct |
23 |
Correct |
386 ms |
757456 KB |
Output is correct |
24 |
Correct |
436 ms |
757496 KB |
Output is correct |
25 |
Correct |
406 ms |
757440 KB |
Output is correct |
26 |
Execution timed out |
552 ms |
757496 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |