Submission #420355

#TimeUsernameProblemLanguageResultExecution timeMemory
420355Aldas25Sorting (IOI15_sorting)C++14
100 / 100
535 ms24320 KiB
#include "sorting.h" #include <bits/stdc++.h> using namespace std; #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define REP(n) FOR(O, 1, (n)) #define f first #define s second #define pb push_back typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<ll> vl; const int MAXN = 200100; int ids[MAXN]; int vals[MAXN]; int whereId[MAXN], whereVal[MAXN]; vii valSwaps; int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { bool isSorted = true; FOR(i, 0, N-1) if (S[i] != i) isSorted = false; if (isSorted) return 0; int le = 1, ri = M; while (le < ri) { int mid = (le+ri)/2; FOR(i, 0, mid-1) P[i] = Q[i] = 0; FOR(i, 0, N-1) { ids[i] = i; vals[i] = S[i]; whereId[i] = i; whereVal[S[i]] = i; } FOR(i, 0, mid-1) { int a = X[i], b = Y[i]; int id1 = whereId[a], id2 = whereId[b]; swap(whereId[a], whereId[b]); swap(ids[id1], ids[id2]); } // cout << " after enemy swaps:"; // FOR(i, 0, N-1) cout << " " << ids[i]; // cout << endl; valSwaps.clear(); FOR(i,0,N-2) { int val1 = vals[i], val2 = ids[i]; if (val1 == val2) continue; int id1 = i, id2 = whereVal[val2]; swap(vals[id1], vals[id2]); swap(whereVal[val1], whereVal[val2]); valSwaps.pb({val1, val2}); } // cout << " valSwaps:" << endl; // for (auto pp : valSwaps) cout << " " << pp.f << ", " << pp.s << endl; FOR(i, 0, N-1) { ids[i] = i; vals[i] = S[i]; whereId[i] = i; whereVal[S[i]] = i; } FOR(i, 0, (int)valSwaps.size()-1) { int a = X[i], b = Y[i]; int id1 = whereId[a], id2 = whereId[b]; swap(whereId[a], whereId[b]); swap(ids[id1], ids[id2]); a = valSwaps[i].f, b = valSwaps[i].s; id1 = whereVal[a], id2 = whereVal[b]; swap(vals[id1], vals[id2]); swap(whereVal[a], whereVal[b]); P[i] = ids[id1]; Q[i] = ids[id2]; } if ((int)valSwaps.size() <= mid) ri = mid; else le = mid+1; } FOR(i, 0, le-1) P[i] = Q[i] = 0; FOR(i, 0, N-1) { ids[i] = i; vals[i] = S[i]; whereId[i] = i; whereVal[S[i]] = i; } FOR(i, 0, le-1) { int a = X[i], b = Y[i]; int id1 = whereId[a], id2 = whereId[b]; swap(whereId[a], whereId[b]); swap(ids[id1], ids[id2]); } // cout << " after enemy swaps:"; // FOR(i, 0, N-1) cout << " " << ids[i]; // cout << endl; valSwaps.clear(); FOR(i,0,N-2) { int val1 = vals[i], val2 = ids[i]; if (val1 == val2) continue; int id1 = i, id2 = whereVal[val2]; swap(vals[id1], vals[id2]); swap(whereVal[val1], whereVal[val2]); valSwaps.pb({val1, val2}); } // cout << " valSwaps:" << endl; // for (auto pp : valSwaps) cout << " " << pp.f << ", " << pp.s << endl; FOR(i, 0, N-1) { ids[i] = i; vals[i] = S[i]; whereId[i] = i; whereVal[S[i]] = i; } FOR(i, 0, (int)valSwaps.size()-1) { int a = X[i], b = Y[i]; int id1 = whereId[a], id2 = whereId[b]; swap(whereId[a], whereId[b]); swap(ids[id1], ids[id2]); a = valSwaps[i].f, b = valSwaps[i].s; id1 = whereVal[a], id2 = whereVal[b]; swap(vals[id1], vals[id2]); swap(whereVal[a], whereVal[b]); P[i] = ids[id1]; Q[i] = ids[id2]; } return le; }
#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...