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 ...
随机推荐
- 使用Crowd集成Confluence与JIRA
一. 独立安装Crowd,步骤1-步骤13的内容二. 设置Confluence使用Crowd进行认证.步骤14-18的内容三. 设置JIRA使用Crowd进行认证,并使用Confluence的组织机构 ...
- JSON数组形式字符串转换为List<Map<String,String>>的8种方法
package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArr ...
- BEX5下新建任务到待办任务
List<OrgUnit> list = new ArrayList<OrgUnit>(); // 1.查询出执行者 String sql = "select t.f ...
- MySQL字符串进行加减乘除的运算
原文链接:https://www.jianshu.com/p/2ab2c0dc3cb5 在mysql当中,字符串类型间进行加减乘除运算的时候,会截取字符串以数字开头的那一部分数字进行运算,如果字符串前 ...
- js变速动画函数封装 回调函数及层级还有透明度
//点击按钮,改变宽度到达一个目标值,高度到达一个目标值 //获取任意一个元素的任意一个属性的当前的值---当前属性的位置值 function getStyle(element, attr) { re ...
- 爬虫之Scrapy框架介绍
Scrapy介绍 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内 ...
- 帝国cms打开慢
发现有个站(http://www.953239.com/)打开很慢,改了php版本后,快了一倍
- 51Nod--1247 可能的路径(gcd)
根据规则可知 假设 (a,b) 可以到达坐标(aa,bb) 那么 aa=a*x+b*y x y 必定有解 所以 我们只要求两个坐标的gcd看是否相等就好 #include<bits/stdc ...
- Hadoop记录- zookeeper 监控指标
目前zookeeper获取监控指标已知的有两种方式: 1.通过zookeeper自带的 four letter words command 获取各种各样的监控指标 2.通过JMX Client连接zo ...
- 069、Calico的默认连通性(2019-04-12 周五)
参考https://www.cnblogs.com/CloudMan6/p/7536746.html Calico 跨主机连通性测试 root@host1:~# docker exec bbo ...