Submission #878775

#TimeUsernameProblemLanguageResultExecution timeMemory
878775niterGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
0 / 100
480 ms596 KiB
#include <bits/stdc++.h> #define pb push_back #define ins isnert #define pii pair<int,int> #define ff first #define ss second #define loop(i,a,b) for(int i = (a); i < (b); i ++) #define op(x) cerr << #x << " = " << x << endl; #define opa(x) cerr << #x << " = " << x << ", "; #define ops(x) cerr << x; #define spac cerr << ' '; #define entr cerr << endl; #define STL(x) cerr << #x << " : "; for(auto &qwe:x) cerr << qwe << ' '; cerr << endl; #define ARR(x, nnn) cerr << #x << " : "; loop(qwe,0,nnn) cerr << x[qwe] << ' '; cerr << endl; #define BAE(x) (x).begin(), (x).end() using namespace std; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); int a[405], b[405], cnt[5]; int main(){ ios::sync_with_stdio(false); cin.tie(0); // freopen("in.txt", "r", stdin); int n; string s; cin >> n >> s; loop(i,0,n){ if(s[i] == 'R') a[i] = 1; else if(s[i] == 'G') a[i] = 2; else a[i] = 3; cnt[a[i]]++; b[i] = a[i]; } int max_cnt = max(max(cnt[1], cnt[2]), cnt[3]); int no_ans_lim; if(n % 2 == 1){ no_ans_lim = (n + 1) / 2; } else{ no_ans_lim = n / 2; } if(max_cnt > no_ans_lim){ cout << "-1\n"; return 0; } int ans = n * (n - 1), tmp; bool can; queue<int> Qfrom[5], Qto[5]; while((long double)clock() / CLOCKS_PER_SEC <= 0.48){ shuffle(b, b + n, RNG); can = true; loop(i,0,n-1){ if(b[i] == b[i+1]){ can = false; break; } } if(!can) continue; tmp = 0; loop(i,0,n){ if(Qto[a[i]].empty()){ Qfrom[a[i]].push(i); } else{ tmp += i - Qto[a[i]].front(); Qto[a[i]].pop(); } if(Qfrom[b[i]].empty()){ Qto[b[i]].push(i); } else{ tmp += i - Qfrom[b[i]].front(); Qfrom[b[i]].pop(); } } // ARR(b, n); ans = min(ans, tmp); } cout << (ans >> 1) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...