Submission #599596

# Submission time Handle Problem Language Result Execution time Memory
599596 2022-07-19T16:15:38 Z beaconmc Sorting (IOI15_sorting) C++14
74 / 100
1000 ms 34856 KB
#include "sorting.h"
#include <bits/stdc++.h>


typedef long long ll;
using namespace std;


#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)

ll n,s[600001],m,x[600001],y[600001],p[600001],q[600001];
vector<vector<ll>> temp;

int check(ll k){
	vector<ll> sus;
	unordered_map<ll, ll> realsus;
	temp.clear();
	FOR(i,0,n){
		sus.push_back(s[i]);
		realsus[s[i]] = i;
	}

	FOR(i,0,k){
		swap(realsus[sus[x[i]]], realsus[sus[y[i]]]);
		swap(sus[x[i]], sus[y[i]]);
	}

	cout << endl;

	ll ans = 0;
	FOR(i,0,n){
		if (realsus[i]==i) continue;
		ans++;
		ll amogus = sus[i];
		temp.push_back({i, sus[i]});

		swap(sus[realsus[i]], sus[i]);
		swap(realsus[i], realsus[amogus]);
	}
	if (ans<k){
		FOR(i,0,k-ans){
			temp.push_back({0,0});
		}
	}
	if (ans <= k) return 1;
	else return 0;
}

void genans(ll k){
	vector<ll> sus;
	unordered_map<ll, ll> realsus;

	FOR(i,0,n){
		sus.push_back(s[i]);
		realsus[s[i]] = i;
	}


	FOR(i,0,k){
		swap(realsus[sus[x[i]]], realsus[sus[y[i]]]);
		swap(sus[x[i]], sus[y[i]]);

		p[i] = realsus[temp[i][0]];
		q[i] = realsus[temp[i][1]];

		swap(sus[realsus[temp[i][0]]], sus[realsus[temp[i][1]]]);
		swap(realsus[temp[i][0]], realsus[temp[i][1]]);
	}
}




int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N;
	FOR(i,0,n){
		s[i] = S[i]; 
	}
	m = M;
	FOR(i,0,m){
		x[i] = X[i];
		y[i] = Y[i];
	}
	ll lo = 0;
	ll hi = n+1;

	while (lo<hi){
		int mid = lo + (hi - lo) / 2;
		if (check(mid)) {
			hi = mid;
		} else {
			lo = mid + 1;
		}
	}
	check(lo);
	genans(lo);
	FOR(i,0,lo){
		P[i] = p[i];
		Q[i] = q[i];
	}

	return lo;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:90:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   90 |   int mid = lo + (hi - lo) / 2;
      |             ~~~^~~~~~~~~~~~~~~
sorting.cpp:100:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  100 |   P[i] = p[i];
      |          ~~~^
sorting.cpp:101:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  101 |   Q[i] = q[i];
      |          ~~~^
sorting.cpp:104:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  104 |  return lo;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 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 212 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 1 ms 212 KB Output is correct
9 Correct 0 ms 212 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 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 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 1 ms 212 KB Output is correct
9 Correct 0 ms 212 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 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 2 ms 724 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 724 KB Output is correct
2 Correct 5 ms 724 KB Output is correct
3 Correct 5 ms 724 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 5 ms 724 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 5 ms 724 KB Output is correct
13 Correct 5 ms 736 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 724 KB Output is correct
2 Correct 5 ms 724 KB Output is correct
3 Correct 5 ms 724 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 5 ms 724 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 5 ms 724 KB Output is correct
13 Correct 5 ms 736 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Execution timed out 1098 ms 34856 KB Time limit exceeded
16 Halted 0 ms 0 KB -