// #pragma GCC optimize("O3")
// #pragma GCC optimization("Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>
const ll mod = 1e9 + 9;
const int MAXN = 400 + 5;
const ll oo = 1e9 + 7;
const int base = 10;
int n;
string s;
vector<int> pos[3];
int ps[3][MAXN];
int f[MAXN][MAXN][MAXN][3];
int dp(int j, int k, int t, int last){
int i=j+k+t;
if(i>n-1){
return 0;
}
if(f[j][k][t][last]!=-1){
return f[j][k][t][last];
}
int ans=oo;
if(i==0 || last!=0){
if(j<=(int)pos[0].size()-1){
int cost1=max(0, ps[1][pos[0][j]]-k);
int cost2=max(0, ps[2][pos[0][j]]-t);
ans=min(ans, dp(j+1, k, t, 0)+cost1+cost2);
}
}
if(i==0 || last!=1){
if(k<=(int)pos[1].size()-1){
int cost1=max(0, ps[0][pos[1][k]]-j);
int cost2=max(0, ps[2][pos[1][k]]-t);
ans=min(ans, dp(j, k+1, t, 1)+cost1+cost2);
}
}
if(i==0 || last!=2){
if(t<=(int)pos[2].size()-1){
int cost1=max(0, ps[0][pos[2][t]]-j);
int cost2=max(0, ps[1][pos[2][t]]-k);
ans=min(ans, dp(j, k, t+1, 2)+cost1+cost2);
}
}
return f[j][k][t][last]=ans;
}
void trace(int j, int k, int t, int last){
int i=j+k+t;
if(i>n-1){
return;
}
int ans=f[j][k][t][last];
if(i==0 || last!=0){
if(j<=(int)pos[0].size()-1){
// ans=min(ans, dp(j+1, k, t, 0)+max(0, pos[0][j]-i));
if(ans==dp(j+1, k, t, 0)+max(0, i-pos[0][j])){
cout << 'R';
trace(j+1, k, t, 0);
return;
}
}
}
if(i==0 || last!=1){
if(k<=(int)pos[1].size()-1){
// ans=min(ans, dp(j, k+1, t, 1)+max(0, pos[1][k]-i));
if(ans==dp(j, k+1, t, 1)+max(0, i-pos[1][k])){
cout << 'G';
trace(j, k+1, t, 1);
return;
}
}
}
if(i==0 || last!=2){
if(t<=(int)pos[2].size()-1){
// ans=min(ans, dp(j, k, t+1, 2)+max(0, pos[2][t]-i));
if(ans==dp(j, k, t+1, 2)+max(0, i-pos[2][t])){
cout << 'Y';
trace(j, k, t+1, 2);
return;
}
}
}
// return f[j][k][t][last]=ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("test.txt", "r", stdin);
// freopen("o2.out", "w", stdout);
if(fopen(".inp", "r")){
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> s;
FOR(i, 0, n-1){
if(i!=0){
ps[0][i]=ps[0][i-1];
ps[1][i]=ps[1][i-1];
ps[2][i]=ps[2][i-1];
}
if(s[i]=='R'){
pos[0].push_back(i);
ps[0][i]++;
}
else if(s[i]=='G'){
pos[1].push_back(i);
ps[1][i]++;
}
else{
pos[2].push_back(i);
ps[2][i]++;
}
}
FOR(j, 0, (int)pos[0].size()+1){
FOR(k, 0, (int)pos[1].size()+1){
FOR(t, 0, (int)pos[2].size()+1){
FOR(last, 0, 2){
f[j][k][t][last]=-1;
}
}
}
}
int ans=dp(0, 0, 0, 0);
if(ans==oo){
ans=-1;
}
cout << ans << '\n';
// trace(0, 0, 0, 0);
// cout << f[0][0][0][0] << ' ';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:109:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:110:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |