要求改换序列,使得没有位置是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的更多相关文章

  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. SP:目录

    ylbtech-SP:目录 商谱开发框架 1.返回顶部 1. 1.WebUIWeChatUI MobileUI 2.Core-Model-Repository-Security -Service3.D ...

  2. 杂项-JS:artTemplate.js

    ylbtech-杂项-JS:artTemplate.js artTemplate 是新一代 javascript 模板引擎,它采用预编译方式让性能有了质的飞跃,并且充分利用 javascript 引擎 ...

  3. Python,PyCharm2017安装教程,包含注册码[转载]

    一,安装PyCharm 1.下载PyCharm 进入https://www.jetbrains.com/pycharm/download/#section=windows官网下载页面,可以到到PyCh ...

  4. 关于android 数据库查询出现 _id column do not exist 的处理

    查询的字段必须带上_id字段,否则就会出现此类异常,导致程序崩溃

  5. css中的定位属性position(转)

    css中的定位属性position   同样的也是上课的时候发现学生难以理解的一些问题拿出来记录一下,希望帮助初学者. 在css中定位属性position的运用在页面中是很常用的,特别是一些结合js来 ...

  6. cookie 、Session 和自定义分页

    cookie cookie的由来 大家都知道Http协议是无状态的. 无状态的意思 是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系, 他不会受前面的请求响应情况直接影响, ...

  7. Linux进程KILL不掉的原因

    做过Linux开发的人通常遇到过一个进程不能kill掉的情况,即使使用的是kill -9方式,而一般的教课书都只说kill -9能杀死任何进程,遇到这种情况时就会感觉到很矛盾,其它这也是正常的,通常有 ...

  8. hdu1048

    #include<stdio.h>#include<string.h>#include<iostream>using namespace std;int main( ...

  9. 功防技术与实践第1.2章,kali初步了解

    20169314 2016-2017-2 <网络攻防实践>/<网络攻击与防范>第2周学习总结 一.教材学习内容总结 1.hacker和cracker的区别 网络攻防分三部分:系 ...

  10. 超级台阶 (NYOJ—76)

    很简单的高中数学题,写出来主要是提醒自己,写完递推公式(尤其是公式)一定要检查多遍. #include<stdio.h> #include<string.h> int M; i ...