QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#489330#7956. Walk Swappingarnold518WA 0ms4112kbC++172.6kb2024-07-24 19:32:332024-07-24 19:32:33

Judging History

你现在查看的是最新测评结果

  • [2024-07-24 19:32:33]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4112kb
  • [2024-07-24 19:32:33]
  • 提交

answer

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 6000;
const int INF = 1e9;

int N, A[MAXN+10], B[MAXN+10], ans=INF;
int Z1[MAXN+10], Z2[MAXN+10];

vector<int> getZ(vector<int> S)
{
    int N=S.size();
    S.insert(S.begin(), 0);
    vector<int> Z(N+1);

    int pos=1;
    for(int i=2; i<=N; i++)
    {
        if(i<=pos+Z[pos]-1) Z[i]=min(pos+Z[pos]-i, Z[i-pos+1]);
        while(i+Z[i]<=N && S[i+Z[i]]==S[Z[i]+1]) Z[i]++;
        if(Z[pos]+pos<Z[i]+i) pos=i;
    }
    Z.erase(Z.begin());
    return Z;
}

void solve()
{
    for(int cyc=0; cyc<N; cyc++)
    {
        for(int i=1; i<=N; i++) B[i+N]=B[i];

        vector<int> V;
        for(int i=2; i<=N; i++) V.push_back(A[i]);
        V.push_back(-1);
        for(int i=1; i<=N+N; i++) V.push_back(B[i]);
        V=getZ(V);
        for(int i=1; i<=N+N; i++) Z1[i]=V[i-1+N];

        V.clear();
        for(int i=N; i>=2; i--) V.push_back(A[i]);
        V.push_back(-1);
        for(int i=N+N; i>=1; i--) V.push_back(B[i]);
        V=getZ(V);
        for(int i=1; i<=N+N; i++) Z2[i]=V[3*N-i];

        for(int i=N; i<=N+N; i++)
        {
            int a=Z2[i], b=Z1[i+1];
            int l=max(0, N-1-a), r=min(b, N-1);
            if(l<=r) ans=min(ans, l+(N-1)*(N+N-i));
        }
        for(int i=0; i<=N; i++)
        {
            int a=Z2[i], b=Z1[i+1];
            int l=max(0, N-1-b), r=min(a, N-1);
            if(l<=r) ans=min(ans, (N-1)*(N+1-i)-r);
        }
        for(int i=0; i<=N; i++)
        {
            int a=Z2[i], b=Z1[i+1];
            int l=max(0, N-1-a), r=min(b, N-1);
            if(l<=r) ans=min(ans, N*(i-1)+N+l-i);            
        }
        for(int i=N; i<=N+N; i++)
        {
            int a=Z2[i], b=Z1[i+1];
            int l=max(0, N-1-b), r=min(a, N-1);
            if(l<=r) ans=min(ans, N*(i-N)+N+N-1-i-r);
        }

        rotate(A+1, A+2, A+N+1);
        rotate(B+1, B+2, B+N+1);
    }
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%d", &A[i]);
    for(int i=1; i<=N; i++) scanf("%d", &B[i]);

    {
        vector<int> VA, VB;
        for(int i=1; i<=N; i++) VA.push_back(A[i]);
        for(int i=1; i<=N; i++) VB.push_back(B[i]);
        sort(VA.begin(), VA.end());
        sort(VB.begin(), VB.end());
        if(VA!=VB) return !printf("-1\n");
    }    

    if(N==1) return !printf("0\n");

    solve();

    if(ans==INF) ans=-1;
    printf("%d\n", ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3780kb

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: 3776kb

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: 3824kb

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: 4112kb

input:

4
1 2 3 4
4 2 1 3

output:

4

result:

wrong answer 1st lines differ - expected: '2', found: '4'