#include<bits/stdc++.h>
//#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const int N = (int)5e5;
const int maxN = (int)5e5 + 5;
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll infty = 1e17;
const ll logn = 18;
const int base = 311;
const int Block_size = 500;
const int ep = 'a';
int cu[] = {0,0,1,-1};
int cv[] = {-1,1,0,0};
int du[] = {-1,-1,+1,1};
int dv[] = {-1,+1,-1,1};
int cled[] = {6,2,5,5,4,5,6,3,7,6};
void InputFile()
{
freopen(".inp","r",stdin);
freopen(".out","w",stdout);
//freopen("test.out","r",stdin);
}
void FastInput()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
int n;
int dpr[401][401][401];
int dpg[401][401][401];
int dpy[401][401][401];
int sup_g[401];
int sup_y[401];
int sup_r[401];
vector<int> save_r,save_g,save_y;
string s;
int Cal_R(int cr,int cg,int cy)
{
int now_pos = save_r[cr-1];
int moved = 0;
int k = sup_g[now_pos];
moved += max(0,cg - k);
k = sup_y[now_pos];
moved += max(0,cy - k);
return now_pos + moved - (cr + cg + cy);
//return max(max(save_g[cg-1],save_r[cr-1]),save_y[cy-1]) - (cr + cg + cy);
}
int Cal_G(int cr,int cg,int cy)
{
int now_pos = save_g[cg-1];
int moved = 0;
int k = sup_r[now_pos];
moved += max(0,cr - k);
k = sup_y[now_pos];
moved += max(0,cy - k);
return now_pos + moved - (cr + cg + cy);
//return max(max(save_g[cg-1],save_r[cr-1]),save_y[cy-1]) - (cr + cg + cy);
}
int Cal_Y(int cr,int cg,int cy)
{
int now_pos = save_y[cy-1];
int moved = 0;
int k = sup_r[now_pos];
moved += max(0,cr - k);
k = sup_g[now_pos];
moved += max(0,cg - k);
return now_pos + moved - (cr + cg + cy);
//return max(max(save_g[cg-1],save_r[cr-1]),save_y[cy-1]) - (cr + cg + cy);
}
void Read()
{
cin >> n >> s;
s.insert(0,"?");
//save_r.push_back(mod);
//save_g.push_back(mod);
//save_y.push_back(mod);
for(int i = 1;i <= n;i++)
{
if(s[i] == 'R') save_r.push_back(i);
if(s[i] == 'G') save_g.push_back(i);
if(s[i] == 'Y') save_y.push_back(i);
sup_r[i] = save_r.size();
sup_g[i] = save_g.size();
sup_y[i] = save_y.size();
}
/// init
for(int i = 1;i <= n;i++)
{
for(int cr = 0;cr <= n;cr++)
{
for(int cg = 0;cg <= n;cg++)
{
dpr[i][cr][cg] = mod;
dpg[i][cr][cg] = mod;
dpy[i][cr][cg] = mod;
}
}
}
dpr[0][0][0] = dpg[0][0][0] = dpy[0][0][0] = 0;
for(int i = 1;i <= n;i++)
{
for(int cr = 0;cr <= (int)save_r.size();cr++)
{
for(int cg = 0;cg <= (int)save_g.size();cg++)
{
int cy = i - cr - cg;
if(cy > save_y.size()) continue;
if(cr + cg > i) break;
if(cr > 0)
{
dpr[i][cr][cg] = min(dpg[i-1][cr-1][cg],dpy[i-1][cr-1][cg]) + Cal_R(cr,cg,cy);
}
if(cg > 0)
{
dpg[i][cr][cg] = min(dpr[i-1][cr][cg-1],dpy[i-1][cr][cg-1]) + Cal_G(cr,cg,cy);
}
if(cy > 0)
{
dpy[i][cr][cg] = min(dpr[i-1][cr][cg],dpg[i-1][cr][cg]) + Cal_Y(cr,cg,cy);
}
}
}
}
int res;
res = min(min(dpr[n][(int)save_r.size()][(int)save_g.size()],dpg[n][(int)save_r.size()][(int)save_g.size()]),dpy[n][(int)save_r.size()][(int)save_g.size()]);
if(res >= (int)1e9) res =-1;
cout << res;
}
void Solve()
{
}
void Debug()
{
//Main_sub();
//Naive();
}
int main()
{
FastInput();
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve();
int test;
//cin >> test;
test = 1;
while(test--)
//for(int prc = 1; prc <= test; prc++)
{
Read();
Solve();
//Debug();
}
}
Compilation message
joi2019_ho_t3.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("O3")
|
joi2019_ho_t3.cpp:8: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
8 | #pragma GCC optimization ("unroll-loops")
|
joi2019_ho_t3.cpp: In function 'void Read()':
joi2019_ho_t3.cpp:131:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | if(cy > save_y.size()) continue;
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void InputFile()':
joi2019_ho_t3.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
1 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
1 ms |
1536 KB |
Output is correct |
18 |
Correct |
8 ms |
18132 KB |
Output is correct |
19 |
Correct |
8 ms |
18132 KB |
Output is correct |
20 |
Correct |
8 ms |
18160 KB |
Output is correct |
21 |
Correct |
7 ms |
18132 KB |
Output is correct |
22 |
Correct |
8 ms |
18132 KB |
Output is correct |
23 |
Correct |
8 ms |
18132 KB |
Output is correct |
24 |
Correct |
8 ms |
18184 KB |
Output is correct |
25 |
Correct |
8 ms |
18192 KB |
Output is correct |
26 |
Correct |
10 ms |
18172 KB |
Output is correct |
27 |
Correct |
8 ms |
18212 KB |
Output is correct |
28 |
Correct |
8 ms |
18132 KB |
Output is correct |
29 |
Correct |
8 ms |
18132 KB |
Output is correct |
30 |
Correct |
8 ms |
18132 KB |
Output is correct |
31 |
Correct |
8 ms |
18132 KB |
Output is correct |
32 |
Correct |
11 ms |
18244 KB |
Output is correct |
33 |
Correct |
8 ms |
17620 KB |
Output is correct |
34 |
Correct |
10 ms |
17620 KB |
Output is correct |
35 |
Correct |
8 ms |
17024 KB |
Output is correct |
36 |
Correct |
8 ms |
17620 KB |
Output is correct |
37 |
Correct |
7 ms |
16468 KB |
Output is correct |
38 |
Correct |
8 ms |
18132 KB |
Output is correct |
39 |
Correct |
8 ms |
18132 KB |
Output is correct |
40 |
Correct |
7 ms |
17620 KB |
Output is correct |
41 |
Correct |
7 ms |
18132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
412 ms |
755548 KB |
Output is correct |
3 |
Correct |
361 ms |
753608 KB |
Output is correct |
4 |
Correct |
319 ms |
755440 KB |
Output is correct |
5 |
Correct |
349 ms |
755552 KB |
Output is correct |
6 |
Correct |
322 ms |
755432 KB |
Output is correct |
7 |
Correct |
327 ms |
753768 KB |
Output is correct |
8 |
Correct |
388 ms |
753676 KB |
Output is correct |
9 |
Correct |
330 ms |
751904 KB |
Output is correct |
10 |
Correct |
323 ms |
755504 KB |
Output is correct |
11 |
Correct |
353 ms |
755572 KB |
Output is correct |
12 |
Correct |
83 ms |
203488 KB |
Output is correct |
13 |
Correct |
146 ms |
358252 KB |
Output is correct |
14 |
Correct |
226 ms |
518520 KB |
Output is correct |
15 |
Correct |
333 ms |
755548 KB |
Output is correct |
16 |
Correct |
329 ms |
755536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1620 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
1 ms |
1536 KB |
Output is correct |
18 |
Correct |
8 ms |
18132 KB |
Output is correct |
19 |
Correct |
8 ms |
18132 KB |
Output is correct |
20 |
Correct |
8 ms |
18160 KB |
Output is correct |
21 |
Correct |
7 ms |
18132 KB |
Output is correct |
22 |
Correct |
8 ms |
18132 KB |
Output is correct |
23 |
Correct |
8 ms |
18132 KB |
Output is correct |
24 |
Correct |
8 ms |
18184 KB |
Output is correct |
25 |
Correct |
8 ms |
18192 KB |
Output is correct |
26 |
Correct |
10 ms |
18172 KB |
Output is correct |
27 |
Correct |
8 ms |
18212 KB |
Output is correct |
28 |
Correct |
8 ms |
18132 KB |
Output is correct |
29 |
Correct |
8 ms |
18132 KB |
Output is correct |
30 |
Correct |
8 ms |
18132 KB |
Output is correct |
31 |
Correct |
8 ms |
18132 KB |
Output is correct |
32 |
Correct |
11 ms |
18244 KB |
Output is correct |
33 |
Correct |
8 ms |
17620 KB |
Output is correct |
34 |
Correct |
10 ms |
17620 KB |
Output is correct |
35 |
Correct |
8 ms |
17024 KB |
Output is correct |
36 |
Correct |
8 ms |
17620 KB |
Output is correct |
37 |
Correct |
7 ms |
16468 KB |
Output is correct |
38 |
Correct |
8 ms |
18132 KB |
Output is correct |
39 |
Correct |
8 ms |
18132 KB |
Output is correct |
40 |
Correct |
7 ms |
17620 KB |
Output is correct |
41 |
Correct |
7 ms |
18132 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
412 ms |
755548 KB |
Output is correct |
44 |
Correct |
361 ms |
753608 KB |
Output is correct |
45 |
Correct |
319 ms |
755440 KB |
Output is correct |
46 |
Correct |
349 ms |
755552 KB |
Output is correct |
47 |
Correct |
322 ms |
755432 KB |
Output is correct |
48 |
Correct |
327 ms |
753768 KB |
Output is correct |
49 |
Correct |
388 ms |
753676 KB |
Output is correct |
50 |
Correct |
330 ms |
751904 KB |
Output is correct |
51 |
Correct |
323 ms |
755504 KB |
Output is correct |
52 |
Correct |
353 ms |
755572 KB |
Output is correct |
53 |
Correct |
83 ms |
203488 KB |
Output is correct |
54 |
Correct |
146 ms |
358252 KB |
Output is correct |
55 |
Correct |
226 ms |
518520 KB |
Output is correct |
56 |
Correct |
333 ms |
755548 KB |
Output is correct |
57 |
Correct |
329 ms |
755536 KB |
Output is correct |
58 |
Correct |
385 ms |
755552 KB |
Output is correct |
59 |
Correct |
367 ms |
755560 KB |
Output is correct |
60 |
Correct |
390 ms |
751716 KB |
Output is correct |
61 |
Correct |
374 ms |
753560 KB |
Output is correct |
62 |
Correct |
342 ms |
755544 KB |
Output is correct |
63 |
Correct |
352 ms |
755532 KB |
Output is correct |
64 |
Correct |
362 ms |
755472 KB |
Output is correct |
65 |
Correct |
368 ms |
753612 KB |
Output is correct |
66 |
Correct |
383 ms |
748972 KB |
Output is correct |
67 |
Correct |
364 ms |
755532 KB |
Output is correct |
68 |
Correct |
385 ms |
753608 KB |
Output is correct |
69 |
Correct |
395 ms |
755544 KB |
Output is correct |
70 |
Correct |
412 ms |
755436 KB |
Output is correct |
71 |
Correct |
369 ms |
753680 KB |
Output is correct |
72 |
Correct |
320 ms |
753648 KB |
Output is correct |
73 |
Correct |
324 ms |
755440 KB |
Output is correct |