Submission #48229

#TimeUsernameProblemLanguageResultExecution timeMemory
48229cheater2kSorting (IOI15_sorting)C++17
20 / 100
4 ms640 KiB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;

int n, m;
int s[MAXN];
int pa[MAXN], pb[MAXN], b[MAXN];
int x[MAXN * 3], y[MAXN * 3];
vector < pair<int,int> > vres;

bool check(int k) {
	vres.clear();
	for (int i = 0; i < n; ++i) {
		b[i] = i;
	}
	for (int i = 0; i < m; ++i) {
		swap(b[x[i]], b[y[i]]);
	}

	// the swaps must satisfy: s[i] = b[i]
	for (int i = 0; i < n; ++i) {
		pb[s[i]] = b[i];
		pa[b[i]] = s[i];
	}

	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		if (pa[i] != i) {
			++cnt;
			// TODO: swap i, pb[i]
			int j = pb[i];
			vres.push_back(make_pair(i, j));
			int p = pa[i], q = pa[j];
			swap(pa[i], pa[j]);
			pb[q] = i;
			pb[p] = j;
		}
	}

	return cnt <= k;
}

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 low = 0, high = m;
  while(low < high) {
  	int mid = ((low + high) >> 1);
  	if (check(mid)) high = mid;
  	else low = mid + 1;
  }

  check(low);
  for (int i = 0; i < low; ++i) {
  	P[i] = vres[i].first;
  	Q[i] = vres[i].second;
  }
  return low;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...