This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define infl 0x3f3f3f3f3f3f3f3f
#define ll long long
template<typename T> void chmin(T& x, T y) { x=min(x,y); }
#define N 400
int n;
char s[N+1];
int pos[3][N+1];
int cnt[3][N+1][3];
int main()
{
scanf("%d %s",&n,s);
for(auto&x:s) x = x=='R'?0:x=='G'?1:x=='Y'?2:-1;
int c[3]={0,0,0};
for(int i=0;i<n;i++) {
pos[s[i]][c[s[i]]]=i;
for(int k=0;k<3;k++)
cnt[s[i]][c[s[i]]][k]=c[k];
c[s[i]]++;
}
vector<vector<vector<vector<ll>>>> dp(c[0]+1,vector<vector<vector<ll>>>(c[1]+1,vector<vector<ll>>(c[2]+1,vector<ll>(3,infl))));
//memset(dp,0x3f,sizeof(dp));
for(int k=0;k<3;k++) dp[0][0][0][k]=0;
for(int i0=0;i0<=c[0];i0++)
for(int i1=0;i1<=c[1];i1++)
for(int i2=0;i2<=c[2];i2++) {
for(int j=0;j<3;j++) {
ll v=dp[i0][i1][i2][j];
for(int k=0;k<3;k++) if(j!=k) {
int i[3]={i0,i1,i2};
ll u = v;
for(int l=0;l<3;l++)u += max(0, cnt[k][i[k]][l] - i[l]);
i[k]++;
for(int ii=0;ii<3;ii++)if(i[ii]>c[ii])goto dont;
chmin(dp[i[0]][i[1]][i[2]][k],u);
dont:;
}
}
}
ll mn = infl;
for(int k=0;k<3;k++) chmin(mn,dp[c[0]][c[1]][c[2]][k]);
if(mn >= infl>>1)printf("-1\n");
else printf("%lld\n",mn);
}
// then to optimize this, we can consider trying everything at once
// so dp[ra][ga][ba][current symbol] will trivialize this
// why didn't i think of this hadsfhlaksdhflkasdjalkfjslkdajflkadsj
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:23:24: warning: array subscript has type 'char' [-Wchar-subscripts]
23 | pos[s[i]][c[s[i]]]=i;
| ~~~^
joi2019_ho_t3.cpp:23:32: warning: array subscript has type 'char' [-Wchar-subscripts]
23 | pos[s[i]][c[s[i]]]=i;
| ~~~^
joi2019_ho_t3.cpp:25:32: warning: array subscript has type 'char' [-Wchar-subscripts]
25 | cnt[s[i]][c[s[i]]][k]=c[k];
| ~~~^
joi2019_ho_t3.cpp:25:40: warning: array subscript has type 'char' [-Wchar-subscripts]
25 | cnt[s[i]][c[s[i]]][k]=c[k];
| ~~~^
joi2019_ho_t3.cpp:26:22: warning: array subscript has type 'char' [-Wchar-subscripts]
26 | c[s[i]]++;
| ~~~^
joi2019_ho_t3.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d %s",&n,s);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |