zoukankan      html  css  js  c++  java
  • code force 893D Credit Card

    Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card.

    She starts with 0 money on her account.

    In the evening of i-th day a transaction ai occurs. If ai > 0, then ai bourles are deposited to Luba's account. If ai < 0, then ai bourles are withdrawn. And if ai = 0, then the amount of money on Luba's account is checked.

    In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d.

    It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1».

    Luba must not exceed this limit, and also she wants that every day her account is checked (the days when ai = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her!

    Input

    The first line contains two integers n, d (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) —the number of days and the money limitation.

    The second line contains n integer numbers a1, a2, ... an ( - 104 ≤ ai ≤ 104), where ai represents the transaction in i-th day.

    Output

    Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.

    Example

    Input
    5 10
    -1 5 0 -5 3
    Output
    0
    Input
    3 4
    -10 0 20
    Output
    -1
    Input
    5 10
    -5 0 10 -11 0
    Output
    2
    #include <iostream>
    #include <cstdio>
    #include <stack>
    using namespace std;
    int n,d,a[100000+20];
    int main()
    {
        scanf("%d%d",&n,&d);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        int minn=0,maxx=0;//当前账户的最小值和最大值
        int ans = 0;
        for(int i=0;i<n;i++)
        {
            if(a[i]==0)
            {
                //如果最小值小于零,则将最小值重新置为零
                if(minn<0)minn=0;
                //如果最大值小于零,则将最大值置为最大额度,次数加一
                //表示第i天早上去了一次银行,并且将账户存到最大额度
                if(maxx<0){maxx=d;ans++;}
            }
            else
            {
                //每次都要更新当前账户的最值
                minn+=a[i];
                maxx+=a[i];
                if(minn>d)//如果最小值大于最大额度输出-1
                {
                    printf("-1
    ");
                    return 0;
                }
                if(maxx>d)//如果最大值大于最大额度则将最大值置为最大额度
                {
                    maxx=d;
                }
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    (转)Xargs用法详解
    (转)运维老鸟教你安装centos6.5如何选择安装包
    (转)正则表达式与三剑客的使用技巧
    php OAuth服务端编写
    前端工程精粹(一):静态资源版本更新与缓存
    静态资源打包:一个javescript 的src引用多个文件,一个link引用多个CSS文件
    ie,cookie,域名与下划线
    HTML中css和js链接中的版本号
    PHP 优化详解
    jqury插件编写
  • 原文地址:https://www.cnblogs.com/--lr/p/7895412.html
Copyright © 2011-2022 走看看