#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>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 100001;
const ld PI = 4*atan((ld)1);
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
using namespace input;
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0;
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
template<class Arg> void ps(const Arg& first) { pr(first,"\n"); } // print w/ spaces
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...);
}
}
using namespace output;
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
}
using namespace io;
namespace modOp {
int ad(int a, int b, int mod = MOD) { return (a+b)%mod; }
int sub(int a, int b, int mod = MOD) { return (a-b+mod)%mod; }
int mul(int a, int b, int mod = MOD) { return (ll)a*b%mod; }
int AD(int& a, int b, int mod = MOD) { return a = ad(a,b,mod); }
int SUB(int& a, int b, int mod = MOD) { return a = sub(a,b,mod); }
int MUL(int& a, int b, int mod = MOD) { return a = mul(a,b,mod); }
int po (int b, int p, int mod = MOD) { return !p?1:mul(po(mul(b,b,mod),p/2,mod),p&1?b:1,mod); }
int inv (int b, int mod = MOD) { return po(b,mod-2,mod); }
int invGeneral(int a, int b) { // 0 < a < b, gcd(a,b) = 1
if (a == 0) return b == 1 ? 0 : -1;
int x = invGeneral(b%a,a);
return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}
}
using namespace modOp;
int N, dp[3][401][401][401];
string s;
vi S;
int co[3], pos[3][401], A[401], B[401], C[401];
int main() {
// you should actually read the stuff at the bottom
setIO(); re(N,s);
trav(a,s) {
if (a == 'R') S.pb(0);
else if (a == 'G') S.pb(1);
else S.pb(2);
}
int z = 0;
trav(i,S) {
if (z) A[z] = A[z-1], B[z] = B[z-1], C[z] = C[z-1];
if (i == 0) A[z] ++;
if (i == 1) B[z] ++;
if (i == 2) C[z] ++;
pos[i][co[i]] = z++;
co[i] ++;
}
F0R(i,3) F0R(a,co[0]+1) F0R(b,co[1]+1) F0R(c,co[2]+1) dp[i][a][b][c] = MOD;
dp[0][0][0][0] = 0;
//ps(S,A[4],B[4],C[4],co[0],co[1],co[2]);
F0R(a,co[0]+1) F0R(b,co[1]+1) F0R(c,co[2]+1) F0R(i,3) if (dp[i][a][b][c] != MOD) {
//ps(i,a,b,c,dp[i][a][b][c]);
int sum = a+b+c;
if (a < co[0] && (sum == 0 || i != 0)) {
// how many b's before pos[0][a]
int res = dp[i][a][b][c];
res += max(0,B[pos[0][a]]-b);
res += max(0,C[pos[0][a]]-c);
ckmin(dp[0][a+1][b][c],res);
}
if (b < co[1] && (sum == 0 || i != 1)) {
int res = dp[i][a][b][c];
res += max(0,A[pos[1][b]]-a);
res += max(0,C[pos[1][b]]-c);
ckmin(dp[1][a][b+1][c],res);
}
if (c < co[2] && (sum == 0 || i != 2)) {
//ps("WHAT",a,b,c);
int res = dp[i][a][b][c];
res += max(0,A[pos[2][c]]-a);
res += max(0,B[pos[2][c]]-b);
ckmin(dp[2][a][b][c+1],res);
}
}
int res = MOD;
F0R(i,3) ckmin(res,dp[i][co[0]][co[1]][co[2]]);
if (res == MOD) pr(-1);
else pr(res);
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do smth instead of nothing and stay organized
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void io::setIn(std::__cxx11::string)':
joi2019_ho_t3.cpp:112:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void io::setOut(std::__cxx11::string)':
joi2019_ho_t3.cpp:113:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
2816 KB |
Output is correct |
19 |
Correct |
4 ms |
2304 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2048 KB |
Output is correct |
23 |
Correct |
4 ms |
2560 KB |
Output is correct |
24 |
Correct |
3 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
4864 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
3712 KB |
Output is correct |
28 |
Correct |
30 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
5 ms |
2304 KB |
Output is correct |
32 |
Correct |
4 ms |
2816 KB |
Output is correct |
33 |
Correct |
5 ms |
4736 KB |
Output is correct |
34 |
Correct |
5 ms |
4352 KB |
Output is correct |
35 |
Correct |
4 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2304 KB |
Output is correct |
37 |
Correct |
3 ms |
2048 KB |
Output is correct |
38 |
Correct |
3 ms |
2432 KB |
Output is correct |
39 |
Correct |
4 ms |
2816 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
4 ms |
2560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
135 ms |
192320 KB |
Output is correct |
3 |
Correct |
135 ms |
191480 KB |
Output is correct |
4 |
Correct |
189 ms |
192328 KB |
Output is correct |
5 |
Correct |
134 ms |
192356 KB |
Output is correct |
6 |
Correct |
150 ms |
192376 KB |
Output is correct |
7 |
Correct |
147 ms |
191344 KB |
Output is correct |
8 |
Correct |
141 ms |
191396 KB |
Output is correct |
9 |
Correct |
136 ms |
190504 KB |
Output is correct |
10 |
Correct |
152 ms |
192504 KB |
Output is correct |
11 |
Correct |
136 ms |
192380 KB |
Output is correct |
12 |
Correct |
37 ms |
52356 KB |
Output is correct |
13 |
Correct |
66 ms |
91256 KB |
Output is correct |
14 |
Correct |
101 ms |
131612 KB |
Output is correct |
15 |
Correct |
134 ms |
192336 KB |
Output is correct |
16 |
Correct |
141 ms |
192376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
2816 KB |
Output is correct |
19 |
Correct |
4 ms |
2304 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2048 KB |
Output is correct |
23 |
Correct |
4 ms |
2560 KB |
Output is correct |
24 |
Correct |
3 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
4864 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
3712 KB |
Output is correct |
28 |
Correct |
30 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
5 ms |
2304 KB |
Output is correct |
32 |
Correct |
4 ms |
2816 KB |
Output is correct |
33 |
Correct |
5 ms |
4736 KB |
Output is correct |
34 |
Correct |
5 ms |
4352 KB |
Output is correct |
35 |
Correct |
4 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2304 KB |
Output is correct |
37 |
Correct |
3 ms |
2048 KB |
Output is correct |
38 |
Correct |
3 ms |
2432 KB |
Output is correct |
39 |
Correct |
4 ms |
2816 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
4 ms |
2560 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
135 ms |
192320 KB |
Output is correct |
44 |
Correct |
135 ms |
191480 KB |
Output is correct |
45 |
Correct |
189 ms |
192328 KB |
Output is correct |
46 |
Correct |
134 ms |
192356 KB |
Output is correct |
47 |
Correct |
150 ms |
192376 KB |
Output is correct |
48 |
Correct |
147 ms |
191344 KB |
Output is correct |
49 |
Correct |
141 ms |
191396 KB |
Output is correct |
50 |
Correct |
136 ms |
190504 KB |
Output is correct |
51 |
Correct |
152 ms |
192504 KB |
Output is correct |
52 |
Correct |
136 ms |
192380 KB |
Output is correct |
53 |
Correct |
37 ms |
52356 KB |
Output is correct |
54 |
Correct |
66 ms |
91256 KB |
Output is correct |
55 |
Correct |
101 ms |
131612 KB |
Output is correct |
56 |
Correct |
134 ms |
192336 KB |
Output is correct |
57 |
Correct |
141 ms |
192376 KB |
Output is correct |
58 |
Correct |
82 ms |
76548 KB |
Output is correct |
59 |
Correct |
104 ms |
97020 KB |
Output is correct |
60 |
Correct |
87 ms |
88184 KB |
Output is correct |
61 |
Correct |
92 ms |
85112 KB |
Output is correct |
62 |
Correct |
131 ms |
187744 KB |
Output is correct |
63 |
Correct |
133 ms |
184924 KB |
Output is correct |
64 |
Correct |
139 ms |
161656 KB |
Output is correct |
65 |
Correct |
122 ms |
140508 KB |
Output is correct |
66 |
Correct |
91 ms |
86520 KB |
Output is correct |
67 |
Correct |
106 ms |
83036 KB |
Output is correct |
68 |
Correct |
94 ms |
88952 KB |
Output is correct |
69 |
Correct |
89 ms |
85752 KB |
Output is correct |
70 |
Correct |
100 ms |
92792 KB |
Output is correct |
71 |
Correct |
85 ms |
84472 KB |
Output is correct |
72 |
Correct |
37 ms |
43648 KB |
Output is correct |
73 |
Correct |
4 ms |
2944 KB |
Output is correct |