Submission #250079

#TimeUsernameProblemLanguageResultExecution timeMemory
250079mode149256Sorting (IOI15_sorting)C++14
100 / 100
890 ms33948 KiB
/*input */ #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #include "sorting.h" using namespace std; namespace my_template { typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<vl> vll; typedef vector<pi> vpi; typedef vector<vpi> vpii; typedef vector<pl> vpl; typedef vector<cd> vcd; typedef vector<pd> vpd; typedef vector<bool> vb; typedef vector<vb> vbb; typedef std::string str; typedef std::vector<str> vs; #define x first #define y second #define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n" const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L; template<typename T> pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); } template<typename T> pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); } template<typename T> T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); } template<typename T> T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); } template<typename T> void print(vector<T> vec, string name = "") { cout << name; for (auto u : vec) cout << u << ' '; cout << '\n'; } } using namespace my_template; const int MOD = 1000000007; const ll INF = std::numeric_limits<ll>::max(); const int MX = 100101; vi pradinis; int n; vpi uzk; bool galima(int m) { vi prad = pradinis; vi pab = pradinis; vi kurPrad(n); vi kurPab(n); // for (int i = 0; i < N; ++i) // kur[i] = i; unordered_set<int> blogi; for (int i = 0; i < m; ++i) swap(pab[uzk[i].x], pab[uzk[i].y]); for (int i = 0; i < n; ++i) { kurPrad[prad[i]] = i; kurPab[pab[i]] = i; if (pab[i] != i) blogi.insert(i); } for (int i = 0; i < m; ++i) { int x = uzk[i].x; int y = uzk[i].y; { int firElem = prad[x]; int secElem = prad[y]; // printf("x = %d, kurPrad[firElem], kurPrad[secElem]); swap(kurPrad[firElem], kurPrad[secElem]); swap(prad[x], prad[y]); } if (blogi.empty()) return true; int found = *(blogi.begin()); blogi.erase(found); // found ends up in wrong place int kitas = pab[found]; swap(prad[ kurPrad[found] ], prad[ kurPrad[kitas] ]); swap(pab[ kurPab[found] ], pab[ kurPab[kitas] ]); swap(kurPrad[found], kurPrad[kitas]); swap(kurPab[found], kurPab[kitas]); if (pab[kurPab[kitas]] == kurPab[kitas]) blogi.erase(kitas); } return blogi.empty(); } vpi gauk(int m) { vi prad = pradinis; vi pab = pradinis; vi kurPrad(n); vi kurPab(n); // for (int i = 0; i < N; ++i) // kur[i] = i; unordered_set<int> blogi; vpi ats; for (int i = 0; i < m; ++i) swap(pab[uzk[i].x], pab[uzk[i].y]); for (int i = 0; i < n; ++i) { kurPrad[prad[i]] = i; kurPab[pab[i]] = i; if (pab[i] != i) blogi.insert(i); } for (int i = 0; i < m; ++i) { int x = uzk[i].x; int y = uzk[i].y; { int firElem = prad[x]; int secElem = prad[y]; // printf("x = %d, kurPrad[firElem], kurPrad[secElem]); swap(kurPrad[firElem], kurPrad[secElem]); swap(prad[x], prad[y]); } if (blogi.empty()) { while ((int)ats.size() < m) ats.emplace_back(0, 0); return ats; } int found = *(blogi.begin()); blogi.erase(found); // found ends up in wrong place int kitas = pab[found]; ats.emplace_back(kurPrad[found], kurPrad[kitas]); swap(prad[ kurPrad[found] ], prad[ kurPrad[kitas] ]); swap(pab[ kurPab[found] ], pab[ kurPab[kitas] ]); swap(kurPrad[found], kurPrad[kitas]); swap(kurPab[found], kurPab[kitas]); if (pab[kurPab[kitas]] == kurPab[kitas]) blogi.erase(kitas); } while ((int)ats.size() < m) ats.emplace_back(0, 0); return ats; } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { pradinis.resize(N); for (int i = 0; i < N; ++i) pradinis[i] = S[i]; n = N; for (int i = 0; i < M; ++i) { uzk.emplace_back(X[i], Y[i]); } int l = 0; int h = M; int m; while (l < h) { m = (l + h) / 2; if (galima(m)) h = m; else l = m + 1; } vpi ats = gauk(l); for (int i = 0; i < l; ++i) { P[i] = ats[i].x; Q[i] = ats[i].y; } return l; }
#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...