Submission #365472

# Submission time Handle Problem Language Result Execution time Memory
365472 2021-02-11T17:35:12 Z Sparky_09 Sorting (IOI15_sorting) C++17
100 / 100
178 ms 21356 KB
#include "sorting.h"
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;

void usaco(string s){
  freopen((s+".in").c_str(), "r", stdin);
  freopen((s+".out").c_str(), "w", stdout);
}

const int N = 6e5 + 9;
int C[N], n, *A, *B, pt[N], dy[N];
bool vis[N];

bool Check(int x){
	int cnt = 0, cb = 0;
	for (int i = 0; i < n; ++i) if (!vis[i]) {
		++cnt;
		for (int j = i; ; j = C[j]){
			vis[j] = 1;
			if (!vis[C[j]]) A[cb] = i, B[cb] = C[j], ++cb;
			else break;
		}
	}
	if(cb > x) return 0;
	for(int i = cb; i < x; ++i) A[i] = B[i] = 0;
	return 1;
}

int findSwapPairs(int NN, int S[], int M, int X[], int Y[], int P[], int Q[]){
  int l = 0, r = NN, mid; A = P; B = Q; n = NN;
	while (l <= r){
		mid = (l + r) >> 1;
		for(int i = 0; i < n; ++i) C[i] = S[i], vis[i] = 0;
		for(int i = 0; i < mid; ++i) swap(C[X[i]], C[Y[i]]);
		if(Check(mid)) r = mid - 1;
		else l = mid + 1;
	}
	for(int i = 0; i < n; ++i) C[i] = S[i], vis[i] = 0;
	for(int i = 0; i < l; ++i) swap(C[X[i]], C[Y[i]]);
	Check(l);
	for(int i = 0; i < n; ++i) pt[i] = dy[i] = i;
	for(int i = l - 1; ~i; --i){
		P[i] = dy[P[i]]; Q[i] = dy[Q[i]];
		swap(pt[X[i]], pt[Y[i]]);
		dy[pt[X[i]]] = X[i];
		dy[pt[Y[i]]] = Y[i];
	}
	return l;
}
/*
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
#ifdef LOCAL_DEFINE
	freopen("input.txt", "r", stdin);
#endif

}
*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:37:40: warning: unused parameter 'M' [-Wunused-parameter]
   37 | int findSwapPairs(int NN, int S[], int M, int X[], int Y[], int P[], int Q[]){
      |                                    ~~~~^
sorting.cpp: In function 'void usaco(std::string)':
sorting.cpp:14:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   14 |   freopen((s+".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sorting.cpp:15:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   15 |   freopen((s+".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 380 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 380 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 620 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 1 ms 620 KB Output is correct
24 Correct 1 ms 620 KB Output is correct
25 Correct 1 ms 620 KB Output is correct
26 Correct 1 ms 620 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 147 ms 18028 KB Output is correct
16 Correct 147 ms 18468 KB Output is correct
17 Correct 165 ms 19948 KB Output is correct
18 Correct 62 ms 16492 KB Output is correct
19 Correct 160 ms 18796 KB Output is correct
20 Correct 149 ms 19820 KB Output is correct
21 Correct 149 ms 19692 KB Output is correct
22 Correct 128 ms 14828 KB Output is correct
23 Correct 178 ms 20204 KB Output is correct
24 Correct 165 ms 20076 KB Output is correct
25 Correct 164 ms 20332 KB Output is correct
26 Correct 148 ms 19692 KB Output is correct
27 Correct 130 ms 18796 KB Output is correct
28 Correct 162 ms 19820 KB Output is correct
29 Correct 169 ms 20076 KB Output is correct
30 Correct 99 ms 18412 KB Output is correct
31 Correct 174 ms 20588 KB Output is correct
32 Correct 153 ms 19436 KB Output is correct
33 Correct 174 ms 20332 KB Output is correct
34 Correct 164 ms 20076 KB Output is correct
35 Correct 137 ms 18488 KB Output is correct
36 Correct 56 ms 17772 KB Output is correct
37 Correct 175 ms 21356 KB Output is correct
38 Correct 167 ms 20140 KB Output is correct
39 Correct 172 ms 20716 KB Output is correct