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 FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
#define __builtin_popcount __builtin_popcountll
using ll=long long; using ld=long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi=acos(0)*2;
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
#define taskname "Vegetable3"
int n;
string s;
int cnt[256];
int x[3][401][3];
int f[401][201][134][3];
bool done[401][201][134][3];
int F(int c0, int c1, int c2, int l){
if(done[c0][c1][c2][l]) return f[c0][c1][c2][l];
done[c0][c1][c2][l]=1;
if(c0+c1+c2==0) return f[c0][c1][c2][l]=0;
int res=1e9;
if((l!=0)&&c0) res=min(res, max(0, c1-x[0][c0][1])+max(0, c2-x[0][c0][2])+F(c0-1, c1, c2, 0));
if((l!=1)&&c1) res=min(res, max(0, c0-x[1][c1][0])+max(0, c2-x[1][c1][2])+F(c0, c1-1, c2, 1));
if((l!=2)&&c2) res=min(res, max(0, c0-x[2][c2][0])+max(0, c1-x[2][c2][1])+F(c0, c1, c2-1, 2));
// cerr<<c0<<' '<<c1<<' '<<c2<<' '<<l<<'\n';
// bug(res);
return f[c0][c1][c2][l]=res;
}
int main(){
#ifdef Aria
if(fopen(taskname".in", "r"))
freopen(taskname".in", "r", stdin);
#endif // Aria
cin>>n>>s;
string all="RGY";
for(char c: s) cnt[c]++;
sort(all.begin(), all.end(), [](char A, char B){
return cnt[A]>cnt[B];
});
bug(all);
for(char &c: s) c=all.find(c);
reverse(s.begin(), s.end());
for(char c: s){
cnt[c]++;
FFOR(i, 0, 3) x[c][cnt[c]][i]=cnt[i];
}
int ans=1e9;
FFOR(i, 0, 3) ans=min(ans, F(cnt[0], cnt[1], cnt[2], i));
if(ans==1e9) ans=-1;
writeln(ans);
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:45:25: warning: array subscript has type 'char' [-Wchar-subscripts]
for(char c: s) cnt[c]++;
^
joi2019_ho_t3.cpp: In lambda function:
joi2019_ho_t3.cpp:47:21: warning: array subscript has type 'char' [-Wchar-subscripts]
return cnt[A]>cnt[B];
^
joi2019_ho_t3.cpp:47:28: warning: array subscript has type 'char' [-Wchar-subscripts]
return cnt[A]>cnt[B];
^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:53:14: warning: array subscript has type 'char' [-Wchar-subscripts]
cnt[c]++;
^
joi2019_ho_t3.cpp:54:26: warning: array subscript has type 'char' [-Wchar-subscripts]
FFOR(i, 0, 3) x[c][cnt[c]][i]=cnt[i];
^
joi2019_ho_t3.cpp:54:33: warning: array subscript has type 'char' [-Wchar-subscripts]
FFOR(i, 0, 3) x[c][cnt[c]][i]=cnt[i];
^
# | 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... |