Gym 100971B Derangement
要求改换序列,使得没有位置是a[i] == i成立。输出最小要换的步数
首先把a[i] == i的位置记录起来,然后两两互相换就可以了。
对于是奇数的情况,和它前一个换或者后一个换就可以,(注意前一个越界或者后一个越界)
这样是不会重复的,因为本来i是a[i] == i的话,换了一个,是不会使得他们两个a[i] == i的
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- #define inf (0x3f3f3f3f)
- typedef long long int LL;
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- #include <string>
- const int maxn = + ;
- int a[maxn];
- vector<int>pos;
- void work ()
- {
- int n;
- cin>>n;
- for (int i = ; i <= n; ++i) scanf ("%d",&a[i]);
- for (int i = ; i <= n; ++i) {
- if (a[i] == i)
- pos.push_back(i);
- }
- printf ("%d\n",pos.size() / + (pos.size() & ));
- if (pos.size() & ) {
- for (int i = ; i < pos.size() - ; i += ) {
- printf ("%d %d\n",pos[i],pos[i + ]);
- }
- int t1 = pos[pos.size() - ] - ;
- int t2 = pos[pos.size() - ] + ;
- int ans;
- if (t1 != ) {
- ans = t1;
- } else {
- ans = t2;
- }
- printf ("%d %d\n",pos[pos.size() - ],ans);
- } else {
- for (int i = ; i < pos.size(); i += ) {
- printf ("%d %d\n",pos[i],pos[i + ]);
- }
- }
- return ;
- }
- int main()
- {
- #ifdef local
- freopen("data.txt","r",stdin);
- #endif
- work ();
- return ;
- }
Gym 100971B Derangement的更多相关文章
- Gym 100971B 水&愚
Description standard input/output Announcement Statements A permutation of n numbers is a sequence ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
随机推荐
- project online get approvals task data 获取审批待办任务接口
调用接口地址:重要 http://xxxx/sites/pwa/_vti_bin/PSI/ProjectServer.svc Header 三个必要参数: 其中SOAPAction写死就行,如果是on ...
- 解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题
刚装好SSMS 17.1准备体验,弹出: 一番搜索,普遍办法都是安装VS2015独立shell.删除某个注册表项什么的,没用,首先这个shell我是装了的,然后也没有那个注册表项.我自己尝试过重装sh ...
- linux日常管理-rsync格式
rsync支持网络到本地,本地到网络,本地到本地拷贝数据,支持增量拷贝.用作备份. man rsync rsync的两大用法.一种是通过shell,一种是deamon. shell pull远程机器 ...
- T-SQL 高级编程
在Sql Server 中访问数据库一般有2种方式: 1.一种是使用应用程序编程接口API 2.数据库语句 变量:局部变量:以@为前缀,如@Age:全局变量以@@为前缀:(Ps:全局变量以系统定义和维 ...
- JAVAWeb SSH框架 上传文件,如2007的EXCEL
下面的代码是上传EXCEL的代码,其实,就是在上传文件到服务器,代码都差不多,只是接收的文件的类型改一下即可. 1.jsp 用的是struts2 标签 代码: <s:file name=&quo ...
- Ubuntu 解决:当执行`sudo apt-get update`命令时 出现的 “apt-get 404 Not Found Package Repository Errors” 问题
Ubuntu 解决:当执行sudo apt-get update或者sudo apt-get install命令是出现的 "apt-get 404 Not Found Package Rep ...
- hbase exporter importer 导出 导入
导出: bin/hbase org.apache.hadoop.hbase.mapreduce.Export bigtable /user/bigtable_bak/ 导入: bin/hbase or ...
- p2341&bzoj1051 受欢迎的牛
传送门(洛谷) 传送门(bzoj) 题目 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C ...
- Webservice 或者HttpRequest请求的时候提示 “指定的注册表项不存在”错误 解决方案
今天又遇到神奇的事情,在使用WebService的时候居然提示“指定的注册表不存在.” The specified registry key does not exist. Google后发现,原来是 ...
- Spring入门第二十七课
声明式事务 直接上代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dri ...