/**
* hello
* author: N29
* created: 2024-11-01 12:38:08
**/
#include<bits/stdc++.h>
using namespace std;
#ifndef BADGNU
#pragma GCC target("sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define int ll
#define ld long double
#define y1 cheza
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N=410;
const int M=5001;
const int B=447;
const int mod=998244353;
const ll INF=1e18;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
const double eps=1e-6;
int n;
int a[N];
int c[N];
int b[N];
int calc(){
int res=0;
for(int i=1;i<=n;i++){
c[i]=a[i];
}
for(int i=1;i<=n;i++){
if(b[i]==b[i-1])return INF;
if(b[i]!=c[i]){
int pos=-1;
for(int j=i;j<=n;j++){
if(c[j]==b[i]){
pos=j;
break;
}
}
if(pos==-1)return INF;
res+=(pos-i);
swap(c[i],c[pos]);
}
}
return res;
}
int cnt[4];
void test(){
cin>>n;
for(int i=1;i<=n;i++){
char x;
cin>>x;
if(x=='R')a[i]=1;
if(x=='G')a[i]=2;
if(x=='Y')a[i]=3;
b[i]=a[i];
c[i]=a[i];
cnt[a[i]]++;
}
vector<int>v;
for(int i=1;i<=3;i++){
v.push_back(cnt[i]);
}
sort(v.begin(),v.end());
v[1]-=v[0];
v[2]-=v[0];
if(abs(v[2]-v[1])>v[0]+1){
cout<<-1<<'\n';
return;
}
int ans=INF;
for(int i=1;i<=n;i++){
b[i]=(i%2)+1;
}
ans=min(ans,calc());
for(int i=1;i<=n;i++){
b[i]=((i+1)%2)+1;
}
ans=min(ans,calc());
for(int i=1;i<=n;i++){
b[i]=a[i];
}
sort(b+1,b+n+1);
while(1ll){
int res=0;
ans=min(ans,calc());
next_permutation(b+1,b+n+1);
if(is_sorted(b+1,b+n+1))break;
}
if(ans==INF){
ans=-1;
}
cout<<ans<<'\n';
}
/*
*/
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// cout.tie(nullptr);
int t2=1;
// cin>>t2;
for(int i=1;i<=t2;i++){
test();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void test()':
joi2019_ho_t3.cpp:96:13: warning: unused variable 'res' [-Wunused-variable]
96 | int res=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
14 ms |
472 KB |
Output is correct |
7 |
Correct |
9 ms |
336 KB |
Output is correct |
8 |
Correct |
13 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Incorrect |
11 ms |
336 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
14 ms |
472 KB |
Output is correct |
7 |
Correct |
9 ms |
336 KB |
Output is correct |
8 |
Correct |
13 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Incorrect |
11 ms |
336 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Execution timed out |
1053 ms |
336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
14 ms |
472 KB |
Output is correct |
7 |
Correct |
9 ms |
336 KB |
Output is correct |
8 |
Correct |
13 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Incorrect |
11 ms |
336 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |