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 ...
随机推荐
- Redis源码解析:13Redis中的事件驱动机制
Redis中,处理网络IO时,采用的是事件驱动机制.但它没有使用libevent或者libev这样的库,而是自己实现了一个非常简单明了的事件驱动库ae_event,主要代码仅仅400行左右. 没有选择 ...
- Crontab 入门
参考网址: http://www.centoscn.com/CentOS/help/2014/0820/3524.html 简单命令 service crond restart //重启crontab ...
- Android中Activity的android:windowSoftInputMode属性
转载 https://blog.csdn.net/qiutiandepaomo/article/details/84028558 windowSoftInputMode属性主要是用来设置窗口软键盘的交 ...
- Eclipse配置Maven详细教程
一.使用eclipse自带的maven插件 首先,现在下载Eclipse Mars之后的版本,基本上都自带了maven插件,无需自己再安装maven. 有几个注意点: 1.默认的本地仓库的目录是在C: ...
- 删除 java代码中所有的注释
删除 java代码中所有的注释.java public class CleanCommons { private static Pattern pattern = Pattern.compile(&q ...
- update当根据条件不同时 更新同一个字段的方法 或多表插入
1.通过存储过程 循环 传值 create or replace procedure p_u isbegin for rs in (select distinct (rks) from rkbz)lo ...
- LINUX常见服务列表
服务名 必需(是/否)用途描述 注解 acon 否 语言支持 特别支持左手书写语言:阿拉伯语,波斯语和希伯莱语 acpi ...
- activity与fragment之间的传递数据
首先activity之间的数据传递就是 用intent和intent+bundle intent 传递 Intent i= new Intent(MainActivity.this,TheAty.cl ...
- jquery购物评分
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python安装第三方模块
1.pip 安装命令: pip install 模块名由于国外网站不稳定可能会出现超时的情况,我们可以自己指定下载源命令如下临时修改 pip install 模块名 -i https://pypi.t ...