Submission #77375

# Submission time Handle Problem Language Result Execution time Memory
77375 2018-09-27T01:50:31 Z shoemakerjo Sorting (IOI15_sorting) C++14
100 / 100
460 ms 12032 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;
#define maxn 200010
#define pii pair<int, int>

int curp[maxn];
bool vis[maxn];
int myplate[maxn];
int plateind[maxn];

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    P[0] = 0;
	Q[0] = 0;
	int lo = 0;
	int hi = M;

	while (lo < hi) {
		int mid = (lo+hi)/2;
		for (int i = 0; i < N; i++) {
			curp[i] = i;
			vis[i] = false;
		}
		for (int i = 0; i < mid; i++) {
			swap(curp[X[i]], curp[Y[i]]);
		}
		int numcomp = 0;

		//i think the below is good (might be an issue though)
		for (int i = 0; i < N; i++) {
			if (vis[S[i]]) continue;
		
			int cur = S[i];
			vis[cur] = true;
			cur = S[curp[cur]]; //what is where I want to me

			while (cur != S[i]) {
				vis[cur] = true;
				cur = S[curp[cur]];
			}
			
			numcomp++;
		}
		int moves = N - numcomp;
		if (moves <= mid) {
			//we are good to go
			hi = mid;
		}
		else lo = mid+1; //going to have to go further
	}

	for (int i = 0; i < N; i++) {
		curp[i] = i;
		vis[i] = false;
	}
	for (int i = 0; i < lo; i++) {
		swap(curp[X[i]], curp[Y[i]]);
	}
	vector<pii> pts; //swap what plates that thing x and thing y are on
	for (int i = 0; i < N; i++) {
		if (vis[S[i]]) continue;
		int cur = S[i];
		vis[cur] = true;
		if (cur == S[curp[cur]]) continue;
		pts.push_back(pii(cur, S[curp[cur]]));
		cur = S[curp[cur]];
		while (cur != S[i]) {
			vis[cur] = true;
			int nextval = S[curp[cur]];
			if (nextval != S[i]) {
				pts.push_back(pii(cur, nextval));
			}
			cur = nextval;
		}
	}
	
	// cout << "here " << endl;

	for (int i = 0; i < N; i++) {
		myplate[S[i]] = i;
		plateind[i] = i;
		curp[i] = i;

	}
	for (int i = 0; i < lo; i++) {

		int p1 = curp[X[i]];
		int p2 = curp[Y[i]];
		swap(plateind[p1], plateind[p2]);
		swap(curp[X[i]], curp[Y[i]]);

		if (i >= pts.size()) {
			P[i] = Q[i] = 0;
		}
		else {
			P[i] = plateind[myplate[pts[i].first]];
			Q[i] = plateind[myplate[pts[i].second]];
			swap(myplate[pts[i].first], myplate[pts[i].second]);
		}
	}

	// cout << "RETURNING " << lo << endl;

	return lo;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:93:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i >= pts.size()) {
       ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 4 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 260 ms 10484 KB Output is correct
16 Correct 260 ms 10716 KB Output is correct
17 Correct 370 ms 11280 KB Output is correct
18 Correct 94 ms 7800 KB Output is correct
19 Correct 254 ms 10140 KB Output is correct
20 Correct 225 ms 10580 KB Output is correct
21 Correct 192 ms 10532 KB Output is correct
22 Correct 247 ms 11384 KB Output is correct
23 Correct 424 ms 11612 KB Output is correct
24 Correct 306 ms 11768 KB Output is correct
25 Correct 303 ms 11900 KB Output is correct
26 Correct 261 ms 10544 KB Output is correct
27 Correct 212 ms 9912 KB Output is correct
28 Correct 279 ms 11344 KB Output is correct
29 Correct 332 ms 11712 KB Output is correct
30 Correct 132 ms 9448 KB Output is correct
31 Correct 303 ms 11832 KB Output is correct
32 Correct 365 ms 11176 KB Output is correct
33 Correct 373 ms 11656 KB Output is correct
34 Correct 307 ms 11636 KB Output is correct
35 Correct 250 ms 10104 KB Output is correct
36 Correct 50 ms 8340 KB Output is correct
37 Correct 427 ms 12032 KB Output is correct
38 Correct 309 ms 11964 KB Output is correct
39 Correct 460 ms 11496 KB Output is correct