QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#489063 | #7956. Walk Swapping | arnold518 | WA | 1ms | 4296kb | C++17 | 4.5kb | 2024-07-24 17:14:13 | 2024-07-24 17:14:13 |
Judging History
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;
template<int MOD>
struct mint
{
int x;
mint() : x(0) {}
mint(int _x) : x(_x) {}
mint operator + (int ot) const { return x+ot>=MOD ? x+ot-MOD : x+ot; }
mint operator - (int ot) const { return x<ot ? x+MOD-ot : x-ot; }
mint operator - () const { return x ? MOD-x : 0; }
mint operator * (int ot) const { return 1ll*x*ot%MOD; }
mint operator += (int ot) { return *this = *this + ot; }
mint operator -= (int ot) { return *this = *this - ot; }
mint operator *= (int ot) { return *this = *this * ot; }
operator int() const { return x; }
bool operator < (int ot) const { return x<ot; }
};
template<int MOD>
mint<MOD> mpow(mint<MOD> a, ll b)
{
mint<MOD> ret=1;
while(b)
{
if(b&1) ret*=a;
b>>=1; a=a*a;
}
return ret;
}
template<int MOD>
mint<MOD> inv(mint<MOD> a) { return mpow<MOD>(a, MOD-2); }
const int MOD1 = 1e9+7;
const int MOD2 = 998244353;
const int P1 = 3;
const int P2 = 7;
typedef pair<mint<MOD1>, mint<MOD2>> pmm;
pmm operator + (const pmm &p, const pmm &q) { return {p.first+q.first, p.second+q.second}; }
pmm operator - (const pmm &p, const pmm &q) { return {p.first-q.first, p.second-q.second}; }
pmm operator * (const pmm &p, const pmm &q) { return {p.first*q.first, p.second*q.second}; }
pmm operator - (const pmm &p) { return {-p.first, -p.second}; }
pmm& operator += (pmm &p, const pmm &q) { p=p+q; return p; }
pmm& operator -= (pmm &p, const pmm &q) { p=p-q; return p; }
pmm& operator *= (pmm &p, const pmm &q) { p=p*q; return p; }
pmm mpow(pmm a, ll b) { return {mpow(a.first, b), mpow(a.second, b)}; }
pmm inv(pmm a) { return {inv(a.first), inv(a.second)}; }
int N, A[MAXN+10], B[MAXN+10];
pmm PP[MAXN+10], IP[MAXN+10];
pmm HA[MAXN+10], HB[MAXN+10];
int ans=INF;
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");
for(int i=1; i<=N; i++) A[i+N]=A[i], B[i+N]=B[i];
PP[0]={1, 1}; IP[0]={1, 1};
PP[1]={P1, P2}; IP[1]=inv(PP[1]);
for(int i=2; i<=N+N; i++) PP[i]=PP[i-1]*PP[1];
for(int i=2; i<=N+N; i++) IP[i]=IP[i-1]*IP[1];
for(int i=1; i<=N+N; i++) HA[i]=HA[i-1]+PP[i]*pmm{A[i], A[i]};
for(int i=1; i<=N+N; i++) HB[i]=HB[i-1]+PP[i]*pmm{B[i], B[i]};
vector<pair<pmm, int>> V4;
for(int i=1; i<N; i++) V4.push_back({(HA[N-1]-HA[i-1])*IP[i-1] + HA[i-1]*PP[N-i], i});
sort(V4.begin(), V4.end());
for(int cyc=0; cyc<N; cyc++)
{
vector<pair<pmm, int>> V0, V1, V2, V3;
for(int i=1; i<=N; i++) if(B[i+cyc]==A[cyc+1]) V0.push_back({(HB[cyc+i-1]-HB[cyc])*IP[cyc]+(HB[cyc+N]-HB[cyc+i])*IP[cyc+1], cyc+i});
for(int i=1; i<=N; i++) if(B[i+cyc]==A[cyc+N+1]) V1.push_back({(HB[cyc+i-1]-HB[cyc])*IP[cyc]+(HB[cyc+N]-HB[cyc+i])*IP[cyc+1], cyc+i});
for(int i=2; i<=N; i++) V2.push_back({(HA[cyc+N]-HA[cyc+i-1])*IP[cyc+i-1] + (HA[cyc+i-1]-HA[cyc+1])*IP[cyc]*PP[N-i], cyc+i});
V3=V4;
sort(V0.begin(), V0.end());
sort(V1.begin(), V1.end());
sort(V2.begin(), V2.end());
for(int l=0, r, j=0; l<V2.size(); l=r)
{
for(r=l; r<V2.size() && V2[r].first==V2[l].first; r++);
int val=INF;
for(; j<V0.size() && V0[j].first<=V2[l].first; j++) if(V0[j].first==V2[l].first) val=min(val, V0[j].second);
if(val!=INF) for(int i=l; i<r; i++) ans=min(ans, (V2[i].second-2-cyc)*N+val-1-cyc);
}
for(int l=0, r, j=0; l<V3.size(); l=r)
{
for(r=l; r<V3.size() && V3[r].first==V3[l].first; r++);
int val=0;
for(; j<V1.size() && V1[j].first<=V3[l].first; j++) if(V1[j].first==V3[l].first) val=max(val, V1[j].second);
if(val!=0) for(int i=l; i<r; i++) ans=min(ans, (N+cyc-V3[i].second)%(N-1)*N+N+cyc-val);
}
V4=V2;
}
if(ans==INF) printf("-1\n");
else printf("%d\n", ans);
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3972kb
input:
4 4 3 2 1 3 4 2 1
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4296kb
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: 3968kb
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: 4296kb
input:
4 1 2 3 4 4 2 1 3
output:
8
result:
wrong answer 1st lines differ - expected: '2', found: '8'