#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> Poz[3];
vector<pair<int,int> > Coresp[3];
int Dp[3][405][405][405];
int getSwaps(int wh, int fir, int sec, int i, int j, int k)
{
int rez=0;
rez=rez+max(0,Coresp[wh][i-1].first-j+1); //cati fir au ramas neasezati pana la i
rez=rez+max(0,Coresp[wh][i-1].second-k+1);
return rez;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1; i<=n; i++)
{
char c;
cin>>c;
if(c=='R')
{
Poz[0].push_back(i);
Coresp[0].push_back({Poz[1].size()-1,Poz[2].size()-1});
}
else if(c=='G')
{
Poz[1].push_back(i);
Coresp[1].push_back({Poz[0].size()-1,Poz[2].size()-1});
}
else
{
Poz[2].push_back(i);
Coresp[2].push_back({Poz[0].size()-1,Poz[1].size()-1});
}
}
for(int i=0; i<=Poz[0].size(); i++)
{
for(int j=0; j<=Poz[1].size(); j++)
{
for(int k=0; k<=Poz[2].size(); k++)
{
Dp[0][i][j][k]=Dp[1][i][j][k]=Dp[2][i][j][k]=1000000000;
}
}
}
Dp[0][0][0][0]=Dp[1][0][0][0]=Dp[2][0][0][0]=0;
for(int i=0; i<=Poz[0].size(); i++)
{
for(int j=0; j<=Poz[1].size(); j++)
{
for(int k=0; k<=Poz[2].size(); k++)
{
if(i<Poz[0].size())
{
Dp[0][i+1][j][k]=min(Dp[0][i+1][j][k],min(Dp[1][i][j][k],Dp[2][i][j][k])+getSwaps(0,1,2,i+1,j,k));
}
if(j<Poz[1].size())
{
Dp[1][i][j+1][k]=min(Dp[1][i][j+1][k],min(Dp[0][i][j][k],Dp[2][i][j][k])+getSwaps(1,0,2,j+1,i,k));
}
if(k<Poz[2].size())
{
Dp[2][i][j][k+1]=min(Dp[2][i][j][k+1],min(Dp[0][i][j][k],Dp[1][i][j][k])+getSwaps(2,0,1,k+1,i,j));
}
}
}
}
int rez=min(Dp[0][Poz[0].size()][Poz[1].size()][Poz[2].size()],min(Dp[1][Poz[0].size()][Poz[1].size()][Poz[2].size()],Dp[2][Poz[0].size()][Poz[1].size()][Poz[2].size()]));
if(rez==1000000000)
rez=-1;
cout<<rez<<"\n";
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:50:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<=Poz[0].size(); i++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:52:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<=Poz[1].size(); j++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:54:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0; k<=Poz[2].size(); k++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:62:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<=Poz[0].size(); i++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:64:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<=Poz[1].size(); j++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:66:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0; k<=Poz[2].size(); k++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:68:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i<Poz[0].size())
~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:73:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j<Poz[1].size())
~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:78:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(k<Poz[2].size())
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 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 |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 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 |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2304 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2944 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
8 ms |
4992 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
6 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
5 ms |
2432 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
6 ms |
4352 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
6 ms |
2304 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2560 KB |
Output is correct |
39 |
Correct |
5 ms |
2688 KB |
Output is correct |
40 |
Correct |
5 ms |
512 KB |
Output is correct |
41 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
99 ms |
194320 KB |
Output is correct |
3 |
Correct |
103 ms |
193400 KB |
Output is correct |
4 |
Correct |
103 ms |
194352 KB |
Output is correct |
5 |
Correct |
93 ms |
194296 KB |
Output is correct |
6 |
Correct |
95 ms |
194296 KB |
Output is correct |
7 |
Correct |
94 ms |
193272 KB |
Output is correct |
8 |
Correct |
92 ms |
193276 KB |
Output is correct |
9 |
Correct |
94 ms |
192504 KB |
Output is correct |
10 |
Correct |
91 ms |
194296 KB |
Output is correct |
11 |
Correct |
93 ms |
194296 KB |
Output is correct |
12 |
Correct |
28 ms |
52864 KB |
Output is correct |
13 |
Correct |
46 ms |
92288 KB |
Output is correct |
14 |
Correct |
65 ms |
132984 KB |
Output is correct |
15 |
Correct |
90 ms |
194296 KB |
Output is correct |
16 |
Correct |
92 ms |
194296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 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 |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2304 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2944 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
8 ms |
4992 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
6 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
5 ms |
2432 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
6 ms |
4352 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
6 ms |
2304 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2560 KB |
Output is correct |
39 |
Correct |
5 ms |
2688 KB |
Output is correct |
40 |
Correct |
5 ms |
512 KB |
Output is correct |
41 |
Correct |
6 ms |
2688 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
99 ms |
194320 KB |
Output is correct |
44 |
Correct |
103 ms |
193400 KB |
Output is correct |
45 |
Correct |
103 ms |
194352 KB |
Output is correct |
46 |
Correct |
93 ms |
194296 KB |
Output is correct |
47 |
Correct |
95 ms |
194296 KB |
Output is correct |
48 |
Correct |
94 ms |
193272 KB |
Output is correct |
49 |
Correct |
92 ms |
193276 KB |
Output is correct |
50 |
Correct |
94 ms |
192504 KB |
Output is correct |
51 |
Correct |
91 ms |
194296 KB |
Output is correct |
52 |
Correct |
93 ms |
194296 KB |
Output is correct |
53 |
Correct |
28 ms |
52864 KB |
Output is correct |
54 |
Correct |
46 ms |
92288 KB |
Output is correct |
55 |
Correct |
65 ms |
132984 KB |
Output is correct |
56 |
Correct |
90 ms |
194296 KB |
Output is correct |
57 |
Correct |
92 ms |
194296 KB |
Output is correct |
58 |
Correct |
66 ms |
77304 KB |
Output is correct |
59 |
Correct |
74 ms |
97784 KB |
Output is correct |
60 |
Correct |
72 ms |
89208 KB |
Output is correct |
61 |
Correct |
70 ms |
86008 KB |
Output is correct |
62 |
Correct |
101 ms |
189560 KB |
Output is correct |
63 |
Correct |
95 ms |
186744 KB |
Output is correct |
64 |
Correct |
95 ms |
163064 KB |
Output is correct |
65 |
Correct |
88 ms |
142000 KB |
Output is correct |
66 |
Correct |
71 ms |
87292 KB |
Output is correct |
67 |
Correct |
70 ms |
83888 KB |
Output is correct |
68 |
Correct |
73 ms |
89848 KB |
Output is correct |
69 |
Correct |
71 ms |
86648 KB |
Output is correct |
70 |
Correct |
80 ms |
93688 KB |
Output is correct |
71 |
Correct |
69 ms |
85240 KB |
Output is correct |
72 |
Correct |
32 ms |
44032 KB |
Output is correct |
73 |
Correct |
7 ms |
2944 KB |
Output is correct |