题目描述

初始给你一个排列p[i],你可以执行以下操作任意多次。

选择一个i,交换p[i]和p[i+1]的值(其实就是交换排列当中两个相邻的元素)。

我们现在希望对于任意的i满足p[i]不等于i,求最少需要执行的操作次数。

输入

输入文件A.in。

第一行一个整数n。

第二行n个整数,其中第i个整数表示p[i]。

输出

输出文件A.out

一行一个整数表示最少的操作次数。

样例输入

5
1 4 3 5 2

样例输出

2

【样例输入2】

2
1 2

【样例输出2】

1

【样例输入3】

9
1 2 4 9 5 8 7 3 6

【样例输出3】

3

【数据范围】

对于 30% 数据 $ n \le 10 $

对于 50% 数据 $ n \le 10^3 $

对于 100% 数据 $ n \le 10^5 $

这道题反正我是感觉我做的挺SB的............一开始写了N个错误的贪心 $ QwQ $ (我也不知道自己在想什么)

反正就是很谜,后来我就 $ xjb $ 乱贪,然后贪过了....大体就是正着扫一遍,遇到 $ i == p_i $ 的就把它和后面的交换

然后反着再扫一遍,就过了......

代码如下:

#include <iostream>
#include <cstdlib>
#include <cstdio> const int N = 1e5 + 5 ; int n,v[N];
int ans; int main(){
scanf("%d" , & n );
for (int i = 1 ; i <= n ; ++ i) scanf("%d" , & v[i]);
for (int i = 1 ; i < n ; ++ i){
if(v[i] == i){
++ ans ;
std::swap( v[i] , v[i + 1] );
}
}
for (int i = n ; i >= 2 ; -- i){
if(v[i] == i){
++ ans ;
std::swap( v[i] , v[i - 1] );
}
}
printf ("%d\n" , ans );
return 0;
}

RDay2-Problem 1 A的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. Mysql完整约束性

    一.介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  2. openstack基础:网络

    Neutron 功能 Neutron 为整个 OpenStack 环境提供网络支持,包括二层交换,三层路由,负载均衡,防火墙和 *** 等.Neutron 提供了一个灵活的框架,通过配置,无论是开源还 ...

  3. 【学习总结】GirlsInAI ML-diary day-11-while循环

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day11 认识while循环执行 对于while/break/continue的认识 新值替换变量 一般while语句 无 ...

  4. tomcat发请求,查看各个环节的耗时时间

    从一台机器给另一台机器tomcat发请求,查看各个环节的耗时时间 - 业精于勤,荒于嬉:行成于思,毁于随. - CSDN博客https://blog.csdn.net/YAOQINGGG/articl ...

  5. vue.js实战——splice使用

    Vue在检测到数组变化时,并不是直接重新渲染整个列表,而是最大化地复用DOM元素.替换的数组中含有相同元素的项不会被重新渲染,因此可以大胆地用新数组来替换就数组,不用担心性能问题. 需要注意的是,以下 ...

  6. 常用CSS3

    (ಥ_ಥ)    啊啊,我的胃好疼啊.感觉最近胃又开始不舒服了.啊——果然老了呢,想当初,我也是不坏金刚之身来着呢,唉,我的可怜的小胃胃   (ಥ_ಥ) 记录几个已经常见的不能再常见的css3样式. ...

  7. sgu438-The_Glorious_Karlutka_River

    Description SGU似乎死了... 题目搬到了Codeforces... Problem - 99999438 - Codeforces Solution 动态最大流. 考虑如果不求时间, ...

  8. 进程间通信(队列、管道)、消费者模型和进程池(apply,apply_async,map)

    一.队列(先进先出) 进程间通信:IPC(Inter-Process Communication) 队列是使用管道和锁定实现,所以Queue是多进程安全的队列,使用Queue可以实现多进程之间的数据传 ...

  9. wrk编译报错gcc: Command not found

    报错信息如下: 问题原因:没有安装gcc 解决办法: yum -y install gcc+ gcc-c++ 若需升级gcc,则采用如下命令: yum -y update gcc

  10. WinForm登录验证

    概述:输错三次禁止登陆,15分钟后才能继续. 图示: Form1代码: using System; using System.Configuration; using System.Data.SqlC ...