Submission #64831

#TimeUsernameProblemLanguageResultExecution timeMemory
64831KubalionzzaleSorting (IOI15_sorting)C++14
0 / 100
512 ms504 KiB
#include "sorting.h" #include <iostream> #include <algorithm> #include <set> std::set<int> notPlaced; int b[200010]; bool check(int n, int a[], int cur, int x[], int y[]) { notPlaced.clear(); for (int i = 0; i < n; ++i) { a[i] = b[i]; } for (int i = cur; i >= 0; --i) { std::swap(a[x[i]], a[y[i]]); } for (int i = 0; i < n; ++i) { if (a[i] != i) notPlaced.insert(i); } for (int i = 0; i <= cur; ++i) { if (notPlaced.size() > 0) { int first = *(notPlaced.begin()); int second = a[first]; std::swap(a[first], a[second]); if (a[first] == first) notPlaced.erase(first); if (a[second] == second) notPlaced.erase(second); } else { continue; } } if (notPlaced.size() == 0) return 1; else return 0; } int findSwapPairs(int n, int a[], int m, int x[], int y[], int xans[], int yans[]) { for (int i = 0; i < n; ++i) { b[i] = a[i]; } int lowest = -1; for (int i = 0; i < m; ++i) { if (check(n, a, i, x, y)) { lowest = i; break; } } /* int lowest = m; int l = 0, r = m - 1; while (l <= r) { int mid = l + (r - l) / 2; if (check(n, a, mid, x, y)) { lowest = std::min(lowest, mid); r = mid - 1; } else { l = mid + 1; } }*/ notPlaced.clear(); for (int i = 0; i < n; ++i) { a[i] = b[i]; } for (int i = lowest; i >= 0; --i) { std::swap(a[x[i]], a[y[i]]); } for (int i = 0; i < n; ++i) { if (a[i] != i) notPlaced.insert(i); } for (int i = 0; i <= lowest; ++i) { if (notPlaced.size() > 0) { int first = *(notPlaced.begin()); int second = a[first]; xans[i] = first; yans[i] = second; std::swap(a[first], a[second]); if (a[first] == first) notPlaced.erase(first); if (a[second] == second) notPlaced.erase(second); } else { xans[i] = 0; yans[i] = 0; } } /* std::cout << lowest + 1 << "\n"; for (int i = 0; i <= lowest; ++i) { std::cout << xans[i] << " " << yans[i] << "\n"; }*/ return lowest + 1; }
#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...