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 ...
随机推荐
- rsync 端口更换(默认873)
一般使用默认端口的话, 在服务端的启动命令为: /usr/bin/rsync --address=192.168.1.23 --daemon 如果在客户端需要换另外的端口侦听, 则使用 /usr/bi ...
- Selenium VS Webdriver
Selenium 是 ThroughtWorks 一个强大的基于浏览器的开源自动化测试工具,它通常用来编写 Web 应用的自动化测试.随着 Selenium 团队发布 Selenium 2(又名 We ...
- Hive 查询优化总结
一.join优化 Join查找操作的基本原则:应该将条目少的表/子查询放在 Join 操作符的左边.原因是在 Join 操作的 Reduce 阶段,位于 Join 操作符左边的表的内容会被加载进内存, ...
- 【机器学习】聚类算法——K均值算法(k-means)
一.聚类 1.基于划分的聚类:k-means.k-medoids(每个类别找一个样本来代表).Clarans 2.基于层次的聚类:(1)自底向上的凝聚方法,比如Agnes (2)自上而下的分裂方法,比 ...
- Cactus项目(又叫MVCAdmin),开源(2016-11-26更新)
Cactus基于之前简单后台管理的改良版本,完善了权限管理,为后续的扩展和管理做了铺垫. 完全开放代码,可供学习交流 目前采用MVC4+Autofac+Dapper制作而成,集成一个简单的Blog和权 ...
- FMDB 使用注意点
关于FMDB最基本的使用我们就不在说了,这个网上大把的文章介绍,我就在这里总结几点我最近在写一个小东西的时候注意到的一点点东西: 一: 怎么看真机上SQLite数据库 我们在开发的过程中肯定有使用到真 ...
- Vim7.4 配置项列表
vim所有的配置项如下,可以根据自己的需要进行配置,每个配置项的详细信息 可以在vim里面使用:help option-name # 配置项名称 缩写词 简要说明 'aleph' 'al' Aleph ...
- php 获取当前的访问的ip
<?php function get_client_ip() { $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_CLIENT_I ...
- List<T>的排序
方法一.sort() (1)当list中存的是简单数据类型时: public void Sort(); List<int> a = new List<int>() { 4, 5 ...
- 2017-10-17 NOIP模拟赛2
a [问题描述]你是能看到第一题的 friends 呢.——hja何大爷对字符串十分有研究,于是天天出字符串题虐杀 zhx.何大爷今天为字符串定义了新的权值计算方法.一个字符串由小写字母组成,字符串的 ...