Codeforces Round #129 (Div. 2) B
Description
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 that l ≤ 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].
题意:可以把区间的数增加相同的值(值的范围是1-n),让数列成为非递减
解法:当然让不符合要求的数字增加到最近的最大值,比如 7 4 1变成7 7 7 ,我们只需要计算7 4,4 1之间的差值就行(增加同一个数,两个数的差值不变的)
#include<bits/stdc++.h>
using namespace std;
long long a[100005];
int pos;
int n;
int d;
int main()
{
long long sum=0;
int pos=0;
int flag=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
d=a[1];
for(int i=2;i<=n;i++)
{
if(a[i]<a[i-1])
{
sum+=(a[i-1]-a[i]);
// pos=(d-a[i]);
}
// cout<<a[i]<<"A"<<endl;
}
cout<<sum<<endl;
return 0;
}
Codeforces Round #129 (Div. 2) B的更多相关文章
- Codeforces Round #129 (Div. 2)
A. Little Elephant and Rozdil 求\(n\)个数中最小值的个数及下标. B. Little Elephant and Sorting \[\sum_{i=1}^{n-1}{ ...
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...
- Codeforces Round #129 (Div. 2) C
Description The Little Elephant very much loves sums on intervals. This time he has a pair of intege ...
- Codeforces Round #129 (Div. 2) A
Description The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. &quo ...
- Educational Codeforces Round 129 (Rated for Div. 2) A-D
Educational Codeforces Round 129 (Rated for Div. 2) A-D A 题目 https://codeforces.com/contest/1681/pro ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- Cannot find PHPUnit in include path (.;C:\php5\pear)
--pear channel-discover pear.phpunit.de --pear install phpunit/PHPUnit 此时会显示: No valid packages foun ...
- UML Design Via Visual Studio-Sequence Diagram
本文主要介绍在Visual Studio中设计时序图,内容如下: 何时使用时序图 时序图元素介绍 条件.循环在时序图中的使用 直接通过代码生成时序图 一.何时使用时序图 当要查看单个用例内若干对象的行 ...
- 简单易懂dubbo入门实例
一.创建Maven多模块项目 项目结构如下 模块介绍: dubbo-api ----API接口 dubbo-consumer ----消费者 dubbo-provider ...
- Poj 1316 Self Numbers(水题)
一.Description In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called se ...
- idea-spark-sbt 打包jar
1.打开idea下的terminal窗口 2.只打包部分项目 sbt insight-import/clean insight-import/assembly 这表示只打包主目录下的insight- ...
- 测试RDP回放
Dim fso,num,flagflag=trueset bag=getobject("winmgmts:\\.\root\cimv2") Set fso=CreateObject ...
- kafka 基础知识梳理(转载)
一.kafka 简介 kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键因 ...
- jquery.html5uploader.js 上传控件
插件地址:http://blog.csdn.net/never_say_goodbye/article/details/8598521 先上个效果图: 相比来说,效果还是很不错的 使用MVC3做服务器 ...
- 关于JAVA中的回调接口传值机制
详见博文http://blog.csdn.net/xiaanming/article/details/8703708
- 关于ArcGis for javascrept之Map类
ArcGis for javascrept_ESRI_Map类: 1. 构造方法:esri.Map(); 参数: extent 如果设置了该选项,一旦这个选项的投影被设置,那么所有的图层都在定义的投 ...