//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],b1[maxn],b2[maxn],b[maxn];
pii ans[maxn];
pii p[maxn];
pii pos[maxn];
bool check(int cnt,bool flag) {
rep(i,0,n) b2[i] = i;
rep(i,0,cnt) swap(b2[p[i].fi],b2[p[i].se]);
rep(i,0,n) b1[b2[i]] = i;
rep(i,0,n) pos[b1[i]].fi = i;
rep(i,0,n) b[i] = a[i];
rep(i,0,n) 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(b1[x],b1[y]);
swap(b[x],b[y]);
pos[b[x]].se = x,pos[b[y]].se = y;
pos[b1[x]].fi = x,pos[b1[y]].fi = y;
}
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:72:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
600 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
524 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
524 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
225 ms |
18660 KB |
Output is correct |
16 |
Correct |
231 ms |
19020 KB |
Output is correct |
17 |
Correct |
298 ms |
19916 KB |
Output is correct |
18 |
Correct |
120 ms |
23372 KB |
Output is correct |
19 |
Correct |
212 ms |
26744 KB |
Output is correct |
20 |
Correct |
213 ms |
27596 KB |
Output is correct |
21 |
Correct |
228 ms |
27744 KB |
Output is correct |
22 |
Correct |
193 ms |
23676 KB |
Output is correct |
23 |
Correct |
238 ms |
29352 KB |
Output is correct |
24 |
Correct |
307 ms |
28848 KB |
Output is correct |
25 |
Correct |
306 ms |
28524 KB |
Output is correct |
26 |
Correct |
210 ms |
26968 KB |
Output is correct |
27 |
Correct |
206 ms |
25908 KB |
Output is correct |
28 |
Correct |
296 ms |
28616 KB |
Output is correct |
29 |
Correct |
298 ms |
27956 KB |
Output is correct |
30 |
Correct |
145 ms |
25056 KB |
Output is correct |
31 |
Correct |
269 ms |
28500 KB |
Output is correct |
32 |
Correct |
227 ms |
28300 KB |
Output is correct |
33 |
Correct |
295 ms |
28812 KB |
Output is correct |
34 |
Correct |
282 ms |
28824 KB |
Output is correct |
35 |
Correct |
218 ms |
26436 KB |
Output is correct |
36 |
Correct |
83 ms |
23724 KB |
Output is correct |
37 |
Correct |
318 ms |
29692 KB |
Output is correct |
38 |
Correct |
300 ms |
28564 KB |
Output is correct |
39 |
Correct |
288 ms |
28700 KB |
Output is correct |