Submission #474213

# Submission time Handle Problem Language Result Execution time Memory
474213 2021-09-17T11:37:15 Z jwvg0425 Sorting (IOI15_sorting) C++17
100 / 100
676 ms 33916 KB
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#include "sorting.h"
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second

using namespace std;

template<typename T, typename Pr = less<T>>
using pq = priority_queue<T, vector<T>, Pr>;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

bool sorted(vector<int> s)
{
	for (int i = 0; i < s.size(); i++)
		if (s[i] != i)
			return false;

	return true;
}

vector<ii> solve(vector<int> s, vector<ii> r, int k)
{
	int n = s.size();
	r.resize(k);
	int m = r.size();
	vector<ii> res;

	vector<int> rev(n), idx(n), pos(n);
	for (int j = 0; j < n; j++)
		pos[s[j]] = j;

	for (int j = 0; j < n; j++)
		rev[j] = j;

	for (int j = 0; j < m; j++)
		swap(rev[r[j].xx], rev[r[j].yy]);

	for (int j = 0; j < n; j++)
		idx[rev[j]] = j;

	int nxt = 0;

	for (int i = 0; i < m; i++)
	{
		swap(s[r[i].xx], s[r[i].yy]);
		swap(pos[s[r[i].xx]], pos[s[r[i].yy]]);
		int a = idx[r[i].xx], b = idx[r[i].yy];
		swap(rev[a], rev[b]);
		swap(idx[rev[a]], idx[rev[b]]);

		// s[x] -> idx[x]
		bool f = false;
		while (nxt < n && !f)
		{
			int x = pos[nxt];
			if (s[x] == idx[x])
			{
				nxt++;
				continue;
			}

			int y = rev[nxt];
			res.emplace_back(x, y);
			swap(s[x], s[y]);
			swap(pos[s[x]], pos[s[y]]);
			f = true;
			nxt++;
		}

		if (!f)
			res.emplace_back(0, 0);
	}

	if (!sorted(s))
	{
		res.clear();
		res.emplace_back(-1, -1);
	}

	return res;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	vector<int> s(N);
	vector<ii> r(M);

	for (int i = 0; i < N; i++)
		s[i] = S[i];

	for (int i = 0; i < M; i++)
	{
		r[i].xx = X[i];
		r[i].yy = Y[i];
	}

	int lo = 0, hi = M;
	vector<ii> res;

	while (lo <= hi)
	{
		int mid = (lo + hi) / 2;
		auto now = solve(s, r, mid);

		if (!now.empty() && now[0].xx == -1)
		{
			lo = mid + 1;
		}
		else
		{
			res = now;
			hi = mid - 1;
		}
	}

	for (int i = 0; i < res.size(); i++)
	{
		P[i] = res[i].xx;
		Q[i] = res[i].yy;
	}

	return res.size();
}

Compilation message

sorting.cpp: In function 'bool sorted(std::vector<int>)':
sorting.cpp:32:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |  for (int i = 0; i < s.size(); i++)
      |                  ~~^~~~~~~~~~
sorting.cpp: In function 'std::vector<std::pair<int, int> > solve(std::vector<int>, std::vector<std::pair<int, int> >, int)':
sorting.cpp:41:16: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   41 |  int n = s.size();
      |          ~~~~~~^~
sorting.cpp:43:16: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   43 |  int m = r.size();
      |          ~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:134:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |  for (int i = 0; i < res.size(); i++)
      |                  ~~^~~~~~~~~~~~
sorting.cpp:140:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  140 |  return res.size();
      |         ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 2 ms 716 KB Output is correct
22 Correct 2 ms 716 KB Output is correct
23 Correct 2 ms 716 KB Output is correct
24 Correct 3 ms 716 KB Output is correct
25 Correct 3 ms 716 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 410 ms 22648 KB Output is correct
16 Correct 499 ms 30860 KB Output is correct
17 Correct 672 ms 32356 KB Output is correct
18 Correct 176 ms 30900 KB Output is correct
19 Correct 412 ms 32724 KB Output is correct
20 Correct 480 ms 33416 KB Output is correct
21 Correct 468 ms 33412 KB Output is correct
22 Correct 397 ms 27856 KB Output is correct
23 Correct 409 ms 33576 KB Output is correct
24 Correct 651 ms 33112 KB Output is correct
25 Correct 655 ms 32696 KB Output is correct
26 Correct 456 ms 33416 KB Output is correct
27 Correct 415 ms 32804 KB Output is correct
28 Correct 616 ms 33180 KB Output is correct
29 Correct 568 ms 32844 KB Output is correct
30 Correct 375 ms 32976 KB Output is correct
31 Correct 607 ms 33632 KB Output is correct
32 Correct 489 ms 32628 KB Output is correct
33 Correct 668 ms 33128 KB Output is correct
34 Correct 519 ms 32984 KB Output is correct
35 Correct 419 ms 32084 KB Output is correct
36 Correct 161 ms 33096 KB Output is correct
37 Correct 676 ms 33916 KB Output is correct
38 Correct 626 ms 32700 KB Output is correct
39 Correct 645 ms 32864 KB Output is correct