Submission #590907

#TimeUsernameProblemLanguageResultExecution timeMemory
590907AlperenTSorting (IOI15_sorting)C++17
100 / 100
161 ms11636 KiB
#include <bits/stdc++.h> #include "sorting.h" using namespace std; int swapcnt(vector<int> v){ int cnt = 0, n = v.size(); vector<bool> vis(n, false); for(int i = 0; i < n; i++){ if(!vis[i]){ int x = i; while(!vis[x]){ cnt++; vis[x] = true; x = v[x]; } cnt--; } } return cnt; } int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]){ vector<int> v, v2; v.assign(n, 0); copy(S, S + n, v.begin()); int l = -1, r = M; while(r - l > 1){ int m = l + (r - l) / 2; v2 = v; for(int i = 0; i < m; i++) swap(v2[X[i]], v2[Y[i]]); if(swapcnt(v2) <= m) r = m; else l = m; } v2 = v; for(int i = 0; i < r; i++) swap(v2[X[i]], v2[Y[i]]); vector<pair<int, int>> swaps; vector<bool> vis(n, false); for(int i = 0; i < n; i++){ if(!vis[i]){ vis[i] = true; int x = i; while(!vis[v2[x]]){ vis[x] = true; swaps.push_back({v2[x], v2[v2[x]]}); x = v2[x]; } } } while(swaps.size() < r) swaps.push_back({0, 0}); vector<int> where(n); for(int i = 0; i < n; i++) where[v[i]] = i; for(int i = 0; i < r; i++){ swap(where[v[X[i]]], where[v[Y[i]]]); swap(v[X[i]], v[Y[i]]); P[i] = where[swaps[i].first]; Q[i] = where[swaps[i].second]; swap(where[v[P[i]]], where[v[Q[i]]]); swap(v[P[i]], v[Q[i]]); } assert(is_sorted(v.begin(), v.end())); return r; }

Compilation message (stderr)

sorting.cpp: In function 'int swapcnt(std::vector<int>)':
sorting.cpp:7:28: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
    7 |     int cnt = 0, n = v.size();
      |                      ~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:73:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   73 |     while(swaps.size() < r) swaps.push_back({0, 0});
      |           ~~~~~~~~~~~~~^~~
#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...