이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
*/
#include <bits/stdc++.h>
#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;
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;
}
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]);
}
int found = -1;
for (int j = 0; j < n; ++j)
{
if (pab[j] != j) {
found = j;
break;
}
}
if (found == -1) return true;
// 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]);
}
int found = -1;
for (int j = 0; j < n; ++j)
{
if (pab[j] != j) {
found = j;
break;
}
}
if (found == -1) return true;
else return false;
}
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;
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;
}
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]);
}
int found = -1;
for (int j = 0; j < n; ++j)
{
if (pab[j] != j) {
found = j;
break;
}
}
if (found == -1) {
while ((int)ats.size() < m) ats.emplace_back(0, 0);
return ats;
}
// 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]);
}
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |