要求改换序列,使得没有位置是a[i] == i成立。输出最小要换的步数

首先把a[i] == i的位置记录起来,然后两两互相换就可以了。

对于是奇数的情况,和它前一个换或者后一个换就可以,(注意前一个越界或者后一个越界)

这样是不会重复的,因为本来i是a[i] == i的话,换了一个,是不会使得他们两个a[i] == i的

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define inf (0x3f3f3f3f)
  8. typedef long long int LL;
  9.  
  10. #include <iostream>
  11. #include <sstream>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #include <queue>
  16. #include <string>
  17. const int maxn = + ;
  18. int a[maxn];
  19. vector<int>pos;
  20. void work ()
  21. {
  22. int n;
  23. cin>>n;
  24. for (int i = ; i <= n; ++i) scanf ("%d",&a[i]);
  25. for (int i = ; i <= n; ++i) {
  26. if (a[i] == i)
  27. pos.push_back(i);
  28. }
  29. printf ("%d\n",pos.size() / + (pos.size() & ));
  30. if (pos.size() & ) {
  31. for (int i = ; i < pos.size() - ; i += ) {
  32. printf ("%d %d\n",pos[i],pos[i + ]);
  33. }
  34. int t1 = pos[pos.size() - ] - ;
  35. int t2 = pos[pos.size() - ] + ;
  36. int ans;
  37. if (t1 != ) {
  38. ans = t1;
  39. } else {
  40. ans = t2;
  41. }
  42. printf ("%d %d\n",pos[pos.size() - ],ans);
  43. } else {
  44. for (int i = ; i < pos.size(); i += ) {
  45. printf ("%d %d\n",pos[i],pos[i + ]);
  46. }
  47. }
  48. return ;
  49. }
  50. int main()
  51. {
  52. #ifdef local
  53. freopen("data.txt","r",stdin);
  54. #endif
  55. work ();
  56. return ;
  57. }

Gym 100971B Derangement的更多相关文章

  1. Gym 100971B 水&愚

    Description standard input/output Announcement   Statements A permutation of n numbers is a sequence ...

  2. 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 ...

  3. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  4. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  5. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  6. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  7. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  8. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  9. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

随机推荐

  1. project online get approvals task data 获取审批待办任务接口

    调用接口地址:重要 http://xxxx/sites/pwa/_vti_bin/PSI/ProjectServer.svc Header 三个必要参数: 其中SOAPAction写死就行,如果是on ...

  2. 解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题

    刚装好SSMS 17.1准备体验,弹出: 一番搜索,普遍办法都是安装VS2015独立shell.删除某个注册表项什么的,没用,首先这个shell我是装了的,然后也没有那个注册表项.我自己尝试过重装sh ...

  3. linux日常管理-rsync格式

    rsync支持网络到本地,本地到网络,本地到本地拷贝数据,支持增量拷贝.用作备份. man rsync rsync的两大用法.一种是通过shell,一种是deamon. shell  pull远程机器 ...

  4. T-SQL 高级编程

    在Sql Server 中访问数据库一般有2种方式: 1.一种是使用应用程序编程接口API 2.数据库语句 变量:局部变量:以@为前缀,如@Age:全局变量以@@为前缀:(Ps:全局变量以系统定义和维 ...

  5. JAVAWeb SSH框架 上传文件,如2007的EXCEL

    下面的代码是上传EXCEL的代码,其实,就是在上传文件到服务器,代码都差不多,只是接收的文件的类型改一下即可. 1.jsp 用的是struts2 标签 代码: <s:file name=&quo ...

  6. 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 ...

  7. hbase exporter importer 导出 导入

    导出: bin/hbase org.apache.hadoop.hbase.mapreduce.Export bigtable /user/bigtable_bak/ 导入: bin/hbase or ...

  8. p2341&bzoj1051 受欢迎的牛

    传送门(洛谷) 传送门(bzoj) 题目 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C ...

  9. Webservice 或者HttpRequest请求的时候提示 “指定的注册表项不存在”错误 解决方案

    今天又遇到神奇的事情,在使用WebService的时候居然提示“指定的注册表不存在.” The specified registry key does not exist. Google后发现,原来是 ...

  10. Spring入门第二十七课

    声明式事务 直接上代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dri ...