#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
int a[300005];
vector<pair<int,int> > last;
int pos[300005];
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;
}
void sp(int aa,int bb) // bb should be changed.
{
if(last[aa].first == last[bb].first && last[aa].second == last[bb].second)
{
swap(last[aa],last[bb]);
return;
}
if(last[aa].second == last[bb].first && last[aa].first == last[bb].second)
{
swap(last[aa],last[bb]);
return;
}
if(last[aa].first == last[bb].first)
{
last[bb].first = last[aa].second;
swap(last[aa],last[bb]);
return;
}
if(last[aa].first == last[bb].second)
{
last[bb].second = last[aa].second;
swap(last[aa],last[bb]);
return;
}
if(last[aa].second == last[bb].first)
{
last[bb].first = last[aa].first;
swap(last[aa],last[bb]);
return;
}
if(last[aa].second == last[bb].second)
{
last[bb].second = last[aa].first;
swap(last[aa],last[bb]);
return ;
}
swap(last[aa],last[bb]);
return;
}
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;
}
//cout<<needtime<<endl;
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<N;i++)
{
cout<<S[i]<<" ";
}
cout<<endl;*/
/*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++)
{
int go = (i-needtime)*2 + 1;
int now = i;
//cout<<now<<" "<<go<<endl;
while(now > go)
{
sp(now-1,now);
// cout<<now<<" "<<go<<endl;
// for(int i=0;i<last.size();i++)cout<<last[i].first<<" "<<last[i].second<<endl;
//cout<<endl;
now--;
}
}
/*cout<<endl;
for(int i=0;i<last.size();i++)swap(S[last[i].first],S[last[i].second]);
for(int i=0;i<N;i++)
{
cout<<S[i]<<" ";
cout<<endl;
}*/
int j = 0;
for(int i=0;i<needtime*2;i++)
{
if(i%2 == 0)continue;
P[j] = last[i].first;
Q[j] = last[i].second;
j++;
}
/*for(int i=0;i<needtime;i++)
{
cout<<P[i]<<" "<<Q[i]<<endl;
}*/
return needtime;
}
Compilation message
sorting.cpp: In function 'bool check(int, int*, int*, int, int*)':
sorting.cpp:25: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]
25 | if(temp.size() > now)return false;
| ~~~~~~~~~~~~^~~~~
sorting.cpp:26: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]
26 | if(temp.size() < now)
| ~~~~~~~~~~~~^~~~~
sorting.cpp:28:16: 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 | 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
312 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
372 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 |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
320 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
372 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
572 KB |
Output is correct |
23 |
Correct |
1 ms |
608 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
576 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 |
6 ms |
468 KB |
Output is correct |
2 |
Correct |
6 ms |
468 KB |
Output is correct |
3 |
Correct |
6 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
7 ms |
468 KB |
Output is correct |
9 |
Correct |
8 ms |
624 KB |
Output is correct |
10 |
Correct |
7 ms |
468 KB |
Output is correct |
11 |
Correct |
7 ms |
616 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
7 ms |
528 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
468 KB |
Output is correct |
2 |
Correct |
6 ms |
468 KB |
Output is correct |
3 |
Correct |
6 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
7 ms |
468 KB |
Output is correct |
9 |
Correct |
8 ms |
624 KB |
Output is correct |
10 |
Correct |
7 ms |
468 KB |
Output is correct |
11 |
Correct |
7 ms |
616 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
7 ms |
528 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Execution timed out |
1039 ms |
22080 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |