Submission #1018724

#TimeUsernameProblemLanguageResultExecution timeMemory
1018724AmirAli_H1Sorting (IOI15_sorting)C++17
74 / 100
1038 ms16212 KiB
// In the name of Allah #include <bits/stdc++.h> #include "sorting.h" using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef complex<ld> cld; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define kill(x) cout << x << '\n', exit(0) #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, q; const int maxn = 2e5 + 7; int A1[maxn], A2[maxn], mark[maxn]; void dfs(int v) { mark[v] = 1; int u = A1[v]; if (!mark[u]) dfs(u); } bool ok(int n, int S[], int q, int X[], int Y[]) { for (int i = 0; i < n; i++) A1[i] = S[i]; for (int i = 0; i < q; i++) { int i1 = X[i], i2 = Y[i]; swap(A1[i1], A1[i2]); } fill(mark, mark + n, 0); int c = 0; for (int i = 0; i < n; i++) { if (!mark[i]) { dfs(i); c++; } } return (n - c <= q); } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { int l = -1, r = M; while (r - l > 1) { int mid = (l + r) / 2; if (ok(N, S, mid, X, Y)) r = mid; else l = mid; } n = N; q = r; for (int i = 0; i < n; i++) { A1[i] = S[i]; A2[i] = S[i]; } for (int i = 0; i < q; i++) { int i1 = X[i], i2 = Y[i]; P[i] = 0; Q[i] = 0; swap(A2[i1], A2[i2]); } for (int i = 0; i < q; i++) { int i1 = X[i], i2 = Y[i]; swap(A1[i1], A1[i2]); int x1 = -1, x2 = -1; for (int j = 0; j < n; j++) { if (A2[j] != j) { x1 = A2[j], x2 = A2[A2[j]]; swap(A2[j], A2[A2[j]]); break; } } if (x1 != -1 && x2 != -1) { P[i] = find(A1, A1 + n, x1) - A1; Q[i] = find(A1, A1 + n, x2) - A1; } i1 = P[i], i2 = Q[i]; swap(A1[i1], A1[i2]); } return q; }

Compilation message (stderr)

sorting.cpp: In function 'bool ok(int, int*, int, int*, int*)':
sorting.cpp:35:29: warning: declaration of 'q' shadows a global declaration [-Wshadow]
   35 | bool ok(int n, int S[], int q, int X[], int Y[]) {
      |                         ~~~~^
sorting.cpp:26:8: note: shadowed declaration is here
   26 | int n, q;
      |        ^
sorting.cpp:35:13: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   35 | bool ok(int n, int S[], int q, int X[], int Y[]) {
      |         ~~~~^
sorting.cpp:26:5: note: shadowed declaration is here
   26 | int n, q;
      |     ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:80:32: warning: conversion from 'long int' to 'int' may change value [-Wconversion]
   80 |    P[i] = find(A1, A1 + n, x1) - A1;
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~
sorting.cpp:81:32: warning: conversion from 'long int' to 'int' may change value [-Wconversion]
   81 |    Q[i] = find(A1, A1 + n, x2) - A1;
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~
#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...