#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 400 + 5 ;
int arr[MAX] ;
string s ;
int dp[MAX][MAX][MAX][4] , cnt[3][MAX] , occ[3][MAX] ;
int n ;
int solve(int idx , int cnt1 , int cnt2 , int cnt3 , int prv)
{
if(idx == n)
return 0 ;
int &ret = dp[idx][cnt1][cnt2][prv] ;
if(ret != -1)
return ret ;
ret = MAX ;
for(int i = 0 ; i <= 2 ; ++i)
{
if(i == prv)
continue ;
int to = -1 ;
if(i == 0)
to = occ[0][cnt1] ;
if(i == 1)
to = occ[1][cnt2] ;
if(i == 2)
to = occ[2][cnt3] ;
if(to == -1)
continue ;
int cost = max(0 , cnt[0][to] - cnt1) + max(0 , cnt[1][to] - cnt2) + max(0 , cnt[2][to] - cnt3) ;
if(i == 0)
ret = min(ret , solve(idx+1 , cnt1+1 , cnt2 , cnt3 , i) + cost) ;
if(i == 1)
ret = min(ret , solve(idx+1 , cnt1 , cnt2+1 , cnt3 , i) + cost) ;
if(i == 2)
ret = min(ret , solve(idx+1 , cnt1 , cnt2 , cnt3+1 , i) + cost) ;
}
return ret ;
}
int main()
{
memset(occ , -1 , sizeof(occ)) ;
memset(dp , -1 , sizeof(dp)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
cin>>s ;
int cnt1 = 0 , cnt2 = 0 , cnt3 = 0 ;
for(int i = 0 ; i < n ; ++i)
{
cnt[0][i] = cnt1 , cnt[1][i] = cnt2 , cnt[2][i] = cnt3 ;
if(s[i] == 'R')
occ[0][cnt1++] = i ;
else if(s[i] == 'G')
occ[1][cnt2++] = i ;
else
occ[2][cnt3++] = i ;
}
int ans = solve(0 , 0 , 0 , 0 , 3) ;
if(ans > n)
ans = -1 ;
return cout<<ans<<"\n" , 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
1040504 KB |
Output is correct |
2 |
Execution timed out |
552 ms |
1040376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
1040504 KB |
Output is correct |
2 |
Execution timed out |
552 ms |
1040376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
487 ms |
1040352 KB |
Output is correct |
2 |
Incorrect |
481 ms |
1040376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
489 ms |
1040504 KB |
Output is correct |
2 |
Execution timed out |
552 ms |
1040376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |