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]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...
随机推荐
- 不使用c的任何库函数 实现字符串到整数的转换 整数到字符串的转换
转载请标明出处:http://www.cnblogs.com/NongSi-Net/p/6805844.html 今天主要总结下:完成编程: 1.除printf函数之外,不用任何c语言库函数,实现将字 ...
- 【转载】[Oracle] Linux下手动创建数据库过程
今天发现一个Oracle测试库的字符集设置不正确,原本的字符集是UTF-8,正确的字符集应该是ZHS16GBK,因为UTF-8是ZHS16GBK的超集,无法修改,只能重建数据库,幸好该测试库上还没有数 ...
- [Swift通天遁地]四、网络和线程-(4)使用Alamofire实现网络请求
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 小HY的四元组
4.7 比赛T1,然而这题爆零了 其实很简单的...其实哈希都不用 所以首先记录每组的差值,按其sort一下再暴力找即可 #include<cstdio> #include<iost ...
- 解决:xxx is not in the sudoers file.This incident will be reported.的解决方法
Linux中普通用户用sudo执行命令时报”xxx is not in the sudoers file.This incident will be reported”错误,解决方法就是在/etc/s ...
- [转]如何借助 TLS/SSL 确保套接字连接的安全(使用 C#/VB/C++ 和 XAML 的 Windows 应用商店应用)
本文转自:http://msdn.microsoft.com/zh-cn/library/windows/apps/jj150597.aspx 本主题将展示在使用 StreamSocket 功能时,如 ...
- 安装office1406错误!!!急死个人。。。。。
因为用到Microsoft.Office.Interop.Word,将word转PDF.所以在服务器上需要安装office,但是报错1406,不能将值插入注册表..... 然后百度各种问题,说需要将注 ...
- SAS进阶《深入解析SAS》之开发多语言支持的SAS程序
SAS进阶<深入解析SAS>之开发多语言支持的SAS程序 1. 多语言支持的应用程序是指该程序在世界给第使用时,其能够处理的数据,以及处理数据的方式.信息展现的方式都符合当地的语言.文化习 ...
- win32绘图基础
获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...
- Canvas清空
当canvs与bitmap绑定时,canvas上绘制会导致bitmap改变内容,而且内容时叠加的.这时候需要清空bitmap上的内容,可以用以下做法. Paint paint = new Paint( ...