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
6
6 2 4 3 5 1
Output
1
2 5 题意:使得i!=pi 移动最少的次数 并输出的交换的两者的位置 题解:找到需要交换的所谓的位置 若个数为偶数,则直接两两交换
若为奇数 则最后一个和前一个位置交换
当为的第一个位置时 注意特判。
 #include<bits/stdc++.h>
using namespace std;
int n;
int a[];
//int where[200005];
int aa[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
//where[a[i]]=i;
}
int ans=;
for(int i=;i<=n;i++)
{
if(a[i]==i)
{
ans++;
aa[ans]=i;
}
}
if(ans%==)
{
cout<<ans/<<endl;
for(int i=;i<ans;i+=)
cout<<aa[i]<<" "<<aa[i+]<<endl;
}
else
{
cout<<ans/+<<endl;
for(int i=;i<ans;i+=)
cout<<aa[i]<<" "<<aa[i+]<<endl;
if(aa[ans]==)
cout<<"1 2"<<endl;
else
cout<<aa[ans]-<<" "<<aa[ans]<<endl;
}
return ;
}

#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;
}

Gym 100971B 水&愚的更多相关文章

  1. Gym 100989F 水&愚&vector

    standard input/output You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the che ...

  2. Gym 100971C 水&愚&三角形

    Description standard input/output Announcement   Statements There is a set of n segments with the le ...

  3. Gym 100971B Derangement

    要求改换序列,使得没有位置是a[i] == i成立.输出最小要换的步数 首先把a[i] == i的位置记录起来,然后两两互相换就可以了. 对于是奇数的情况,和它前一个换或者后一个换就可以,(注意前一个 ...

  4. UESTC 2016 Summer Training #1 Div.2

    最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria ( ...

  5. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  6. codeforces Gym 100187L L. Ministry of Truth 水题

    L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  7. Codeforces gym 100685 C. Cinderella 水题

    C. CinderellaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/C ...

  8. 水题 Gym 100553K Knockout Racing

    题目传送门 /* 题意:有若干个点在一个区间内来回移动,1m/s. 水题:n^2的复杂度能解决,注意时间可能大于一个周期,要取模 */ #include <cstdio> #include ...

  9. CodeForces Gym 100685C Cinderella (水题)

    题意:给定 n 个杯子,里面有不同体积的水,然后问你要把所有的杯子的水的体积都一样,至少要倒少多少个杯子. 析:既然最后都一样,那么先求平均数然后再数一下,哪个杯子的开始的体积就大于平均数,这是一定要 ...

随机推荐

  1. css属性选择器=,~=,^=,$=,*=,|=

    http://www.w3school.com.cn/css/css_selector_attribute.asp =. property和value必须完全一致 : ~=.“约等于”?: ^=. 从 ...

  2. Bootstrap HTML编码规范

    语法 1.用两个空格来代替制表符(Tab)--这是唯一能保证在所有的环境下获得一致展现的方法. 2.嵌套元素应当缩进一次(即两个空格). 3.对于属性的定义,属性值确保全部都用双引(避免使用单引号). ...

  3. C# sizeof运算符

    一.C# sizeof运算符 sizeof运算符用于获取值类型的字节数. 二.示例 using System;using System.Collections.Generic;using System ...

  4. 【SQL】连接 —— 内连接、外连接、左连接、右连接、交叉连接

    连接 · 内连接 · 外连接 · 左连接 · 右连接 · 全连接 · 交叉连接 · 匹配符号(+)  连接  根据表之间的关系,呈现跨表查询的结果.     外连接     内连接 左连接 右连接 全 ...

  5. 网络流_Edmond-Karp算法、Dinic算法

    转载:网络流基础篇——Edmond-Karp算法             BY纳米黑客 网络流的相关定义: 源点:有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点. 汇点:另一个点也很特殊, ...

  6. (79)zabbix key总是not supported的解决方法

    zabbix定义好key之后,总是会出现Not supported,看到这个问题,大家不用着急,问题其实很容易解决,首先鼠标点击当前key的大红叉上,会显示出报错内容. 常见的有: 1. zabbix ...

  7. Java中的finally

    基础用法: int f1() { try{ return 1; }finally { System.out.println("finall执行"); } } @Test publi ...

  8. wusir 面试题答案在老男孩的视频里

    注意:你问答案在哪里?答案在视频里了,就是不给你写. 第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C#.C+ ...

  9. [jzoj5233]概率博弈(树形DP)

    Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵

  10. 遗传算法 | C++版GA_TSP

    嗯哼,时隔半年,再次有时间整理关于组合优化问题——旅行商问题(Traveling Salesman Problem, TSP),这次采用的是经典遗传算法(Genetic Algorithm, GA)进 ...