先上题目:

Little Elephant and Sorting
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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 < nai ≤ ai + 1 holds.

Input

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.

Output

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.

Sample test(s)
input
3
1 2 3
output
0
input
3
3 2 1
output
2
input
4
7 4 1 47
output
6
Note

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的更多相关文章

  1. 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 ...

  2. CodeForces - 204C Little Elephant and Furik and Rubik

    CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...

  3. Educational Codeforces Round 67 D. Subarray Sorting

    Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...

  4. Codeforces D. Little Elephant and Interval(思维找规律数位dp)

    题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...

  5. Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp

    Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...

  6. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  7. CodeForces 259A Little Elephant and Chess

     Little Elephant and Chess Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  8. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  9. Codeforces 204A Little Elephant and Interval

    http://codeforces.com/problemset/problem/204/A 题意:给定一个[L,R]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...

随机推荐

  1. AngularJS过滤器filter-保留小数-渲染页面-小数点-$filter

    AngularJS      保留小数 默认是保留3位 固定的套路是 {{deom | number:4}} 意思就是保留小数点 的后四位 在渲染页面的时候 加入这儿个代码 用来精确浮点数,指定小数点 ...

  2. MySQL数据库笔记总结

    MySQL数据库总结 一.数据库简介 1. 数据 所谓数据(Data)是指对客观事物进行描述并可以鉴别的符号,这些符号是可识别的.抽象的.它不仅仅指狭义上的数字,而是有多种表现形式:字母.文字.文本. ...

  3. 小HY的四元组

    4.7 比赛T1,然而这题爆零了 其实很简单的...其实哈希都不用 所以首先记录每组的差值,按其sort一下再暴力找即可 #include<cstdio> #include<iost ...

  4. Git学习之序

    最近在忙毕业论文的事,需要用NS2仿真,其中需要修改NS2的源码,故想藉此机会学习一下Git,方便代码的管理. 由于我在以前实习的时候接触过代码管理工具SVN,因此对代码管理的一些概念还是有的.如果从 ...

  5. android 中的Context(一)

    context的功能如此强大,它是activity的父类. public abstract class Context { ... public abstract Object getSystemSe ...

  6. BZOJ 2829 凸包

    思路: 把信用卡周围去掉  只剩下中间的长方形 最后的答案加上一个圆 //By SiriusRen #include <bits/stdc++.h> using namespace std ...

  7. 海量文本信息查Top-k

    问题描述: 有1千万条短信,一条一行,有重复.在5分钟之内,找出重复出现的前10条. 方案一: 1.分组进行边扫描边建散列表.建立哈希表,使用头,尾和中间随便两个字节作为Hash Code, 插入到H ...

  8. phpstorm如何在同一个文件夹打开多个目录

    phpstorm默认一个窗口只显示一个项目,如果新建一个项目,他会给你个选项卡,问你是在新窗口打开新项目还是在本窗口打开. 能不能在一个窗口打开多个项目呢?就像sublime text那样,其实是可以 ...

  9. Android:用签名打包后微信分享失效

    刚开始使用微信分享,申请的微信appid也可以在直接使用,分享成功! 当我使用自己的签名打包分享时却分享失败,一闪而过,好郁闷的说,为什么之前没有打包就可以,签名打包后就不可以了... 开始查找各种资 ...

  10. SQL基本操作——COVERT

    CONVERT() 函数是把日期转换为新数据类型的通用函数.CONVERT() 函数可以用不同的格式显示日期/时间数据. --语法 CONVERT(data_type(length),data_to_ ...