Submission #241862

# Submission time Handle Problem Language Result Execution time Memory
241862 2020-06-26T02:53:10 Z Nightlight Sorting (IOI15_sorting) C++14
100 / 100
182 ms 29792 KB
#include <bits/stdc++.h>
using namespace std;

int N, M;
int pos[200005];
int S[200005], A[200005];
int X[600005], Y[600005];
int P[600005], Q[600005];
int SA[600005], SB[600005];
int res;
int pp;
bool qq;
void balance(int now) {
	while(pp <= now) swap(S[X[pp]], S[Y[pp]]), pp++;
	while(pp > now + 1) swap(S[X[pp - 1]], S[Y[pp - 1]]), pp--;
}

void SWAP(int i, int j) {
  int ii = pos[A[i]];
  int jj = pos[A[j]];
  pos[A[i]] = jj;
  pos[A[j]] = ii;
  swap(A[i], A[j]);
}

bool trysort(int limit) {
	int cnt = 0;
//	cout << limit << "\n";
	for(int i = 0; i < N; i++) {
		A[i] = S[i];
//		cout << A[i] << " ";
		pos[A[i]] = i;
	}
//	cout << "\n";
	for(int i = 0; i < N; i++) {
		if(A[i] != i) {
			SA[cnt] = i, SB[cnt] = A[i];
			cnt++;
			SWAP(i, pos[i]);
//			if(qq)for(int j = 0; j < N; j++) cout << A[j] << " ";
//      if(qq)cout << "\n";
		}
	}
	for(int i = cnt; i < limit; i++) {
		SA[i] = 0, SB[i] = 0;
	}
//	cout << cnt << "\n\n";
	return cnt <= limit;
}

void findans() {
  for(int i = res - 1; i >= 0; i--) {
    P[i] = pos[SA[i]], Q[i] = pos[SB[i]];
    SWAP(P[i], Q[i]);
    SWAP(X[i + 1], Y[i + 1]);
  }
}

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 = 1; i <= M; i++) X[i] = x[i - 1], Y[i] = y[i - 1];
	int l = 0, r = M;
  while(l <= r) {
		int mid = (l + r) >> 1;
		balance(mid);
		if(trysort(mid)) {
			res = mid;
			r = mid - 1;
		}else l = mid + 1;
	}
	balance(res);
	qq = 1;
	trysort(res);
  findans();
	for(int i = 0; i < res; i++) {
		p[i] = P[i], q[i] = Q[i];
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 416 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 416 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 6 ms 768 KB Output is correct
22 Correct 6 ms 768 KB Output is correct
23 Correct 6 ms 768 KB Output is correct
24 Correct 6 ms 768 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 816 KB Output is correct
27 Correct 6 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 640 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 768 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 640 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 768 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 155 ms 26232 KB Output is correct
16 Correct 147 ms 26872 KB Output is correct
17 Correct 175 ms 28408 KB Output is correct
18 Correct 85 ms 22040 KB Output is correct
19 Correct 139 ms 26232 KB Output is correct
20 Correct 150 ms 27000 KB Output is correct
21 Correct 148 ms 27024 KB Output is correct
22 Correct 148 ms 23800 KB Output is correct
23 Correct 167 ms 29432 KB Output is correct
24 Correct 167 ms 29048 KB Output is correct
25 Correct 174 ms 28712 KB Output is correct
26 Correct 145 ms 27000 KB Output is correct
27 Correct 130 ms 25976 KB Output is correct
28 Correct 172 ms 28664 KB Output is correct
29 Correct 170 ms 28028 KB Output is correct
30 Correct 115 ms 25180 KB Output is correct
31 Correct 172 ms 28664 KB Output is correct
32 Correct 167 ms 28408 KB Output is correct
33 Correct 179 ms 28920 KB Output is correct
34 Correct 177 ms 28920 KB Output is correct
35 Correct 139 ms 25848 KB Output is correct
36 Correct 70 ms 23804 KB Output is correct
37 Correct 179 ms 29792 KB Output is correct
38 Correct 172 ms 28680 KB Output is correct
39 Correct 182 ms 28736 KB Output is correct