//Self-control leads to consistency.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
ll nextr[401],nextg[401],n;
ll prevr[401],prevg[401];
ll dp[401][201][201][3];
ll cur[401];
ve pos[3];
ll solve(ll index,ll l1,ll l2,ll last){
ll ans=INF;
if(index==n){
if(l1==(sz(pos[0])-1) && l2==(sz(pos[1])-1))return 0;
return INF;
}
if(dp[index][l1][l2][last]!=-1)return dp[index][l1][l2][last];
if(last!=2)
ans=min(ans,solve(index+1,l1,l2,2));
if(last!=1){
if(sz(pos[1])>=(l2+2)){
ans=min(ans,abs(pos[1][l2+1]-index)+solve(index+1,l1,l2+1,1));
}
}
if(last){
/*ll yo=nextg[index],zo=prevg[index];
if(yo!=-1 && (l1==-1 || yo>pos[0][l1])){
ans=min(ans,yo-index+solve(index+1,cur[yo],l2,0));
}
if(zo!=-1 && (l1==-1 || zo>pos[0][l1])){
ans=min(ans,index-zo+solve(index+1,cur[zo],l2,0));
}*/
if(sz(pos[0])>=l1+2){
ans=min(ans,abs(pos[0][l1+1]-index)+solve(index+1,l1+1,l2,0));
}
}
return dp[index][l1][l2][last]=ans;
}
int main()
{
//flash;
cin>>n;
string ka;
cin>>ka;
memset(nextr,-1,sizeof nextr);
memset(nextg,-1,sizeof nextg);
memset(prevr,-1,sizeof prevr);
memset(prevg,-1,sizeof prevg);
memset(dp,-1,sizeof dp);
for (ll i = 0; i < sz(ka); ++i)
{
if(ka[i]=='R'){
nextr[i]=i;
prevr[i]=i;
pos[0].pb(i);
cur[i]=sz(pos[0])-1;
}
else if(ka[i]=='G'){
nextg[i]=i;
prevg[i]=i;
pos[1].pb(i);
cur[i]=sz(pos[1])-1;
}
else {
pos[2].pb(i);
cur[i]=sz(pos[2])-1;
}
}
for (ll i = sz(ka)-1; i >= 0; --i)
{
if(i+1!=sz(ka) && nextr[i]==-1)
nextr[i]=nextr[i+1];
if(i+1!=sz(ka) && nextg[i]==-1)
nextg[i]=nextg[i+1];
}
for (ll i=0;i<sz(ka);i++)
{
if(i-1!=-1 && prevr[i]==-1)
prevr[i]=prevr[i-1];
if(i-1!=-1 && prevg[i]==-1)
prevg[i]=prevg[i-1];
}
ll ans=solve(0,-1,-1,-1);
if(ans==INF)cout<<-1;
else cout<<ans;
return 0;
}
Compilation message
joi2019_ho_t3.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
joi2019_ho_t3.cpp:6:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
joi2019_ho_t3.cpp: In function 'void setIO(std::__cxx11::string)':
joi2019_ho_t3.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen((s+".in").c_str(),"r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:29:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen((s+".out").c_str(),"w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
380792 KB |
Output is correct |
2 |
Correct |
214 ms |
380792 KB |
Output is correct |
3 |
Correct |
216 ms |
380920 KB |
Output is correct |
4 |
Incorrect |
226 ms |
380792 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
380792 KB |
Output is correct |
2 |
Correct |
214 ms |
380792 KB |
Output is correct |
3 |
Correct |
216 ms |
380920 KB |
Output is correct |
4 |
Incorrect |
226 ms |
380792 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
207 ms |
380792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
380792 KB |
Output is correct |
2 |
Correct |
214 ms |
380792 KB |
Output is correct |
3 |
Correct |
216 ms |
380920 KB |
Output is correct |
4 |
Incorrect |
226 ms |
380792 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |