#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, S[200009], M, X[200009], Y[200009], A[200009], B[200009], C[200009];
vector<pair<int, int>> solve(int pos) {
// 第一部
for (int i = 0; i < N; i++) A[i] = S[i];
for (int i = 0; i < pos; i++) swap(A[X[i]], A[Y[i]]);
vector<pair<int, int>>Z;
for (int i = 0; i < N; i++) {
int s1 = 0;
for (int j = 0; j < N; j++) { if (A[j] == i) s1 = j; }
swap(A[i], A[s1]); Z.push_back(make_pair(i, s1));
}
if (Z.size() > pos) return vector<pair<int, int>>{make_pair(-1, -1)};
while (Z.size() < pos) Z.push_back(make_pair(0, 0));
// 第二部
vector<pair<int, int>>ZZ;
for (int i = 0; i < N; i++) { A[i] = S[i]; B[S[i]] = i; C[i] = S[i]; }
for (int i = 0; i < pos; i++) swap(C[X[i]], C[Y[i]]);
for (int i = 0; i < pos; i++) {
swap(A[X[i]], A[Y[i]]); swap(B[A[X[i]]], B[A[Y[i]]]);
int num1 = C[Z[i].first], num2 = C[Z[i].second];
int p1 = B[num1], p2 = B[num2];
ZZ.push_back(make_pair(p1, p2));
swap(A[p1], A[p2]); swap(B[A[p1]], B[A[p2]]); swap(C[Z[i].first], C[Z[i].second]);
}
return ZZ;
}
bool check(vector<pair<int, int>>res) {
for (int i = 0; i < N; i++) A[i] = S[i];
for (int i = 0; i < res.size(); i++) {
swap(A[X[i]], A[Y[i]]);
swap(A[res[i].first], A[res[i].second]);
}
for (int i = 0; i < N; i++) { if (A[i] != i) return false; }
return true;
}
int findSwapPairs(int NN, int SS[], int MM, int XX[], int YY[], int P[], int Q[]) {
N = NN; M = MM;
for (int i = 0; i < NN; i++) S[i] = SS[i];
for (int i = 0; i < MM; i++) { X[i] = XX[i]; Y[i] = YY[i]; }
int L = 0, R = M + 1, M_; vector<pair<int, int>>ret;
for (int i = 0; i < 20; i++) {
M_ = (L + R) / 2;
vector<pair<int, int>> z = solve(M_);
if (z == vector<pair<int, int>>{make_pair(-1, -1)}) L = M_;
else { R = M_; ret = z; }
}
for (int i = 0; i < ret.size(); i++) { P[i] = ret[i].first; Q[i] = ret[i].second; }
if (check(ret) == false) return -1;
return ret.size();
}
Compilation message
sorting.cpp: In function 'std::vector<std::pair<int, int> > solve(int)':
sorting.cpp:18:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (Z.size() > pos) return vector<pair<int, int>>{make_pair(-1, -1)};
~~~~~~~~~^~~~~
sorting.cpp:19:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (Z.size() < pos) Z.push_back(make_pair(0, 0));
~~~~~~~~~^~~~~
sorting.cpp: In function 'bool check(std::vector<std::pair<int, int> >)':
sorting.cpp:37:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < res.size(); i++) {
~~^~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:57:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ret.size(); i++) { P[i] = ret[i].first; Q[i] = ret[i].second; }
~~^~~~~~~~~~~~
sorting.cpp:60:17: warning: conversion to 'int' from 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' may alter its value [-Wconversion]
return ret.size();
~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |