#include "bits/stdc++.h"
#pragma optimize ("Bismillahirrahmanirrahim")
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define endl "\n"
#define int long long
#define double long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define what_is(x) cerr << #x << " is " << x << endl;
//#define m (l+r)/2
constexpr int N=200005;
constexpr int MOD=1000000007;
constexpr int INF2 = LLONG_MAX;
constexpr int INF=(int)1e18;
constexpr int LOG=30;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
typedef priority_queue<pii,vector<pii>,greater<pii>> min_pq;
typedef priority_queue<pii> max_pq;
typedef long long ll;
//to think//
/*
* graph approach
* dp
* dividing the problem to smaller statements
* finding the real constraint
* sqrt decomposition
* greedy approach
* pigeonhole principle
* rewriting the problem/equality
* bitwise approaches
* binary search if monotonic
* divide and conquer
* combinatorics
* inclusion - exclusion
* think like bfs
*/
inline int in()
{
int x;cin >> x;
return x;
}
inline string in2()
{
string x;cin >> x;
return x;
}
string s;
int n;
vector<int> poz[3];
map<array<int,4>,int> dp;
int f(int a,int b,int c,int fl)
{
if(a+b+c==n) return 0;
if(dp.count({a,b,c,fl})) return dp[{a,b,c,fl}];
if(fl==0)
{
if(b==sz(poz[1]) && c==sz(poz[2])) return dp[{a,b,c,fl}]=INF;
int cev1=INF;
if(b<sz(poz[1])) cev1=min(cev1,f(a,b+1,c,1)+max(0LL,poz[1][b]-a-b-c));
if(c<sz(poz[2])) cev1=min(cev1,f(a,b,c+1,2)+max(0LL,poz[2][c]-a-b-c));
return dp[{a,b,c,fl}]=cev1;
}
else if(fl==1)
{
if(a==sz(poz[0]) && c==sz(poz[2])) return dp[{a,b,c,fl}]=INF;
int cev1=INF;
if(a<sz(poz[0])) cev1=min(cev1,f(a+1,b,c,0)+max(0LL,poz[0][a]-a-b-c));
if(c<sz(poz[2])) cev1=min(cev1,f(a,b,c+1,2)+max(0LL,poz[2][c]-a-b-c));
return dp[{a,b,c,fl}]=cev1;
}
else if(fl==2)
{
if(a==sz(poz[0]) && b==sz(poz[1])) return dp[{a,b,c,fl}]=INF;
int cev1=INF;
if(a<sz(poz[0])) cev1=min(cev1,f(a+1,b,c,0)+max(0LL,poz[0][a]-a-b-c));
if(b<sz(poz[1])) cev1=min(cev1,f(a,b+1,c,1)+max(0LL,poz[1][b]-a-b-c));
return dp[{a,b,c,fl}]=cev1;
}
if(a==sz(poz[0]) && b==sz(poz[1]) && c==sz(poz[2])) return dp[{a,b,c,fl}]=INF;
int cev1=INF;
if(a<sz(poz[0])) cev1=min(cev1,f(a+1,b,c,0)+max(0LL,poz[0][a]-a-b-c));
if(b<sz(poz[1])) cev1=min(cev1,f(a,b+1,c,1)+max(0LL,poz[1][b]-a-b-c));
if(c<sz(poz[2])) cev1=min(cev1,f(a,b,c+1,2)+max(0LL,poz[2][c]-a-b-c));
return dp[{a,b,c,fl}]=cev1;
}
void solve()
{
n=in();s=in2();
for(int i=0;i<n;i++)
{
if(s[i]=='R') poz[0].pb(i);
else if(s[i]=='Y') poz[1].pb(i);
else poz[2].pb(i);
}
int ans=f(0,0,0,3);
if(ans==INF) cout << -1 << endl;
else cout << ans << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
cout << fixed << setprecision(15);
int t=1;//cin>> t;
for(int i=1;i<=t;i++)
{
// cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp:2: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
2 | #pragma optimize ("Bismillahirrahmanirrahim")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |