답안 #643872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643872 2022-09-23T07:01:44 Z ghostwriter 정렬하기 (IOI15_sorting) C++14
100 / 100
393 ms 24932 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#include "grader.cpp"
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
    Tran The Bao
    VOI23 gold medal
*/
namespace subtask1234 {
	const int N = 500;
	int pos[N], S1[N];
	bool sorted(int N, int S[]) {
		bool sorted = 1;
		FOR(i, 1, N - 1)
			if (S[i] < S[i - 1])
				sorted = 0;
		return sorted;
	}
	int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
		FOR(i, 0, N - 1) {
			S1[i] = S[i];
			pos[i] = i;
		}
		FOS(i, M - 1, 0) {
			int x = X[i], y = Y[i];
			swap(pos[x], pos[y]);
		}
		if (sorted(N, S1)) return 0;
		FOR(i, 0, N - 2) {
			if (sorted(N, S1)) return i + 1;
			int x = X[i], y = Y[i], cur = -1, npos = -1;
			swap(S1[x], S1[y]);
			swap(pos[x], pos[y]);
			FOR(j, 0, N - 1)
				if (S1[j] == i)
					cur = j;
			FOR(j, 0, N - 1)
				if (pos[j] == i)
					npos = j;
			P[i] = cur;
			Q[i] = npos;
			swap(S1[cur], S1[npos]);
			if (sorted(N, S1)) return i + 1;
		}
		FOR(i, N - 1, M - 1) {
			if (sorted(N, S1)) return i + 1;
			int x = X[i], y = Y[i];
			swap(S1[x], S1[y]);
			P[i] = Q[i] = 0;
			if (sorted(N, S1)) return i + 1;
		}
		return M;
	}
}
namespace solution {
	const int N = 2e5;
	int pos[N], ipos[N], ipos1[N], S1[N];
	bool sorted(int N, int S[]) {
		bool sorted = 1;
		FOR(i, 1, N - 1)
			if (S[i] < S[i - 1])
				sorted = 0;
		return sorted;
	}
	int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
		if (sorted(N, S)) return 0;
		int l = 1, r = M, ans = -1;
		WHILE(l <= r) {
			int mid = l + (r - l) / 2;
			FOR(i, 0, N - 1) {
				S1[i] = S[i];
				pos[i] = i;
				ipos1[S1[i]] = i;
			}
			FOS(i, mid - 1, 0) {
				int x = X[i], y = Y[i];
				swap(pos[x], pos[y]);
			}
			FOR(i, 0, N - 1) ipos[pos[i]] = i;
			int cur = 0;
			FOR(i, 0, mid - 1) {
				int x = X[i], y = Y[i];
				swap(S1[x], S1[y]);
				swap(ipos1[S1[x]], ipos1[S1[y]]);
				swap(pos[x], pos[y]);
				swap(ipos[pos[x]], ipos[pos[y]]);
				P[i] = Q[i] = 0;
				WHILE(cur < N && ipos[cur] == ipos1[cur]) ++cur;
				if (cur >= N) continue;
				P[i] = ipos1[cur];
				Q[i] = ipos[cur];
				swap(S1[P[i]], S1[Q[i]]);
				swap(ipos1[S1[P[i]]], ipos1[S1[Q[i]]]);
			}
			if (sorted(N, S1)) {
				ans = mid;
				r = mid - 1;
			}
			else l = mid + 1;
		}
		FOR(i, 0, M - 1) P[i] = Q[i] = 0;
		FOR(i, 0, N - 1) {
			S1[i] = S[i];
			pos[i] = i;
			ipos1[S1[i]] = i;
		}
		FOS(i, ans - 1, 0) {
			int x = X[i], y = Y[i];
			swap(pos[x], pos[y]);
		}
		FOR(i, 0, N - 1) ipos[pos[i]] = i;
		int cur = 0;
		FOR(i, 0, ans - 1) {
			int x = X[i], y = Y[i];
			swap(S1[x], S1[y]);
			swap(ipos1[S1[x]], ipos1[S1[y]]);
			swap(pos[x], pos[y]);
			swap(ipos[pos[x]], ipos[pos[y]]);
			P[i] = Q[i] = 0;
			WHILE(cur < N && ipos[cur] == ipos1[cur]) ++cur;
			if (cur >= N) continue;
			P[i] = ipos1[cur];
			Q[i] = ipos[cur];
			swap(S1[P[i]], S1[Q[i]]);
			swap(ipos1[S1[P[i]]], ipos1[S1[Q[i]]]);
		}
		return ans;
	}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	// if (N <= 500 && M >= N) return subtask1234::findSwapPairs(N, S, M, X, Y, P, Q);
	return solution::findSwapPairs(N, S, M, X, Y, P, Q);
}
/*
*/
/*
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
*/

Compilation message

