Submission #599604

# Submission time Handle Problem Language Result Execution time Memory
599604 2022-07-19T16:20:32 Z beaconmc Sorting (IOI15_sorting) C++14
74 / 100
1000 ms 29244 KB
#pragma GCC optimize("O3")
#include "sorting.h"
#include <bits/stdc++.h>


typedef long long ll;
#define ll int
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;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 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 340 KB Output is correct
5 Correct 0 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 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 596 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 6 ms 596 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 5 ms 616 KB Output is correct
9 Correct 6 ms 596 KB Output is correct
10 Correct 6 ms 596 KB Output is correct
11 Correct 6 ms 664 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 644 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 596 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 6 ms 596 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 5 ms 616 KB Output is correct
9 Correct 6 ms 596 KB Output is correct
10 Correct 6 ms 596 KB Output is correct
11 Correct 6 ms 664 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 644 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Execution timed out 1085 ms 29244 KB Time limit exceeded
16 Halted 0 ms 0 KB -