//by szh
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
#include "sorting.h"
const int maxn = 6e5+10;
int n,m;
int a[maxn],b[maxn];
pii ans[maxn];
pii p[maxn];
vector <int> q[maxn];
int bg[maxn];
int id[maxn];
pii pos[maxn];
pii qid[maxn];
bool check(int cnt,bool flag) {
rep(i,0,n) {
bg[i] = 0;
while (!q[i].empty()) q[i].pop_back();
q[i].pb(i);
id[i] = i;
b[i] = a[i];
}
rep(i,0,cnt) {
int x=p[i].fi,y=p[i].se;
if (x==y) continue;
q[id[x]].pb(y);
q[id[y]].pb(x);
swap(id[x],id[y]);
qid[i] = {id[x],id[y]};
}
rep(i,0,n) {
pos[q[i].back()].fi = q[i][bg[i]];
pos[a[i]].se = i;
}
int hi = 0;
rep(i,0,cnt) {
int x = p[i].fi,y=p[i].se;
if (x!=y) {
swap(b[x],b[y]);
pos[b[x]].se = x,pos[b[y]].se = y;
bg[qid[i].fi]++,bg[qid[i].se]++;
pos[q[qid[i].fi].back()].fi = q[qid[i].fi][bg[qid[i].fi]];
pos[q[qid[i].se].back()].fi = q[qid[i].se][bg[qid[i].se]];
}
pii cur = {0,0};
while (hi<n and pos[hi].fi==pos[hi].se) hi++;
if (hi<n) cur = pos[hi];
x = cur.fi,y=cur.se;
if (x!=y) {
swap(b[x],b[y]);
pos[b[x]].se = x,pos[b[y]].se = y;
}
ans[i] = cur;
if (flag and hi==n) return true;
}
while (hi<n and pos[hi].fi==pos[hi].se) hi++;
return hi==n;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N,m= M;
int l=0,r=M;
rep(i,0,n) a[i] =S[i];
rep(i,0,M) p[i] = {X[i],Y[i]};
while (l<r) {
int mid=l+r>>1;
if (check(mid,true)) r = mid;
else l = mid+1;
}
check(l,false);
rep(i,0,l) P[i] = ans[i].fi, Q[i] = ans[i].se;
return l;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:91:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14460 KB |
Output is correct |
3 |
Correct |
8 ms |
14548 KB |
Output is correct |
4 |
Correct |
9 ms |
14464 KB |
Output is correct |
5 |
Correct |
9 ms |
14548 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
10 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14460 KB |
Output is correct |
15 |
Correct |
8 ms |
14548 KB |
Output is correct |
16 |
Correct |
9 ms |
14464 KB |
Output is correct |
17 |
Correct |
9 ms |
14548 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14456 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14808 KB |
Output is correct |
22 |
Correct |
9 ms |
14804 KB |
Output is correct |
23 |
Correct |
10 ms |
14796 KB |
Output is correct |
24 |
Correct |
10 ms |
14804 KB |
Output is correct |
25 |
Correct |
10 ms |
14824 KB |
Output is correct |
26 |
Correct |
9 ms |
14804 KB |
Output is correct |
27 |
Correct |
9 ms |
14804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14676 KB |
Output is correct |
2 |
Correct |
10 ms |
14804 KB |
Output is correct |
3 |
Correct |
11 ms |
14676 KB |
Output is correct |
4 |
Correct |
10 ms |
14676 KB |
Output is correct |
5 |
Correct |
9 ms |
14676 KB |
Output is correct |
6 |
Correct |
9 ms |
14676 KB |
Output is correct |
7 |
Correct |
10 ms |
14676 KB |
Output is correct |
8 |
Correct |
12 ms |
14836 KB |
Output is correct |
9 |
Correct |
11 ms |
14736 KB |
Output is correct |
10 |
Correct |
13 ms |
14676 KB |
Output is correct |
11 |
Correct |
11 ms |
14776 KB |
Output is correct |
12 |
Correct |
11 ms |
14676 KB |
Output is correct |
13 |
Correct |
13 ms |
14676 KB |
Output is correct |
14 |
Correct |
9 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14676 KB |
Output is correct |
2 |
Correct |
10 ms |
14804 KB |
Output is correct |
3 |
Correct |
11 ms |
14676 KB |
Output is correct |
4 |
Correct |
10 ms |
14676 KB |
Output is correct |
5 |
Correct |
9 ms |
14676 KB |
Output is correct |
6 |
Correct |
9 ms |
14676 KB |
Output is correct |
7 |
Correct |
10 ms |
14676 KB |
Output is correct |
8 |
Correct |
12 ms |
14836 KB |
Output is correct |
9 |
Correct |
11 ms |
14736 KB |
Output is correct |
10 |
Correct |
13 ms |
14676 KB |
Output is correct |
11 |
Correct |
11 ms |
14776 KB |
Output is correct |
12 |
Correct |
11 ms |
14676 KB |
Output is correct |
13 |
Correct |
13 ms |
14676 KB |
Output is correct |
14 |
Correct |
9 ms |
14676 KB |
Output is correct |
15 |
Correct |
380 ms |
42816 KB |
Output is correct |
16 |
Correct |
715 ms |
45004 KB |
Output is correct |
17 |
Execution timed out |
1035 ms |
49008 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |