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

      

  • 相关阅读:
    day044CSS相关属性之盒子模型、float浮动、overflow溢出属性、position定位
    day045JavaScript的介绍、数据类型
    day043 前端之CSS引入方式、选择器、相关属性
    day042前端之HTML
    攻防世界wp--web simple_php
    hackbar的谷歌和火狐破解方式
    攻防世界wp--web cookie
    攻防世界wp-misc stegano
    攻防世界wp-misc 功夫再高也怕菜刀
    攻防世界wp-misc base64stego
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9479941.html
Copyright © 2011-2022 走看看