#include <bits/stdc++.h>
#define INF 2000000000
using namespace std;
vector <int> v[3];
int dp[3][410][410][410];
char s[430];
int calcul (int nr0 , int nr1 , int nr2 , int curr){
int poz , st , dr , mid;
if (curr == 0)
curr = v[0][nr0];
else if (curr == 1)
curr = v[1][nr1];
else curr = v[2][nr2];
/// curr e poz initiala
poz = curr;
if (!v[0].empty()){
st = 0;
dr = nr0 - 1;
while (st <= dr){
mid = (st + dr) / 2;
if (v[0][mid] <= curr)
st = mid + 1;
else dr = mid - 1;
}
poz += (nr0 - st); /// st = primul mai mare decat curr
}
if (!v[1].empty()){
st = 0;
dr = nr1 - 1;
while (st <= dr){
mid = (st + dr) / 2;
if (v[1][mid] <= curr)
st = mid + 1;
else dr = mid - 1;
}
poz += (nr1 - st); /// st = primul mai mare decat curr
}
if (!v[2].empty()){
st = 0;
dr = nr2 - 1;
while (st <= dr){
mid = (st + dr) / 2;
if (v[2][mid] <= curr)
st = mid + 1;
else dr = mid - 1;
}
poz += (nr2 - st); /// st = primul mai mare decat curr
}
return poz - (nr0 + nr1 + nr2 + 1);
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int n , i , sum , j , k , sol;
fscanf (fin,"%d\n",&n);
fgets (s + 1 , 420 , fin); /// hehe
for (i = n ; i ; i--){
if (s[i] == 'R'){
v[0].push_back(i);
}
else if (s[i] == 'G'){
v[1].push_back(i);
}
else {
v[2].push_back(i);
}
}
reverse(v[0].begin() , v[0].end());
reverse(v[1].begin() , v[1].end());
reverse(v[2].begin() , v[2].end());
/// dp[i][j][k][0 / 1 / 2] = i 0 , j 1 , k 2 , current e 0 / 1 / 2
for (sum = 1 ; sum <= n ; sum++){
for (i = 0 ; i <= v[0].size() ; i++){
for (j = 0 ; j <= v[1].size() && i + j <= sum ; j++){
k = sum - i - j;
if (k >= 0 && k <= v[2].size()){
dp[0][i][j][k] = dp[1][i][j][k] = dp[2][i][j][k] = INF;
}
}
}
}
if (!v[0].empty())
dp[0][1][0][0] = v[0][0] - 1;
if (!v[1].empty())
dp[1][0][1][0] = v[1][0] - 1;
if (!v[2].empty())
dp[2][0][0][1] = v[2][0] - 1;
/// initializari
sol = 2000000000;
for (sum = 2 ; sum <= n ; sum++){
for (i = 0 ; i <= v[0].size() ; i++){
for (j = 0 ; j <= v[1].size() && i + j <= sum ; j++){
k = sum - i - j;
if (k < 0 || k > v[2].size())
continue;
/// acum calculezi propriu zis
if (i){ /// incerci sa pui 0
dp[0][i][j][k] = min(dp[1][i - 1][j][k] , dp[2][i - 1][j][k]) + calcul(i - 1 , j , k , 0);
}
if (j){ /// incerci sa pui 1
dp[1][i][j][k] = min(dp[0][i][j - 1][k] , dp[2][i][j - 1][k]) + calcul(i , j - 1 , k , 1);
}
if (k){ /// incerci sa pui 2
dp[2][i][j][k] = min(dp[0][i][j][k - 1] , dp[1][i][j][k - 1]) + calcul(i , j , k - 1 , 2);
}
if (sum == n){
sol = min( min(sol , dp[0][i][j][k]) , min(dp[1][i][j][k] , dp[2][i][j][k]) );
}
}
}
}
if (sol == INF)
sol = -1;
if (n == 1)
sol = 0;
fprintf (fout,"%d",sol);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:96:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0 ; i <= v[0].size() ; i++){
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:98:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0 ; j <= v[1].size() && i + j <= sum ; j++){
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:101:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k >= 0 && k <= v[2].size()){
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:126:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0 ; i <= v[0].size() ; i++){
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:128:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0 ; j <= v[1].size() && i + j <= sum ; j++){
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:131:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k < 0 || k > v[2].size())
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:71:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d\n",&n);
~~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:72:11: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
fgets (s + 1 , 420 , fin); /// hehe
~~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2304 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
7 ms |
2944 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
7 ms |
2560 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
4992 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2852 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2792 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
7 ms |
4352 KB |
Output is correct |
35 |
Correct |
7 ms |
3200 KB |
Output is correct |
36 |
Correct |
7 ms |
2304 KB |
Output is correct |
37 |
Correct |
6 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2560 KB |
Output is correct |
39 |
Correct |
7 ms |
2816 KB |
Output is correct |
40 |
Correct |
5 ms |
512 KB |
Output is correct |
41 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
113 ms |
196600 KB |
Output is correct |
3 |
Correct |
112 ms |
195704 KB |
Output is correct |
4 |
Correct |
112 ms |
196600 KB |
Output is correct |
5 |
Correct |
119 ms |
196708 KB |
Output is correct |
6 |
Correct |
110 ms |
196600 KB |
Output is correct |
7 |
Correct |
110 ms |
195704 KB |
Output is correct |
8 |
Correct |
110 ms |
195704 KB |
Output is correct |
9 |
Correct |
111 ms |
194680 KB |
Output is correct |
10 |
Correct |
113 ms |
196600 KB |
Output is correct |
11 |
Correct |
110 ms |
196600 KB |
Output is correct |
12 |
Correct |
31 ms |
53376 KB |
Output is correct |
13 |
Correct |
53 ms |
93304 KB |
Output is correct |
14 |
Correct |
77 ms |
134520 KB |
Output is correct |
15 |
Correct |
112 ms |
196600 KB |
Output is correct |
16 |
Correct |
113 ms |
196600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2304 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
7 ms |
2944 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
7 ms |
2560 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
4992 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2852 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2792 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
7 ms |
4352 KB |
Output is correct |
35 |
Correct |
7 ms |
3200 KB |
Output is correct |
36 |
Correct |
7 ms |
2304 KB |
Output is correct |
37 |
Correct |
6 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2560 KB |
Output is correct |
39 |
Correct |
7 ms |
2816 KB |
Output is correct |
40 |
Correct |
5 ms |
512 KB |
Output is correct |
41 |
Correct |
6 ms |
2688 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
113 ms |
196600 KB |
Output is correct |
44 |
Correct |
112 ms |
195704 KB |
Output is correct |
45 |
Correct |
112 ms |
196600 KB |
Output is correct |
46 |
Correct |
119 ms |
196708 KB |
Output is correct |
47 |
Correct |
110 ms |
196600 KB |
Output is correct |
48 |
Correct |
110 ms |
195704 KB |
Output is correct |
49 |
Correct |
110 ms |
195704 KB |
Output is correct |
50 |
Correct |
111 ms |
194680 KB |
Output is correct |
51 |
Correct |
113 ms |
196600 KB |
Output is correct |
52 |
Correct |
110 ms |
196600 KB |
Output is correct |
53 |
Correct |
31 ms |
53376 KB |
Output is correct |
54 |
Correct |
53 ms |
93304 KB |
Output is correct |
55 |
Correct |
77 ms |
134520 KB |
Output is correct |
56 |
Correct |
112 ms |
196600 KB |
Output is correct |
57 |
Correct |
113 ms |
196600 KB |
Output is correct |
58 |
Correct |
486 ms |
78328 KB |
Output is correct |
59 |
Correct |
490 ms |
99028 KB |
Output is correct |
60 |
Correct |
492 ms |
90104 KB |
Output is correct |
61 |
Correct |
483 ms |
87164 KB |
Output is correct |
62 |
Correct |
123 ms |
191736 KB |
Output is correct |
63 |
Correct |
148 ms |
188920 KB |
Output is correct |
64 |
Correct |
264 ms |
165112 KB |
Output is correct |
65 |
Correct |
365 ms |
143700 KB |
Output is correct |
66 |
Correct |
293 ms |
88332 KB |
Output is correct |
67 |
Correct |
310 ms |
84856 KB |
Output is correct |
68 |
Correct |
345 ms |
90872 KB |
Output is correct |
69 |
Correct |
359 ms |
87800 KB |
Output is correct |
70 |
Correct |
379 ms |
94868 KB |
Output is correct |
71 |
Correct |
448 ms |
86392 KB |
Output is correct |
72 |
Correct |
132 ms |
44664 KB |
Output is correct |
73 |
Correct |
25 ms |
2944 KB |
Output is correct |