• E. Bus Video System


    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.

    If xx is the number of passengers in a bus just before the current bus stop and yy is the number of passengers in the bus just after current bus stop, the system records the number yxy−x. So the system records show how number of passengers changed.

    The test run was made for single bus and nn bus stops. Thus, the system recorded the sequence of integers a1,a2,,ana1,a2,…,an (exactly one number for each bus stop), where aiai is the record for the bus stop ii. The bus stops are numbered from 11 to nn in chronological order.

    Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww (that is, at any time in the bus there should be from 00 to ww passengers inclusive).

    Input

    The first line contains two integers nn and w(1n1000,1w109)(1≤n≤1000,1≤w≤109) — the number of bus stops and the capacity of the bus.

    The second line contains a sequence a1,a2,,ana1,a2,…,an (106ai106)(−106≤ai≤106), where aiai equals to the number, which has been recorded by the video system after the ii-th bus stop.

    Output

    Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.

    Examples
    input
    Copy
    3 5
    2 1 -3
    output
    Copy
    3
    input
    Copy
    2 4
    -1 1
    output
    Copy
    4
    input
    Copy
    4 10
    2 4 1 2
    output
    Copy
    2
    Note

    In the first example initially in the bus could be 00, 11 or 22 passengers.

    In the second example initially in the bus could be 11, 22, 33 or 44 passengers.

    In the third example initially in the bus could be 00 or 11 passenger.

    维护一个最小值和最大值。然后求上下界的交。

    #include <iostream>
    #include <bits/stdc++.h>
    #define maxn 1005
    using namespace std;
    int a[maxn]={0};
    int main()
    {
        int n,w,i;
        scanf("%d%d",&n,&w);
    
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int sum=0;
        int minum=1e9+5;
        int maxim=0;
        for(i=1;i<=n;i++)
        {   minum=min(minum,sum);
            maxim=max(maxim,sum);
            sum+=a[i];
        }
        minum=min(0,min(minum,sum));
        maxim=max(0,max(maxim,sum));
        if(abs(minum)>w||abs(maxim)>w)
        {
            printf("0
    ");
            return 0;
        }
        else
        {
           int l1=abs(minum);
           int r2=abs(w-maxim);
           int ans=r2-l1+1;
           if(ans<0) ans=0;
           cout<<ans<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    【2020-02-02】禅修无处不在
    【2020-02-01】接受改变这个常态
    2 分法查找内容
    python 单例模式
    day 34 js 基础后部分 BOM 和 事件和正则
    第三次网编考试
    day 33js 后续 函数.对象
    爬虫 自动生成请求头教程
    请求数据分析 xpath语法 与lxml库
    sanic 计划学习这个
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9039800.html
Copyright © 2020-2023  润新知