Submission #96896

# Submission time Handle Problem Language Result Execution time Memory
96896 2019-02-12T16:22:36 Z figter001 Sorting (IOI15_sorting) C++14
100 / 100
259 ms 20456 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 2e5+50;

vector<int> a;
vector<pair<int,int>> s;
int idx[maxn];

bool can(int md){
	vector<int> b = a;
	for(int i=0;i<md;i++)
		swap(b[s[i].first],b[s[i].second]);
	int cnt = 0;
	for(int i=0;i<a.size();i++){
		while(b[i] != i){
			cnt++;
			swap(b[i],b[b[i]]);
		}
	}
	return cnt <= md;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    for(int i=0;i<N;i++)a.push_back(S[i]);
    for(int i=0;i<M;i++)s.push_back({X[i],Y[i]});
    int md,lo=0,hi = M,ans;
	while(lo <= hi){
		md = (lo + hi)/2;
		if(can(md)){
			hi = md-1;
			ans = md;
		}else lo = md+1;
	}
	vector<int> b = a;
	for(int i=0;i<ans;i++)swap(a[s[i].first],a[s[i].second]);
	vector<pair<int,int>> c;
	for(int i=0;i<a.size();i++){
		while(a[i] != i){
			c.push_back({a[a[i]],a[i]});
			swap(a[i],a[a[i]]);
		}
	}
	while(c.size() < ans)c.push_back({0,0});
	a = b;
	for(int i=0;i<N;i++)idx[a[i]] = i;
	for(int i=0;i<ans;i++){
		int l = s[i].first;
		int r = s[i].second;
		swap(idx[a[l]],idx[a[r]]);
		swap(a[l],a[r]);
		l = c[i].first;
		r = c[i].second;
		P[i] = idx[l];
		Q[i] = idx[r];
		swap(a[P[i]],a[Q[i]]);
		swap(idx[l],idx[r]);
	}
	return ans;
}

Compilation message

sorting.cpp: In function 'bool can(int)':
sorting.cpp:18:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<a.size();i++){
              ~^~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:41:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<a.size();i++){
              ~^~~~~~~~~
sorting.cpp:47:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(c.size() < ans)c.push_back({0,0});
        ~~~~~~~~~^~~~~
sorting.cpp:30:24: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
     int md,lo=0,hi = M,ans;
                        ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 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 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 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 256 KB Output is correct
8 Correct 2 ms 356 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 300 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 3 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 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 256 KB Output is correct
8 Correct 2 ms 356 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 300 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 256 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 556 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 3 ms 512 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 4 ms 512 KB Output is correct
12 Correct 4 ms 576 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 556 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 3 ms 512 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 4 ms 512 KB Output is correct
12 Correct 4 ms 576 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 184 ms 17716 KB Output is correct
16 Correct 190 ms 18104 KB Output is correct
17 Correct 256 ms 19236 KB Output is correct
18 Correct 73 ms 14556 KB Output is correct
19 Correct 151 ms 16208 KB Output is correct
20 Correct 166 ms 16756 KB Output is correct
21 Correct 177 ms 16836 KB Output is correct
22 Correct 196 ms 19212 KB Output is correct
23 Correct 213 ms 19640 KB Output is correct
24 Correct 229 ms 19376 KB Output is correct
25 Correct 234 ms 19672 KB Output is correct
26 Correct 159 ms 16568 KB Output is correct
27 Correct 133 ms 15900 KB Output is correct
28 Correct 249 ms 19148 KB Output is correct
29 Correct 234 ms 19092 KB Output is correct
30 Correct 98 ms 14820 KB Output is correct
31 Correct 212 ms 19540 KB Output is correct
32 Correct 198 ms 19012 KB Output is correct
33 Correct 237 ms 19768 KB Output is correct
34 Correct 173 ms 19436 KB Output is correct
35 Correct 158 ms 16028 KB Output is correct
36 Correct 64 ms 14920 KB Output is correct
37 Correct 259 ms 20456 KB Output is correct
38 Correct 233 ms 19476 KB Output is correct
39 Correct 240 ms 19932 KB Output is correct