Increasing Array

今天大早上的起来写博客是为什么呢

没错!今天是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快乐鸭!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bamboo_Day

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值