RDay2-Problem 1 A
题目描述
初始给你一个排列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的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- Effective STL 读书笔记
Effective STL 读书笔记 标签(空格分隔): 未分类 慎重选择容器类型 标准STL序列容器: vector.string.deque和list(双向列表). 标准STL管理容器: set. ...
- __init__和__new__的区别
根据官方文档: __init__是当实例对象创建完成后被调用的,然后设置对象属性的一些初始值. __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例,是个静态方法. 也 ...
- 控制结构(1): 分枝/叶子(branch/leaf)
// 下一篇:卫语句(guard clause) 典型代码: function doSomething1(){ // ... } function doSomething2(){ // ... } f ...
- 深入Eureka/Feign/Hystrix原理学习(1)
第一步: 创建注册中心项目,引入cloud discovery相关依赖. ①在pom文件中引入相关依赖. ②在启动类上加上@EnableEurekaServer注解,标注这是一个注 册中心. ③在ap ...
- 笔记本装双系统!win10+Linux!所有的坑自己一个个爬过来,纪念一下。
笔记本装双系统!win10+Linux!所有的坑自己一个个爬过来,纪念一下. 2018年09月16日 21:27:19 Corax_2ven 阅读数:14038 写在前面,装了大概5遍,装了删删了 ...
- DataTable转list时 可空类型的转换问题
public class UtilHelper { public static IList<T> ConvertTo<T>(DataTable table) { if (tab ...
- photoshop编辑pdf文件
对于PDF文件透明背景的问题 PDF文件背景是透明的,如何使其变成白色 怎样通过photoshop打开多页PDF,编辑后仍保存为多页 注意shift全选,"页面选项"处的'裁剪到' ...
- Python--Linux上安装Python
Linux 上安装 Python 官网下载:https://www.python.org/downloads/ 本文安装包下载链接:https://pan.baidu.com/s/1uL2JyoY_g ...
- MySQL安装-二进制软件包安装
MySQL 双版本安装 安装mysql AB (RPM) -mysql官方的RPM包 安装MySQL 5.6.19版本 安装之前需要将系统自带的关于mysql软件全部卸载掉 rpm -e (加 ...
- shell 基础(二)变量
1. shell变量的定义 1)Shell 支持以下三种定义变量的方式: variable=value variable='value' variable="value" 特点 1 ...