B. Derangement
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ofswap operations.

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

Examples
input
6 6 2 4 3 5 1
output
1 2 5
题意:给你1-n数字组成的一个序列,你可以进行a,b操作把a,b位置上的数字交换位置,问最少进行多少次交换
才使得整个序列中不存在第i个位置的数字为i这样情况。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
using namespace std;
const double eps=1e-10; int a[200006][4];
int main()
{
int n;
while(~scanf("%d",&n))
{
int cnt=0,last=0;
for(int i=1;i<=n;i++)
{
int u;
scanf("%d",&u);
if(u==i)
{
cnt++;
if(last!=0)
{
a[cnt/2][0]=last;
a[cnt/2][1]=u;
last=0;
}
else last=u;
}
}
if(cnt%2!=0) {
a[cnt/2+1][0]=last;
if(last!=1) a[cnt/2+1][1]=1;
else a[cnt/2+1][1]=2;
}
printf("%d\n",cnt/2+cnt%2);
for(int i=1;i<=cnt/2+cnt%2;i++)
printf("%d %d\n",a[i][0],a[i][1]);
}
return 0;
}

分析:其实这个问题其他的都分析很好,就错了一个地方,就是当还剩余最后一个数字无法进行交换时,这时

很显然我们只需要把他与其他的数字中任意一个进行下交换,所以我想的就是与第一个数字交换,,,,,但是,没有

考虑到1,3,2这样的序列,按照我的想法输出的应该是第一个与第一个交换,,,,思维死角

暑假集训 div1 B Derangement 交换数字 思维死角的更多相关文章

  1. STL 入门 (17 暑假集训第一周)

    快速全排列的函数 头文件<algorithm> next_permutation(a,a+n) ---------------------------------------------- ...

  2. 暑假集训Day1 整数划分

    题目大意: 如何把一个正整数N(N长度<20)划分为M(M>=1)个部分,使这M个部分的乘积最大.N.M从键盘输入,输出最大值及一种划分方式. 输入格式: 第一行一个正整数T(T<= ...

  3. 2015UESTC 暑假集训总结

    day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...

  4. 暑假集训Day2 互不侵犯(状压dp)

    这又是个状压dp (大型自闭现场) 题目大意: 在N*N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ...

  5. 暑假集训 #3div2 C Sequence 数字找规律

    C. Sequence (64 Mb, 1 sec / test)Integer sequences are very interesting mathematical objects. Let us ...

  6. 暑假集训#2 div1 J 四点直角 J - Space Invader 四点共面+跨立实验

    题意:给你四个点,判断能否先依次通过A,B两点,然后再在某个地方只进行一次直角转弯再一次经过C,D两点: #include <iostream> #include <cstdio&g ...

  7. 暑假集训 #2 div1 I - Lada Priora 精度处理

    I - Lada Priora Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. 2013ACM暑假集训总结-致将走上大三征途的我

    回想起这个暑假,从开始与雄鹰一起的纠结要不要进集训队,与吉吉博博组队参加地大邀请赛,害怕进不了集训队.当时激励我月份开始接触的,记得当时在弄运动会来着,然后就问了雄鹰一些输入输出的东西,怀着满心的期待 ...

  9. [补档]暑假集训D5总结

    %dalao 今天又有dalao来讲课,讲的是网络流 网络流--从入门到放弃:7-29dalao讲课笔记--https://hzoi-mafia.github.io/2017/07/29/27/   ...

随机推荐

  1. Python学习【day01】- Python初识

    Python下载:https://www.python.org/downloads/ 下载后进行安装,安装后的Python我们称之为Python解析器 1.打印“Hello World” 安装后的Py ...

  2. 设计模式:建造者模式(Builder)

    流水作业大家应该都清楚吧!在流水作业中,我们可以将一些复杂的东西给构建出来,例如汽车.我们都知道汽车内部构件比较复杂,由很多部件组成,例如车轮.车门.发动机.方向盘等等,对于我们用户来说我们并不需要知 ...

  3. 2019中山纪念中学夏令营-Day4[JZOJ]

    Begin (题目的排序方式:难易程度) 什么是对拍: 对拍是一种在写完程序后,验证自己程序是不是正解的比较方便的方法. 实现过程: 对同一道题,再打一个暴力程序,然后用一些大数据等跑暴力程序来进行验 ...

  4. Python-RabbitMQ-fanout(广播模式)

    生产者:fanout_publiser.py import pika import sys connection = pika.BlockingConnection(pika.ConnectionPa ...

  5. redis的string和list

  6. luogu题解 P2419 【牛大赛Cow Contest】传递丢包

    题目链接: https://www.luogu.org/problemnew/show/P2419 分析: "在交际网络中,给定若干元素和若干对二元关系,且关系具有传递性. 通过传递性推导出 ...

  7. access数据库根据指定日期进行查询

    获取指定日期的记录 1.select Field1 from  A  where format("yyyy-MM-dd",Field1)=#2011-10-07# 有时不能获取记录 ...

  8. O007、KVM 存储虚拟化

    参考https://www.cnblogs.com/CloudMan6/p/5273283.html   KVM 的存储虚拟化是通过存储池(Storage Pool) 和 卷(Volume)来管理的. ...

  9. 【转载】Linux GCC常用命令

    作者:ggjucheng 出处:https://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2 ...

  10. 多线程编程-- part 4 线程间的通信

    线程间的相互作用 线程之间需要一些协调通信,来共同完成一件任务. Object类相关的方法:notify(),notifyAll(),wait().会被所有的类继承,这些方法是final不能被重写.他 ...