Submission #1018957

# Submission time Handle Problem Language Result Execution time Memory
1018957 2024-07-10T11:17:37 Z amirhoseinfar1385 Sorting (IOI15_sorting) C++14
0 / 100
1 ms 8796 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
int all[maxn],n,q,fake[maxn],vis[maxn],lnk[maxn];
pair<int,int>alltagh[maxn];

void khor(){
	for(int i=0;i<n;i++){
		cout<<fake[i]<<" ";
	}
	cout<<endl;
	for(int i=0;i<n;i++){
		cout<<all[i]<<" ";
	}
	cout<<endl;
	for(int i=0;i<n;i++){
		cout<<lnk[i]<<endl;
	}
	cout<<endl;
	cout<<endl;
}

vector<vector<int>> besaz(int ind){
	vector<vector<int>>ret;
	for(int i=0;i<n;i++){
		fake[i]=all[i];
		vis[i]=0;
	}
	for(int i=1;i<=ind;i++){
		swap(fake[alltagh[i].first],fake[alltagh[i].second]);
	}
	for(int i=0;i<n;i++){
		if(vis[i]==0){
			ret.push_back({});
			int now=i;
			while(vis[now]==0){
//				cout<<now<<" "<<fake[now]<<endl;
				vis[now]=1;
				ret.back().push_back(fake[now]);
				now=fake[now];
			}
		}
	}
	return ret;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n=N;
    for(int i=0;i<n;i++){
    	all[i]=S[i];
    	lnk[all[i]]=i;
    }
    q=M;
    for(int i=1;i<=q;i++){
    	alltagh[i]=make_pair(X[i-1],Y[i-1]);
    }
    int low=-1,high=q,mid;
    /*while(high-low>1){
    	mid=(high+low)>>1;
    	vector<vector<int>>te=besaz(mid);
    	if(n-(int)te.size()<=mid){
    		high=mid;
    	}else{
    		low=mid;
    	}
    }*/
    for(int i=0;i<=q;i++){
    	if(besaz(i).size()<=i){
    		high=i;
    		break;
    	}
    }
   	vector<vector<int>>ger=besaz(high);
   	int now=0;
   	for(int i=0;i<(int)ger.size();i++){
   		//cout<<i<<" "<<(int)ger[i].size()<<endl;
   		//int tof=ger[i][0];
   		for(int j=1;j<(int)ger[i].size();j++){
   			P[now]=min(ger[i][j],ger[i][j-1]);
   			Q[now]=max(ger[i][j],ger[i][j-1]);
   		//	cout<<"chymige: "<<i<<" "<<j<<" "<<ger[i][j]<<" "<<ger[i][j-1]<<" "<<now<<" "<<P[0]<<" "<<Q[0]<<endl;
   			now++;
   		}
   	}
   	if(now>q){
   		exit(0);
   	}
   	for(int i=now;i<q;i++){
   		P[i]=Q[i]=0;
   	}
   	for(int i=1;i<=q;i++){
   		swap(lnk[all[alltagh[i].first]],lnk[all[alltagh[i].second]]);
   		swap(all[alltagh[i].first],all[alltagh[i].second]);
   		P[i-1]=lnk[P[i-1]];
   		Q[i-1]=lnk[Q[i-1]];
   		swap(lnk[all[P[i-1]]],lnk[all[Q[i-1]]]);
   		swap(all[P[i-1]],all[Q[i-1]]);
   	}
   	for(int i=0;i<n;i++){
   		if(all[i]!=i){
   			exit(0);
   		}
   	}
	return high;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:69:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   69 |      if(besaz(i).size()<=i){
      |         ~~~~~~~~~~~~~~~^~~
sorting.cpp:58:9: warning: unused variable 'low' [-Wunused-variable]
   58 |     int low=-1,high=q,mid;
      |         ^~~
sorting.cpp:58:23: warning: unused variable 'mid' [-Wunused-variable]
   58 |     int low=-1,high=q,mid;
      |                       ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8536 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 1 ms 8540 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8796 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8796 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -