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. Activiti学习记录(四)

    1 连线 注意:如果将流程图放置在和java类相同的路径,需要配置: 1.1 部署流程定义+启动流程实例 ProcessEngine processEngine = ProcessEngines.ge ...

  2. 黑马基础阶段测试题:定义一个int类型的数组,数组中元素为{5,7,3,9,4}。求出数组中的最小值,并判断最小值是否为偶数,如果是偶数则输出“最小值为偶数”,如果不是偶数则输出“最小值为奇数”。打印如下:

    package com.swift; import java.util.Arrays; public class ArrayTest { public static void main(String[ ...

  3. MySQL DBA从小白到大神实战

    MySQL5.6 For CentOS 6.6 源码编译安装 o1.关闭防火墙o2.配置sysctl.confo3.检查操作系统上是否安装了MySQLo4.下载mysql源码包o5.添加用户和组o6. ...

  4. 【Python学习之九】asyncio—异步IO

    asyncio 这是python3.4引入的标准库,直接内置对异步IO的支持.asyncio的编程模型就是一个消息循环.从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程 ...

  5. Cluster - 基本概念

    1.1 什么是集群 简单的说,集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点(node).一个理想的集群是,用户从来不会意识到集群系统 ...

  6. Linux时区修改

    Linux修改时区的正确方法 CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 [root@centos7 ~]# ...

  7. (转)Windows 支持 DirectX 和 OpenGL,为什么大多数 PC 游戏还是 DirectX 开发?

    事实上在早年OpenGL即使在游戏领域也是对DirectX压倒性的优势.John Carmack曾嘲讽DirectX是"horribly broken" 的API.直到Direct ...

  8. B1061 判断题 (15分)

    B1061 判断题 (15分) 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 M,分别是学生人 ...

  9. Java 关键字volatile 与 synchronized 作用与区别

     1,volatile 它所修饰的变量不保留拷贝,直接访问主内存中的.    在Java内存模型中,有main memory,每个线程也有自己的memory (例如寄存器).为了性能,一个线程会在自己 ...

  10. Android开发——HandlerThread以及IntentService详解

    .HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...