Submission #567807

# Submission time Handle Problem Language Result Execution time Memory
567807 2022-05-24T08:34:11 Z ngpin04 Sorting (IOI15_sorting) C++17
100 / 100
345 ms 28800 KB
#include <bits/stdc++.h>
#include "sorting.h"
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end() 
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r) {
	return l + rd() % (r - l + 1);
}
const int N = 6e5 + 5; 
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);

int cur[N];
int a[N];
int s[N];
int x[N];
int y[N];
int p[N];
int q[N];
int n,m;

bool vis[N];

bool check(int r) {
	for (int i = 0; i < n; i++)
		a[i] = s[i];

	for (int i = 0; i <= r; i++) 
		swap(a[x[i]], a[y[i]]);
	
	// cerr << r << "\n";
	for (int i = 0; i < n; i++) {
		vis[i] = false;
		// cerr << a[i] << " \n"[i == n - 1];
	}

	vector <pair <int, int>> sw;
	for (int i = 0; i < n; i++) if (!vis[i]) {
		int j = i;
		vector <int> s(1, j);
		vis[j] = true;
		while (!vis[a[j]]) {
			j = a[j];
			vis[j] = true;
			s.push_back(j);
		}

		for (int j = 1; j < (int) s.size(); j++)
			sw.push_back({a[s[j - 1]], a[s[j]]});
	}

	if (sw.size() > r + 1)
		return false;

	for (int i = 0; i < n; i++)
		cur[a[i]] = i;	

	for (int i = r; i >= 0; i--) {
		if (sw.size() == 0) {
			p[i] = q[i] = 0;
			continue;
		}
		auto [u, v] = sw.back();
		sw.pop_back();
		p[i] = cur[u];
		q[i] = cur[v];

		swap(a[cur[u]], a[cur[v]]);
		swap(cur[u], cur[v]);

		swap(cur[a[x[i]]], cur[a[y[i]]]);
		swap(a[x[i]], a[y[i]]);
	}	
	return true;
}

int solve() {
	int lo = -2;
	int hi = m;
	while (hi - lo > 1) {
		int mid = (lo + hi) >> 1;
		if (check(mid))
			hi = mid;
		else
			lo = mid;
	}

	check(hi);
	return hi + 1;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N;
    m = M;
    for (int i = 0; i < n; i++)
    	s[i] = S[i];

    for (int i = 0; i < m; i++)
    	x[i] = X[i], y[i] = Y[i];

    int res = solve();
    for (int i = 0; i < res; i++) 
    	P[i] = p[i], Q[i] = q[i];

    for (int i = 0; i < res; i++) {
    	swap(s[x[i]], s[y[i]]);
    	swap(s[p[i]], s[q[i]]);
    }

    // for (int i = 0; i < n; i++)
    // 	cerr << s[i] << " \n"[i == n - 1];
	return res;
}

//#include "grader.cpp"

Compilation message

sorting.cpp: In function 'int rand(int, int)':
sorting.cpp:20:11: warning: conversion from 'std::mersenne_twister_engine<long unsigned int, 64, 312, 156, 31, 13043109905998158313, 29, 6148914691236517205, 17, 8202884508482404352, 37, 18444473444759240704, 43, 6364136223846793005>::result_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   20 |  return l + rd() % (r - l + 1);
      |         ~~^~~~~~~~~~~~~~~~~~~~
sorting.cpp: In function 'bool check(int)':
sorting.cpp:55:16: warning: declaration of 's' shadows a global declaration [-Wshadow]
   55 |   vector <int> s(1, j);
      |                ^
sorting.cpp:30:5: note: shadowed declaration is here
   30 | int s[N];
      |     ^
sorting.cpp:63:12: warning: declaration of 'j' shadows a previous local [-Wshadow]
   63 |   for (int j = 1; j < (int) s.size(); j++)
      |            ^
sorting.cpp:54:7: note: shadowed declaration is here
   54 |   int j = i;
      |       ^
sorting.cpp:67:16: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   67 |  if (sw.size() > r + 1)
      |      ~~~~~~~~~~^~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:107:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
  107 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:22:11: note: shadowed declaration is here
   22 | const int N = 6e5 + 5;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 440 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 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 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 440 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 2 ms 668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 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 2 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 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 2 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 250 ms 17764 KB Output is correct
16 Correct 282 ms 25952 KB Output is correct
17 Correct 286 ms 27308 KB Output is correct
18 Correct 148 ms 25728 KB Output is correct
19 Correct 241 ms 27580 KB Output is correct
20 Correct 262 ms 28428 KB Output is correct
21 Correct 253 ms 28372 KB Output is correct
22 Correct 267 ms 22612 KB Output is correct
23 Correct 292 ms 28800 KB Output is correct
24 Correct 301 ms 28348 KB Output is correct
25 Correct 326 ms 27632 KB Output is correct
26 Correct 252 ms 26572 KB Output is correct
27 Correct 226 ms 25744 KB Output is correct
28 Correct 283 ms 27748 KB Output is correct
29 Correct 314 ms 26864 KB Output is correct
30 Correct 201 ms 24964 KB Output is correct
31 Correct 345 ms 27536 KB Output is correct
32 Correct 267 ms 27532 KB Output is correct
33 Correct 308 ms 27888 KB Output is correct
34 Correct 304 ms 27564 KB Output is correct
35 Correct 256 ms 27092 KB Output is correct
36 Correct 131 ms 23932 KB Output is correct
37 Correct 331 ms 28664 KB Output is correct
38 Correct 316 ms 27556 KB Output is correct
39 Correct 323 ms 27760 KB Output is correct