QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#489415#7956. Walk Swappingarnold518WA 0ms3828kbC++173.0kb2024-07-24 20:01:482024-07-24 20:01:49

Judging History

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

  • [2024-07-24 20:01:49]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3828kb
  • [2024-07-24 20:01:48]
  • 提交

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+1; 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+1; i++) V.push_back(B[i]);
        V=getZ(V);
        for(int i=1; i<=N+N+1; 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+1; i>=1; i--) V.push_back(B[i]);
        V=getZ(V);
        for(int i=1; i<=N+N+1; i++) Z2[i]=V[3*N+1-i];

        for(int i=2; i<=N; i++) printf("%d ", A[i]); printf(" : A\n");
        for(int i=1; i<=N+N+1; i++) printf("%d ", B[i]); printf(" : B\n");
        for(int i=1; i<=N+N+1; i++) printf("%d ", Z1[i]); printf(" : Z1\n");
        for(int i=1; i<=N+N+1; i++) printf("%d ", Z2[i]); printf(" : Z2\n");

        for(int i=N; i<=N+N; i++)
        {
            int a=Z2[i], b=min(Z1[i+1], N+N-i);
            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=min(Z1[i+1], N+N-i);
            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=1; i<=N+1; i++)
        {
            int a=min(Z2[i], i-1), b=Z1[i+1];
            int l=max(0, N-1-b), r=min(a, N-1);
            if(l<=r) ans=min(ans, (N-1)*(i-1)+l);
        }
        for(int i=N+1; i<=N+N+1; i++)
        {
            int a=min(Z2[i], i-1), b=Z1[i+1];
            int l=max(0, N-1-a), r=min(b, N-1);
            if(l<=r) ans=min(ans, (N-1)*(i-N)-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: 0
Wrong Answer
time: 0ms
memory: 3828kb

input:

4
4 3 2 1
3 4 2 1

output:

3 2 1  : A
3 4 2 1 3 4 2 1 3  : B
1 0 0 0 1 0 0 0 1  : Z1
0 0 0 2 0 0 0 2 0  : Z2
2 1 4  : A
4 2 1 3 4 2 1 3 4  : B
0 2 0 0 0 2 0 0 0  : Z1
1 0 0 0 1 0 0 0 1  : Z2
1 4 3  : A
2 1 3 4 2 1 3 4 2  : B
0 1 0 0 0 1 0 0 0  : Z1
0 0 1 0 0 0 1 0 0  : Z2
4 3 2  : A
1 3 4 2 1 3 4 2 1  : B
0 0 1 0 0 0 1 0 0  :...

result:

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