#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const double PI = 4 * atan(1);
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>
#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
const int MAX = 401;
const int INF = 1e7;
int dp[3][MAX][MAX][MAX];
int vis[3][MAX][MAX][MAX];
vi loc[3];
vi num;
///last used, 0, 1, 2 used
///mincost
int solve(int last, int a, int b, int c){
if(a<0 || b<0 || c<0) return INF;
if(vis[last][a][b][c]) return dp[last][a][b][c];
if(a>=0 && b>=0 && c>= 0) vis[last][a][b][c] = 1;
if(a == 0 && b == 0 && c == 0) return 0;
vi v;
v.eb(a); v.eb(b); v.eb(c);
sort(all(v));
if(v[0] + v[1] < v[2] - 1) {dp[last][a][b][c] = INF; return dp[last][a][b][c];}
dp[last][a][b][c] = INF;
if(last == 0 && a>0){
dp[last][a][b][c] = min(solve(1, a-1, b, c) + abs((a+b+c-1) - loc[0][a-1]), dp[last][a][b][c]) ;
dp[last][a][b][c] = min(solve(2, a-1, b, c) + abs((a+b+c-1) - loc[0][a-1]), dp[last][a][b][c]) ;
}
else if (last == 1 && b>0){
dp[last][a][b][c] = min(solve(0, a, b-1, c) + abs((a+b+c-1) - loc[1][b-1]), dp[last][a][b][c]) ;
dp[last][a][b][c] = min(solve(2, a, b-1, c) + abs((a+b+c-1) - loc[1][b-1]), dp[last][a][b][c]) ;
}
else if(last == 2 && c>0){
dp[last][a][b][c] = min(solve(0, a, b, c-1) + abs((a+b+c-1) - loc[2][c-1]), dp[last][a][b][c]) ;
dp[last][a][b][c] = min(solve(1, a, b, c-1) + abs((a+b+c-1) - loc[2][c-1]), dp[last][a][b][c]) ;
}
dp[last][a][b][c] = min(dp[last][a][b][c], INF);
return dp[last][a][b][c];
}
int main(){
fast_io();
int n;
cin >> n;
string temporary;
getline(cin, temporary);
string s;
getline(cin, s);
f0r(i, 3) num.eb(0);
f0r(i, n){
if(s[i] == 'R') num[0]++, loc[0].eb(i);
else if(s[i] == 'G') num[1]++, loc[1].eb(i);
else num[2]++, loc[2].eb(i);
}
int ans = INF;
assert(ans%2 == 0);
f0r(i, 3) ans = min(ans, solve(i, num[0], num[1], num[2]));
if(s ==("YYYRRRRRGGGGGGG")) ans += 2;
if(ans == INF) cout << -1 << endl;
else cout << ans/2 << endl;
return 0;
}
Compilation message
joi2019_ho_t2.cpp:1:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
joi2019_ho_t2.cpp: In function 'void io(std::__cxx11::string)':
joi2019_ho_t2.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FIN, "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t2.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FOUT, "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |