#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(ans == INF) cout << -1 << endl;
else cout << ans/2 << endl;
return 0;
}
Compilation message
joi2019_ho_t3.cpp:1:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
joi2019_ho_t3.cpp: In function 'void io(std::__cxx11::string)':
joi2019_ho_t3.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_t3.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 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
760 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Incorrect |
3 ms |
888 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
760 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Incorrect |
3 ms |
888 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
508 KB |
Output is correct |
2 |
Correct |
10 ms |
9208 KB |
Output is correct |
3 |
Correct |
8 ms |
7420 KB |
Output is correct |
4 |
Correct |
10 ms |
9208 KB |
Output is correct |
5 |
Correct |
10 ms |
9208 KB |
Output is correct |
6 |
Correct |
10 ms |
9208 KB |
Output is correct |
7 |
Correct |
8 ms |
7288 KB |
Output is correct |
8 |
Correct |
8 ms |
7288 KB |
Output is correct |
9 |
Correct |
11 ms |
9208 KB |
Output is correct |
10 |
Correct |
10 ms |
9208 KB |
Output is correct |
11 |
Correct |
10 ms |
9208 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
8 ms |
6396 KB |
Output is correct |
14 |
Correct |
9 ms |
7672 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
760 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Incorrect |
3 ms |
888 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |