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.
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 one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
6 0 1 0 3 0 2
6
5 0 1 2 1 2
1
5 0 1 1 2 2
0
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;
}