Submission #1104077

#TimeUsernameProblemLanguageResultExecution timeMemory
1104077Champ_NamanGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
15 / 100
1 ms592 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define nl '\n' inline void solve(){ int n; cin>>n; string s; cin>>s; int a[n]; int cnt0 = 0, cnt1 = 0, cnt2 = 0; for(int i=0; i<n; i++){ if(s[i] == 'R') a[i] = 0, cnt0++; else if(s[i] == 'G') a[i] = 1, cnt1++; else a[i] = 2, cnt2++; } if(abs(cnt1-cnt0) > 1){ cout<<-1; return; } int ans = 1e18; if(cnt1 >= cnt0){ int b[n]; for(int i=0; i<n; i++) b[i] = a[i]; int subans = 0; for(int i=0; i<n; i++){ int x = (i % 2 ? 0 : 1); if(b[i] == x) continue; for(int j=i+1; j<n; j++){ if(b[j] != x) continue; for(int k=j; k>i; k--){ swap(b[k], b[k-1]); subans++; } break; } } ans = subans; } if(cnt0 >= cnt1){ int b[n]; for(int i=0; i<n; i++) b[i] = a[i]; int subans = 0; for(int i=0; i<n; i++){ int x = (i % 2 ? 1 : 0); if(b[i] == x) continue; for(int j=i+1; j<n; j++){ if(b[j] != x) continue; for(int k=j; k>i; k--){ swap(b[k], b[k-1]); subans++; } break; } } ans = min(subans, ans); } cout<<ans<<nl; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL); int t = 1; //cin>>t; while(t--) solve(); 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...