#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fs first
#define sc second
#define N 1000010
#define pb push_back
#define ii pair<ll,ll>
int a[N],ktr[N],f[N],pos[N];
vector<ii>tv;
void DFS(int u)
{
ktr[u]=1;
if(ktr[a[u]]==0)
DFS(a[u]);
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[])
{
int res=-1,l=0,r=m;
while(l<=r)
{
int mid=(l+r)/2;
// cout<<mid<<endl;
for(int i=0;i<n;i++)
a[i]=s[i];
for(int i=0;i<mid;i++)
swap(a[x[i]],a[y[i]]);
memset(ktr,0,sizeof(ktr));
int cnt=n;
for(int i=0;i<n;i++)
if(ktr[i]==0)
{
cnt--;
DFS(i);
}
if(cnt<=mid)
{
//cout<<"cc\n";
r=mid-1;
res=mid;
for(int i=0;i<n;i++)
f[i]=a[i];
}else
l=mid+1;
}
// for(int i=0;i<n;i++)cout<<f[i]<<" ";cout<<endl;
for(int i=0;i<n;i++)
pos[f[i]]=i;
for(int i=0;i<n;i++)
if(f[i]!=i)
{
int t=f[i];
// cout<<i<<" "<<t<<endl;
tv.pb({i,t});
swap(f[i],f[pos[i]]);
swap(pos[i],pos[t]);
}
// for(int i=0;i<n;i++)cout<<f[i]<<" ";cout<<endl;
for(int i=0;i<n;i++)
{
a[i]=s[i];
pos[a[i]]=i;
}
for(int i=0;i<res;i++)
{
if(i+1>tv.size())
{
p[i]=0;
q[i]=0;
swap(a[x[i]],a[y[i]]);
swap(pos[a[x[i]]],pos[a[y[i]]]);
continue;
}
// cout<<"cc\n"<<endl;
swap(a[x[i]],a[y[i]]);
swap(pos[a[x[i]]],pos[a[y[i]]]);
int u=pos[tv[i].fs],v=pos[tv[i].sc];
p[i]=u;
q[i]=v;
swap(a[u],a[v]);
swap(pos[a[u]],pos[a[v]]);
}
#ifdef SKY
for(int i=0;i<res;i++)cout<<p[i]<<" "<<q[i]<<endl;
for(int i=0;i<n;i++)cout<<a[i]<<" ";cout<<endl;
#endif // SKY
return res;
}
#ifdef SKY
int main()
{
freopen("A.inp","r",stdin);
freopen("A.out","w",stdout);
int n;
cin>>n;
int s[n];
for(int i=0;i<n;i++)
cin>>s[i];
int m;
cin>>m;
int x[m],y[m],p[m],q[m];
for(int i=0;i<m;i++)
{
cin>>x[i]>>y[i];
// cout<<x[i]<<" "<<y[i]<<endl;
}
cout<<findSwapPairs(n,s,m,x,y,p,q);
return 0;
}
#endif // SKY
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:71:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if(i+1>tv.size())
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
5 ms |
4180 KB |
Output is correct |
7 |
Correct |
3 ms |
4148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
5 ms |
4180 KB |
Output is correct |
7 |
Correct |
3 ms |
4148 KB |
Output is correct |
8 |
Correct |
3 ms |
4180 KB |
Output is correct |
9 |
Correct |
3 ms |
4180 KB |
Output is correct |
10 |
Correct |
3 ms |
4180 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4152 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
4 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
5 ms |
4180 KB |
Output is correct |
7 |
Correct |
3 ms |
4148 KB |
Output is correct |
8 |
Correct |
3 ms |
4180 KB |
Output is correct |
9 |
Correct |
3 ms |
4180 KB |
Output is correct |
10 |
Correct |
3 ms |
4180 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Correct |
3 ms |
4180 KB |
Output is correct |
13 |
Correct |
3 ms |
4152 KB |
Output is correct |
14 |
Correct |
3 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
4 ms |
4180 KB |
Output is correct |
18 |
Correct |
3 ms |
4180 KB |
Output is correct |
19 |
Correct |
3 ms |
4156 KB |
Output is correct |
20 |
Correct |
4 ms |
4180 KB |
Output is correct |
21 |
Correct |
4 ms |
4436 KB |
Output is correct |
22 |
Correct |
4 ms |
4436 KB |
Output is correct |
23 |
Correct |
5 ms |
4420 KB |
Output is correct |
24 |
Correct |
4 ms |
4416 KB |
Output is correct |
25 |
Correct |
4 ms |
4420 KB |
Output is correct |
26 |
Correct |
4 ms |
4436 KB |
Output is correct |
27 |
Correct |
4 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4436 KB |
Output is correct |
3 |
Correct |
6 ms |
4436 KB |
Output is correct |
4 |
Correct |
5 ms |
4308 KB |
Output is correct |
5 |
Correct |
4 ms |
4360 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
4 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4424 KB |
Output is correct |
9 |
Correct |
4 ms |
4376 KB |
Output is correct |
10 |
Correct |
4 ms |
4436 KB |
Output is correct |
11 |
Correct |
4 ms |
4436 KB |
Output is correct |
12 |
Correct |
5 ms |
4436 KB |
Output is correct |
13 |
Correct |
4 ms |
4436 KB |
Output is correct |
14 |
Correct |
4 ms |
4292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4436 KB |
Output is correct |
3 |
Correct |
6 ms |
4436 KB |
Output is correct |
4 |
Correct |
5 ms |
4308 KB |
Output is correct |
5 |
Correct |
4 ms |
4360 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
4 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4424 KB |
Output is correct |
9 |
Correct |
4 ms |
4376 KB |
Output is correct |
10 |
Correct |
4 ms |
4436 KB |
Output is correct |
11 |
Correct |
4 ms |
4436 KB |
Output is correct |
12 |
Correct |
5 ms |
4436 KB |
Output is correct |
13 |
Correct |
4 ms |
4436 KB |
Output is correct |
14 |
Correct |
4 ms |
4292 KB |
Output is correct |
15 |
Correct |
137 ms |
25428 KB |
Output is correct |
16 |
Correct |
153 ms |
25916 KB |
Output is correct |
17 |
Correct |
162 ms |
27152 KB |
Output is correct |
18 |
Correct |
67 ms |
19392 KB |
Output is correct |
19 |
Correct |
126 ms |
23800 KB |
Output is correct |
20 |
Correct |
137 ms |
24684 KB |
Output is correct |
21 |
Correct |
140 ms |
24792 KB |
Output is correct |
22 |
Correct |
130 ms |
22452 KB |
Output is correct |
23 |
Correct |
145 ms |
27960 KB |
Output is correct |
24 |
Correct |
168 ms |
27600 KB |
Output is correct |
25 |
Correct |
163 ms |
27260 KB |
Output is correct |
26 |
Correct |
134 ms |
24688 KB |
Output is correct |
27 |
Correct |
118 ms |
23700 KB |
Output is correct |
28 |
Correct |
161 ms |
27240 KB |
Output is correct |
29 |
Correct |
151 ms |
26472 KB |
Output is correct |
30 |
Correct |
99 ms |
22528 KB |
Output is correct |
31 |
Correct |
167 ms |
26948 KB |
Output is correct |
32 |
Correct |
150 ms |
27168 KB |
Output is correct |
33 |
Correct |
159 ms |
27636 KB |
Output is correct |
34 |
Correct |
196 ms |
27480 KB |
Output is correct |
35 |
Correct |
129 ms |
23756 KB |
Output is correct |
36 |
Correct |
56 ms |
20544 KB |
Output is correct |
37 |
Correct |
166 ms |
28284 KB |
Output is correct |
38 |
Correct |
209 ms |
27292 KB |
Output is correct |
39 |
Correct |
170 ms |
27464 KB |
Output is correct |