Submission #645238

# Submission time Handle Problem Language Result Execution time Memory
645238 2022-09-26T13:33:19 Z Benmath Sorting (IOI15_sorting) C++14
74 / 100
1000 ms 29076 KB
#include<bits/stdc++.h>
#include "sorting.h"
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
   int n=N;
   int bro=0;
   int R;
   int l=0;
   int r=n-1;
   int ans=n-1;
   while(l<=r){
   	int k=l+(r-l)/2;

 
    int arr[n];
    int vis[n];
    vector<int>adjl[n+1];
    for(int j=0;j<n;j++){
        arr[j]=j;
        vis[j]=0;
    }
   for(int j=(k-1);j>=0;j--){
    swap(arr[X[j]],arr[Y[j]]);
   }
   int bro=0;
   
   
   for(int j=0;j<n;j++){
   	adjl[S[j]].push_back(arr[j]);
}
for(int j=0;j<n;j++){
	if(vis[j]==0){
		queue<int>q;
		q.push(j);
		vis[j]++;
		while(!q.empty()){
			int a=q.front();

			q.pop();
			bro++;
			for(int i=0;i<adjl[a].size();i++){
				if(vis[adjl[a][i]]==0){
					q.push(adjl[a][i]);
					vis[adjl[a][i]]++;
				}
			}
		}
		bro--;
	}
}
   if(bro<=k){
    r=k-1;
    ans=min(ans,k);
   }else{
   	l=k+1;
   }

   }
   R=ans;
   int k1=ans;
    int loc[n];
    int loc1[n];
    int arr[n];
    int vis[n];
     bro=0;
    vector<int>adjl[n+1];
    for(int j=0;j<n;j++){
        arr[j]=j;
        vis[j]=0;
    }
   for(int j=(k1-1);j>=0;j--){
    swap(arr[X[j]],arr[Y[j]]);
   }
   
   vector<int>v;
   for(int j=0;j<n;j++){
   	adjl[S[j]].push_back(arr[j]);
}
for(int j=0;j<n;j++){
	if(vis[j]==0){
		queue<int>q;
		q.push(j);
		vis[j]++;
		while(!q.empty()){
			int a=q.front();
			bro++;
			if(a!=j){
				v.push_back(a);
			}
			q.pop();
			for(int i=0;i<adjl[a].size();i++){
				if(vis[adjl[a][i]]==0){
					q.push(adjl[a][i]);
					vis[adjl[a][i]]++;
				}
			}
		}
		bro--;
	}
}
     for(int i=0;i<n;i++){
        loc[S[i]]=i;
        loc1[arr[i]]=i;
    }
    int bro1=0;
    for(int i=0;i<k1;i++){
        int x1=loc[S[X[i]]];
        int x2=loc[S[Y[i]]];
       swap(S[X[i]],S[Y[i]]);
       loc[S[X[i]]]=x1;
       loc[S[Y[i]]]=x2;
       x1=loc1[arr[X[i]]];
       x2=loc1[arr[Y[i]]];
        swap(arr[X[i]],arr[Y[i]]);
       loc1[arr[X[i]]]=x1;
       loc1[arr[Y[i]]]=x2;
      if(bro1<bro){
        int loc11=loc[v[bro1]];
        int loc22=loc1[v[bro1]];
        P[i]=loc11;
        Q[i]=loc22;
        swap(S[loc11],S[loc22]);
        loc[S[loc11]]=loc11;
        loc[S[loc22]]=loc22;
        bro1++;
      }else{
          P[i]=0;
          Q[i]=0;
      }
    }
    

    return R;
   /*
   for(int i=0;i<=(n-2);i++){
      swap(S[X[i]],S[Y[i]]);
      int loc=i;
      for(int j=n-2;j>i;j--){
         if(X[j]==loc){
            loc=Y[j];
         }else{
            if(Y[j]==loc){
               loc=X[j];
            }
         }
      }

      int loc1=-1;
      for(int j=0;j<n;j++){
         if(S[j]==i){
            loc1=j;
            break;
         }
      }
      if(loc1!=loc){
         P[i]=loc1;
         Q[i]=loc;
         swap(S[P[i]],S[Q[i]]);
      }else{
         P[i]=loc1;
         Q[i]=loc1;
      }
   }
    return R;
    */

}
/*
int main(){

}
*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:25:8: warning: declaration of 'bro' shadows a previous local [-Wshadow]
   25 |    int bro=0;
      |        ^~~
sorting.cpp:6:8: note: shadowed declaration is here
    6 |    int bro=0;
      |        ^~~
sorting.cpp:41:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |    for(int i=0;i<adjl[a].size();i++){
      |                ~^~~~~~~~~~~~~~~
sorting.cpp:91:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |    for(int i=0;i<adjl[a].size();i++){
      |                ~^~~~~~~~~~~~~~~
sorting.cpp:4:39: warning: unused parameter 'M' [-Wunused-parameter]
    4 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 300 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 300 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 508 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 564 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 2 ms 568 KB Output is correct
4 Correct 3 ms 532 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 2 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 2 ms 568 KB Output is correct
4 Correct 3 ms 532 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 2 ms 440 KB Output is correct
15 Correct 981 ms 29076 KB Output is correct
16 Execution timed out 1050 ms 26352 KB Time limit exceeded
17 Halted 0 ms 0 KB -