Submission #138310

# Submission time Handle Problem Language Result Execution time Memory
138310 2019-07-29T17:43:03 Z Sorting Sorting (IOI15_sorting) C++14
0 / 100
3 ms 508 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

int n, m;
int *s, *x, *y, *p, *q;
int tmp[N], tmp2[N], where[N];
bool used[N];

int timer = 0;

int dfs(int u){
	used[u] = true;

	int cnt = 1;

	if(!used[ tmp[u] ]){
		cnt += dfs(tmp[u]);
	}

	return cnt;
}

int dfs_ans (int u){
	used[u] = true;

	int cnt = 1;

	if(!used[ tmp[u] ]){
		dfs(tmp[u]);

		swap(where[ tmp2[x[u]] ], where[ tmp2[y[u]] ]);
		swap(tmp2[x[u]], tmp2[y[u]]);

		p[timer] = where[tmp[u]];
		q[timer++] = where[u];

	}

	return cnt;
}

bool check(int mid){
	for(int i = 0; i < n; i++){
		tmp[i] = s[i]; 
		used[i] = false;
	}

	for(int i = 0; i < mid; i++){
		swap(tmp[ x[i] ], tmp[ y[i] ]);
	}

	int cnt = 0;

	for(int i = 0; i < n; i++){
		if(!used[i]){
			cnt += dfs(i) - 1;
		}
	}

	return (cnt <= mid);
}

void find_ans(int mid){
	for(int i = 0; i < n; i++){
		tmp[i] = s[i]; 
		tmp2[i] = s[i];
		where[s[i]] = i;
		used[i] = false;
	}

	for(int i = 0; i < mid; i++){
		swap(tmp[ x[i] ], tmp[ y[i] ]);
		//swap(where[ s[x[i]] ], where[ s[y[i]] ]);
	}

	int cnt = 0;

	for(int i = 0; i < n; i++){
		if(!used[i]){
			dfs_ans(i);
		}
	}

	while(timer < mid){
		p[timer] = 0;
		q[timer++] = 0;
	}
}

int findSwapPairs(int _n, int _s[], int _m, int _x[], int _y[], int _p[], int _q[]){
	n = _n, s = _s, m = _m, x = _x, y = _y, p = _p, q = _q;

	int l = 0, r = m - 1;

	while(l != r){
		int mid = (l + r) >> 1;

		if(check(mid)){
			r = mid;
		}
		else{
			l = mid + 1;
		}
	}

	find_ans(l);

	return l;
}

Compilation message

sorting.cpp: In function 'void find_ans(int)':
sorting.cpp:79:6: warning: unused variable 'cnt' [-Wunused-variable]
  int cnt = 0;
      ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Incorrect 2 ms 380 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Incorrect 2 ms 380 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Incorrect 2 ms 380 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 508 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 508 KB Output isn't correct
2 Halted 0 ms 0 KB -