日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 205B 】Little Elephant and Sorting (思维)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 205B 】Little Elephant and Sorting (思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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?aiby?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?nelements, is sorted in the non-decreasing order if for any?i?(1?≤?i?<?n)?ai?≤?ai?+?1holds.

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?%I64d?specifier.

Examples

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

題目大意:

? ?給你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 second using 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 0x3f3f3f3f using 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。

總結

以上是生活随笔為你收集整理的【CodeForces - 205B 】Little Elephant and Sorting (思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。