QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#488947 | #7956. Walk Swapping | arnold518 | TL | 183ms | 4204kb | C++17 | 4.2kb | 2024-07-24 16:34:54 | 2024-07-24 16:34:54 |
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 = 3000;
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");
PP[0]={1, 1}; IP[0]={1, 1};
PP[1]={P1, P2}; IP[1]=inv(PP[1]);
for(int i=2; i<=N; i++) PP[i]=PP[i-1]*PP[1];
for(int i=2; i<=N; i++) IP[i]=IP[i-1]*IP[1];
for(int cyc=0; cyc<N; cyc++)
{
vector<pair<pmm, int>> V, V2, V3;
for(int i=1; i<=N; i++) HA[i]=HA[i-1]+PP[i]*pmm{A[i], A[i]};
for(int i=1; i<=N; i++) HB[i]=HB[i-1]+PP[i]*pmm{B[i], B[i]};
for(int i=1; i<=N; i++) V.push_back({HB[i-1]+(HB[N]-HB[i])*IP[1], i});
for(int i=2; i<=N; i++) V2.push_back({(HA[N]-HA[i-1])*IP[i-1] + (HA[i-1]-HA[1])*PP[N-i], i});
for(int i=1; i<N; i++) V3.push_back({(HA[N-1]-HA[i-1])*IP[i-1] + HA[i-1]*PP[N-i], i});
sort(V.begin(), V.end());
sort(V2.begin(), V2.end());
if(N<=1000)
{
sort(V3.begin(), V3.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<V.size() && V[j].first<=V2[l].first; j++) if(V[j].first==V2[l].first) val=min(val, V[j].second);
if(val!=INF) for(int i=l; i<r; i++) ans=min(ans, (V2[i].second-2)*N+val-1);
}
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<V.size() && V[j].first<=V3[l].first; j++) if(V[j].first==V3[l].first) val=max(val, V[j].second);
if(val!=0) for(int i=l; i<r; i++) ans=min(ans, (N-V3[i].second)%(N-1)*N+N-val);
}
rotate(A+1, A+2, A+N+1);
rotate(B+1, B+2, B+N+1);
}
if(ans==INF) printf("-1\n");
else printf("%d\n", ans);
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3932kb
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: 3868kb
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: 3908kb
input:
6 4 1 3 6 2 5 6 2 1 3 4 5
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
4 1 2 3 4 4 2 1 3
output:
2
result:
ok single line: '2'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3932kb
input:
6 4 1 3 6 2 5 6 2 5 3 4 1
output:
13
result:
ok single line: '13'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3940kb
input:
6 4 1 3 3 2 5 3 2 5 3 4 1
output:
12
result:
ok single line: '12'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3936kb
input:
4 4 3 2 1 1 3 2 4
output:
1
result:
ok single line: '1'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3916kb
input:
5 4 3 5 2 1 1 3 5 4 2
output:
2
result:
ok single line: '2'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3932kb
input:
5 4 3 5 2 1 1 4 3 5 2
output:
4
result:
ok single line: '4'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3976kb
input:
5 1 1 1 2 1 2 1 1 1 1
output:
2
result:
ok single line: '2'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3992kb
input:
4 4 3 2 1 1 3 2 4
output:
1
result:
ok single line: '1'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
4 4 3 2 1 1 3 4 2
output:
2
result:
ok single line: '2'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3944kb
input:
10 2 3 1 1 3 4 5 5 6 1 1 1 3 4 5 3 5 1 7 2
output:
-1
result:
ok single line: '-1'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3932kb
input:
5 1 4 5 3 2 5 3 2 1 4
output:
8
result:
ok single line: '8'
Test #15:
score: 0
Accepted
time: 0ms
memory: 4200kb
input:
5 1 2 3 4 5 5 2 1 3 4
output:
3
result:
ok single line: '3'
Test #16:
score: 0
Accepted
time: 0ms
memory: 4136kb
input:
10 1 2 3 4 5 6 7 8 9 10 8 4 2 3 1 5 7 6 9 10
output:
-1
result:
ok single line: '-1'
Test #17:
score: 0
Accepted
time: 0ms
memory: 4204kb
input:
10 1 2 3 2 2 2 1 1 1 1 2 1 1 1 1 1 2 2 3 2
output:
41
result:
ok single line: '41'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3916kb
input:
10 1 1 1 2 2 4 4 4 2 2 1 1 2 2 4 4 2 2 1 4
output:
12
result:
ok single line: '12'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3908kb
input:
12 3 3 3 2 2 2 4 4 2 2 1 3 3 3 2 2 4 4 2 2 2 1 3 3
output:
13
result:
ok single line: '13'
Test #20:
score: 0
Accepted
time: 0ms
memory: 4196kb
input:
12 3 3 2 2 2 2 4 4 2 2 1 2 2 2 2 2 4 4 2 2 2 1 3 3
output:
19
result:
ok single line: '19'
Test #21:
score: 0
Accepted
time: 0ms
memory: 3936kb
input:
12 3 3 2 4 2 2 4 4 2 2 1 2 2 2 2 2 4 4 2 2 4 1 3 3
output:
-1
result:
ok single line: '-1'
Test #22:
score: 0
Accepted
time: 0ms
memory: 3932kb
input:
20 3 4 4 5 1 2 3 2 5 1 1 5 3 2 4 5 1 2 3 5 2 3 5 4 4 5 1 2 3 2 5 3 1 1 5 3 2 4 5 1
output:
49
result:
ok single line: '49'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3940kb
input:
20 3 4 4 5 1 2 3 2 5 1 1 5 3 2 4 5 1 2 3 5 3 2 3 4 5 1 2 3 5 4 4 5 1 2 3 2 5 1 1 5
output:
158
result:
ok single line: '158'
Test #24:
score: 0
Accepted
time: 0ms
memory: 4204kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 99 1...
output:
109
result:
ok single line: '109'
Test #25:
score: 0
Accepted
time: 2ms
memory: 3916kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 100 ...
output:
89
result:
ok single line: '89'
Test #26:
score: 0
Accepted
time: 2ms
memory: 4012kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 51 5...
output:
4924
result:
ok single line: '4924'
Test #27:
score: 0
Accepted
time: 0ms
memory: 4200kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 51 5...
output:
4905
result:
ok single line: '4905'
Test #28:
score: 0
Accepted
time: 2ms
memory: 3940kb
input:
100 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16 17 17 17 17 17 18 18 18 18 18 19 19 19 19 19 20 20 20 20 20 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 1...
output:
3990
result:
ok single line: '3990'
Test #29:
score: 0
Accepted
time: 2ms
memory: 3936kb
input:
100 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 ...
output:
125
result:
ok single line: '125'
Test #30:
score: 0
Accepted
time: 0ms
memory: 4100kb
input:
100 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 16 16 16 16 16 17 17 17 17 17 18 18 18 18 18 19 19 19 19 19 20 20 20 20 20 9 9 9 9 9 10 10 10 10 10 11 11 11 15 11 1...
output:
-1
result:
ok single line: '-1'
Test #31:
score: 0
Accepted
time: 2ms
memory: 3872kb
input:
100 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 ...
output:
79
result:
ok single line: '79'
Test #32:
score: 0
Accepted
time: 2ms
memory: 3848kb
input:
100 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 ...
output:
-1
result:
ok single line: '-1'
Test #33:
score: 0
Accepted
time: 2ms
memory: 3916kb
input:
100 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ...
output:
0
result:
ok single line: '0'
Test #34:
score: 0
Accepted
time: 183ms
memory: 4056kb
input:
1000 458 51 4 190 103 444 401 456 34 970 169 517 283 66 571 282 233 161 32 376 168 616 993 347 213 597 334 652 471 532 552 987 353 613 665 305 477 632 331 293 939 598 175 813 10 890 423 560 502 857 277 18 283 461 6 231 233 648 929 75 896 807 900 2 582 84 81 107 255 145 909 562 492 58 218 575 7 610 6...
output:
243210
result:
ok single line: '243210'
Test #35:
score: 0
Accepted
time: 183ms
memory: 3984kb
input:
1000 323 302 194 991 814 729 763 611 294 128 752 772 136 693 300 575 118 965 888 999 288 837 534 470 436 195 8 958 448 216 697 295 53 341 577 460 543 724 79 593 490 800 218 336 7 448 401 716 7 920 740 21 87 208 202 422 812 528 196 475 633 732 170 423 975 901 412 117 829 457 420 941 664 644 858 473 9...
output:
243013
result:
ok single line: '243013'
Test #36:
score: -100
Time Limit Exceeded
input:
3000 2057 2074 2349 1090 597 671 2074 92 892 304 1749 2393 935 2200 2859 293 2392 127 1960 1991 1842 2447 1783 1669 599 1936 887 1490 296 478 2334 250 864 1697 1833 325 1801 2950 2704 51 1868 2860 820 1495 1123 1 2122 2135 1826 294 612 1771 2181 160 1358 587 931 409 934 2622 516 2199 399 1008 1446 3...