Submission #68024

#TimeUsernameProblemLanguageResultExecution timeMemory
68024MKopchevSorting (IOI15_sorting)C++14
Compilation error
0 ms0 KiB
#include<bits/stdc++.h> #include "sorting.h" using namespace std; const int nmax=2e5+42; int arr[nmax]; bool been[nmax]; vector< pair<int,int> > change; int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]) { int ok=N,not_ok=-1; //while(ok-not_ok>1) ok=-1; while(1) { ok++; int av=ok; //test av for(int j=0;j<N;j++) arr[j]=S[j]; for(int j=0;j<av;j++) swap(arr[X[j]],arr[Y[j]]); memset(been,0,sizeof(been)); int now=0; for(int i=0;i<N;i++) if(been[i]==0) { int j=i,d=0; while(been[j]==0) { been[j]=1; d++; j=arr[j]; } now=now+d-1; } //cout<<av<<" -> "<<now<<endl; if(now<=av)break; } for(int j=0;j<N;j++) arr[j]=S[j]; for(int j=0;j<ok;j++) swap(arr[X[j]],arr[Y[j]]); memset(been,0,sizeof(been)); for(int i=0;i<N;i++) if(been[i]==0) { int j=i,d=0; while(been[j]==0) { been[j]=1; d++; j=arr[j]; if(i!=j)change.push_back({min(i,j),max(i,j)}); } } while(change.size()<ok) { change.push_back(0,0); } for(auto k:change) { swap(arr[k.first],arr[k.second]); } for(int i=0;i<N;i++) if(arr[i]!=i)return -1;//??? int SZ=change.size(); //reverse(change.begin(),change.end()); for(int i=0;i<SZ;i++) { pair<int,int> now=change[i]; for(int j=ok-1;j>i;j--) { int a=-1,b=-1,q=-1; if(X[j]==now.first)a=X[j],b=Y[j],q=now.second; else if(Y[j]==now.first)a=Y[j],b=X[j],q=now.second; else if(X[j]==now.second)a=X[j],b=Y[j],q=now.first; else if(Y[j]==now.second)a=Y[j],b=X[j],q=now.first; if(a!=-1)now={b,q}; //cout<<"after "<<j<<" step, "<<now.first<<" "<<now.second<<endl; } P[i]=now.first; Q[i]=now.second; //cout<<i<<" -> "<<now.first<<" "<<now.second<<endl; } //test for(int j=0;j<N;j++) arr[j]=S[j]; for(int i=0;i<ok;i++) { swap(arr[X[i]],arr[Y[i]]); swap(arr[P[i]],arr[Q[i]]); //for(int j=0;j<N;j++)cout<<arr[j]<<" ";cout<<endl; } for(int i=0;i<N;i++) assert(arr[i]==i); return ok; } /* int n=5; int S[5]={4,3,2,1,0}; int m=6; int X[6]={0,1,2,3,0,1}; int Y[6]={1,2,3,4,1,2}; int P[6]; int Q[6]; */ /* int n=3; int S[3]={0,2,1}; int m=3; int X[3]={0,1,2}; int Y[3]={1,2,0}; int P[3]; int Q[3]; */ /* const int n=5; int S[n]={3,0,4,2,1}; const int m=6; int X[m]={1,4,2,1,0}; int Y[m]={1,0,3,4,4}; int P[m]; int Q[m]; int main() { int val=findSwapPairs(n,S,m,X,Y,P,Q); cout<<val<<endl; for(int j=0;j<val;j++) cout<<P[j]<<" "<<Q[j]<<endl; } */

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:60:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(change.size()<ok)
           ~~~~~~~~~~~~~^~~
sorting.cpp:62:29: error: no matching function for call to 'std::vector<std::pair<int, int> >::push_back(int, int)'
         change.push_back(0,0);
                             ^
In file included from /usr/include/c++/7/vector:64:0,
                 from /usr/include/c++/7/queue:61,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:86,
                 from sorting.cpp:1:
/usr/include/c++/7/bits/stl_vector.h:939:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::pair<int, int>]
       push_back(const value_type& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:939:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_vector.h:953:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::pair<int, int>]
       push_back(value_type&& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:953:7: note:   candidate expects 1 argument, 2 provided
sorting.cpp:71:23: warning: conversion to 'int' from 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' may alter its value [-Wconversion]
     int SZ=change.size();
            ~~~~~~~~~~~^~
sorting.cpp:10:14: warning: unused variable 'not_ok' [-Wunused-variable]
     int ok=N,not_ok=-1;
              ^~~~~~
sorting.cpp:8:37: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[])
                                     ^