Submission #88731

#TimeUsernameProblemLanguageResultExecution timeMemory
88731qkxwsmSorting (IOI15_sorting)C++14
0 / 100
3 ms512 KiB
#include "sorting.h" #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; random_device(rd); mt19937 rng(rd()); const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); struct custom_hash { template<class T> unsigned long long operator()(T v) const { unsigned long long x = v; x += FIXED_RANDOM; x += 11400714819323198485ull; x = (x ^ (x >> 30)) * 13787848793156543929ull; x = (x ^ (x >> 27)) * 10723151780598845931ull; return x ^ (x >> 31); } }; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>; template<class T> void ckmin(T &a, T b) { a = min(a, b); } template<class T> void ckmax(T &a, T b) { a = max(a, b); } long long expo(long long a, long long e, long long mod) { return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod)); } template<class T, class U> T nmod(T &x, U mod) { if (x >= mod) x -= mod; } template<class T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); } template<class T> T randomize(T mod) { return (uniform_int_distribution<T>(0, mod - 1))(rng); } #define y0 ___y0 #define y1 ___y1 #define MP make_pair #define MT make_tuple #define PB push_back #define PF push_front #define fi first #define se second #define DBG(x) cerr << #x << " = " << x << endl; #define SZ(x) ((int) (x.size())) #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) #define ALL(x) x.begin(), x.end() const long double PI = 4.0 * atan(1.0); const long double EPS = 1e-9; #define MAGIC 347 #define SINF 10007 #define CO 1000007 #define INF 1000000007 #define BIG 1000000931 #define LARGE 1696969696967ll #define GIANT 2564008813937411ll #define LLINF 2696969696969696969ll #define MAXN 600013 typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pdd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vd; typedef vector<pii> vpi; typedef vector<pll> vpl; typedef vector<pdd> vpd; int N, M; int arr[MAXN]; pii swp[MAXN]; vpi ans; int ord[MAXN], rord[MAXN], val[MAXN], pos[MAXN]; void sw(int p, int q, bool flag) { //POSITION p, q! swap(val[p], val[q]); swap(pos[val[p]], pos[val[q]]); if (!flag) { swap(rord[p], rord[q]); swap(ord[rord[p]], ord[rord[q]]); } else { ans.PB({p, q}); } // cerr << "state:\n"; // FOR(i, 0, N) // { // cerr << rord[i] << ' '; // } // cerr << endl; // FOR(i, 0, N) // { // cerr << val[i] << ' '; // } // cerr << endl; // cerr << p << ' ' << q << endl; } bool check(int x) { FOR(i, 0, N) { ord[i] = i; rord[i] = i; val[i] = arr[i]; pos[arr[i]] = i; } FORD(i, x, 0) { //ord: a[i] = x => the one that will end up at position i is currently at x //rord: a[i] = x => the one that is currently at i will end up at position x pii p = swp[i]; swap(rord[p.fi], rord[p.se]); swap(ord[rord[p.fi]], ord[rord[p.se]]); } // cerr << "state:\n"; // FOR(i, 0, N) // { // cerr << rord[i] << ' '; // } // cerr << endl; // FOR(i, 0, N) // { // cerr << arr[i] << ' '; // } // cerr << endl; // FOR(i, 0, N) // { // cerr << ord[i] << ' '; // } // cerr << endl; ans.clear(); int iter = 0; FOR(i, 0, x) { pii p = swp[i]; sw(p.fi, p.se, 0); while (iter < N && rord[pos[iter]] == iter) { iter++; } // DBG(iter); if (iter == N) { int x = randomize(N); ans.PB({x, x}); continue; } sw(pos[iter], ord[iter], 1); } return (iter == N); } int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) { N = n; FOR(i, 0, N) { arr[i] = s[i]; } M = m; FOR(i, 0, M) { swp[i] = {x[i], y[i]}; } check(3); FOR(i, 0, SZ(ans)) { p[i] = ans[i].fi; q[i] = ans[i].se; } return SZ(ans); int lo = 0, hi = M; while(hi > lo) { int mid = (hi + lo) >> 1; if (check(mid)) { hi = mid; } else { lo = mid + 1; } } check(lo); FOR(i, 0, SZ(ans)) { p[i] = ans[i].fi; q[i] = ans[i].se; } return SZ(ans); }

Compilation message (stderr)

sorting.cpp: In function 'bool check(int)':
sorting.cpp:180:8: warning: declaration of 'int x' shadows a parameter [-Wshadow]
    int x = randomize(N);
        ^
sorting.cpp:134:16: note: shadowed declaration is here
 bool check(int x)
                ^
#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...