题目描述

初始给你一个排列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 c connector 多条sql语句执行示例

      //  假设参数 sql已经包含多条sql语句.如 sql = "insert into table1(...) values(...); update table2 set a=1;& ...

  2. 静态类(C#)

    基本简介: 类可以声明为 static 的,以指示它仅包含静态成员.不能使用 new 关键字创建静态类的实例.静态类在加载包含该类的程序或命名空间时由 .NET Framework 公共语言运行库 ( ...

  3. Thymeleaf的超链接与AJAX的跳转问题

    //th:href :超链接<a th:href="@{/list}"></a>//可以在其他页面跳转yt <form id="msform ...

  4. Python——assert(断言函数)

    一.断言函数的作用 python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假.可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会 ...

  5. Converting PDF to Text in C#

    Parsing PDF files in .NET using PDFBox and IKVM.NET (managed code). Download source files - 82 kB [c ...

  6. 一个".java"源文件中是否可以包括多个类

    可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 现在我们编个测试文件来测试一番(一个程序员要具有用于探索的精神   -.-     手动滑稽) 1.编写一个 a ...

  7. socketserver和socket的补充(验证客户端合法性)

    一.socket的补充 1.参数 socket.socket(family=AF_INET,type=SOCK_STREAM,proto=0,fileno=None) 参数说明: family 地址系 ...

  8. linux18.04+jdk11.0.2+hadoop3.1.2部署伪分布式

    1. 下载 安装hadoop3.1.2http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.1.2/hadoop-3.1.2.tar.gz 注意 ...

  9. URL传递中文参数乱码问题

    web项目开发中,经常遇到中文参数乱码问题,而且有时候明明测试服务器上好用,换个正式环境就不用的情况也经常出现,今天做一个记录,防止自己以后忘记 1.地址栏url请求带中文参数 这类

  10. python之模块之shutil模块

    shutil -- --High-level file operations  高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说: ...