Submission #71683

#TimeUsernameProblemLanguageResultExecution timeMemory
71683NavickSorting (IOI15_sorting)C++17
100 / 100
452 ms22544 KiB
#include <bits/stdc++.h> #include "sorting.h" #define F first #define S second #define pii pair<int, int> #define pb push_back typedef long long ll; using namespace std; const int maxN = 2e5 + 10, maxM = 3 * maxN; int x[maxM], y[maxM]; int n, q[maxN], tmp[maxN], p[maxN]; bool mark[maxN]; vector <int> a1 ,a2; int check(int m, bool f = 0) { for (int i=0; i<m; i++) { int a = p[x[i]], b = p[y[i]]; p[x[i]] = b, p[y[i]] = a; } memset(mark, 0, sizeof mark); int ted = 0; vector <pii > swaps; for (int i=0; i<n; i++) { if(mark[i]) continue ; ted ++; int curr = i; vector <int> vc; while(!mark[curr]) { mark[curr] = true; if(curr != i) vc.pb(curr); curr = p[curr]; } reverse(vc.begin(), vc.end()); for (auto X : vc){ swaps.pb({X, i});} //if(f)cout << X << ',' << i << endl;} } if(ted < n - m) return 0; else if(!f) return 1; for (int i=0; i<n; i++) p[i] = tmp[i], q[p[i]] = i; for (int i=0; i<m; i++) { int a = p[x[i]], b = p[y[i]]; p[x[i]] = b, p[y[i]] = a; q[b] = x[i], q[a] = y[i]; if(i >= (int)swaps.size()) continue ; int v1 = swaps[i].F, v2 = swaps[i].S; int xi = q[v1], yi = q[v2]; a = p[xi], b = p[yi]; p[xi] = b, p[yi] = a; q[b] = xi, q[a] = yi; a1.pb(xi); a2.pb(yi); } while((int)a1.size() < m) a1.pb(0), a2.pb(0); return 1; } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { n = N; int lo = -1, hi = M; for (int i=0; i<M; i++) x[i] = X[i], y[i] = Y[i]; for (int i=0; i<n; i++) tmp[i] = S[i]; while(hi - lo > 1) { for (int i=0; i<n; i++) p[i] = S[i]; int mid = (lo + hi)/2; if(check(mid)) hi = mid; else lo = mid; } for (int i=0; i<n; i++) p[i] = S[i]; check(hi, 1); for (int i=0; i<hi; i++) P[i] = a1[i], Q[i] = a2[i]; return hi; }
#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...