/*input
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#include "sorting.h"
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
vi pradinis;
int n;
vpi uzk;
bool galima(int m) {
vi prad = pradinis;
vi pab = pradinis;
vi kurPrad(n);
vi kurPab(n);
// for (int i = 0; i < N; ++i)
// kur[i] = i;
unordered_set<int> blogi;
for (int i = 0; i < m; ++i)
swap(pab[uzk[i].x], pab[uzk[i].y]);
for (int i = 0; i < n; ++i)
{
kurPrad[prad[i]] = i;
kurPab[pab[i]] = i;
if (pab[i] != i)
blogi.insert(i);
}
for (int i = 0; i < m; ++i)
{
int x = uzk[i].x;
int y = uzk[i].y;
{
int firElem = prad[x];
int secElem = prad[y];
// printf("x = %d, kurPrad[firElem], kurPrad[secElem]);
swap(kurPrad[firElem], kurPrad[secElem]);
swap(prad[x], prad[y]);
}
if (blogi.empty()) return true;
int found = *(blogi.begin());
blogi.erase(found);
// found ends up in wrong place
int kitas = pab[found];
swap(prad[ kurPrad[found] ], prad[ kurPrad[kitas] ]);
swap(pab[ kurPab[found] ], pab[ kurPab[kitas] ]);
swap(kurPrad[found], kurPrad[kitas]);
swap(kurPab[found], kurPab[kitas]);
if (pab[kurPab[kitas]] == kurPab[kitas])
blogi.erase(kitas);
}
return blogi.empty();
}
vpi gauk(int m) {
vi prad = pradinis;
vi pab = pradinis;
vi kurPrad(n);
vi kurPab(n);
// for (int i = 0; i < N; ++i)
// kur[i] = i;
unordered_set<int> blogi;
vpi ats;
for (int i = 0; i < m; ++i)
swap(pab[uzk[i].x], pab[uzk[i].y]);
for (int i = 0; i < n; ++i)
{
kurPrad[prad[i]] = i;
kurPab[pab[i]] = i;
if (pab[i] != i)
blogi.insert(i);
}
for (int i = 0; i < m; ++i)
{
int x = uzk[i].x;
int y = uzk[i].y;
{
int firElem = prad[x];
int secElem = prad[y];
// printf("x = %d, kurPrad[firElem], kurPrad[secElem]);
swap(kurPrad[firElem], kurPrad[secElem]);
swap(prad[x], prad[y]);
}
if (blogi.empty()) {
while ((int)ats.size() < m) ats.emplace_back(0, 0);
return ats;
}
int found = *(blogi.begin());
blogi.erase(found);
// found ends up in wrong place
int kitas = pab[found];
ats.emplace_back(kurPrad[found], kurPrad[kitas]);
swap(prad[ kurPrad[found] ], prad[ kurPrad[kitas] ]);
swap(pab[ kurPab[found] ], pab[ kurPab[kitas] ]);
swap(kurPrad[found], kurPrad[kitas]);
swap(kurPab[found], kurPab[kitas]);
if (pab[kurPab[kitas]] == kurPab[kitas])
blogi.erase(kitas);
}
while ((int)ats.size() < m) ats.emplace_back(0, 0);
return ats;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
pradinis.resize(N);
for (int i = 0; i < N; ++i)
pradinis[i] = S[i];
n = N;
for (int i = 0; i < M; ++i) {
uzk.emplace_back(X[i], Y[i]);
}
int l = 0;
int h = M;
int m;
while (l < h) {
m = (l + h) / 2;
if (galima(m))
h = m;
else
l = m + 1;
}
vpi ats = gauk(l);
for (int i = 0; i < l; ++i)
{
P[i] = ats[i].x;
Q[i] = ats[i].y;
}
return l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
580 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
580 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
646 ms |
22496 KB |
Output is correct |
16 |
Correct |
698 ms |
30716 KB |
Output is correct |
17 |
Correct |
788 ms |
31976 KB |
Output is correct |
18 |
Correct |
197 ms |
28508 KB |
Output is correct |
19 |
Correct |
856 ms |
30604 KB |
Output is correct |
20 |
Correct |
735 ms |
30944 KB |
Output is correct |
21 |
Correct |
728 ms |
31356 KB |
Output is correct |
22 |
Correct |
665 ms |
27656 KB |
Output is correct |
23 |
Correct |
756 ms |
33320 KB |
Output is correct |
24 |
Correct |
760 ms |
32516 KB |
Output is correct |
25 |
Correct |
792 ms |
32216 KB |
Output is correct |
26 |
Correct |
697 ms |
31272 KB |
Output is correct |
27 |
Correct |
508 ms |
28884 KB |
Output is correct |
28 |
Correct |
801 ms |
33764 KB |
Output is correct |
29 |
Correct |
810 ms |
33156 KB |
Output is correct |
30 |
Correct |
501 ms |
28884 KB |
Output is correct |
31 |
Correct |
890 ms |
33948 KB |
Output is correct |
32 |
Correct |
679 ms |
32080 KB |
Output is correct |
33 |
Correct |
879 ms |
32564 KB |
Output is correct |
34 |
Correct |
838 ms |
32484 KB |
Output is correct |
35 |
Correct |
881 ms |
30332 KB |
Output is correct |
36 |
Correct |
72 ms |
23524 KB |
Output is correct |
37 |
Correct |
836 ms |
33360 KB |
Output is correct |
38 |
Correct |
793 ms |
32292 KB |
Output is correct |
39 |
Correct |
794 ms |
32392 KB |
Output is correct |