Submission #474200

# Submission time Handle Problem Language Result Execution time Memory
474200 2021-09-17T10:46:03 Z jwvg0425 Sorting (IOI15_sorting) C++17
74 / 100
1000 ms 23876 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;

	for (int i = 0; i < m && !sorted(s); i++)
	{
		vector<int> rev(n), idx(n);
		swap(s[r[i].xx], s[r[i].yy]);
		for (int j = 0; j < n; j++)
			rev[j] = j;

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

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

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

			int j = find(all(s), idx[x]) - s.begin();

			res.emplace_back(x, j);
			swap(s[x], s[j]);
			f = true;
		}

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

	if (!sorted(s))
		res.clear();

	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())
		{
			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:66:33: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   66 |    int j = find(all(s), idx[x]) - s.begin();
      |            ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:116: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]
  116 |  for (int i = 0; i < res.size(); i++)
      |                  ~~^~~~~~~~~~~~
sorting.cpp:122:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  122 |  return res.size();
      |         ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 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 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 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 0 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 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 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 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 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 0 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 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 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 1 ms 204 KB Output is correct
21 Correct 193 ms 860 KB Output is correct
22 Correct 186 ms 788 KB Output is correct
23 Correct 204 ms 772 KB Output is correct
24 Correct 181 ms 768 KB Output is correct
25 Correct 190 ms 900 KB Output is correct
26 Correct 182 ms 812 KB Output is correct
27 Correct 183 ms 836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 283 ms 588 KB Output is correct
2 Correct 332 ms 580 KB Output is correct
3 Correct 319 ms 568 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 92 ms 564 KB Output is correct
6 Correct 114 ms 544 KB Output is correct
7 Correct 213 ms 588 KB Output is correct
8 Correct 337 ms 580 KB Output is correct
9 Correct 325 ms 660 KB Output is correct
10 Correct 346 ms 580 KB Output is correct
11 Correct 307 ms 556 KB Output is correct
12 Correct 197 ms 548 KB Output is correct
13 Correct 360 ms 584 KB Output is correct
14 Correct 6 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 283 ms 588 KB Output is correct
2 Correct 332 ms 580 KB Output is correct
3 Correct 319 ms 568 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 92 ms 564 KB Output is correct
6 Correct 114 ms 544 KB Output is correct
7 Correct 213 ms 588 KB Output is correct
8 Correct 337 ms 580 KB Output is correct
9 Correct 325 ms 660 KB Output is correct
10 Correct 346 ms 580 KB Output is correct
11 Correct 307 ms 556 KB Output is correct
12 Correct 197 ms 548 KB Output is correct
13 Correct 360 ms 584 KB Output is correct
14 Correct 6 ms 588 KB Output is correct
15 Execution timed out 1068 ms 23876 KB Time limit exceeded
16 Halted 0 ms 0 KB -