Submission #560231

# Submission time Handle Problem Language Result Execution time Memory
560231 2022-05-11T07:47:38 Z Koosha_mv Sorting (IOI15_sorting) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "sorting.h"

const int N=1e6+99;

int n,m,a[N],b[N],s[N],t[N],pos[N],mark[N];
vector<pair<int,int>> ans;

int Cnt(){
	fill(mark,mark+N,0);
	int ans=0;
	f(i,0,n){
		if(mark[i]) continue ;
		ans++;
		for(int u=i;mark[u]==0;u=b[u]){
			mark[u]=1;
		}
	}
	return ans;
}
bool check(int x){
	f(i,0,n) b[i]=a[i];
	f(i,0,x+1) swap(b[s[i]],b[t[i]]);
	return n-Cnt()<=x+1;
}
void do_it(int x){
	check(x);
	fill(mark,mark+N,0);
	f(i,0,n){
		if(mark[i]) continue ;
		mark[i]=1;
		for(int u=b[i];mark[u]==0;u=b[u]){
			ans.pb({i,u});
		}
	}
}
int findSwapPairs(int _n, int A[], int _m, int S[], int T[], int P[], int Q[]) {
  n=_n,m=_m;
	int c=1;
	f(i,0,n){
		a[i]=A[i];
		if(a[i]!=i) c=0;
	}
	f(i,0,m) s[i]=S[i],t[i]=T[i];
	int l=-1,r=m;
	while(l+1<r){
		int mid=(l+r)>>1;
		if(check(mid)) r=mid;
		else l=mid;
	}
	do_it(r);
	if(ans.size()<r+1) ans.pb({0,0});
	f(i,0,n) pos[a[i]]=i;
	f(i,0,r+1){
		swap(pos[a[s[i]]],pos[a[t[i]]]);
		swap(a[s[i]],a[t[i]]);
		P[i]=pos[ans[i].F];
		Q[i]=pos[ans[i].S];
		swap(pos[a[P[i]]],pos[a[Q[i]]]);
		swap(a[P[i]],a[Q[i]]);
	}
	/dbga(a,0,n);
	return r+1;
}
/*
int32_t main(){
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n,m,a[100],s[100],t[100],p[100],q[100];
	cin>>n;
	f(i,0,n) cin>>a[i];
	cin>>m;
	f(i,0,m) cin>>s[i]>>t[i];
	cout<<findSwapPairs(n,a,m,s,t,p,q)<<"R"<<endl;
}*/

Compilation message

sorting.cpp: In function 'int Cnt()':
sorting.cpp:31:6: warning: declaration of 'ans' shadows a global declaration [-Wshadow]
   31 |  int ans=0;
      |      ^~~
sorting.cpp:27:23: note: shadowed declaration is here
   27 | vector<pair<int,int>> ans;
      |                       ^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:72:15: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |  if(ans.size()<r+1) ans.pb({0,0});
      |     ~~~~~~~~~~^~~~
sorting.cpp:82:2: error: expected primary-expression before '/' token
   82 |  /dbga(a,0,n);
      |  ^