#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 6e5;
int N, M, ans;
int S[MAXN+10], X[MAXN+10], Y[MAXN+10], *P, *Q;
int A[MAXN+10], B[MAXN+10], T[MAXN+10];
bool vis[MAXN+10];
bool solve(int R)
{
for(int i=1; i<=N; i++) A[i]=S[i];
for(int i=1; i<=R; i++)
{
swap(A[X[i]], A[Y[i]]);
}
vector<pii> V;
for(int i=1; i<=N; i++) vis[i]=false;
for(int i=1; i<=N; i++)
{
if(vis[i]) continue;
for(int now=i; ; now=A[now])
{
vis[now]=true;
if(vis[A[now]]) break;
V.push_back({A[now], A[A[now]]});
}
}
if(V.size()>R) return false;
for(int i=1; i<=N; i++) T[i]=i, A[i]=i;
for(int i=1; i<=R; i++) P[i]=Q[i]=1;
for(int i=R; i>=1; i--)
{
if(!V.empty())
{
pii t=V.back(); V.pop_back();
pii a=pii(T[t.first], T[t.second]);
P[i]=a.first; Q[i]=a.second;
swap(A[a.first], A[a.second]);
swap(T[t.first], T[t.second]);
}
pii a=pii(P[i], Q[i]);
pii t=pii(A[a.first], A[a.second]);
swap(A[a.first], A[a.second]);
swap(T[t.first], T[t.second]);
}
for(int i=0; i<R; i++)
{
P[i]=P[i+1]-1;
Q[i]=Q[i+1]-1;
}
}
int findSwapPairs(int _N, int _S[], int _M, int _X[], int _Y[], int _P[], int _Q[])
{
N=_N; M=_M; P=_P; Q=_Q;
for(int i=1; i<=N; i++) S[i]=_S[i-1]+1;
for(int i=1; i<=M; i++) X[i]=_X[i-1]+1, Y[i]=_Y[i-1]+1;
solve(M);
return M;
}
Compilation message
sorting.cpp: In function 'bool solve(int)':
sorting.cpp:36:13: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
36 | if(V.size()>R) return false;
| ~~~~~~~~^~
sorting.cpp:24:14: warning: control reaches end of non-void function [-Wreturn-type]
24 | vector<pii> V;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
620 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 |
620 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 |
620 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 |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
1004 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
1004 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |