Codeforces 847 H. Load Testing (技巧)

Description

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 ≤ 10^9), 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.

 

Examples input

5
1 4 3 2 5

 

Examples output

6

 

题意

给出一个数组,现在我们想让它前半部分递增,后半部分递减,问需要改变数的大小之和。

 

思路

统计原数组距离最小递增序列的前缀距离与原数组距离最小递减序列的后缀距离。

然后从左到右枚举一遍即可。

 

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef __int64 LL;
const int maxn = 1e5+10;
LL a[maxn],b[maxn];
LL c[maxn],d[maxn];
LL e[maxn],f[maxn];
LL n;

int main()
{
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    LL lmaxf = a[0];
    e[0] = lmaxf;
    for(int i=1; i<n; i++)
    {
        c[i] = c[i-1];
        if(a[i]<=lmaxf)
        {
            c[i] += lmaxf - a[i] + 1;
            lmaxf += 1;
        }
        else
            lmaxf = max(lmaxf,a[i]);
        e[i] = lmaxf;
    }
    lmaxf = a[n-1];
    f[n-1] = lmaxf;
    for(int i=n-2; i>=0; i--)
    {
        d[i] = d[i+1];
        if(a[i]<=lmaxf)
        {
            d[i] += lmaxf - a[i] + 1;
            lmaxf += 1;
        }
        else
            lmaxf = max(lmaxf,a[i]);
        f[i] = lmaxf;
    }
    LL ans = LLONG_MAX;
    for(int i=0; i<n; i++)
    {
        LL ci = max(e[i],f[i]) - e[i];
        ans = min(ans,c[i]+d[i+1]+ci);
    }
    cout<<ans<<endl;
    return 0;
}
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值