今天大早上的起来写博客是为什么呢
没错!今天是1024,祝各位神犇1024快乐!
今天讲的还是CSES的题目(因为题目简单)
进入正题 上题目!
- Time limit: 1.00 s
- Memory limit: 512 MB
You are given an array of n integers. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element.
On each move, you may increase the value of any element by one. What is the minimum number of moves required?
Input
The first input line contains an integer n the size of the array.
Then, the second line contains nn integers x1,x2,…,xn: the contents of the array.
Output
Print the minimum number of moves.
Constraints
- 1≤n≤2⋅10^5
- 1≤xi≤10^9
Example
Input:5
3 2 5 1 7
Output:5
题目翻译
时间限制:1.00 s
内存限制:512mb
给定一个由n个整数组成的数组。你想要修改数组,使其递增,也就是说,每个元素至少与前一个元素一样大。
在每次移动中,您可以将任何元素的值增加1。最少需要多少步?
输入
第一个输入行包含一个整数n:数组的大小。
然后,第二行包含n个整数x1,x2,…,xn:数组的内容。
输出
输出最小步数。
约束
1≤n≤2⋅10^5
1≤xi≤10^9
样例
输入:
5
3 2 5 1 7
输出:
5
思路
说实话第一眼看到这道题的时候我还愣一下,以为是最长上升子序列,仔细一看,原来就是个垃圾,这题很简单,输入怎么输我就不说了,可以直接在输入的时候就处理好,这样可以少些一个循环(超不超时的无所谓,主要就是懒)
上代码!
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200005] ;
long long sum;
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
if(i){
if(a[i] < a[i-1]){
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
}
cout << sum << endl;
return 0;
}
最后还是再次祝大家1024快乐鸭!