Submission #124471

# Submission time Handle Problem Language Result Execution time Memory
124471 2019-07-03T11:44:05 Z RockyB Sorting (IOI15_sorting) C++17
0 / 100
5 ms 504 KB
/// In The Name Of God

#include "sorting.h"
#include <bits/stdc++.h>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)

#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

const int MAXN = (int)2e5 + 7;

using namespace std;

int n, m;
int a[MAXN], b[MAXN], x[MAXN], y[MAXN];

bool check(int len) {
	rep(i, 0, n) {
		b[i] = a[i];
	}
	rep(i, 0, min(len, m)) {
		swap(b[x[i]], b[y[i]]);
	}
	int cnt = 0;
	rep(i, 0, n) {
		if (b[i] != i) {
			++cnt;
			swap(b[i], b[b[i]]);
		}
	}
	if (cnt > len) return 0;
	return 1;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	{
		n = N;
		m = M;
		rep(i, 0, n) {
			a[i] = S[i];
		}
		rep(i, 0, m) {
			x[i] = X[i];
			y[i] = Y[i];
		}
	} // copying
	int l = 1, r = n * 2, res = -1;	
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid)) res = mid, r = mid - 1;
		else l = mid + 1;
	}
	assert(res != -1);
	// opt len found

	rep(i, 0, n) {
		b[i] = a[i];
	}
	rep(i, 0, min(m, res)) {
		swap(b[x[i]], b[y[i]]);
	}
	vector <int> st;
	int go[n];
	rep(i, 0, n) {
		go[b[i]] = i;
	}
	rep(i, 0, n) {
		if (b[i] != i) {
			st.pb(b[i]);
			//swap(b[i], b[b[i]]);
		}
	}

	int ptr = 0;
	if (!sz(st)) {
		ptr = 1;
		P[ptr] = X[ptr];
		Q[ptr] = Y[ptr];
	}
	swap(a[X[ptr]], a[Y[ptr]]);
	for (auto x : st) {
		if (go[x] == x) {
			//cerr << "BAD -> " << x << nl;
			P[ptr] = 0;
			Q[ptr] = 0;

			/*
			cerr << " -> " << x << nl;
		rep(i, 0, n) cerr << a[i] << ' ';
		cerr << nl;
		rep(i, 0, n) cerr << go[i] << ' ';
		cerr << nl << nl;*/
			continue;
		}
		
	
		

		int p1 = find(a, a + n, x) - a;

		rep(i, 0, n) {
			if (go[i] == x) {
				int p2 = find(a, a + n, i) - a;
				P[ptr] = p1;
				Q[ptr] = p2;
				swap(go[x], go[i]);
				swap(a[p1], a[p2]);
				ptr++;
				if (ptr < m) {
					swap(a[X[ptr]], a[Y[ptr]]);
					///// don't use/// swap(go[a[X[ptr]]], go[a[Y[ptr]]]);
				}
				break;
			}
		}

		/*
		cerr << " -> " << x << nl;
		rep(i, 0, n) cerr << a[i] << ' ';
		cerr << nl;
		rep(i, 0, n) cerr << go[i] << ' ';
		cerr << nl << nl;
		*/
	}
	/*
	rep(i, 0, n) cerr << a[i] << ' ';
		cerr << nl;*/
	

	return res;
}

#ifdef IOI2018

namespace gen {

	int get(int x, bool ok = 1) {

	}
	void gen() {
		int N, M;
		N = rand() % 5 + 1;
		int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
		for (int i = 0; i < N; ++i)
			S[i] = i;
		random_shuffle(S, S + N);

		printf ("%d\n", N);
		int A[N];
		for (int i = 0; i < N; i++) {
				A[i] = S[i];
				printf ("%d ", A[i]);
		}
		printf ("\n");


		M = rand() % 5 + 1;
		printf ("%d\n", M);
		int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
		for (int i = 0; i < M; ++i) {
				X[i] = rand() % N;
				Y[i] = rand() % N;
				printf ("%d %d\n", X[i], Y[i]);
		}
		int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
		int ans = findSwapPairs(N, S, M, X, Y, P, Q);
		printf("%d\n", ans);
		assert(ans <= 30 * N);
		for (int i = 0; i < ans; ++i) {
					swap(A[X[i]], A[Y[i]]);
					swap(A[P[i]], A[Q[i]]);
			}
			for (int i = 0; i < N; i++) {
				assert(A[i] == i);
					printf("%d ", A[i]);
			}
	}

}

int main() {
	rep(i, 1, 100) {
		printf ("Case: %d\n", i);
		srand(i);
		gen :: gen();
		printf ("\n");
	}
}

#endif

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:95:12: warning: declaration of 'x' shadows a global declaration [-Wshadow]
  for (auto x : st) {
            ^
sorting.cpp:30:23: note: shadowed declaration is here
 int a[MAXN], b[MAXN], x[MAXN], y[MAXN];
                       ^
sorting.cpp:113:30: warning: conversion to 'int' from 'long int' may alter its value [-Wconversion]
   int p1 = find(a, a + n, x) - a;
            ~~~~~~~~~~~~~~~~~~^~~
sorting.cpp:117:32: warning: conversion to 'int' from 'long int' may alter its value [-Wconversion]
     int p2 = find(a, a + n, i) - a;
              ~~~~~~~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 504 KB Output isn't correct
2 Halted 0 ms 0 KB -