#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int n, m, x[600005], y[600005], s[600005];
vector<pi> swaps;
bool sortable(int limit){
//cerr << "STARTING " << limit << '\n';
swaps.clear();
vector<int> p(n), rev(n);
vector<int> fp(n);
vector<int> vis(n,0);
vector<pi> temp;
for (int i = 0; i < n; ++i){
p[i] = i;
}
for (int i = 0; i < limit; ++i){
swap(p[x[i]],p[y[i]]);
}
for (int i = 0; i < n; ++i){
fp[i] = s[p[i]];
}
for (int i = 0; i < n; ++i){
if (!vis[i]){
vis[i] = 1;
int idx = fp[i];
while (idx != i){
vis[idx] = 1;
temp.push_back(pi(idx,fp[idx]));
idx = fp[idx];
}
}
}
if ((int) temp.size() > limit) return false;
while ((int) temp.size() < limit) temp.push_back(pi(0,0));
for (int i = 0; i < n; ++i){
p[i] = s[i];
rev[s[i]] = i;
}
for (int i = 0; i < limit; ++i){
//cerr << x[i] << ' ' << y[i] << '\n';
swap(rev[p[x[i]]],rev[p[y[i]]]);
swap(p[x[i]],p[y[i]]);
//for (auto it : p) cerr << it << ' ';
//cerr << '\n';
int s1 = rev[temp[i].first], s2 = rev[temp[i].second];
//cerr << s1 << ' ' << s2 << '\n';
swaps.push_back(pi(s1,s2));
swap(rev[p[s1]],rev[p[s2]]);
swap(p[s1],p[s2]);
//for (auto it : p) cerr << it << ' ';
//cerr << '\n';
}
return true;
}
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){
s[i] = S[i];
}
for (int i = 0; i < M; ++i){
x[i] = X[i];
y[i] = Y[i];
}
int ans = 0;
if (sortable(0)) return 0;
for (int i = 19; i >= 0; --i){
int test = ans + (1 << i);
if (test > M) continue;
if (!sortable(test)) ans = test;
}
ans++;
sortable(ans);
for (int i = 0; i < ans; ++i){
P[i] = swaps[i].first;
Q[i] = swaps[i].second;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
2 ms |
896 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
297 ms |
23944 KB |
Output is correct |
16 |
Correct |
326 ms |
24776 KB |
Output is correct |
17 |
Correct |
408 ms |
25916 KB |
Output is correct |
18 |
Correct |
47 ms |
13816 KB |
Output is correct |
19 |
Correct |
281 ms |
24864 KB |
Output is correct |
20 |
Correct |
311 ms |
25304 KB |
Output is correct |
21 |
Correct |
322 ms |
25232 KB |
Output is correct |
22 |
Correct |
299 ms |
25860 KB |
Output is correct |
23 |
Correct |
357 ms |
26160 KB |
Output is correct |
24 |
Correct |
344 ms |
26100 KB |
Output is correct |
25 |
Correct |
341 ms |
26280 KB |
Output is correct |
26 |
Correct |
295 ms |
25212 KB |
Output is correct |
27 |
Correct |
274 ms |
24888 KB |
Output is correct |
28 |
Correct |
384 ms |
25776 KB |
Output is correct |
29 |
Correct |
377 ms |
25996 KB |
Output is correct |
30 |
Correct |
230 ms |
24908 KB |
Output is correct |
31 |
Correct |
354 ms |
26288 KB |
Output is correct |
32 |
Correct |
320 ms |
25660 KB |
Output is correct |
33 |
Correct |
373 ms |
26356 KB |
Output is correct |
34 |
Correct |
333 ms |
26124 KB |
Output is correct |
35 |
Correct |
276 ms |
24616 KB |
Output is correct |
36 |
Correct |
121 ms |
25040 KB |
Output is correct |
37 |
Correct |
433 ms |
27152 KB |
Output is correct |
38 |
Correct |
352 ms |
26256 KB |
Output is correct |
39 |
Correct |
371 ms |
26632 KB |
Output is correct |