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 个杯子,里面有不同体积的水,然后问你要把所有的杯子的水的体积都一样,至少要倒少多少个杯子. 析:既然最后都一样,那么先求平均数然后再数一下,哪个杯子的开始的体积就大于平均数,这是一定要 ...
随机推荐
- js 数组方法大集合,各方法是否改变原有的数组详解
不会改变原来数组的有: concat()---连接两个或更多的数组,并返回结果. every()---检测数组元素的每个元素是否都符合条件. some()---检测数组元素中是否有元素符合指定条件. ...
- SOA架构,dubbo,Zookeeper
1. 分析 由于项目是基于soa的架构,表现层和服务层是不同的工程.所以要实现查询需要两个系统之间进行通信. 如何实现远程通信? 1.Webservice:效率不高基于soap协议.项目中不推荐使用. ...
- 问题002:我们要使用的Java是哪个版本的?什么是JVM、JRE、JDK、IDE、API?
三个版本:1.java SE 标准版 2.java EE企业版 3.Java ME 小型版本 JVM (java virtual machine) java虚拟机 JRE(java runtime e ...
- STL笔记(に)--vector容器
Vector 1.可变长的动态数组 2.需包含头文件#include<vector> (当然,如果用了万能头文件#include<bits/stdc++.h>则可忽略) 3.支 ...
- 在windows上搭建镜像yum站的方法
在windows上搭建镜像yum站的方法(附bat脚本) 分类: 运维基本功,其他 方法一:支持rsync的网站 对于常用的centos.Ubuntu.等使用官方yum源在 http://mi ...
- 02.VUE学习二之数据绑定
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站
现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...
- 史上最权威的 Activiti 框架学习
Activiti5 是 由 Alfresco 软件在 2010 年 5 月 17 日发布的业务流程管理( BPM) 框架,它是覆盖了业务流程管理.工作流.服务协作等领域 的一个开源的.灵活的. ...
- 8、HTML DOM总结
1.HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model):HTML DOM 模型被构造为对象的树. 2.DOM 方法 < ...
- Windows网络编程笔记4 -- Winsock 协议相关知识
Win32平台上的Winsock编程,Winsock是一个与协议无关的接口.以下协议是我们需要了解的: 网络协议的特征包括: 1. 面向消息 2. 面向连接和无线接 3. 可靠性和次序性 4. ...