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 个杯子,里面有不同体积的水,然后问你要把所有的杯子的水的体积都一样,至少要倒少多少个杯子. 析:既然最后都一样,那么先求平均数然后再数一下,哪个杯子的开始的体积就大于平均数,这是一定要 ...
随机推荐
- Struts动态联动效果
<%@page contentType="text/html;charset=utf-8"%> <%@ include file="../../comm ...
- C#Aspose操作Word & Excel简版(后会研究补充更多功能)
利用Aspose操作Word & Excel首先要在项目中标引用Aspose.Words.dll和Aspose.Cells.dll. 首先说一说向Word中写入数据,目前做的是向Word中的标 ...
- SAP系统管理中常见问题解答(转载)
1.如何查看SAP系统的位数? system——status看 Platform ID Platform 32-bit 64-bit --------------------------------- ...
- Dart Socket 与Java Socket连接
------------------------------------------------------------- Dart SocketClient----------------- ...
- Linux 服务器用户权限管理改造方案与实施项目
Linux 服务器用户权限管理改造方案与实施项目 在了解公司业务流程后,提出权限整改方案改进公司超级权限root泛滥的现状. 我首先撰写方案后,给boss看,取得boss的支持后,召集大家开会讨论. ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...
- 【File】文件操作(初识文件操作一)
一,初识文件流 看到标题就知道接下来的所有操作对象都是面对文件进行的.那么问题来了.在java中目录是不是也属于文件呢?答案是yes.既然目录也属于文件,那么对于目录跟文件的区分就显现出来了.在接下来 ...
- python之随机数random模块
random模块:用于生成随机数 import random #random模块:用于生成随机数 li = [] for i in range(7): r = random.randrange(0,3 ...
- Appium运行时没有启动activity的权限:A new session could not be created.(Original error: Permission to start activity denied)
小白搞appium,遇到启动不了activity的问题: 查找解决方案说是跟AndroidManifest.xml有关系,参考:https://github.com/appium/appium/iss ...
- Android Studio 快捷键(包含自定义)终极版
[F] [F] F2 在错误代码之间切换 F3 往前定位(Shift + F3:往后定位 )有问题 F4\Ctrl+鼠标点击\Ctrl+B 转到定义,查看类继承关系 F5 但不调试进入函数内部. ...