# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
705387 |
2023-03-04T08:30:31 Z |
bin9638 |
Sorting (IOI15_sorting) |
C++17 |
|
5 ms |
4436 KB |
#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=0,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)
{
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]=x[i];
q[i]=y[i];
continue;
}
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:70: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]
70 | if(i+1>tv.size())
| ~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4152 KB |
Output is correct |
2 |
Correct |
2 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 |
3 ms |
4288 KB |
Output is correct |
6 |
Incorrect |
3 ms |
4156 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 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 |
2 ms |
4152 KB |
Output is correct |
14 |
Correct |
2 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 |
3 ms |
4288 KB |
Output is correct |
18 |
Incorrect |
3 ms |
4156 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4436 KB |
Output is correct |
2 |
Correct |
4 ms |
4388 KB |
Output is correct |
3 |
Incorrect |
5 ms |
4360 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4436 KB |
Output is correct |
2 |
Correct |
4 ms |
4388 KB |
Output is correct |
3 |
Incorrect |
5 ms |
4360 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |