#include "sorting.h"
#include <bits/stdc++.h>
#ifdef local
#define debug(a...) qqbx(#a, a)
#define pary(a...) danb(#a, a)
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\e[1;32m(" << s << ") = ("), ..., (std::cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\e[0m\n";
}
#else
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
if (is_sorted(S, S+N)) return 0;
vector<pair<int,int>> best(M, pair<int,int>(-1, -1));
for (int R = 1; R <= N; R++) {
vector<int> targ(N);
for (int i = 0; i < N; i++) targ[i] = i;
for (int i = R-1; i >= 0; i--)
swap(targ[X[i]], targ[Y[i]]);
vector<int> s(S, S+N);
vector<int> pos(N);
for (int i = 0; i < N; i++)
pos[s[i]] = i;
vector<pair<int,int>> ans;
for (int i = 0, it = 0; i < R; i++) {
{
int a = X[i], b = Y[i];
swap(targ[a], targ[b]);
swap(pos[s[a]], pos[s[b]]);
swap(s[a], s[b]);
}
{
while (it < N && pos[it] == pos[targ[pos[it]]]) ++it;
if (it == N) {
ans.emplace_back(0, 0);
} else {
;
int a = pos[it];
int b = pos[targ[a]];
swap(pos[s[a]], pos[s[b]]);
swap(s[a], s[b]);
ans.emplace_back(a, b);
}
}
}
// pary(all(s));
if (is_sorted(all(s))) {
if (best.size() > ans.size())
best = ans;
}
}
for (size_t i = 0; i < best.size(); i++)
tie(P[i], Q[i]) = best[i];
return best.size();
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:69:21: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
69 | return best.size();
| ~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
6 ms |
460 KB |
Output is correct |
22 |
Correct |
6 ms |
564 KB |
Output is correct |
23 |
Correct |
6 ms |
588 KB |
Output is correct |
24 |
Correct |
6 ms |
612 KB |
Output is correct |
25 |
Correct |
5 ms |
588 KB |
Output is correct |
26 |
Correct |
6 ms |
588 KB |
Output is correct |
27 |
Correct |
6 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
420 KB |
Output is correct |
2 |
Correct |
78 ms |
432 KB |
Output is correct |
3 |
Correct |
71 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
91 ms |
452 KB |
Output is correct |
6 |
Correct |
84 ms |
420 KB |
Output is correct |
7 |
Correct |
86 ms |
452 KB |
Output is correct |
8 |
Correct |
75 ms |
424 KB |
Output is correct |
9 |
Correct |
80 ms |
420 KB |
Output is correct |
10 |
Correct |
78 ms |
432 KB |
Output is correct |
11 |
Correct |
96 ms |
428 KB |
Output is correct |
12 |
Correct |
94 ms |
436 KB |
Output is correct |
13 |
Correct |
91 ms |
428 KB |
Output is correct |
14 |
Correct |
48 ms |
420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
420 KB |
Output is correct |
2 |
Correct |
78 ms |
432 KB |
Output is correct |
3 |
Correct |
71 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
91 ms |
452 KB |
Output is correct |
6 |
Correct |
84 ms |
420 KB |
Output is correct |
7 |
Correct |
86 ms |
452 KB |
Output is correct |
8 |
Correct |
75 ms |
424 KB |
Output is correct |
9 |
Correct |
80 ms |
420 KB |
Output is correct |
10 |
Correct |
78 ms |
432 KB |
Output is correct |
11 |
Correct |
96 ms |
428 KB |
Output is correct |
12 |
Correct |
94 ms |
436 KB |
Output is correct |
13 |
Correct |
91 ms |
428 KB |
Output is correct |
14 |
Correct |
48 ms |
420 KB |
Output is correct |
15 |
Execution timed out |
1088 ms |
11520 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |