Gym 100971B Water & Fool

Source: Internet
Author: User

Description

Standard Input/output
Announcement
  • Statements

    A permutation of n numbers is a sequence of the integers from 1 to n where all number is occurred E Xactly once. If a permutation p1, P2, ..., pn have an index i su Ch that pi = i, this index is called a fixed point.

    A derangement is a permutation without any fixed points.

    Let's denote the operation swap(a, b) as swapping elements on positions a and b.

    For the given permutation find the minimal number of swaps operations needed to turn it into derangement.

Input

The first line contains an integer n(2≤ n ≤200000)-the number of elements in a Permuta tion.

The second line contains the elements of the permutation- n distinct integers from 1 to n .

Output

In the first line output a single integer k -the minimal number of swaps operations needed to TR Ansform the permutation into derangement.

In each of the next k lines output integers ai and bI (1≤ ai, bin)-the arguments of C23>swap operations.

If There is multiple possible solutions, output any of them.

Sample Input

Input
6
6 2 4 3 5 1
Output
1
2 5

Test instructions: Make I! =pi moves the fewest number of times and outputs the position of both of the interchanges

The puzzle: Find the so-called position to be exchanged if the number is even, then direct 22 exchange

When the first position is given, pay attention to the special sentence.
1#include <bits/stdc++.h>2 using namespacestd;3 intN;4 inta[200005];5 //int where[200005];6 intaa[200005];7 intMain ()8 {9scanf"%d",&n);Ten      for(intI=1; i<=n;i++) One         { Ascanf"%d",&a[i]); -             //where[a[i]]=i; -         } the     intans=0; -      for(intI=1; i<=n;i++) -     { -         if(a[i]==i) +         { -ans++; +aa[ans]=i; A         } at     } -     if(ans%2==0) -     { -cout<<ans/2<<Endl; -          for(intI=1; i<ans;i+=2) -cout<<aa[i]<<" "<<aa[i+1]<<Endl; in     } -     Else to     { +cout<<ans/2+1<<Endl; -          for(intI=1; i<ans;i+=2) thecout<<aa[i]<<" "<<aa[i+1]<<Endl; *         if(aa[ans]==1) $cout<<"1 2"<<Endl;Panax Notoginseng         Else -cout<<aa[ans]-1<<" "<<aa[ans]<<Endl; the     } +     return 0; A}

#include <bits/stdc++.h>
using namespaceStd;
intN;
intA[200005];
//int where[200005];
intAa[200005];
int Main()
{
scanf("%d",&N);
for(intI=1;I<=N;I++)
{
scanf("%d",&A[I]);
//where[a[i]]=i;
}
intAns=0;
for(intI=1;I<=N;I++)
{
if(A[I]==I)
{
Ans++;
Aa[Ans]=I;
}
}
if(Ans%2==0)
{
cout<<Ans/2<<Endl;
for(intI=1;I<Ans;I+=2)
cout<<Aa[I]<<" "<<Aa[I+1]<<Endl;
}
Else
{
cout<<Ans/2+1<<Endl;
for(intI=1;I<Ans;I+=2)
cout<<Aa[I]<<" "<<Aa[I+1]<<Endl;
if(Aa[Ans]==1)
cout<<"1 2"<<Endl;
Else
cout<<Aa[Ans]-1<<" "<<Aa[Ans]<<Endl;
}
return 0;
}

Gym 100971B Water & Fool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.