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 ...
随机推荐
- [转]Unity3D学习笔记(四)天空、光晕和迷雾
原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...
- VMware Workstation虚拟机安装Windows 7系统
1.进入VMware Workstation虚拟机软件界面,选择新建虚拟机
- python tarfile模块基本使用
1.压缩一个文件夹下的所有文件 #coding=utf8 import os import tarfile __author__ = 'Administrator' def main(): cwd = ...
- /*去hover动画效果*/
<!DOCTYPE html> /*去hover动画效果*/ <html lang="en"> <head> <meta charset= ...
- Windchill 查询功能
一.使用SearchCondition 查询语句中用容器中的containerReference.key.id名称来代替数据库中的字段idA3containerReference /** * ...
- 5、bam格式转为bigwig格式
1.Bam2bigwig(工具) https://www.researchgate.net/publication/301292288_Bam2bigwig_a_tool_to_convert_bam ...
- 使用python ftplib包递归下载文件夹及文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-06-11 09:35:49 # @Author : Yaheng Wang ...
- idea使用的知识
1. 如何设置,使IntelliJ IDEA智能提示忽略大小写. 很好用,在settings->Editor->General-->Code Completion里设置.2. ...
- PHP获取原生POST数据
To get the Raw Post Data: <?php $postdata = file_get_contents("php://input"); ?> 参考官 ...
- C#回调函数的简单讲解与应用例子
using System; namespace CallBackTest{ class Program //用户层,执行输入等操作 { static void Main(string[] args) ...