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;
    }
    

      

  • 相关阅读:
    delegate
    URL、Session、Cookies、Server.Transfer、Application和跨页面传送,利弊比较
    C#中页面之间传值传参的六种方法
    Java学习之路:2、Mysql 链接与查询
    Java学习之路:1、HelloWorld
    Memcache 分布式解决方案 之 : 普通 Hash 分布
    每日一记:搭建Memcached + php 缓存系统
    四、记一次失败的 CAS 搭建 之 结果总是那么伤(客户端)
    三、记一次失败的 CAS 搭建 之 服务端配置
    二、记一次失败的 CAS 搭建 之 证书配置
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9479941.html
Copyright © 2011-2022 走看看