Gym 100971B 水&愚
Description
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
6
6 2 4 3 5 1
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 水&愚的更多相关文章
- Gym 100989F 水&愚&vector
standard input/output You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the che ...
- Gym 100971C 水&愚&三角形
Description standard input/output Announcement Statements There is a set of n segments with the le ...
- Gym 100971B Derangement
要求改换序列,使得没有位置是a[i] == i成立.输出最小要换的步数 首先把a[i] == i的位置记录起来,然后两两互相换就可以了. 对于是奇数的情况,和它前一个换或者后一个换就可以,(注意前一个 ...
- UESTC 2016 Summer Training #1 Div.2
最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria ( ...
- Codeforces Gym 100531G Grave 水题
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
- 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 ...
- Codeforces gym 100685 C. Cinderella 水题
C. CinderellaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/C ...
- 水题 Gym 100553K Knockout Racing
题目传送门 /* 题意:有若干个点在一个区间内来回移动,1m/s. 水题:n^2的复杂度能解决,注意时间可能大于一个周期,要取模 */ #include <cstdio> #include ...
- CodeForces Gym 100685C Cinderella (水题)
题意:给定 n 个杯子,里面有不同体积的水,然后问你要把所有的杯子的水的体积都一样,至少要倒少多少个杯子. 析:既然最后都一样,那么先求平均数然后再数一下,哪个杯子的开始的体积就大于平均数,这是一定要 ...
随机推荐
- CUDA的软件体系
CUDA的软件堆栈由以下三层构成:CUDA Library.CUDA runtime API.CUDA driver API,如图所示,CUDA的核心是CUDA C语言,它包含对C语言的最小扩展集和一 ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)(1)
阿里的云服务器准备好以后,我们首先要做的就是把自己购买的磁盘空间挂载到系统里面,我们为服务器选择的是 Linux 系统,确切说的是 CentOS 系统. 默认阿里云服务器带了一个 20G 的空间,一般 ...
- MySQL - DELETE 多表联合条件删除
DELETE t0 FROM t_demo t0, t_user u WHERE t0.id = 6 AND t0.id = u.id
- PyCharm 2018.1 软件汉化
下载汉化包 链接: https://pan.baidu.com/s/1buLFINImW_3cNzP8HsB4cA 密码: fqpu 安装汉化包 找到pycharm安装目录 直接把刚刚下载的汉化包复制 ...
- day 52 Django基础一之web框架的本质
Django基础一之web框架的本质 django第一天 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于D ...
- du 与df 统计系统磁盘不一致原因与解决方法
事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...
- 4、python中的布尔值和None
一.布尔值 1.布尔值只有两个:True.Flase,表示python语句的真与假: 2.在python早期的版本,布尔值用1和0表示. 二.None 1.None表示虚无,什么也没有: 2.千万不要 ...
- 文件的特殊权限(SUID,SGID,SBIT)
文件的一般权限:r w x 对应 421 文件的特殊权限:SUID SGID SBIT对应 421 文件的隐藏权限:chattr设置隐藏权限,lsattr查看文件的隐藏权限. 文件访问控制列表: ...
- Android 多线程 打地鼠游戏
前言:最近比较喜欢多线程了,看到了一些线程案例,这里总结一下打地鼠游戏的整个过程. 1.首先是主活动要加载的布局,我一般就喜欢早点把这个写上,这样就好在主活动中玩弄这些控件了.闲话不多说,一个Fram ...
- JavaScript 计时事件-setInterval()-clearInterval() -setTimeout()-clearTimeout()
(PS:JavaScript 一个设定的时间间隔之后来执行代码,我们称之为计时事件.) JavaScript 计时事件 通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码 ...