이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std ;
const short inf = 32767 ;
const short MAX = 401 ;
string s ;
short dp[MAX][MAX][MAX][3] , vis[MAX][MAX][MAX][3] , cnt[3][MAX] , occ[3][MAX] ;
short n ;
short solve(short idx , short cnt1 , short cnt2 , short cnt3 , short prv)
{
if(idx == n)
return 0 ;
short &ret = dp[cnt1][cnt2][cnt3][prv] ;
if(vis[cnt1][cnt2][cnt3][prv])
return ret ;
vis[cnt1][cnt2][cnt3][prv] = 1 ;
ret = inf ;
for(int i = 0 ; i <= 2 ; ++i)
{
if(i == prv && idx != 0)
continue ;
short 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 ;
short cost = max(0 , cnt[0][to] - cnt1) + max(0 , cnt[1][to] - cnt2) + max(0 , cnt[2][to] - cnt3) , now ;
if(i == 0)
now = solve(idx+1 , cnt1+1 , cnt2 , cnt3 , i) ;
if(i == 1)
now = solve(idx+1 , cnt1 , cnt2+1 , cnt3 , i) ;
if(i == 2)
now = solve(idx+1 , cnt1 , cnt2 , cnt3+1 , i) ;
if(cost > inf-now)
continue ;
now += cost ;
ret = min(ret , now) ;
}
return ret ;
}
int main()
{
memset(occ , -1 , sizeof(occ)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
cin>>s ;
short 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 ;
}
short ans = solve(0 , 0 , 0 , 0 , 0) ;
if(ans >= inf)
ans = -1 ;
return cout<<ans<<"\n" , 0 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |