#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,m,b[200005],x[200005],y[200005],p[200005];
vector<pii> ans;
int a[200005],pos[200005],val[200005],c[200005];
bool check(int h, bool mode) {
for (int i=0; i<n; ++i) a[i]=i, val[i]=i, c[i]=b[i], p[i]=i;
for (int i=h; i>=0; --i) swap(a[x[i]],a[y[i]]);
vector<pii> v;
for (int i=0; i<n; ++i) pos[c[i]]=i;
for (int i=0; i<n; ++i) {
if (c[i]!=a[i]) {
v.push_back(pii(i,pos[a[i]]));
swap(pos[a[i]],pos[c[i]]);
swap(c[pos[c[i]]],c[i]);
}
}
reverse(v.begin(),v.end());
if (mode) {
for (int i=0; i<=h; ++i) {
swap(val[p[x[i]]],val[p[y[i]]]);
swap(p[x[i]],p[y[i]]);
if (v.size()) {
ans.push_back(pii(val[v.back().first],val[v.back().second]));
v.pop_back();
} else ans.push_back(pii(0,0));
}
}
if (v.size()<=h+1) return true;
else return false;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N; m=M;
for (int i=0; i<n; ++i) b[i]=S[i], x[i]=X[i], y[i]=Y[i];
bool ah=true;
for (int i=0; i<n; ++i) if (b[i]!=i) ah=false;
if (ah) return 0;
int l=0, r=m-1;
while (l<r) {
int mid=(l+r)/2;
if (check(mid,0)) r=mid;
else l=mid+1;
}
check(l,1);
for (int i=0; i<ans.size(); ++i) P[i]=ans[i].first, Q[i]=ans[i].second;
return ans.size();
}
Compilation message
sorting.cpp: In function 'bool check(int, bool)':
sorting.cpp:37:14: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | if (v.size()<=h+1) return true;
| ~~~~~~~~^~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:57:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i=0; i<ans.size(); ++i) P[i]=ans[i].first, Q[i]=ans[i].second;
| ~^~~~~~~~~~~
sorting.cpp:58:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
58 | return ans.size();
| ~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
320 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
320 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 |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
584 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
580 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
520 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
520 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
15 |
Correct |
125 ms |
26860 KB |
Output is correct |
16 |
Correct |
132 ms |
27544 KB |
Output is correct |
17 |
Correct |
164 ms |
28764 KB |
Output is correct |
18 |
Correct |
37 ms |
15564 KB |
Output is correct |
19 |
Correct |
93 ms |
24804 KB |
Output is correct |
20 |
Correct |
99 ms |
25612 KB |
Output is correct |
21 |
Correct |
98 ms |
25592 KB |
Output is correct |
22 |
Correct |
123 ms |
24208 KB |
Output is correct |
23 |
Correct |
137 ms |
29848 KB |
Output is correct |
24 |
Correct |
143 ms |
29396 KB |
Output is correct |
25 |
Correct |
144 ms |
29000 KB |
Output is correct |
26 |
Correct |
107 ms |
25552 KB |
Output is correct |
27 |
Correct |
95 ms |
24524 KB |
Output is correct |
28 |
Correct |
141 ms |
29048 KB |
Output is correct |
29 |
Correct |
133 ms |
28212 KB |
Output is correct |
30 |
Correct |
89 ms |
22916 KB |
Output is correct |
31 |
Correct |
137 ms |
28776 KB |
Output is correct |
32 |
Correct |
145 ms |
28896 KB |
Output is correct |
33 |
Correct |
144 ms |
29328 KB |
Output is correct |
34 |
Correct |
140 ms |
29368 KB |
Output is correct |
35 |
Correct |
92 ms |
24508 KB |
Output is correct |
36 |
Correct |
50 ms |
21624 KB |
Output is correct |
37 |
Correct |
143 ms |
30056 KB |
Output is correct |
38 |
Correct |
139 ms |
29036 KB |
Output is correct |
39 |
Correct |
140 ms |
29200 KB |
Output is correct |