#include "sorting.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define mp make_pair
#define pub push_back
#define pob pop_back()
#define ss second
#define ff first
#define mt make_tuple
#define pof pop_front()
#define fbo find_by_order
#define ook order_of_key
#define lb lower_bound
#define ub upper_bound
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
using pll = pair <ll, ll>;
using pii = pair <int, int>;
int aiz[200001], n, x[600001], y[600001], s[200001], dta[200001];
vector <pii> ans;
bool can(int k)
{
ans.clear();
for (int i = 0; i < n; i++)
aiz[i] = s[i];
for (int i = 0; i < k; i++)
swap(aiz[x[i]], aiz[y[i]]);
for (int i = 0; i < n; i++)
dta[aiz[i]] = i;
int p = 0;
for (int i = 0; i < n; i++)
{
if ( dta[i] != i )
{
int tm = aiz[i];
p++;
ans.pub(mp(i, tm));
swap(aiz[dta[i]], aiz[dta[tm]]);
swap(dta[i], dta[tm]);
}
}
if ( p <= k )
return 1;
return 0;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l = 0, r = M;
n = N;
for (int i = 0; i < M; i++)
{
x[i] = X[i];
y[i] = Y[i];
}
for (int i = 0; i < n; i++)
s[i] = S[i];
while (l + 1 < r)
{
int mid = (l + r) / 2;
if ( can(mid) )
r = mid;
else
l = mid;
}
if ( can(l) )
{
for (int i = 0; i < n; i++)
aiz[i] = s[i];
for(int i = 0 ; i < n; i++)
dta[aiz[i]] = i;
for(int i = 0; i < l; i++)
{
swap(aiz[x[i]], aiz[y[i]]);
swap(dta[aiz[x[i]]],dta[aiz[y[i]]]);
if ( i < ans.size() )
{
Q[i] = dta[ans[i].ss];
P[i] = dta[ans[i].ff];
} else {
P[i] = Q[i] = 0;
}
swap(aiz[dta[ans[i].ff]],aiz[dta[ans[i].ss]]);
swap(dta[ans[i].ff],dta[ans[i].ss]);
}
return l;
} else {
can(r);
for (int i = 0; i < n; i++)
aiz[i] = s[i];
for(int i = 0 ; i < n; i++)
dta[aiz[i]] = i;
for(int i = 0; i < r; i++)
{
swap(aiz[x[i]], aiz[y[i]]);
swap(dta[aiz[x[i]]],dta[aiz[y[i]]]);
if ( i < ans.size() )
{
Q[i] = dta[ans[i].ss];
P[i] = dta[ans[i].ff];
} else {
P[i] = Q[i] = 0;
}
swap(aiz[dta[ans[i].ff]],aiz[dta[ans[i].ss]]);
swap(dta[ans[i].ff],dta[ans[i].ss]);
}
return r;
}
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:98:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if ( i < ans.size() )
~~^~~~~~~~~~~~
sorting.cpp:125:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if ( i < ans.size() )
~~^~~~~~~~~~~~
# |
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 |
380 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 |
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 |
380 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 |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 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 |
380 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 |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
504 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 |
3 ms |
636 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
3 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
197 ms |
24312 KB |
Output is correct |
16 |
Correct |
203 ms |
24808 KB |
Output is correct |
17 |
Correct |
280 ms |
26340 KB |
Output is correct |
18 |
Correct |
85 ms |
22124 KB |
Output is correct |
19 |
Correct |
191 ms |
24680 KB |
Output is correct |
20 |
Correct |
199 ms |
25536 KB |
Output is correct |
21 |
Correct |
195 ms |
25464 KB |
Output is correct |
22 |
Correct |
194 ms |
21608 KB |
Output is correct |
23 |
Correct |
217 ms |
27156 KB |
Output is correct |
24 |
Correct |
255 ms |
26728 KB |
Output is correct |
25 |
Correct |
281 ms |
26600 KB |
Output is correct |
26 |
Correct |
200 ms |
24940 KB |
Output is correct |
27 |
Correct |
174 ms |
23916 KB |
Output is correct |
28 |
Correct |
248 ms |
26624 KB |
Output is correct |
29 |
Correct |
228 ms |
25804 KB |
Output is correct |
30 |
Correct |
142 ms |
23024 KB |
Output is correct |
31 |
Correct |
235 ms |
26344 KB |
Output is correct |
32 |
Correct |
216 ms |
26180 KB |
Output is correct |
33 |
Correct |
250 ms |
26748 KB |
Output is correct |
34 |
Correct |
220 ms |
26728 KB |
Output is correct |
35 |
Correct |
184 ms |
24296 KB |
Output is correct |
36 |
Correct |
69 ms |
21312 KB |
Output is correct |
37 |
Correct |
260 ms |
27508 KB |
Output is correct |
38 |
Correct |
242 ms |
26472 KB |
Output is correct |
39 |
Correct |
243 ms |
26688 KB |
Output is correct |