CodeForces - 205B - Little Elephant and Sorting
先上题目:
1 second
256 megabytes
standard input
standard output
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such thatl ≤ i ≤ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds.
The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing.
In a single line print a single integer — the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
3
1 2 3
0
3
3 2 1
2
4
7 4 1 47
6
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
题意:给出一个序列,每次可以将一个区间里面的数都加一,最终使整个序列非递减,问最少的操作数的多少。
一开始想的是单调栈,于是一直想不出可以通过自己的数据的思路。
后来想了一下其实可以扫描序列,记录下已扫描的部分的最大值,然后如果当前位置的值比前一个数小,说明需要加上两者的差值才能保证前方是非递减的,然后就是如果当前值比前一个值大,那么如果当前值比最大值还要大,那么就说明前面的部分统计的次数可以加入ans了,因为后面是和前面分开的,如果当前值比最大值小,那么说明在区间增加的一定次数(比统计值小的次数)就可以达到最大值,当达到最大值以后这个位置后面的序列就不能再和前面的序列一起加一了,但是因为需要统计前面的次数,所以修改次数那里需要减少前方修改多出来的那部分次数。
最后统计一下就可以了。说起来可能有点模糊,详细见代码。
上代码:
#include <cstdio>
#include <cstring>
#define MAX 100002
#define ll long long
using namespace std; ll s[MAX]; int main()
{
int n;
ll ans,q,m;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
scanf("%I64d",&s[i]);
}
ans=q=;
m=s[];
for(int i=;i<=n;i++){
if(s[i-]>=s[i]){
q+=s[i-]-s[i];
}
else{
if(m<s[i]){
m=s[i];
ans+=q;
q=;
}else{
ans+=q-(m-s[i]);
q=m-s[i];
}
}
}
ans+=q;
printf("%I64d\n",ans);
}
return ;
}
/*Little Elephant and Sorting*/
CodeForces - 205B - Little Elephant and Sorting的更多相关文章
- CodeForces - 258D Little Elephant and Broken Sorting
Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...
- CodeForces - 204C Little Elephant and Furik and Rubik
CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- Codeforces D. Little Elephant and Interval(思维找规律数位dp)
题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp
Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- CodeForces 259A Little Elephant and Chess
Little Elephant and Chess Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- Codeforces 204A Little Elephant and Interval
http://codeforces.com/problemset/problem/204/A 题意:给定一个[L,R]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...
随机推荐
- Mysql 数据库编码问题
数据库建表后,插入数据,如果数据位汉子,将提示错误:“incorrect string value....”. 解决方法:改变数据库编码 第一种方法:改变database 编码: alter data ...
- E20171006-hm
trace vt. 跟踪,追踪; 追溯,探索; 探索; 查找; vi. 沿着一小径或道路前进; 可以追溯的; n. 痕迹; 痕迹,踪迹; 微量,极少量; [植 ...
- frameset的target属性
使用frameset时的target属性 (2012-09-18 08:19:31) 转载▼ 分类: java技术之路 一般常用的有四个属性 _blank 浏览器总在一个新打开.未命名的窗口中载入 ...
- kmp的练习们
//poj3461 Oulipo //kmp模板 统计子串在母串中的位置 #include<iostream> #include<cstdio> #include<cst ...
- Log4J2的 PatternLayout
Log4J2 PatternLayout 参考 日志样例 : 2018-10-21 07:30:05,184 INFO - DeviceChannelServiceImpl.java:434[defa ...
- [Windows Server 2008] Apache+PHP安全设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:Win2008 ...
- 搜索条件两个时间,通过php数组排序,保证select语句between时间 前小后大
//搜索条件两个时间,通过数组排序,保证select语句between时间 前小后大 $sort_array=[$_POST['clockDate1'],$_POST['clockDate2']]; ...
- 排序算法JavaScript版
冒泡排序 function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (va ...
- git怎么克隆远程仓库到本地仓库
参考: https://blog.csdn.net/zhangzeshan/article/details/81564990 不知道为什么输入git的克隆地址就会提示密码错误 ,使用http地址就直接 ...
- MFC CAD控制权问题
begineditorcommand(); 隐藏对话框 把控制权交给CAD completeeditorcommand(); 完成交互返回到应用程序 canceleditorcommand CAD被 ...