zoukankan      html  css  js  c++  java
  • Load Testing CodeForces

    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 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 
     5 const int maxn=1e5+5;
     6 
     7 struct node{ ll num,mco; } d1[maxn],d2[maxn];
     8 
     9 ll n;
    10 ll a[maxn];
    11 
    12 void Inite(){
    13     for(int i=0;i<=n+1;i++){
    14         d1[i].mco=0;
    15         d2[i].mco=0;
    16     }
    17 }
    18 
    19 void Solve(){
    20     if(n==1||n==2){ cout<<"0"<<endl; return; }
    21     
    22     int temp=0;
    23     for(int i=1;i<=n;i++){
    24         if(temp<a[i]){
    25             d1[i].mco=d1[i-1].mco;
    26             d1[i].num=a[i];
    27             temp=a[i];
    28         }
    29         else{
    30             d1[i].mco=d1[i-1].mco+temp-a[i]+1;
    31             d1[i].num=temp+1;
    32             temp=temp+1;
    33         }
    34     }
    35     temp=0;
    36     for(int i=n;i>=1;i--){
    37         if(temp<a[i]){
    38             d2[i].mco=d2[i+1].mco;
    39             d2[i].num=a[i];
    40             temp=a[i];
    41         }
    42         else{
    43             d2[i].mco=d2[i+1].mco+temp-a[i]+1;
    44             d2[i].num=temp+1;
    45             temp=temp+1;
    46         }
    47     }
    48     
    49     ll ans=1e15;
    50     for(int i=1;i<=n;i++) ans=min(ans,d1[i-1].mco+d2[i+1].mco+max(d1[i].num,d2[i].num)-a[i]);
    51     cout<<ans<<endl;
    52 }
    53 
    54 int main()
    55 {   cin>>n;
    56     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    57     Inite();
    58     Solve();    
    59 }
  • 相关阅读:
    容器之队列的使用
    容器之栈的使用
    rapidxml的使用
    C++判断文件夹是否存在并创建文件夹
    VS2017,不能将const char *转为char *
    CSS_day01_选择器
    HTML_day02_列表
    HTML_day01基本标签
    python_day3
    python_day2
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7694249.html
Copyright © 2011-2022 走看看