# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
959585 |
2024-04-08T13:20:30 Z |
8pete8 |
Sorting (IOI15_sorting) |
C++17 |
|
0 ms |
0 KB |
#include "sorting.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
//#define int long long
#define double long double
const int mod=1e9+7,mxn=3e5+5,lg=60,inf=1e9,minf=-1e9;
int n;
int getmin(vector<int>v,vector<int>pos){
int ans=0;
vector<int>rev(n);
for(int i=0;i<n;i++)rev[v[i]]=i;
for(int i=0;i<n;i++){
if(v[pos[i]]==v[rev[i]])continue;
rev[v[pos[i]]]=rev[i];
swap(v[pos[i]],v[rev[i]]);
ans++;
}
return ans;
}
int findSwapPairs(int N, vector<int>v, int m, vector<int>x, vector<int>y, vector<int>p, vector<int>q) {
n=N;
int l=0,r=m;
vector<int>pos(n);
for(int i=0;i<n;i++)pos[i]=i;//position that will end up at i
vector<int>o;
for(int i=0;i<n;i++)o.pb(v[i]);
auto get=[&](int mid){
int ans=0,cur=0;
vector<int>rev(n);
for(int i=0;i<n;i++)rev[v[i]]=i;
vector<int>pos2(n);//where we will end up standing at i
for(int i=0;i<n;i++)pos2[i]=i;
for(int i=0;i<mid;i++)swap(pos2[x[i]],pos2[y[i]]);
for(int i=0;i<n;i++)pos[pos2[i]]=i;
vector<pii>what(mid);
for(int i=0;i<mid;i++){
swap(pos[pos2[x[i]]],pos[pos2[y[i]]]);
swap(pos2[x[i]],pos2[y[i]]);
swap(rev[v[x[i]]],rev[v[y[i]]]);
swap(v[x[i]],v[y[i]]);
while(cur<n&&pos[cur]==rev[cur])cur++;
if(cur<n){
what[i]={pos[cur],rev[cur]};
swap(rev[v[what[i].f]],rev[v[what[i].s]]);
swap(v[what[i].f],v[what[i].s]);
cur++;
ans++;
}
else what[i]={0,0};
}
while(cur<n&&pos[cur]==rev[cur])cur++;
return make_pair((cur>=n),what);
};
pair<int,vector<pii>>ans{inf,{}};
while(l<=r){
int mid=l+(r-l)/2;
int cnt=0;
pair<int,vector<pii>>bru=get(mid);
if(bru.f)r=mid-1,ans=min(ans,{mid,bru.s});
else l=mid+1;
for(int i=0;i<n;i++)v[i]=o[i];
}
for(int i=0;i<ans.f;i++)p[i]=ans.s[i].f,q[i]=ans.s[i].s;
return ans.f;
}/*
int32_t main(){
int n;cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++)cin>>v[i];
int m;cin>>m;
vector<int>x(m),y(m);
for(int i=0;i<m;i++)cin>>x[i]>>y[i];
vector<int>p(m),q(m);
cout<<findSwapPairs(n,v,m,x,y,p,q);
}*/
/*
observation:
each starting element will end up somewhere after swapping
each element have distinct path to the end position
we can swap element to swap their path
swap can be made in any order?
if the min num of swap <= num of action
then its doable
min num of swap can increase by at most 1 per action (swapping it back to same pos)
so num of action-min num of swap is monotone
we can do bs? and check o(n)?
finding min num of swap: greedy?
*/
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, std::vector<int>, int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
sorting.cpp:88:7: warning: unused variable 'cnt' [-Wunused-variable]
88 | int cnt=0;
| ^~~
/usr/bin/ld: /tmp/cc5pbHRn.o: in function `main':
grader.c:(.text.startup+0x4eb): undefined reference to `findSwapPairs(int, int*, int, int*, int*, int*, int*)'
collect2: error: ld returned 1 exit status