#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pp;
int n,m;
int state=0;
int pos[200010];
int s[200010];
int sw[600010][2];
bool ch[200010];
int ans;
vector<pp> pq;
void swaps(int p1,int p2)
{
int temp=s[p1];
s[p1]=s[p2];
s[p2]=temp;
}
void change_to_state(int st)
{
while(state<st)
{
state++;
swaps(sw[state][0],sw[state][1]);
}
while(state>st)
{
swaps(sw[state][0],sw[state][1]);
state--;
}
}
int count_cycle()
{
for(int i=0;i<n;i++) ch[i]=false;
int cycle_num=0,cur;
for(int i=0;i<n;i++)
{
if(ch[i]) continue;
cycle_num++;
ch[i]=true;
cur=s[i];
while(cur!=i)
{
ch[cur]=true;
cur=s[cur];
}
}
return cycle_num;
}
void find_ans()
{
int hi=m,low=0,av;
while(hi>low)
{
av=(hi+low)>>1;
change_to_state(av);
if(count_cycle()+av>=n) hi=av;
else low=av+1;
}
ans=hi;
return;
}
void get_pq()
{
change_to_state(ans);
for(int i=0;i<n;i++)
{
while(s[i]!=i)
{
pq.push_back({i,s[i]});
swaps(i,s[i]);
}
}
while(pq.size()<ans)
{
pq.push_back({1,1});
}
}
void change_pq()
{
for(int i=0;i<n;i++) pos[i]=i;
int temp;
for(int i=ans-2;i>=0;i--)
{
swaps(pos[sw[i+2][0]],pos[sw[i+2][1]]);
temp=pos[sw[i+2][0]];
pos[sw[i+2][0]]=pos[sw[i+2][1]];
pos[sw[i+2][1]]=temp;
pq[i]={s[pq[i].first],s[pq[i].second]};
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N;
m=M;
for(int i=0;i<n;i++)
{
s[i]=S[i];
}
for(int i=0;i<m;i++)
{
sw[i+1][0]=X[i];
sw[i+1][1]=Y[i];
}
find_ans();
get_pq();
change_pq();
for(int i=0;i<ans;i++)
{
P[i]=pq[i].first;
Q[i]=pq[i].second;
}
return ans;
}
Compilation message
sorting.cpp: In function 'void get_pq()':
sorting.cpp:80:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
80 | while(pq.size()<ans)
| ~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
3 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
560 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
568 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
448 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
568 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
448 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
143 ms |
22812 KB |
Output is correct |
16 |
Correct |
138 ms |
23344 KB |
Output is correct |
17 |
Correct |
151 ms |
25068 KB |
Output is correct |
18 |
Correct |
70 ms |
19280 KB |
Output is correct |
19 |
Correct |
128 ms |
23260 KB |
Output is correct |
20 |
Correct |
131 ms |
24020 KB |
Output is correct |
21 |
Correct |
148 ms |
24128 KB |
Output is correct |
22 |
Correct |
118 ms |
20044 KB |
Output is correct |
23 |
Correct |
146 ms |
25672 KB |
Output is correct |
24 |
Correct |
148 ms |
25572 KB |
Output is correct |
25 |
Correct |
150 ms |
25596 KB |
Output is correct |
26 |
Correct |
128 ms |
23848 KB |
Output is correct |
27 |
Correct |
120 ms |
22968 KB |
Output is correct |
28 |
Correct |
147 ms |
25012 KB |
Output is correct |
29 |
Correct |
153 ms |
24988 KB |
Output is correct |
30 |
Correct |
95 ms |
22204 KB |
Output is correct |
31 |
Correct |
154 ms |
25600 KB |
Output is correct |
32 |
Correct |
135 ms |
24608 KB |
Output is correct |
33 |
Correct |
155 ms |
25732 KB |
Output is correct |
34 |
Correct |
163 ms |
25340 KB |
Output is correct |
35 |
Correct |
137 ms |
23008 KB |
Output is correct |
36 |
Correct |
58 ms |
20744 KB |
Output is correct |
37 |
Correct |
160 ms |
26804 KB |
Output is correct |
38 |
Correct |
154 ms |
25396 KB |
Output is correct |
39 |
Correct |
155 ms |
25964 KB |
Output is correct |