Submission #567800

# Submission time Handle Problem Language Result Execution time Memory
567800 2022-05-24T08:28:39 Z ngpin04 Sorting (IOI15_sorting) C++17
54 / 100
2 ms 760 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 = 2e5 + 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 = -1;
	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 = 2e5 + 5;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 316 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 1 ms 340 KB Output is correct
2 Correct 1 ms 340 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 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 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 308 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 760 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 704 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Incorrect 2 ms 576 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Incorrect 2 ms 576 KB Output isn't correct
5 Halted 0 ms 0 KB -