C - Load Testing CodeForces - 847H

Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.

Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.

Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.

For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.

Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of requests from friends in the i-th minute of the load testing.

Output

Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.

Example
Input

5
1 4 3 2 5

Output

6

Input

5
1 2 2 2 1

Output

1

Input

7
10 20 40 50 70 90 30

Output

0

Note

In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.

In the second example it is enough to make one additional request in the third minute, so the answer is 1.

In the third example the load already satisfies all conditions described in the statement, so the answer is 0.

题意:给出一串数列,要求改成符合标准的数列需要添加几个数。
符合标准的数列:由一个递增数列和一个递减数列组成,并且连续,不能出现相等的数字,可以只出现递增数列或者只有递减数列。
思路:拿第一组数据来说,1 4 3 2 5,如果是递增数列则应为1 4 5 6 7(从第一个往后看) ,那么每个数都差了0 0 2 4 2,前i个累加起来就是0 0 2 6 8将其存在f[]数组里。如果只是递减数列则为9 8 7 6 5(从最后一个往前看),则他们的差为8 4 4 4 0,后i个累加起来就是20 12 8 4 0存在g[]数组里,f[i]+g[i+1]刚好为一整个数列的差值,求这n+1个里面最小的那个(可以全是f[i]或者g[i],所以是n+1个)

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
long long int n,a[1000010],b[1000010],f[1000010],g[1000010],s,min;
int main()
{
    int i,n;
    s=0;
    min = 1e18;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        b[i] = a[i];
    }
    for(i=1;i<=n;i++)
    {
        if(a[i]<=a[i-1])
        {
            f[i] = f[i-1] + a[i-1]+1 - a[i];
            a[i] = a[i-1]+1;
        }
        else
            f[i] = f[i-1];
    }
    for(i=n;i>=1;i--)
    {
        if(b[i]<=b[i+1])
        {
            g[i] = g[i+1]+b[i+1]+1-b[i];
            b[i] = b[i+1]+1;
        }
        else
            g[i] = g[i+1];
    }
    for(int k=0;k<=n;k++)
    {
        s = f[k]+g[k+1];
        if(a[k] == b[k+1]) s++;
        if(s < min)
            min = s;
    }
   printf("%lld\n",min);
   return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值