#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
namespace debug {
const int DEBUG = true;
template<class T1, class T2>
void pr(const pair<T1, T2> &x);
template<class T, size_t SZ>
void pr(const array<T, SZ> &x);
template<class T>
void pr(const vector<T> &x);
template<class T>
void pr(const set<T> &x);
template<class T1, class T2>
void pr(const map<T1, T2> &x);
template<class T>
void pr(const T &x) { if (DEBUG) cout << x; }
template<class T, class... Ts>
void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }
template<class T1, class T2>
void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }
template<class T>
void prIn(const T &x) {
pr("{");
bool fst = 1;
for (auto &a : x) {
pr(fst ? "" : ", ", a), fst = 0;
}
pr("}");
}
template<class T, size_t SZ>
void pr(const array<T, SZ> &x) { prIn(x); }
template<class T>
void pr(const vector<T> &x) { prIn(x); }
template<class T>
void pr(const set<T> &x) { prIn(x); }
template<class T1, class T2>
void pr(const map<T1, T2> &x) { prIn(x); }
void ps() { pr("\n"), cout << flush; }
template<class Arg, class... Args>
void ps(const Arg &first, const Args &... rest) {
pr(first, " ");
ps(rest...);
}
}
using namespace debug;
int N;
vi a, X, Y;
vector<pi> swaps;
vi mut;
bool sortK(int K) {
// ps("call:", K);
swaps.clear();
mut.clear();
mut.insert(mut.end(), a.begin(), a.end());
for (int i = 0; i < K; i++) {
swap(mut[X[i]], mut[Y[i]]);
}
vector<pi> aSwaps;
for (int i = 0; i < N; i++) {
while (mut[i] != i) {
int goI = mut[i];
aSwaps.pb({i, goI});
swap(mut[i], mut[goI]);
}
}
// ps("false check");
if (aSwaps.size() > K) {
return false;
}
vi loc(N);
mut.clear();
mut.insert(mut.end(), a.begin(), a.end());
for (int i = 0; i < N; i++) {
loc[mut[i]] = i;
}
// ps("final step");
for (int i = 0; i < aSwaps.size(); i++) {
swap(mut[X[i]], mut[Y[i]]);
loc[mut[X[i]]] = X[i];
loc[mut[Y[i]]] = Y[i];
// ps("added:", i);
swaps.pb({loc[aSwaps[i].f], loc[aSwaps[i].s]});
}
return true;
}
int findSwapPairs(int iN, int iS[], int M, int iX[], int iY[], int P[], int Q[]) {
// ps("called");
N = iN;
for (int i = 0; i < N; i++) {
a.pb(iS[i]);
}
for (int i = 0; i < M; i++) {
X.pb(iX[i]);
Y.pb(iY[i]);
}
int low = 0;
int hi = M;
while (low != hi) {
int mid = (low + hi) / 2;
if (sortK(mid)) {
hi = mid;
} else {
low = mid + 1;
}
}
// ps("here");
sortK(low);
for (int i = 0; i < low; i++) {
if (swaps.size() <= i) {
P[i] = 0;
Q[i] = 0;
} else {
P[i] = swaps[i].f;
Q[i] = swaps[i].s;
}
}
return low;
}
Compilation message
sorting.cpp: In function 'bool sortK(int)':
sorting.cpp:106:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (aSwaps.size() > K) {
~~~~~~~~~~~~~~^~~
sorting.cpp:118:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < aSwaps.size(); i++) {
~~^~~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:157:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (swaps.size() <= i) {
~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
4 ms |
760 KB |
Output is correct |
22 |
Correct |
4 ms |
780 KB |
Output is correct |
23 |
Correct |
4 ms |
780 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
3 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
254 ms |
27844 KB |
Output is correct |
16 |
Correct |
297 ms |
28744 KB |
Output is correct |
17 |
Correct |
378 ms |
29828 KB |
Output is correct |
18 |
Correct |
118 ms |
25088 KB |
Output is correct |
19 |
Correct |
518 ms |
28288 KB |
Output is correct |
20 |
Correct |
228 ms |
28928 KB |
Output is correct |
21 |
Correct |
229 ms |
29212 KB |
Output is correct |
22 |
Correct |
272 ms |
25444 KB |
Output is correct |
23 |
Correct |
374 ms |
30820 KB |
Output is correct |
24 |
Correct |
360 ms |
30456 KB |
Output is correct |
25 |
Correct |
355 ms |
30176 KB |
Output is correct |
26 |
Correct |
224 ms |
26468 KB |
Output is correct |
27 |
Correct |
185 ms |
25476 KB |
Output is correct |
28 |
Correct |
328 ms |
30224 KB |
Output is correct |
29 |
Correct |
332 ms |
29380 KB |
Output is correct |
30 |
Correct |
149 ms |
23992 KB |
Output is correct |
31 |
Correct |
377 ms |
30008 KB |
Output is correct |
32 |
Correct |
313 ms |
29800 KB |
Output is correct |
33 |
Correct |
342 ms |
30436 KB |
Output is correct |
34 |
Correct |
341 ms |
30524 KB |
Output is correct |
35 |
Correct |
244 ms |
27972 KB |
Output is correct |
36 |
Correct |
80 ms |
22148 KB |
Output is correct |
37 |
Correct |
448 ms |
30720 KB |
Output is correct |
38 |
Correct |
345 ms |
30128 KB |
Output is correct |
39 |
Correct |
361 ms |
29700 KB |
Output is correct |