#include <iostream>
#include <vector>
using namespace std;
int N, S[200009], M, X[200009], Y[200009], A[200009];
vector<pair<int, int>> solve(int pos) {
// 74 点解法
vector<pair<int, int>>Z; int cx = 0;
for (int i = 0; i < N; i++) {
vector<int>B(N, 0), C(N, 0);
for (int j = 0; j < N; j++) { B[j] = S[j]; C[j] = j; }
for (int j = 0; j < cx; j++) {
swap(B[X[j]], B[Y[j]]);
swap(B[Z[j].first], B[Z[j].second]);
}
for (int j = pos - 1; j >= cx; j--) swap(C[X[j]], C[Y[j]]);
int s1 = 0, s2 = 0;
for (int j = 0; j < N; j++) {
if (B[j] == i) s1 = j; if (C[j] == i) s2 = j;
}
if (s1 == s2) continue;
Z.push_back(make_pair(s1, s2)); cx++;
}
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]]);
swap(A[Z[i].first], A[Z[i].second]);
}
for (int i = 0; i < N; i++) {
if (A[i] != i) return vector<pair<int, int>>{make_pair(-1, -1)};
}
while (Z.size() < pos) Z.push_back(make_pair(0, 0));
return Z;
}
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 = N + 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; }
return ret.size();
}
Compilation message
sorting.cpp: In function 'std::vector<std::pair<int, int> > solve(int)':
sorting.cpp:22:4: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if (B[j] == i) s1 = j; if (C[j] == i) s2 = j;
^~
sorting.cpp:22:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if (B[j] == i) s1 = j; if (C[j] == i) s2 = j;
^~
sorting.cpp:35: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 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:51: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:52: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();
~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
306 ms |
536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
306 ms |
536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |