暑假集训 div1 B Derangement 交换数字 思维死角
2 seconds
256 megabytes
standard input
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.
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.
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.
6 6 2 4 3 5 1
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 交换数字 思维死角的更多相关文章
- STL 入门 (17 暑假集训第一周)
快速全排列的函数 头文件<algorithm> next_permutation(a,a+n) ---------------------------------------------- ...
- 暑假集训Day1 整数划分
题目大意: 如何把一个正整数N(N长度<20)划分为M(M>=1)个部分,使这M个部分的乘积最大.N.M从键盘输入,输出最大值及一种划分方式. 输入格式: 第一行一个正整数T(T<= ...
- 2015UESTC 暑假集训总结
day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...
- 暑假集训Day2 互不侵犯(状压dp)
这又是个状压dp (大型自闭现场) 题目大意: 在N*N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ...
- 暑假集训 #3div2 C Sequence 数字找规律
C. Sequence (64 Mb, 1 sec / test)Integer sequences are very interesting mathematical objects. Let us ...
- 暑假集训#2 div1 J 四点直角 J - Space Invader 四点共面+跨立实验
题意:给你四个点,判断能否先依次通过A,B两点,然后再在某个地方只进行一次直角转弯再一次经过C,D两点: #include <iostream> #include <cstdio&g ...
- 暑假集训 #2 div1 I - Lada Priora 精度处理
I - Lada Priora Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 2013ACM暑假集训总结-致将走上大三征途的我
回想起这个暑假,从开始与雄鹰一起的纠结要不要进集训队,与吉吉博博组队参加地大邀请赛,害怕进不了集训队.当时激励我月份开始接触的,记得当时在弄运动会来着,然后就问了雄鹰一些输入输出的东西,怀着满心的期待 ...
- [补档]暑假集训D5总结
%dalao 今天又有dalao来讲课,讲的是网络流 网络流--从入门到放弃:7-29dalao讲课笔记--https://hzoi-mafia.github.io/2017/07/29/27/ ...
随机推荐
- [转帖]国产CPU性能最全盘点 宜良性竞争优胜劣汰
国产CPU性能最全盘点 宜良性竞争优胜劣汰 电子工程专辑的网站内容 其实里面说的不尽全面 比如龙芯和申威就放到一块了 一个是 MIPS 一个是Alpha 明显不一样的东西 x86的应该都不行 而且. ...
- (4.15)mysql备份还原——物理备份之XtraBackup的下载与安装
关键词:mysql物理备份,XtraBackup,XtraBackup安装,XtraBackup下载 实践链接:https://www.cnblogs.com/gered/p/11147193.htm ...
- Luogu P2569 [SCOI2010] 股票交易
此题链接到dp常见优化方法 开始的时候被纪念品误导,以为是多支股票,后来发现事情不妙: 这道题知道的是某一只股票的走势: \(Solution\): \(70pts\): 设\(f[i][j]\)表示 ...
- Spring Boot 获取yaml配置文件信息
Spring boot 项目启动过程中: org.springframework.boot.SpringApplication#prepareEnvironment 当程序步入listeners.en ...
- Redis windows服务器配置可远程连接
Redis配置远程可访问:修改redis.conf或redis.windows-service.conf配置文件. 具体通过 1:将绑定的本机给注释掉,找到这行 bind 127.0.0.1,然后在前 ...
- Linux分布式消息队列RocketMQ部署与监控--双Master
环境准备:CentOS_6.5_x64 IP: 192.168.0.249 dbTest249 Master1 IP: 192.168.0.251 webTest251 Master2 下载 ali ...
- kubeDNS workflow(service registration and discovery)
Service discovery In k8s, we usually will more than make an application instance, and also the corre ...
- Delphi SpeedButton组件
- Delphi Form组件
- 外网访问VMware(Centos7.0,NAT模式)搭建的web服务器应用
首先参考 https://www.cnblogs.com/studyhard-cq/p/11551755.html 设置好NAT模式,能访问公网. 1.打开VMware,点击左上角编辑 ...