codeforces Riverside Curio

C. Riverside Curio
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.

Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.

Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.

Input

The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.

The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.

Output

Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.

Examples
input
Copy
6
0 1 0 3 0 2
output
6
input
Copy
5
0 1 2 1 2
output
1
input
Copy
5
0 1 1 2 2
output
0
Note

In the first example, the following figure shows an optimal case.

Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.

In the second example, the following figure shows an optimal case.

题意:  小明每天去观察水的位置  给你n 个数  表示n天能看到的在水面上的白线。

(如果今天比最低水位线还低  那么就画一条白线) 你要求出这n 天水下!!的白线

思路:   第一:  我们需要贪心的处理出来每天的白线的总数量。  我们知道今天的白线总数量和昨天的白线总数量的差值小于等于1 。  所以我们利用这一点求出每天的白线总数量 

首先  今天在水面上的白线总数量我们是知道的  ,那么在水面和水面之上的白线总数量就为m[i]+1   , 那么因为今天的白线总数量不会比昨天的少  那么 肯定   a[i]=max(a[i],a[i-1])   (2->  n),但是还有一个条件就是今天的白线总数量最多只能比昨天的数量大一  ,,所以 我们可以得到:  a[i]=max(a[i],a[i+1]-1);   (n-1 ->  1)  . 那么我们就可以贪心的算出每天最小的白线总数量。那么结果就出来了。

#include<bits/stdc++.h>
#define N 100005

using namespace std;

int m[N];
int a[N];

int n;

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>m[i];
		a[i]=m[i]+1;
	}
	
	for(int i=1;i<=n;i++) a[i]=max(a[i],a[i-1]);
	
	for(int i=n-1;i>=1;i--)
	{
		a[i]=max(a[i],a[i+1]-1);
	}
	for(int i=1;i<=n;i++) m[i]++;
	
	long long ans=0;
	for(int i=1;i<=n;i++)
	{
		ans+=(a[i]-m[i]);
	}
	
	printf("%lld\n",ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值