# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1144886 | SmuggingSpun | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++20 | 68 ms | 7864 KiB |
#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
int n;
string s;
namespace sub1{
void solve(){
unordered_map<string, int>f;
f[s] = 1;
queue<string>q;
q.push(s);
while(!q.empty()){
s = q.front();
q.pop();
bool flag = true;
for(int i = 1; i < n; i++){
if(s[i] == s[i - 1]){
flag = false;
break;
}
}
if(flag){
return void(cout << f[s] - 1);
}
int N = f[s];
for(int i = 1; i < n; i++){
swap(s[i], s[i - 1]);
if(f[s] == 0){
f[s] = N + 1;
q.push(s);
}
swap(s[i], s[i - 1]);
}
}
cout << -1;
}
}
namespace sub3{
void solve(){
int R = count(s.begin(), s.end(), 'R'), G = n - R;
if(abs(R - G) > 1){
return void(cout << -1);
}
if(R == G){
int ans_1 = 0, ans_2 = 0;
for(int i = 0, r = 0, g = 0; i < n; i++){
if(s[i] == 'R'){
ans_1 += abs(i - r);
r += 2;
}
else{
ans_2 += abs(i - g);
g += 2;
}
}
cout << min(ans_1, ans_2);
}
else if(R == G + 1){
int ans = 0;
for(int i = 0, j = 0; i < n; i++){
if(s[i] == 'R'){
ans += abs(i - j);
j += 2;
}
}
cout << ans;
}
else{
int ans = 0;
for(int i = 0, j = 0; i < n; i++){
if(s[i] == 'G'){
ans += abs(i - j);
j += 2;
}
}
cout << ans;
}
}
}
namespace sub24{
void solve(){
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> s;
if(n <= 15){
sub1::solve();
}
else if(find(s.begin(), s.end(), 'Y') == s.end()){
sub3::solve();
}
else{
sub24::solve();
}
}
Compilation message (stderr)
# | 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... |