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 ...
随机推荐
- Poj_1004_FinancialManagement
一.Description Larry graduated this year and finally has a job. He's making a lot of money, but someh ...
- Erlang generic standard behaviours -- gen_server noblock call
在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...
- HDOJ1059(多重背包)
1.解法一:多重背包 #include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a& ...
- smbpasswd和pdbedit
samba用户管理: smbpasswd :smbpasswd命令属于samba套件,能够实现添加或删除samba用户和为用户修改密码. smbpasswd [options] USERNAME -a ...
- oracle--pl/sql变量定义----
一.变量介绍 在编写pl/sql程序时,可以定义变量和常量:在pl/sql程序中包括有: 1).标量类型(scalar) 2).复合类型(composite) --用于操作单条记录 3).参照类型(r ...
- [bzoj3670] [NOI2014] [lg2375] 动物园
nxt数组为KMP的next数组num[i]储存了i前面可以匹配的串的个数.先在KMP求nxt中顺便求出num最后再找到对于i的最大的前后缀不重叠的可匹配的j,ans*=(num[j]+1)%1000 ...
- SVD实例
>> X = rand(5,7) X = 0.9797 0.1365 0.6614 0.5828 0.2259 0.2091 0.5678 0.2714 0.0118 0.2844 0.4 ...
- CSS如何计算优先级?如何计算权重?
(1) 优先级就近原则,同权重以最近者为准 载入样式以最后载入的样式为准: 同权重下:内联样式表(标签内部) > 嵌入样式表(当前文件) > 外部样式表(外部文件) !import > ...
- 【总结整理】JQuery基础学习---DOM篇
前言: 先介绍下需要用到的浏览器提供的一些原生的方法(这里不处理低版本的IE兼容问题) 创建流程比较简单,大体如下: 创建节点(常见的:元素.属性和文本) 添加节点的一些属性 加入到文档中 流程中涉及 ...
- p2055&bzoj1433 假期的宿舍
传送门(洛谷) 传送门(bzoj) 题目 学校放假了······有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如A 和B都是学校的学生,A要回家,而C来看B,C与A不认识. ...