This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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));
| ~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |