Submission #492894

#TimeUsernameProblemLanguageResultExecution timeMemory
492894BiazGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
20 / 100
1096 ms332 KiB
#include <bits/stdc++.h> #define int long long //#define double long double #define Nanase_Kurumi_aka_menhera_chan_is_mine ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pb push_back #define pi pair<double,int> #define ALL(i) i.begin(),i.end() #define gcd(i,j) __gcd(i,j) #define fi first #define se second #define eps 0.00000001 #define ist insert #define DNE nullptr //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") //#pragma GCC optimize("O2") int max(int x,int y){return x>=y?x:y;} int min(int x,int y){return x>=y?y:x;} using namespace std; typedef int ll; const int N=4005; const int M=1000005; const int MOD=1000000007;//998244353; const int INF=1000000000000000000;//2147483647; int n,ans; string s; int c[3];// R G Y vector<char> vc; int calc(){ string t=s; int res=0; for (int i=0;i<n;i++){ for (int j=i;j<n;j++) if (vc[i]==t[j]){ res+=j-i; for (int k=j;k>i;k--) swap(t[k],t[k-1]); break; } } return res; } void dfs(int R,int G,int Y,int pre){ if (R+Y+G==0){ ans=min(ans,calc()); return; } if (R!=0&&pre!=0){ vc.pb('R'); dfs(R-1,G,Y,0); vc.pop_back(); } if (G!=0&&pre!=1){ vc.pb('G'); dfs(R,G-1,Y,1); vc.pop_back(); } if (Y!=0&&pre!=2){ vc.pb('Y'); dfs(R,G,Y-1,2); vc.pop_back(); } } inline void sol(){ cin >>n>>s; for (int i=0;i<n;i++){ if (s[i]=='R') c[0]++; else if (s[i]=='G') c[1]++; else c[2]++; } ans=INF; dfs(c[0],c[1],c[2],-1); if (ans==INF) cout <<-1; else cout <<ans; } signed main(){ Nanase_Kurumi_aka_menhera_chan_is_mine int _=1; //cin >>_; while (_--) sol(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...