Submission #819130

#TimeUsernameProblemLanguageResultExecution timeMemory
819130vjudge1Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
20 / 100
1088 ms157440 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAXN = 1e5 + 5; const ll INF = 1e9; #define fi first #define se second ll n; string s; map <string, bool> vis; queue <pair <ll, string> > q; bool cek(string x){ for(ll i = 0; i < n-1; i++){ if(x[i] == x[i+1]){ return false; } } return true; } void bfs(string x){ vis[x] = true; q.push({0, x}); while(!q.empty()){ ll discur = q.front().fi; string cur = q.front().se; // cout << cur << " " << discur << endl; q.pop(); if(cek(cur)){ cout << discur << endl; exit(0); } for(ll i = 0; i < n-1; i++){ if(((i-1 >= 0 && cur[i] == cur[i-1]) || (i+2 <n && cur[i+1] == cur[i+2])) && cur[i] != cur[i+1]){ swap(cur[i], cur[i+1]); if(!vis[cur]){ vis[cur] = true; q.push({discur+1, cur}); } swap(cur[i], cur[i+1]); } } } cout << -1 << endl; return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; bool kun = false; for(ll i = 0; i < n; i++){ if(s[i] == 'Y') kun = true; } if(!kun){ ll mer = 0, ijo = 0; for(ll i = 0; i < n; i++){ if(s[i] == 'R') mer++; else ijo++; } if(abs(mer - ijo) > 1){ cout << -1 << endl; return 0; } // cout << mer << " " << ijo << endl; ll ans1 = INF, ans2 = INF; if(mer >= ijo){ ans1 = 0; ll las = -1; for(ll i = 0; i < n; i++){ // cout << las << endl; if(i % 2 == 0){ ll tmp = las + 1; while(s[tmp] != 'R'){ tmp++; } // cout << tmp << " " << i << endl; ans1 += abs(tmp - i); las = tmp; } } } if(ijo >= mer){ ans2 = 0; ll las = -1; for(ll i = 0; i < n; i++){ if(i % 2 == 0){ ll tmp = las + 1; while(s[tmp] != 'G'){ tmp++; } ans2 += abs(tmp - i); las = tmp; } } } // cout << ans1 << " " << ans2 << endl; cout << min(ans1, ans2) << endl; exit(0); } bfs(s); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...