#ifdef LOCAL
#else
#include "sorting.h"
#endif
#include <bits/stdc++.h>
#define fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef double db;
int inf = 2e09; ll infll = 2e18; int mod = 1e09+7;
int findSwapPairs(int n, int s[], int m, int x[], int y[], int P[], int Q[]){
vector<int> t(n); //skalowanie
for(int i = 0; i < n; ++i) t[i] = s[i];
sort(t.begin(), t.end());
unordered_map<int, int> mp;
for(int i = 0; i < n; ++i) mp[t[i]] = i;
for(int i = 0; i < n; ++i) s[i] = mp[s[i]];
bool isSorted = 1; // sprawdzenie, czy ciąg jest już posortowany
for(int i = 1; i < n; ++i) if(s[i-1] > s[i]) isSorted = 0;
if(isSorted) return 0;
int l = 1, r = m, mid;
vector<int> where_to_put(n), pos(n), where_in_wtp(n);
while(1){
bool endnow = l == r;
mid = (l+r)>>1;
for(int i = 0; i < n; ++i) t[i] = s[i], pos[s[i]] = i, where_to_put[i] = i;
for(int i = 0; i < mid; ++i) swap(t[x[i]], t[y[i]]), swap(pos[t[x[i]]], pos[t[y[i]]]), swap(where_to_put[x[i]], where_to_put[y[i]]);
for(int i = 0; i < n; ++i) t[i] = s[i], pos[t[i]] = i, where_in_wtp[where_to_put[i]] = i;
/*for(int j = 0; j < n; ++j) printf("%d ", where_to_put[j]);
pn;
for(int j = 0; j < n; ++j) printf("%d ", where_in_wtp[j]);
pn;
pn;*/
int cur = 0, tmp;
for(int i = 0; i < mid; ++i){
//for(int j = 0; j < n; ++j) printf("%d ", t[j]);
//pn;
//printf("%d %d - %d %d\n", where_to_put[where_in_wtp[x[i]]], where_to_put[where_in_wtp[y[i]]], where_in_wtp[x[i]], where_in_wtp[y[i]]);
swap(pos[t[x[i]]], pos[t[y[i]]]);
swap(where_to_put[where_in_wtp[x[i]]], where_to_put[where_in_wtp[y[i]]]);
swap(where_in_wtp[x[i]], where_in_wtp[y[i]]);
swap(t[x[i]], t[y[i]]);
//for(int j = 0; j < n; ++j) printf("%d ", t[j]);
//pn;
while(cur < n){
if(pos[cur] == where_to_put[cur]) ++cur;
else{
P[i] = pos[cur], Q[i] = where_to_put[cur];
tmp = pos[cur];
swap(pos[cur], pos[t[where_to_put[cur]]]);
swap(t[tmp], t[where_to_put[cur]]);
break;
}
}
if(n <= cur) P[i] = 0, Q[i] = 0;
//printf("%d\n", cur);
}
if(endnow) break;
isSorted = 1;
for(int i = 1; i < n; ++i) if(t[i-1] > t[i]) isSorted = 0;
if(isSorted) r = mid;
else l = mid+1;
}
//printf("%d %d\n", n, m);
for(int i = 0; i < l; ++i) if(P[i] > Q[i]) swap(P[i], Q[i]);
return l;
}
#ifdef LOCAL
int main(){
//int T = 1;
//for(++T; --T; ) answer();
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
432 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
0 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
428 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
612 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
700 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
612 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
700 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
528 KB |
Output is correct |
15 |
Correct |
152 ms |
25972 KB |
Output is correct |
16 |
Correct |
178 ms |
26712 KB |
Output is correct |
17 |
Correct |
218 ms |
29896 KB |
Output is correct |
18 |
Correct |
51 ms |
23600 KB |
Output is correct |
19 |
Correct |
144 ms |
29480 KB |
Output is correct |
20 |
Correct |
156 ms |
29056 KB |
Output is correct |
21 |
Correct |
149 ms |
29052 KB |
Output is correct |
22 |
Correct |
151 ms |
23168 KB |
Output is correct |
23 |
Correct |
181 ms |
28744 KB |
Output is correct |
24 |
Correct |
224 ms |
28360 KB |
Output is correct |
25 |
Correct |
214 ms |
27932 KB |
Output is correct |
26 |
Correct |
169 ms |
29056 KB |
Output is correct |
27 |
Correct |
154 ms |
28544 KB |
Output is correct |
28 |
Correct |
203 ms |
28540 KB |
Output is correct |
29 |
Correct |
213 ms |
28080 KB |
Output is correct |
30 |
Correct |
132 ms |
29944 KB |
Output is correct |
31 |
Correct |
202 ms |
28640 KB |
Output is correct |
32 |
Correct |
165 ms |
27908 KB |
Output is correct |
33 |
Correct |
219 ms |
28416 KB |
Output is correct |
34 |
Correct |
187 ms |
28152 KB |
Output is correct |
35 |
Correct |
146 ms |
29052 KB |
Output is correct |
36 |
Correct |
77 ms |
28636 KB |
Output is correct |
37 |
Correct |
200 ms |
29052 KB |
Output is correct |
38 |
Correct |
207 ms |
28036 KB |
Output is correct |
39 |
Correct |
199 ms |
28220 KB |
Output is correct |