#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
//#define int long long
//#define ld long double
using namespace std;
using ll=long long;
const int inf = 1e9;
const int MN =400+5;
const int mod=1e9+7;
int n;
string s;
int cnt[MN][MN][4][4];
int dp[MN][MN][MN][4];
int a[MN],b[MN],c[MN];
void solve() {
cin>>n>>s;
// s="#"+s;
int n1=0,n2=0,n3=0;
for (int i=0; i<n; i++) {
if(s[i]=='R') a[++n1]=i;
else if(s[i]=='Y') b[++n2]=i;
else c[++n3]=i;
}
int k;
k=0;
for (int i=1; i<=n1; i++) {
k=0;
for (int j=1; j<=n2; j++) {
if(b[j]>a[i]) k++;
cnt[i][j][1][2]=k;
// cout<<cnt[i][j][1][2]<<" ";
}
}
k=0;
for (int i=1; i<=n1; i++) {
k=0;
for (int j=1; j<=n3; j++) {
if(c[j]>a[i]) k++;
cnt[i][j][1][3]=k;
}
}
k=0;
for (int i=1; i<=n2; i++) {
k=0;
for (int j=1; j<=n1; j++) {
if(a[j]>b[i]) k++;
cnt[i][j][2][1]=k;
}
}
k=0;
for (int i=1; i<=n2; i++) {
k=0;
for (int j=1; j<=n3; j++) {
if(c[j]>b[i]) k++;
cnt[i][j][2][3]=k;
}
}
k=0;
for (int i=1; i<=n3; i++) {
k=0;
for (int j=1; j<=n1; j++) {
if(a[j]>c[i]) k++;
cnt[i][j][3][1]=k;
}
}
k=0;
for (int i=1; i<=n3; i++) {
k=0;
for (int j=1; j<=n2; j++) {
if(b[j]>c[i]) k++;
cnt[i][j][3][2]=k;
}
}
for (int i=0; i<=n; i++) {
for (int j=0; j<=n; j++) {
for (int l=0; l<=n; l++) {
dp[i][j][l][1]=dp[i][j][l][2]=dp[i][j][l][3]=inf;
}
}
}
dp[0][0][0][1]=dp[0][0][0][2]=dp[0][0][0][3]=0;
for (int i=0; i<=n1; i++) {
for (int j=0; j<=n2; j++) {
for (int l=0; l<=n3; l++) {
if(i>0) dp[i][j][l][1] = min(dp[i-1][j][l][2],dp[i-1][j][l][3]) + cnt[i][j][1][2] + cnt[i][l][1][3];
if(j>0) dp[i][j][l][2] = min(dp[i][j-1][l][1],dp[i][j-1][l][3]) + cnt[j][i][2][1] + cnt[j][l][2][3];
if(l>0) dp[i][j][l][3] = min(dp[i][j][l-1][2],dp[i][j][l-1][1]) + cnt[l][i][3][1] + cnt[l][j][3][2];
}
}
}
int ans=min({dp[n1][n2][n3][1], dp[n1][n2][n3][2], dp[n1][n2][n3][3]});
if(ans>=inf) cout<<-1;
else cout<<ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
int t=1;
// cin>>t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1448 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1448 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
18 |
Correct |
7 ms |
19120 KB |
Output is correct |
19 |
Correct |
8 ms |
19016 KB |
Output is correct |
20 |
Correct |
8 ms |
19028 KB |
Output is correct |
21 |
Correct |
8 ms |
19120 KB |
Output is correct |
22 |
Correct |
9 ms |
19028 KB |
Output is correct |
23 |
Correct |
8 ms |
19028 KB |
Output is correct |
24 |
Correct |
8 ms |
19028 KB |
Output is correct |
25 |
Correct |
8 ms |
19156 KB |
Output is correct |
26 |
Correct |
8 ms |
19156 KB |
Output is correct |
27 |
Correct |
8 ms |
19028 KB |
Output is correct |
28 |
Correct |
8 ms |
19028 KB |
Output is correct |
29 |
Correct |
8 ms |
19028 KB |
Output is correct |
30 |
Correct |
8 ms |
19028 KB |
Output is correct |
31 |
Correct |
8 ms |
19016 KB |
Output is correct |
32 |
Correct |
8 ms |
19028 KB |
Output is correct |
33 |
Correct |
7 ms |
18448 KB |
Output is correct |
34 |
Correct |
7 ms |
18508 KB |
Output is correct |
35 |
Correct |
7 ms |
17748 KB |
Output is correct |
36 |
Correct |
7 ms |
18388 KB |
Output is correct |
37 |
Correct |
7 ms |
17108 KB |
Output is correct |
38 |
Correct |
8 ms |
19028 KB |
Output is correct |
39 |
Correct |
8 ms |
19088 KB |
Output is correct |
40 |
Correct |
7 ms |
18516 KB |
Output is correct |
41 |
Correct |
8 ms |
19152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
351 ms |
1024744 KB |
Output is correct |
3 |
Correct |
363 ms |
1019684 KB |
Output is correct |
4 |
Correct |
344 ms |
1024648 KB |
Output is correct |
5 |
Correct |
377 ms |
1024680 KB |
Output is correct |
6 |
Correct |
360 ms |
1024720 KB |
Output is correct |
7 |
Correct |
359 ms |
1019648 KB |
Output is correct |
8 |
Correct |
348 ms |
1019624 KB |
Output is correct |
9 |
Correct |
345 ms |
1014480 KB |
Output is correct |
10 |
Correct |
351 ms |
1024700 KB |
Output is correct |
11 |
Correct |
350 ms |
1024720 KB |
Output is correct |
12 |
Correct |
102 ms |
273652 KB |
Output is correct |
13 |
Correct |
156 ms |
482504 KB |
Output is correct |
14 |
Correct |
234 ms |
698488 KB |
Output is correct |
15 |
Correct |
359 ms |
1024724 KB |
Output is correct |
16 |
Correct |
370 ms |
1024788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1448 KB |
Output is correct |
10 |
Correct |
1 ms |
1492 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
18 |
Correct |
7 ms |
19120 KB |
Output is correct |
19 |
Correct |
8 ms |
19016 KB |
Output is correct |
20 |
Correct |
8 ms |
19028 KB |
Output is correct |
21 |
Correct |
8 ms |
19120 KB |
Output is correct |
22 |
Correct |
9 ms |
19028 KB |
Output is correct |
23 |
Correct |
8 ms |
19028 KB |
Output is correct |
24 |
Correct |
8 ms |
19028 KB |
Output is correct |
25 |
Correct |
8 ms |
19156 KB |
Output is correct |
26 |
Correct |
8 ms |
19156 KB |
Output is correct |
27 |
Correct |
8 ms |
19028 KB |
Output is correct |
28 |
Correct |
8 ms |
19028 KB |
Output is correct |
29 |
Correct |
8 ms |
19028 KB |
Output is correct |
30 |
Correct |
8 ms |
19028 KB |
Output is correct |
31 |
Correct |
8 ms |
19016 KB |
Output is correct |
32 |
Correct |
8 ms |
19028 KB |
Output is correct |
33 |
Correct |
7 ms |
18448 KB |
Output is correct |
34 |
Correct |
7 ms |
18508 KB |
Output is correct |
35 |
Correct |
7 ms |
17748 KB |
Output is correct |
36 |
Correct |
7 ms |
18388 KB |
Output is correct |
37 |
Correct |
7 ms |
17108 KB |
Output is correct |
38 |
Correct |
8 ms |
19028 KB |
Output is correct |
39 |
Correct |
8 ms |
19088 KB |
Output is correct |
40 |
Correct |
7 ms |
18516 KB |
Output is correct |
41 |
Correct |
8 ms |
19152 KB |
Output is correct |
42 |
Correct |
1 ms |
328 KB |
Output is correct |
43 |
Correct |
351 ms |
1024744 KB |
Output is correct |
44 |
Correct |
363 ms |
1019684 KB |
Output is correct |
45 |
Correct |
344 ms |
1024648 KB |
Output is correct |
46 |
Correct |
377 ms |
1024680 KB |
Output is correct |
47 |
Correct |
360 ms |
1024720 KB |
Output is correct |
48 |
Correct |
359 ms |
1019648 KB |
Output is correct |
49 |
Correct |
348 ms |
1019624 KB |
Output is correct |
50 |
Correct |
345 ms |
1014480 KB |
Output is correct |
51 |
Correct |
351 ms |
1024700 KB |
Output is correct |
52 |
Correct |
350 ms |
1024720 KB |
Output is correct |
53 |
Correct |
102 ms |
273652 KB |
Output is correct |
54 |
Correct |
156 ms |
482504 KB |
Output is correct |
55 |
Correct |
234 ms |
698488 KB |
Output is correct |
56 |
Correct |
359 ms |
1024724 KB |
Output is correct |
57 |
Correct |
370 ms |
1024788 KB |
Output is correct |
58 |
Correct |
391 ms |
1023400 KB |
Output is correct |
59 |
Correct |
363 ms |
1023308 KB |
Output is correct |
60 |
Correct |
374 ms |
1013044 KB |
Output is correct |
61 |
Correct |
362 ms |
1017980 KB |
Output is correct |
62 |
Correct |
350 ms |
1025036 KB |
Output is correct |
63 |
Correct |
350 ms |
1024588 KB |
Output is correct |
64 |
Correct |
363 ms |
1024396 KB |
Output is correct |
65 |
Correct |
384 ms |
1019052 KB |
Output is correct |
66 |
Correct |
363 ms |
1007868 KB |
Output is correct |
67 |
Correct |
366 ms |
1023416 KB |
Output is correct |
68 |
Correct |
365 ms |
1018084 KB |
Output is correct |
69 |
Correct |
404 ms |
1023160 KB |
Output is correct |
70 |
Correct |
371 ms |
1023264 KB |
Output is correct |
71 |
Correct |
365 ms |
1018380 KB |
Output is correct |
72 |
Correct |
354 ms |
1019852 KB |
Output is correct |
73 |
Correct |
358 ms |
1025684 KB |
Output is correct |