# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
138329 |
2019-07-29T18:32:56 Z |
Sorting |
Sorting (IOI15_sorting) |
C++14 |
|
272 ms |
24388 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;
}
void make_swap(int l, int r){
swap(tmp2[l], tmp2[r]);
swap(where[ tmp2[l] ], where[ tmp2[r] ]);
}
void dfs_ans (int u){
//cout << "dfs_ans " << u << endl;
//cout << tmp[u] << endl;
used[u] = true;
if(!used[ tmp[u] ]){
dfs_ans(tmp[u]);
make_swap(x[timer], y[timer]);
p[timer] = where[ tmp[u] ];
q[timer] = where[ tmp[tmp[u]] ];
swap(tmp[u], tmp[tmp[u]]);
//cout << "swap " << tmp[u] << " " << tmp[tmp[u]] << endl;
make_swap(p[timer], q[timer]);
++timer;
}
}
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;
while(l != r){
int mid = (l + r) >> 1;
if(check(mid)){
r = mid;
}
else{
l = mid + 1;
}
}
find_ans(l);
return l;
}
/*int _s[N], _x[N], _y[N], _p[N], _q[N];
int main(){
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> _s[i];
}
for(int i = 0; i < m; i++){
cin >> _x[i] >> _y[i];
}
int ans = findSwapPairs(n, _s, m, _x, _y, _p, _q);
cout << ans << "\n";
for(int i = 0; i < ans; i++){
cout << _p[i] << " " << _q[i] << "\n";
}
return 0;
}*/
/*
5 6
1 2 3 4 0
0 0
0 0
0 0
0 0
0 0
0 0
*/
Compilation message
sorting.cpp: In function 'void find_ans(int)':
sorting.cpp:89: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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
408 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
408 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
656 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
508 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
504 KB |
Output is correct |
8 |
Correct |
4 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
504 KB |
Output is correct |
8 |
Correct |
4 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
221 ms |
19448 KB |
Output is correct |
16 |
Correct |
219 ms |
21368 KB |
Output is correct |
17 |
Correct |
241 ms |
21496 KB |
Output is correct |
18 |
Correct |
75 ms |
15864 KB |
Output is correct |
19 |
Correct |
193 ms |
18728 KB |
Output is correct |
20 |
Correct |
194 ms |
19576 KB |
Output is correct |
21 |
Correct |
180 ms |
19544 KB |
Output is correct |
22 |
Correct |
191 ms |
16760 KB |
Output is correct |
23 |
Correct |
219 ms |
23032 KB |
Output is correct |
24 |
Correct |
238 ms |
22788 KB |
Output is correct |
25 |
Correct |
272 ms |
21624 KB |
Output is correct |
26 |
Correct |
177 ms |
19292 KB |
Output is correct |
27 |
Correct |
157 ms |
18680 KB |
Output is correct |
28 |
Correct |
241 ms |
23036 KB |
Output is correct |
29 |
Correct |
245 ms |
20088 KB |
Output is correct |
30 |
Correct |
112 ms |
18040 KB |
Output is correct |
31 |
Correct |
222 ms |
20432 KB |
Output is correct |
32 |
Correct |
221 ms |
22392 KB |
Output is correct |
33 |
Correct |
233 ms |
21740 KB |
Output is correct |
34 |
Correct |
221 ms |
20728 KB |
Output is correct |
35 |
Correct |
173 ms |
18564 KB |
Output is correct |
36 |
Correct |
58 ms |
17016 KB |
Output is correct |
37 |
Correct |
243 ms |
24388 KB |
Output is correct |
38 |
Correct |
230 ms |
23416 KB |
Output is correct |
39 |
Correct |
240 ms |
23672 KB |
Output is correct |