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]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...
随机推荐
- putty+Xmanager登陆Linux,实现图形界面操作.
- jsp jquery js 的基本路径获取
引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决! 1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...
- AbstractRoutingDataSource动态选择数据源
当我们项目变大后,有时候需要多个数据源,接下来我们讲一种能等动态切换数据源的例子. 盗一下图: 单数据源的场景(一般的Web项目工程这样配置进行处理,就已经比较能够满足我们的业务需求) 多数据源多Se ...
- E20170620-hm
dump n. 垃圾场; 仓库; 无秩序地累积; vt. 倾倒; 倾销; 丢下,卸下; 摆脱,扔弃; terminate vt. 结束; 使终结; 解雇; 到达终点站; adj. 结束的; d ...
- [App Store Connect帮助]一、 App Store Connect 使用入门(3)首页概述
从首页可以访问 App Store Connect 的各个部分.您仅能访问每个部分中与您的用户职能相关联的功能. [提示]通过点按任何页面顶部的“App Store Connect”,您可以随时返回 ...
- [Swift通天遁地]九、拔剑吧-(7)创建旋转和弹性的页面切换效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]九、拔剑吧-(9)创建支持缩放、移动、裁切的相机视图控制器
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Akka源码分析-Remote-位置透明
上一篇博客中,我们研究了remote模式下如何发消息给远程actor,其实无论如何,最终都是通过RemoteActorRef来发送消息的.另外官网也明确说明了,ActorRef是可以忽略网络位置的,这 ...
- JavaScript--Array 数组对象
Array 数组对象 数组对象是一个对象的集合,里边的对象可以是不同类型的.数组的每一个成员对象都有一个“下标”,用来表示它在数组中的位置,是从零开始的 数组定义的方法: 1. 定义了一个空数组: v ...
- 【BZOJ2944】[Poi2000]代码(卡特兰数)
这题在网上找不到题解,硬写一下午终于写出来了-- 题目: BZOJ2944 分析: 首先明确: 比较两棵节点数相同的二叉树时,根节点是第一关键字,左子树是第二关键字,右子树是第三关键字: 然后我们分析 ...