Problem Description

You are given a permutation p1,p2,…,pN consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have pi≠i for all 1≤i≤N. Find the minimum required number of operations to achieve this.

Constraints

  • 2≤N≤105
  • p1,p2,..,pN is a permutation of 1,2,..,N.

Input

The input is given from Standard Input in the following format:

N
p1 p2 .. pN

Output

Print the minimum required number of operations

Example

Sample Input 1

5
1 4 3 5 2

Sample Output 1

2
Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition. This is the minimum possible number, so the answer is 2.

Sample Input 2

2
1 2

Sample Output 2

1
Swapping 1 and 2 satisfies the condition.

Sample Input 3

2
2 1

Sample Output 3

0
The condition is already satisfied initially.

Sample Input 4

9
1 2 4 9 5 8 7 3 6

Sample Output 4

3

题意:给出一个长度为 n 的序列 a,序列中的数分别为 1~n,现在可以对序列中相邻元素进行交换,问最少交换多少次能使得 ai≠i

思路:从前向后扫一遍记录是否需要交换即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;int a[N];
int main() {int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);int res=0;for(int i=1;i<=n-1;i++){if(a[i]==i){swap(a[i],a[i+1]);res++;}}if(a[n]==n)res++;printf("%d\n",res);return 0;
}

Derangement(AtCoder-3525)相关推荐

  1. AtCoder Beginner Contest 198 (A ~ F)题解

    目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...

  2. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

  3. Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)题解

    文章目录 A - Tiny Arithmetic Sequence B - Do you know the second highest mountain? C - Secret Number D - ...

  4. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200)题解

    文章目录 A - Century B - 200th ABC-200 C - Ringo's Favorite Numbers 2 D - Happy Birthday! 2 E - Patisser ...

  5. freee Programming Contest 2022(AtCoder Beginner Contest 264)A~D题详细讲解

    目录 博主赛情 网站链接 比赛简介 Contest Information Reason why freee needs AtCoder users freee's business content ...

  6. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  7. 惹某人de集训第4周学习摘录(习题+感悟)

    我是个没有感情的WA题机器 (一)课堂内容 创建二叉树 根据先序遍历和中序遍历建树输出后序遍历 贪心!贪心! 归并排序经典题 题目描述 Input Output Sample Input Sample ...

  8. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

  9. 基于javaweb的医院药品管理系统(idea版本)

    一.项目简介 本项目采用idea工具开发,jsp+servlet技术编写,样式采用了layui和bootstrap前段框架,数据库采用的是mysql,navicat开发工具. 系统一共分为四个角色分别 ...

  10. (移动端)省市区三级联动

    index.html <!DOCTYPE html> <html><head><meta charset="UTF-8"><t ...

最新文章

  1. 4.1 ucGUI 图片显示方法
  2. 运行PHP出现No input file specified错误解决办法
  3. [云炬python3玩转机器学习]4-3 训练数据集,测试数据集
  4. codeigniter在nginx安装配置及URL重写
  5. [蓝桥杯][2015年第六届真题]表格计算(递归+记忆化)
  6. thrift编写服务端 客户端
  7. python mysql编码_Python和MySql:Unicode和编码
  8. java学生管理系统遇到的难题_学生信息管理系统错误总结
  9. 获取p12证书详情,返回证书详情实体
  10. 杰理ac692x芯片编程软件_杰理AC692N系列开发入门教程
  11. 中国优秀软件架构师感悟录
  12. JDK1.8优雅的集合排序(集合的排序)
  13. Power Apps 拍照上传
  14. PyS2:RDD编程基础(一)
  15. SWAT入门小问题的解决
  16. 北京航空航天大学计算机学院保研,北京航空航天大学计算机学院(专业学位)计算机技术保研...
  17. 上穿均线压力的大阳线特征:
  18. 只需5步——在轻量应用服务器部署Hexo博客
  19. 【烈日炎炎战后端 】MyBatis(0.4万字)
  20. 超详细Redis入门教程——Redis概述

热门文章

  1. [转载]Programming MS Office 2000 Web Components第二章第三节
  2. 在pocket pc 2003上播放声音
  3. 盘点谷歌、Facebook和IBM的重磅AI项目
  4. html页面怎么解决跨域问题,前端web开发html如何避免js的跨域访问
  5. c++ sleep函数_《PHP扩展开发》-hook-(hook原来的sleep)
  6. 做到阿里P7和考上985/211哪个更难?
  7. 马化腾生日当天 微信支付居然崩溃了//(ㄒoㄒ)//
  8. 关于Jeecg互联网化dubbo改造方案(下)
  9. weixin微信公众号一站到底游戏代码(有点普通)
  10. Google Chrome调试js代码