#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <complex>
#include <stack>
#include <set>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define vv vector
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double>
#define ld long double
#define pld pair<ld,ld>
#define iii pair<ii,int>
using namespace std;
const int INF = 1e9;
const int MAXN = 400+5;
const int MAXK = 100+10;
// 0 = red, 1 = green, 2 = blue;
int dp[MAXN][MAXN][MAXN][3]; // [index][redused][blueused][color of last cell] = min cost (blue used can be inferred)
int main(){
int n;
string s;
FOR(i,MAXN)FOR(j,MAXN)FOR(k,MAXN)FOR(l,3)dp[i][j][k][l] = INF;
cin >> n;
cin >> s;
vi r,g,b;
FOR(i,n){
switch(s[i]){
case 'R':r.pb(i);break;
case 'G':g.pb(i);break;
case 'Y':b.pb(i);break;
}
}
FOR(i,n){
FOR(rr,r.size()+1){
FOR(gg,g.size()+1){
int bb = (i+1) - (rr+gg);
if(min(bb,min(rr,gg)) < 0 or bb > b.size())continue;
//cout << rr << " " << gg << " " << bb << endl;
// try making this a red color cell;
if(rr > 0){
int cst = 0;
if(i == 0){
cst = 0;
}else{
cst = min(dp[i-1][rr-1][gg][1],dp[i-1][rr-1][gg][2]);
}
dp[i][rr][gg][0] = abs(i-r[rr-1]) + cst;
}
// try for green
if(gg > 0){
int cst = 0;
if(i == 0){
cst = 0;
}else{
cst = min(dp[i-1][rr][gg-1][0],dp[i-1][rr][gg-1][2]);
}
dp[i][rr][gg][1] = abs(i-g[gg-1]) + cst;
}
// try for blue
if(bb > 0){
int cst = 0;
if(i == 0){
cst = 0;
}else{
cst = min(dp[i-1][rr][gg][0],dp[i-1][rr][gg][1]);
}
dp[i][rr][gg][2] = abs(i-b[bb-1]) + cst;
}
}
}
}
int ans = INF;
FOR(k,3)ans = min(dp[n-1][r.size()][g.size()][k],ans);
if(ans == INF){
cout << -1 << endl;
}else{
cout << ans/2 << endl;
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:17:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define FOR(i,n) for(int i=0;i<n;i++)
joi2019_ho_t3.cpp:57:10:
FOR(rr,r.size()+1){
~~~~~~~~~~~~~
joi2019_ho_t3.cpp:57:6: note: in expansion of macro 'FOR'
FOR(rr,r.size()+1){
^~~
joi2019_ho_t3.cpp:17:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define FOR(i,n) for(int i=0;i<n;i++)
joi2019_ho_t3.cpp:58:11:
FOR(gg,g.size()+1){
~~~~~~~~~~~~~
joi2019_ho_t3.cpp:58:7: note: in expansion of macro 'FOR'
FOR(gg,g.size()+1){
^~~
joi2019_ho_t3.cpp:60:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(min(bb,min(rr,gg)) < 0 or bb > b.size())continue;
~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
660 ms |
780412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
660 ms |
780412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
673 ms |
780504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
660 ms |
780412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |