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 ...
随机推荐
- hdu-1728(贪心&&bfs的灵活运用吧)
链接 [https://vjudge.net/contest/256476#problem/D] 题意 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到 ...
- Redisson 分布式锁
Redisson_百度百科https://baike.baidu.com/item/Redisson/20856570 redission 分布式锁 - 穆穆兔兔 - 博客园https://www.c ...
- js判断数组中有没有指定元素
list.findIndex(target => target.sel === 指定元素) ,如果找到返回下标,如果没有返回-1
- HttpInvoker http请求工具类
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import j ...
- python之路3-元组、列表、字典、集合
1.元组 特点:一旦创建,内容不可修改,又叫只读列表 a= ('wang','zhang','zhao') print(a.count('zhao')) print(a.index('wang')) ...
- LODOP提示、报错、现象,简短问答
提示升级提示:“打印控件需要升级!点击这里执行升级,升级后请重新进入."“Web打印服务CLodop需升级!点击这里执行升级,升级后请刷新页面.”(新版提示) 参考http://www.c- ...
- 修改帝国cms栏目后,如何更新
修改栏目后,要依次做如下更新: 1. 2. 3. 如果只是修改了栏目里的属性,只需要做第三步就行了
- LOJ #6285 分块入门9
题意:区间众数,不带修改,带修改刚看了一眼没看懂cls在讲啥QAQ. 题解:按照代码中那个sqrt(n/2/log2(n))大小分块,可以用均值不等式证明的,就是假设查询和n同级,然后一通爆算就可以得 ...
- java 11 增加了一系列的字符串处理方法,Optional 加强 ,改进的文件API
增加了一系列的字符串处理方法 如以下所示. // 判断字符串是否为空白 " ".isBlank(); // true // 去除首尾空白 " Javastack &quo ...
- mac安装postman
[在线方式]: 1. chrome扩展中搜postman,选择Postman Interceptor 添加到chrome. 2.安装完毕后,点击地址栏右边对应的按钮 点击 Postman Chrome ...