AtCoder Regular Contest 082 D Derangement
AtCoder Regular Contest 082 D Derangement
与下标相同与下个交换就好了。。。。
Define a sequence of ’o’ and ’x’ of length N as follows: if pi ̸= i, the i-th symbol is ’o’, otherwise the i-th symbol is ’x’. Our objective is to change this sequence to ’ooo...ooo’.
• If there is a part ”ox” (or ”xo”) in the sequence, we can change it to ”oo” by swapping these two elements. (∵ If pi = x(x ̸= i) and pi+1 = i + 1 in the initial sequence, after the swap, both pi = i + 1 and pi+1 = x will be ’o’.) • If there is a part ”xx” in the sequence, we can change it to ”oo” by swapping these two elements. (∵ If pi = i(x ̸= i) and pi+1 = i + 1 in the initial sequence, after the swap, both pi = i + 1 and pi+1 = i will be ’o’.)
Thus, we should check the sequence from left to right, and if we find an ’x’ at the i-th position, we should swap i and i + 1 (unless i = N, in this case we should swap i and i−1).
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
int a[N]; int main()
{
int n;
while(cin>>n)
{
for(int i=;i<=n;i++) cin>>a[i];
int cnt=;
for(int i=;i<=n;i++){
if(a[i]==i){
cnt++;
swap(a[i],a[i+]);
}
}
cout<<cnt<<endl;
}
return ;
}
AtCoder Regular Contest 082 D Derangement的更多相关文章
- 【推导】AtCoder Regular Contest 082 D - Derangement
题意:给你一个排列a,每次可以交换相邻的两个数.让你用最少的交换次数使得a[i] != i. 对于两个相邻的a[i]==i的数,那么一次交换必然可以使得它们的a[i]都不等于i. 对于两个相邻的,其中 ...
- 【AtCoder Regular Contest 082】Derangement
[链接]点击打开链接 [题意] 在这里写题意 [题解] 贪心. 连续一块的p[i]==i的话,对答案的贡献就应该为(这个连续块的长度+1)/2; 长度为1的也正确. (也即两两相邻的互换位置.) [错 ...
- AtCoder Regular Contest 082
我都出了F了……结果并没有出E……atcoder让我差4分上橙是啥意思啊…… C - Together 题意:把每个数加1或减1或不变求最大众数. #include<cstdio> #in ...
- AtCoder Regular Contest 082 ABCD
A #include<bits/stdc++.h> using namespace std; ]; int n,m; int main(){ cin>>n>>m; ...
- AtCoder Regular Contest 082 (ARC082) E - ConvexScore 计算几何 计数
原文链接http://www.cnblogs.com/zhouzhendong/p/8934254.html 题目传送门 - ARC082 E 题意 给定二维平面上的$n$个点,定义全集为那$n$个点 ...
- 【推导】【模拟】AtCoder Regular Contest 082 F - Sandglass
题意:有个沙漏,一开始bulb A在上,bulb B在下,A内有a数量的沙子,每一秒会向下掉落1.然后在K个时间点ri,会将沙漏倒置.然后又有m个询问,每次给a一个赋值ai,然后询问你在ti时刻,bu ...
- 【计算几何】【推导】【补集转化】AtCoder Regular Contest 082 E - ConvexScore
题意:平面上给你N个点.对于一个“凸多边形点集”(凸多边形点集被定义为一个其所有点恰好能形成凸多边形的点集)而言,其对答案的贡献是2^(N个点内在该凸多边形点集形成的凸包内的点数 - 该凸多边形点集的 ...
- AtCoder Regular Contest 082 E
Problem Statement You are given N points (xi,yi) located on a two-dimensional plane. Consider a subs ...
- AtCoder Regular Contest 082 F
Problem Statement We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contai ...
随机推荐
- Rabbitmq交换机三种模式介绍
1.topic 将路由键和某模式进行匹配.此时队列需要绑定要一个模式上.符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词.因此“abc.#”能够匹配到“abc.def.ghi”,但是“abc. ...
- 【python之路23】递归
1.递归的基础 举例说明:老师要班里坐在最后的一排学生要一本书,老师对前面的人说你向最后一排的同学要一本书,那么最前面的人跟坐在第2排的人说,第2排的人跟第3排的人说,当命令传递到最后一排时,最后一排 ...
- Hackerrank--XOR love (Math系列)
题目链接 Devendra loves the XOR operation very much which is denoted by ∧ sign in most of the programmin ...
- JSP/FTL 中获取param、request、session、application中的值
Java JSP(EL表达式) FTL ① <% page.getAttribute("attr") %> ${pageScope .attr} - ② reque ...
- 删除 java代码中所有的注释
删除 java代码中所有的注释.java public class CleanCommons { private static Pattern pattern = Pattern.compile(&q ...
- springMVC的功能和优点
spring MVC是一个分层的java web开发框架,MVC模式提供了一个分层的体系结构,其中每一层对其它层进行了抽象,具体如下: 1.模型(Model):应用程序所使用的特定域信息的表现形式 2 ...
- NKOJ3485 【2015多校联训4】数据
问题描述 Mr_H 出了一道信息学竞赛题,就是给 n 个数排序.输入格式是这样的:试题有若干组数据.每组数据的第一个是一个整数 n,表示总共有 n 个数待排序:接下来 n 个整数,分别表示这n 个待排 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]
[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...
- Leetcode645.Set Mismatch错误的集合
集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 nums 代表了集合 S ...
- LUOGU P1081 开车旅行 (noip 2012)
传送门 解题思路 这道题刚了一下午,主要就刚在set那里了.先写了一个暴力70分..之后优化预处理,看着大佬神犇们都用的什么双向链表之类的东西,本蒟蒻不会,又懒得手写平衡树,就拿了个set搞了搞,感觉 ...