Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
来源:互联网 发布:成都湛源软件 编辑:程序博客网 时间:2024/05/21 09:48
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.
31 2 3
0
33 2 1
2
47 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].
题意:刚开始以为是要最少的次数把序列排成有序的,后来才发现是要在每个数上加数,然后是数列是非递减序列,我的思路是只要是后面一个数比前面的小,那么就加数直到两个数相等,以此类推,算出总次数。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int main(){int n,i;long long a,b,sum=0; while(~scanf("%d",&n)) { for (i=1;i<=n;i++){b=a; scanf("%d",&a);if (i>1&&a<b){ sum+=b-a;}}cout<<sum<<endl; }return 0;}
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #136 (Div. 2) B. Little Elephant and Numbers
- Codeforces Round #129 (Div. 2)A. Little Elephant and Rozdil
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #136 (Div. 2) / 221B Little Elephant and Numbers (数论)
- Codeforces Round #129 (Div. 1) A Little Elephant and Interval
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) A. Little Elephant and Function
- Codeforces Round #136 (Div. 2) C. Little Elephant and Problem
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) Little Elephant and Array
- <codeforces>Little Elephant and Sorting
- B. Little Elephant and Sorting
- nginx 获取源IP
- DWR入门配置
- dsdcss
- wxPython中文教程 简单入门加实例
- 集群技术
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- win7/8运行StartUML是报错解决方案
- Ubuntu安装JDK7
- leetcode: Remove Element
- 作业3a. 编写计算“两个整数的最大公约数”程序
- SQLServer char、varchar、nchar、nvarchar的区别
- ttf 字的水平距与垂直距
- RTEMS POSIX API 用户指南
- 解题笔记(21)——字符串的排列组合问题