题干:
The Little Elephant loves sortings.
He has an arrayaconsisting ofnintegers. Let's number the array elements from 1 ton, then thei-th element will be denoted asai. The Little Elephant can make one move to choose an arbitrary pair of integerslandr(1 ≤ l ≤ r ≤ n)and increaseaiby1for allisuch thatl ≤ i ≤ r.
Help the Little Elephant find the minimum number of moves he needs to convert arrayato an arbitrary array sorted in the non-decreasing order. Arraya, consisting ofnelements, is sorted in the non-decreasing order if for anyi(1 ≤ i < n)ai ≤ ai + 1holds.
Input
The first line contains a single integern(1 ≤ n ≤ 105)— the size of arraya. The next line containsnintegers, separated by single spaces — arraya(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%lldspecifier to read or write 64-bit integers in С++. It is preferred to use thecin,coutstreams or the%I64dspecifier.
Examples
Input
31 2 3
Output
0
Input
33 2 1
Output
2
Input
47 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is0.
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].
题目大意:
给你n个数,定义一次操作:任选一个区间+1,目的是让后一个数不能小于前一个数(不下降子序列),然后最小操作数是多少?
解题报告:
脑补一下过程就好了。就好了。。注意longlong。(好像好多人int交是错的)
首先肯定是从前往后扫,因为这个问题如果倒着找的话,你不知道应该加到多少(因为前面的数字还没有定下来)。从前往后找但是相对差值是不变的,因为每次加的时候肯定要连带着后面的数字一块加,这样是最方便的。想到这里代码就出来了。
AC代码:
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<map>#include<vector>#include<set>#include<string>#include<cmath>#include<cstring>#define ll long long#define pb push_back#define pm make_pair#define fi first#define se secondusing namespace std;const int MAX = 2e5 + 5;ll a[MAX];int main(){int n;ll ans = 0,cur = 0;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i); cur = a[1];for(int i = 1; i<=n; i++) {if(a[i] < cur) {ans += (cur-a[i]);}cur = a[i];}printf("%lld\n",ans);return 0 ;}
错误代码(找到的一个错误代码):总之这个代码错误很多,比如应该是if(arr[i]<base),再比如else中也应该更新base,,,但是有一个值得注意的点就是那个else中,不能更新minn!!
#include <iostream>#include <algorithm>#include <string>#include <map>#include <set>#include <vector>#include <stack>#include <queue>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;int main(){int n,i;ll base=0;;cin>>n;ll arr[100010]={0},count=0,mint=0;for(i=0;i<n;i++){cin>>arr[i];if(arr[i]>=base){count+=base-mint;base=arr[i];mint=arr[i];}else{mint=min(mint,arr[i]);}}cout<<count+(base-mint)<<endl;return 0;}
总结:
其实整个题的思路是,看样例猜算法,猜出个大概的,(或者猜出好多个算法),然后证明一下看哪个是正确的,或者帮助我们排除掉一些错误的。
思维过程是,这题首先想到是不能模拟的啊,1e5的数据量,500ms的时间,只能O(n),所以肯定不能模拟整个过程,然后开始找规律,找到,写代码,提交,AC。