This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
int ans1[6000010],v[3000010],r,n,ans2[6000010],ans,poz[3000010];
int main()
{
int t;
cin>>t;
++t;
while(--t)
{
int ver=0;
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>v[i];
poz[v[i]]=i;
}
n/=2;
for(int i=1;i<=n;++i)
{
if(v[i]+v[2*n+1-i]!=2*n+1)
{
ver=1;
break;
}
if(v[i]>n)
++ver;
}
if(ver%2==1)
{
cout<<-1<<'\n';
continue;
}
ans=0;
ver=0;
for(int i=1;i<=n;++i)
{
if(v[i]>n)
{
if(poz[i]>n&&poz[i]+i!=2*n+1)
{
++ans;
ans1[ans]=poz[i];
ans2[ans]=i;
swap(v[i],v[poz[i]]);
swap(v[2*n+1-i],v[2*n+1-poz[i]]);
swap(poz[v[2*n+1-i]],poz[v[2*n+1-poz[i]]]);
swap(poz[v[i]],poz[poz[i]]);
}
else if(poz[i]<=n&&poz[poz[i]]>n&&i+poz[poz[i]]!=n*2+1)
{
++ans;
ans1[ans]=poz[i];
ans2[ans]=i;
swap(v[i],v[poz[poz[i]]]);
swap(v[2*n+1-i],v[2*n+1-poz[poz[i]]]);
swap(poz[v[2*n+1-i]],poz[v[2*n+1-poz[poz[i]]]]);
swap(poz[v[i]],poz[poz[poz[i]]]);
}
}
}
for(int i=1;i<=n;++i)
{
if(v[i]>n)
{
if(ver==0)
ver=i;
else
{
++ans;
ans1[ans]=ver;
ans2[ans]=2*n-i+1;
swap(v[i],v[2*n-ver+1]);
swap(v[ver],v[2*n-i+1]);
swap(poz[v[i]],poz[v[2*n-ver+1]]);
swap(poz[v[ver]],poz[v[2*n-i+1]]);
ver=0;
}
}
}
for(int i=1;i<=n;++i)
{
while(v[i]!=i)
{
++ans;
ans1[ans]=v[i];
ans2[ans]=i;
swap(v[i],v[v[i]]);
}
}
cout<<ans<<' '<<ans<<'\n';
for(int i=1;i<=ans;++i)
cout<<ans1[i]<<' '<<ans2[i]<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |