zoukankan      html  css  js  c++  java
  • cf978E Bus Video System

    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
    3 5
    2 1 -3
    output
    3
    input
    2 4
    -1 1
    output
    4
    input
    4 10
    2 4 1 2
    output
    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 <bits/stdc++.h>
    using namespace std;
    const int MAXN=200010;
    const int INF=0x3f3f3f3f;
    int sum[MAXN];
    
    int main()
    {
        int n,w,a;
        scanf("%d%d",&n,&w);
        int MAX=0,MIN=INF;
        for (int i = 1; i <=n ; ++i) {
            scanf("%d",&a);
            sum[i]=sum[i-1]+a;
            MAX=max(MAX,sum[i]);
            MIN=min(MIN,sum[i]);
        }
        MAX=w-MAX;
        MIN=MIN>0?0:-MIN;
        if(MAX-MIN+1<0) printf("0
    ");
        else printf("%d
    ",MAX-MIN+1);
    
        return 0;
    }
    

      

  • 相关阅读:
    用GDB调试程序(一)
    关于“鸡脚神”的看法
    Oracle 经典SQL 专为笔试准备
    怎样设计接口?
    myeclipse6.0下载及注冊码
    VB连接Mysql数据库
    开源html5_kiwijs_helloworld
    server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh
    js实现自己定义鼠标右键-------Day45
    C/C++程序猿必须熟练应用的开源项目
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9479941.html
Copyright © 2011-2022 走看看