Submission #99206

#TimeUsernameProblemLanguageResultExecution timeMemory
99206eriksuenderhaufSorting (IOI15_sorting)C++11
100 / 100
748 ms30724 KiB
//#pragma GCC optimize("O3") #include <bits/stdc++.h> #include "sorting.h" #define pii pair<int, int> #define pll pair<long long, long long> #define vii vector<pii> #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; typedef long long ll; const int MAXN = 1e6 + 5; int n = 0, m = 0; int x[MAXN], y[MAXN], p[MAXN], q[MAXN], sz[MAXN], ind2[MAXN]; int p2[MAXN], q2[MAXN], par[MAXN], a[MAXN], ind[MAXN]; int qry(int x) { if (x == par[x]) return x; return par[x] = qry(par[x]); } void join(int x, int y) { x = qry(x), y = qry(y); if (x == y) return; if (sz[x] > sz[y]) swap(x, y); sz[y] += sz[x]; par[x] = y; } bool solve(int cnt) { for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1, ind[i] = i; for (int i = 0; i < cnt; i++) { swap(a[x[i]], a[y[i]]); swap(ind[x[i]], ind[y[i]]); } for (int i = 0; i < n; i++) join(i, a[i]); vii ans; int cnt2 = 0; for (int i = 0; i < n; i++) { if (qry(i) != i) continue; cnt2 += sz[i] - 1; int nx = a[i], pr = i; while (nx != i) { ans.pb({ind[i], ind[nx]}); pr = nx; nx = a[nx]; } } while (ans.size() < cnt) ans.pb({0, 0}); for (int i = 0; i < n; i++) ind[i] = i, ind2[i] = i; for (int i = 0; i < cnt; i++) { swap(ind2[ind[x[i]]], ind2[ind[y[i]]]); swap(ind[x[i]], ind[y[i]]); p[i] = ind2[ans[i].fi], q[i] = ind2[ans[i].se]; } return (ans.size() == cnt); } void cp(int cnt) { for (int i = 0; i < cnt; i++) p2[i] = p[i], q2[i] = q[i]; } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { n = N, m = M; int lo = 0, hi = n - 1, ans = -1; for (int i = 0; i < m; i++) x[i] = X[i], y[i] = Y[i]; while (lo <= hi) { int mi = (lo + hi) / 2; for (int i = 0; i < n; i++) a[i] = S[i]; if (solve(mi)) { hi = mi - 1, ans = mi; cp(ans); } else lo = mi + 1; } for (int i = 0; i < ans; i++) P[i] = p2[i], Q[i] = q2[i]; return ans; }

Compilation message (stderr)

sorting.cpp: In function 'int qry(int)':
sorting.cpp:19:14: warning: declaration of 'x' shadows a global declaration [-Wshadow]
 int qry(int x) {
              ^
sorting.cpp:16:5: note: shadowed declaration is here
 int x[MAXN], y[MAXN], p[MAXN], q[MAXN], sz[MAXN], ind2[MAXN];
     ^
sorting.cpp: In function 'void join(int, int)':
sorting.cpp:24:23: warning: declaration of 'y' shadows a global declaration [-Wshadow]
 void join(int x, int y) {
                       ^
sorting.cpp:16:14: note: shadowed declaration is here
 int x[MAXN], y[MAXN], p[MAXN], q[MAXN], sz[MAXN], ind2[MAXN];
              ^
sorting.cpp:24:23: warning: declaration of 'x' shadows a global declaration [-Wshadow]
 void join(int x, int y) {
                       ^
sorting.cpp:16:5: note: shadowed declaration is here
 int x[MAXN], y[MAXN], p[MAXN], q[MAXN], sz[MAXN], ind2[MAXN];
     ^
sorting.cpp: In function 'bool solve(int)':
sorting.cpp:47:24: warning: variable 'pr' set but not used [-Wunused-but-set-variable]
         int nx = a[i], pr = i;
                        ^~
sorting.cpp:54:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (ans.size() < cnt)
            ~~~~~~~~~~~^~~~~
sorting.cpp:63:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     return (ans.size() == cnt);
             ~~~~~~~~~~~^~~~~~
#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...