Gym 100971B 水&愚

Description

standard input/output
Announcement

  • Statements

    A permutation of n numbers is a sequence of integers from 1 to n where each number is occurred exactly once. If a permutation p1, p2, ..., pn has an index i such 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 swap operations needed to turn it into derangement.

Input

The first line contains an integer n(2 ≤ n ≤ 200000) — the number of elements in a permutation.

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 swap operations needed to transform the permutation into derangement.

In each of the next k lines output two integers ai and bi(1 ≤ ai, bi ≤ n) — the arguments of swap operations.

If there are multiple possible solutions, output any of them.

Sample Input

Input

66 2 4 3 5 1

Output

12 5

题意:使得i!=pi  移动最少的次数  并输出的交换的两者的位置

题解:找到需要交换的所谓的位置 若个数为偶数,则直接两两交换     若为奇数 则最后一个和前一个位置交换      当为的第一个位置时 注意特判。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 int a[200005];
 5 //int where[200005];
 6 int aa[200005];
 7 int main()
 8 {
 9     scanf("%d",&n);
10     for(int i=1;i<=n;i++)
11         {
12             scanf("%d",&a[i]);
13             //where[a[i]]=i;
14         }
15     int ans=0;
16     for(int i=1;i<=n;i++)
17     {
18         if(a[i]==i)
19         {
20             ans++;
21             aa[ans]=i;
22         }
23     }
24     if(ans%2==0)
25     {
26         cout<<ans/2<<endl;
27         for(int i=1;i<ans;i+=2)
28         cout<<aa[i]<<" "<<aa[i+1]<<endl;
29     }
30     else
31     {
32         cout<<ans/2+1<<endl;
33         for(int i=1;i<ans;i+=2)
34         cout<<aa[i]<<" "<<aa[i+1]<<endl;
35         if(aa[ans]==1)
36             cout<<"1 2"<<endl;
37         else
38             cout<<aa[ans]-1<<" "<<aa[ans]<<endl;
39     }
40     return 0;
41 }

#include<bits/stdc++.h>
using namespace std;
int n;
int a[200005];
//int where[200005];
int aa[200005];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            //where[a[i]]=i;
        }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==i)
        {
            ans++;
            aa[ans]=i;
        }
    }
    if(ans%2==0)
    {
        cout<<ans/2<<endl;
        for(int i=1;i<ans;i+=2)
        cout<<aa[i]<<" "<<aa[i+1]<<endl;
    }
    else
    {
        cout<<ans/2+1<<endl;
        for(int i=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;
}

时间: 2024-12-21 05:38:12

Gym 100971B 水&愚的相关文章

Gym 100989F 水&amp;愚&amp;vector

standard input/output You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the cheaters in the Algorithms course. N students cheated and failed this semester and they all want to know who Mahone is in order to take revenge! Agent Ma

Gym 100971C 水&amp;愚&amp;三角形

Description standard input/output Announcement Statements There is a set of n segments with the lengths li. Find a segment with an integer length so that it could form a non-degenerate triangle with any two segments from the set, or tell that such se

Gym 100971B Derangement

要求改换序列,使得没有位置是a[i] == i成立.输出最小要换的步数 首先把a[i] == i的位置记录起来,然后两两互相换就可以了. 对于是奇数的情况,和它前一个换或者后一个换就可以,(注意前一个越界或者后一个越界) 这样是不会重复的,因为本来i是a[i] == i的话,换了一个,是不会使得他们两个a[i] == i的 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath

UESTC 2016 Summer Training #1 Div.2

最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria (B) 不会捉 Gym 100989E 水 Gym 100989F 水 Mission in Amman (B) 没看题 Queue (A) 感觉题意理解得有问题啊 1 #include <iostream> 2 #include <cstdio> 3 #include <cstr

ecjtu-summer training #4

Gym - 100952A 水题,看谁的时间少谁就赢了,不然就是平局. 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 7 int main(){ 8 int n; 9 cin>>n; 10 while(n--){ 11 int h1,h2,m2,m1,s1,s2; 12 cin &

codeforces Gym 100187L L. Ministry of Truth 水题

L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description Andrey works in the Ministry of Truth. His work is changing articles in newspapers and magazines so that they praise the Party an

Codeforces gym 100685 C. Cinderella 水题

C. CinderellaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/C Description Cinderella is given a task by her Stepmother before she is allowed to go to the Ball. There are N (1 ≤ N ≤ 1000) bottles with water in th

水题 Gym 100553K Knockout Racing

题目传送门 1 /* 2 题意:有若干个点在一个区间内来回移动,1m/s. 3 水题:n^2的复杂度能解决,注意时间可能大于一个周期,要取模 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 const int MAXN = 1

UVaLive 6591 &amp;&amp; Gym 100299L Bus (水题)

题意:略. 析:不解释,水题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set>