sorting.cpp: In function 'bool subtask1234::sorted(int, int*)':
sorting.cpp:46:18: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   46 |  bool sorted(int N, int S[]) {
      |              ~~~~^
sorting.cpp:44:12: note: shadowed declaration is here
   44 |  const int N = 500;
      |            ^
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:48:3: note: in expansion of macro 'FOR'
   48 |   FOR(i, 1, N - 1)
      |   ^~~
sorting.cpp: In function 'int subtask1234::findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:53:24: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   53 |  int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                    ~~~~^
sorting.cpp:44:12: note: shadowed declaration is here
   44 |  const int N = 500;
      |            ^
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:54:3: note: in expansion of macro 'FOR'
   54 |   FOR(i, 0, N - 1) {
      |   ^~~
sorting.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
sorting.cpp:58:3: note: in expansion of macro 'FOS'
   58 |   FOS(i, M - 1, 0) {
      |   ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:63:3: note: in expansion of macro 'FOR'
   63 |   FOR(i, 0, N - 2) {
      |   ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:68:4: note: in expansion of macro 'FOR'
   68 |    FOR(j, 0, N - 1)
      |    ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:71:4: note: in expansion of macro 'FOR'
   71 |    FOR(j, 0, N - 1)
      |    ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:79:3: note: in expansion of macro 'FOR'
   79 |   FOR(i, N - 1, M - 1) {
      |   ^~~
sorting.cpp: In function 'bool solution::sorted(int, int*)':
sorting.cpp:92:18: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   92 |  bool sorted(int N, int S[]) {
      |              ~~~~^
sorting.cpp:90:12: note: shadowed declaration is here
   90 |  const int N = 2e5;
      |            ^
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:94:3: note: in expansion of macro 'FOR'
   94 |   FOR(i, 1, N - 1)
      |   ^~~
sorting.cpp: In function 'int solution::findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:99:24: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   99 |  int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                    ~~~~^
sorting.cpp:90:12: note: shadowed declaration is here
   90 |  const int N = 2e5;
      |            ^
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:104:4: note: in expansion of macro 'FOR'
  104 |    FOR(i, 0, N - 1) {
      |    ^~~
sorting.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
sorting.cpp:109:4: note: in expansion of macro 'FOS'
  109 |    FOS(i, mid - 1, 0) {
      |    ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:113:4: note: in expansion of macro 'FOR'
  113 |    FOR(i, 0, N - 1) ipos[pos[i]] = i;
      |    ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:115:4: note: in expansion of macro 'FOR'
  115 |    FOR(i, 0, mid - 1) {
      |    ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:135:3: note: in expansion of macro 'FOR'
  135 |   FOR(i, 0, M - 1) P[i] = Q[i] = 0;
      |   ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:136:3: note: in expansion of macro 'FOR'
  136 |   FOR(i, 0, N - 1) {
      |   ^~~
sorting.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
sorting.cpp:141:3: note: in expansion of macro 'FOS'
  141 |   FOS(i, ans - 1, 0) {
      |   ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:145:3: note: in expansion of macro 'FOR'
  145 |   FOR(i, 0, N - 1) ipos[pos[i]] = i;
      |   ^~~
sorting.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sorting.cpp:147:3: note: in expansion of macro 'FOR'
  147 |   FOR(i, 0, ans - 1) {
      |   ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 2 ms 584 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 520 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 532 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 516 KB Output is correct
14 Correct 1 ms 528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 520 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 532 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 516 KB Output is correct
14 Correct 1 ms 528 KB Output is correct
15 Correct 239 ms 22092 KB Output is correct
16 Correct 309 ms 22576 KB Output is correct
17 Correct 393 ms 23724 KB Output is correct
18 Correct 41 ms 13392 KB Output is correct
19 Correct 238 ms 23088 KB Output is correct
20 Correct 242 ms 23748 KB Output is correct
21 Correct 254 ms 23756 KB Output is correct
22 Correct 235 ms 19116 KB Output is correct
23 Correct 269 ms 24704 KB Output is correct
24 Correct 387 ms 24272 KB Output is correct
25 Correct 382 ms 24012 KB Output is correct
26 Correct 240 ms 23664 KB Output is correct
27 Correct 223 ms 23088 KB Output is correct
28 Correct 353 ms 24176 KB Output is correct
29 Correct 335 ms 23824 KB Output is correct
30 Correct 166 ms 22732 KB Output is correct
31 Correct 346 ms 24256 KB Output is correct
32 Correct 263 ms 23848 KB Output is correct
33 Correct 372 ms 24244 KB Output is correct
34 Correct 309 ms 24220 KB Output is correct
35 Correct 226 ms 22732 KB Output is correct
36 Correct 77 ms 22096 KB Output is correct
37 Correct 366 ms 24932 KB Output is correct
38 Correct 352 ms 23940 KB Output is correct
39 Correct 361 ms 24240 KB Output is correct