Submission #397091

#TimeUsernameProblemLanguageResultExecution timeMemory
397091Andyvanh1Sorting (IOI15_sorting)C++14
100 / 100
209 ms21256 KiB
#include <iostream> #include <algorithm> #include <vector> #include <set> #include <stack> #include <queue> #include <map> #include <math.h> using namespace std; #define pb push_back #define INF INT32_MAX #define vt vector typedef vt<int> vi; typedef pair<int,int> pii; typedef tuple<int,int,int> tiii; typedef long long ll; #define MOD 1000000007 bool works(int n, int s[], int m, int x[],int y[], int val){ for(int i = 0; i < val; i++){ swap(s[x[i]],s[y[i]]); } vt<bool> visited(n); for(int i = 0; i < n; i++){ visited[i] = false; } int ans = 0; for(int i = 0; i < n; i++){ if(!visited[i]){ int j = s[i]; visited[i] = true; while(j!=i){ ans++; visited[j] = true; j = s[j]; } } } for(int i = val-1; i >= 0; i--){ swap(s[x[i]],s[y[i]]); } return val>=ans; } int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]){ int l = 0, r= m-1; while(l!=r){ int mid = (l+r)>>1; if(works(n,s,m,x,y,mid)){ r = mid; }else{ l = mid+1; } } int op[r]; int oq[r]; for(int i = 0; i < r; i++){ swap(s[x[i]],s[y[i]]); } int index = 0; vt<bool> visited(n); for(int i = 0; i < n; i++){ visited[i] = false; } for(int i = 0; i < n; i++){ if(!visited[i]){ int j = i; visited[i] = true; vi curop; vi curoq; while(s[j]!=i){ visited[s[j]] = true; curop.pb(j); j = s[j]; curoq.pb(j); } for(int i = index; i < index+curop.size(); i++){ op[i] = curop[index+curop.size()-1-i]; oq[i] = curoq[index+curop.size()-1-i]; } index+=curop.size(); } } for(int i = index; i < r; i++){ op[i]=1; oq[i]=1; } for(int i = r-1; i >= 0; i--){ swap(s[x[i]],s[y[i]]); } int revs[n]; for(int i = 0; i < n; i++){ revs[s[i]] = i; } for(int i = 0; i < r; i++){ swap(revs[s[x[i]]],revs[s[y[i]]]); swap(s[x[i]],s[y[i]]); p[i] = revs[op[i]]; q[i] = revs[oq[i]]; } return r; }

Compilation message (stderr)

sorting.cpp: In function 'bool works(int, int*, int, int*, int*, int)':
sorting.cpp:28:32: warning: unused parameter 'm' [-Wunused-parameter]
   28 | bool works(int n, int s[], int m, int x[],int y[], int val){
      |                            ~~~~^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:96:21: warning: declaration of 'i' shadows a previous local [-Wshadow]
   96 |             for(int i = index; i < index+curop.size(); i++){
      |                     ^
sorting.cpp:82:13: note: shadowed declaration is here
   82 |     for(int i = 0; i < n; i++){
      |             ^
sorting.cpp:96:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |             for(int i = index; i < index+curop.size(); i++){
      |                                ~~^~~~~~~~~~~~~~~~~~~~
sorting.cpp:100:31: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  100 |             index+=curop.size();
      |                               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...