#include "sorting.h"
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); ++i)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; --i)
#define rrep2(i, n, t) for(ll i = ll(n)-1; i >= ll(t); --i)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define SZ(a) int(a.size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
vi to(n);
iota(all(to), 0);
vi rev(n);
iota(all(rev), 0);
rep(i, m + 1) {
// cerr << i << endl;
// rep(j, n) cerr << to[j] << ' ' << rev[j] << endl;
vvi ls;
vb seen(n);
rep(j, n) {
if (seen[j]) continue;
ls.pb({});
int now = j;
while (true) {
seen[now] = true;
ls.back().pb(now);
int nx = s[rev[now]];
if (seen[nx]) break;
now = nx;
}
}
if (n - SZ(ls) > i) {
swap(to[rev[x[i]]], to[rev[y[i]]]);
swap(rev[x[i]], rev[y[i]]);
continue;
}
vp ev;
for (const vi &v: ls) {
rep2(j, 1, SZ(v)) ev.eb(v[0], v[j]);
}
rep(j, i) {
swap(rev[to[x[j]]], rev[to[y[j]]]);
swap(to[x[j]], to[y[j]]);
if (j < SZ(ev)) {
p[j] = rev[ev[j].first];
q[j] = rev[ev[j].second];
} else {
p[j] = q[j] = 0;
}
}
return i;
}
assert(false);
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:57:23: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
57 | int now = j;
| ^
sorting.cpp:85:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
85 | return i;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 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 |
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 |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 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 |
0 ms |
212 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 |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
548 KB |
Output is correct |
23 |
Correct |
4 ms |
560 KB |
Output is correct |
24 |
Correct |
8 ms |
448 KB |
Output is correct |
25 |
Correct |
5 ms |
568 KB |
Output is correct |
26 |
Correct |
7 ms |
568 KB |
Output is correct |
27 |
Correct |
12 ms |
580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
448 KB |
Output is correct |
2 |
Correct |
36 ms |
456 KB |
Output is correct |
3 |
Correct |
38 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
20 ms |
572 KB |
Output is correct |
6 |
Correct |
45 ms |
572 KB |
Output is correct |
7 |
Correct |
103 ms |
596 KB |
Output is correct |
8 |
Correct |
36 ms |
468 KB |
Output is correct |
9 |
Correct |
44 ms |
440 KB |
Output is correct |
10 |
Correct |
38 ms |
460 KB |
Output is correct |
11 |
Correct |
90 ms |
500 KB |
Output is correct |
12 |
Correct |
103 ms |
596 KB |
Output is correct |
13 |
Correct |
177 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
448 KB |
Output is correct |
2 |
Correct |
36 ms |
456 KB |
Output is correct |
3 |
Correct |
38 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
20 ms |
572 KB |
Output is correct |
6 |
Correct |
45 ms |
572 KB |
Output is correct |
7 |
Correct |
103 ms |
596 KB |
Output is correct |
8 |
Correct |
36 ms |
468 KB |
Output is correct |
9 |
Correct |
44 ms |
440 KB |
Output is correct |
10 |
Correct |
38 ms |
460 KB |
Output is correct |
11 |
Correct |
90 ms |
500 KB |
Output is correct |
12 |
Correct |
103 ms |
596 KB |
Output is correct |
13 |
Correct |
177 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Execution timed out |
1041 ms |
15908 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |