QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#769913 | #7956. Walk Swapping | Aestivate | WA | 0ms | 3724kb | C++20 | 1.7kb | 2024-11-21 19:52:46 | 2024-11-21 19:52:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#define int long long
#define F first
#define S second
#define pb push_back
const int mol=998244353;
int a[3005], b[3005], now[3005];
int n;
int ans=1e9;
void calc(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
now[j]=a[(j+i)%n];
}
int l=n, r=-1;
for(int j=0;j<n;j++){
if(now[j]!=b[j]){
r=j;
if(l==n) l=j;
}
}
if(l==n) {
ans=min(ans, i*n);
continue;
}
bool ok=1;
if(now[l]!=b[r]) ok=0;
for(int i=l+1;i<=r;i++){
if(now[i]!=b[i-1]) ok=0;
}
if(ok) ans=min(ans, r-l+i*(n-1));
ok=1;
if(now[r]!=b[l]) continue;
for(int i=r+1;i<n;i++){
if(now[i]!=b[i-1]) ok=0;
}
if(now[0]!=b[n-1]) ok=0;
for(int i=1;i<=l;i++){
if(now[i]!=b[i-1]){
ok=0;
}
}
if(ok) ans=min(ans, i*(n-1)+l+n-r);
}
}
//1 2 2 1 1 2
//1 1 2 2 2 1
void solve(){
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
calc();
reverse(a, a+n);
reverse(b, b+n);
calc();
if(ans==1e9) ans=-1;
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
// int t;
// cin>>t;
// while(t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3688kb
input:
4 4 3 2 1 3 4 2 1
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
6 2 1 1 2 2 1 1 2 2 2 1 1
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
6 4 1 3 6 2 5 6 2 1 3 4 5
output:
-1
result:
ok single line: '-1'
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3680kb
input:
4 1 2 3 4 4 2 1 3
output:
4
result:
wrong answer 1st lines differ - expected: '2', found: '4'