#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
int a[300005];
vector<pair<int,int> > last;
int pos[300005];
int AA[300005][2];
int val[300005][2];
vector<pair<int,int> > pp;
bool check(int now,int X[],int Y[],int N,int S[])
{
for(int i=0;i<N;i++)a[i] = S[i];
for(int i=0;i<now;i++)swap(a[X[i]],a[Y[i]]);
for(int i=0;i<N;i++)pos[a[i]] = i;
int ans = 0;
vector<pair<int,int> > temp;
for(int i=0;i<N;i++)
{
if(a[i] == i)continue;
int aa = i;
int bb = pos[i];
swap(a[aa],a[bb]);
pos[a[bb]] = bb;
temp.push_back(make_pair(aa,bb));
ans++;
}
if(temp.size() > now)return false;
if(temp.size() < now)for(int i=1;i<= now-temp.size();i++)temp.push_back(make_pair(0,0));
if(ans <= now)
{
pp = temp;
return true;
}
else return false;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
for(int i=0;i<N;i++)a[i] = S[i];
int needtime = 0;
int l = 0;
int r = M;
while(l <= r)
{
int mid = (l+r)>>1;
if(check(mid,X,Y,N,S) == true)
{
r = mid - 1;
needtime = mid;
}
else l = mid + 1;
}
for(int i=0;i<N;i++)
{
a[i] = S[i];
pos[a[i]] = i;
}
for(int i=0;i<needtime;i++)last.push_back(make_pair(X[i],Y[i]));
for(int i=0;i<needtime;i++)last.push_back(pp[i]);
/*for(int i=0;i<last.size();i++)cout<<last[i].first<<" "<<last[i].second<<endl;
cout<<endl;*/
for(int i=needtime;i<needtime*2;i++)
{
AA[i-needtime][0] = last[i].first;
AA[i-needtime][1] = last[i].second;
}
for(int i=0;i<needtime;i++)
{
swap(a[X[i]],a[Y[i]]);
swap(pos[a[X[i]]],pos[a[Y[i]]]);
}
for(int i=0;i<needtime;i++)// we find all
{
val[i][0] = a[AA[i][0]];
val[i][1] = a[AA[i][1]];
swap(a[AA[i][0]],a[AA[i][1]]);
swap(pos[a[AA[i][0]]],pos[a[AA[i][1]]]);
}
for(int i=0;i<N;i++)
{
a[i] = S[i];
pos[a[i]] = i;
}
/*for(int i=0;i<needtime;i++)
{
cout<<val[i][0]<<" "<<val[i][1]<<endl;
}
cout<<endl;*/
for(int i=0;i<needtime;i++)
{
swap(a[X[i]],a[Y[i]]);
swap(pos[a[X[i]]],pos[a[Y[i]]]);
P[i] = pos[val[i][0]];
Q[i] = pos[val[i][1]];
swap(a[P[i]],a[Q[i]]);
swap(pos[a[P[i]]],pos[a[Q[i]]]);
}
//test:
for(int i=0;i<N;i++)a[i] = S[i];
for(int i=0;i<needtime;i++)
{
swap(a[X[i]],a[Y[i]]);
swap(a[P[i]],a[Q[i]]);
}
/*for(int i=0;i<N;i++)cout<<a[i]<<" ";
cout<<endl;*/
return needtime;
}
Compilation message
sorting.cpp: In function 'bool check(int, int*, int*, int, int*)':
sorting.cpp:27:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
27 | if(temp.size() > now)return false;
| ~~~~~~~~~~~~^~~~~
sorting.cpp:28:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
28 | if(temp.size() < now)for(int i=1;i<= now-temp.size();i++)temp.push_back(make_pair(0,0));
| ~~~~~~~~~~~~^~~~~
sorting.cpp:28:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if(temp.size() < now)for(int i=1;i<= now-temp.size();i++)temp.push_back(make_pair(0,0));
| ~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
260 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
260 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
260 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
456 KB |
Output is correct |
8 |
Correct |
1 ms |
540 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
456 KB |
Output is correct |
8 |
Correct |
1 ms |
540 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
118 ms |
25440 KB |
Output is correct |
16 |
Correct |
124 ms |
25848 KB |
Output is correct |
17 |
Correct |
138 ms |
27348 KB |
Output is correct |
18 |
Correct |
50 ms |
19616 KB |
Output is correct |
19 |
Correct |
89 ms |
24324 KB |
Output is correct |
20 |
Correct |
94 ms |
25416 KB |
Output is correct |
21 |
Correct |
95 ms |
25436 KB |
Output is correct |
22 |
Correct |
110 ms |
22640 KB |
Output is correct |
23 |
Correct |
131 ms |
28332 KB |
Output is correct |
24 |
Correct |
137 ms |
27840 KB |
Output is correct |
25 |
Correct |
137 ms |
27472 KB |
Output is correct |
26 |
Correct |
96 ms |
24828 KB |
Output is correct |
27 |
Correct |
92 ms |
23672 KB |
Output is correct |
28 |
Correct |
132 ms |
27324 KB |
Output is correct |
29 |
Correct |
128 ms |
26172 KB |
Output is correct |
30 |
Correct |
76 ms |
21076 KB |
Output is correct |
31 |
Correct |
128 ms |
26604 KB |
Output is correct |
32 |
Correct |
123 ms |
27336 KB |
Output is correct |
33 |
Correct |
136 ms |
27812 KB |
Output is correct |
34 |
Correct |
131 ms |
27752 KB |
Output is correct |
35 |
Correct |
88 ms |
24384 KB |
Output is correct |
36 |
Correct |
46 ms |
18640 KB |
Output is correct |
37 |
Correct |
139 ms |
28516 KB |
Output is correct |
38 |
Correct |
135 ms |
27348 KB |
Output is correct |
39 |
Correct |
135 ms |
27564 KB |
Output is correct |