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;
    }
  • 相关阅读:
    万能转换器boost::lexical_cast
    6.1.5 文本文件与二进制文件
    HDU4002 Find the maximum [数论]
    HDU4001 To Miss Our Children Time [DP]
    HDU3247 Resource Archiver [AC自动机+DP]
    HDU2457 DNA repair [AC自动机+DP]
    HDU2825 Wireless Password [AC自动机+压缩DP]
    ZOJ3228 Searching the String [AC自动机]
    HDU4003 Find Metal Mineral [树形DP]
    ZOJ3494 BCD Code [AC自动机+DP]
  • 原文地址:https://www.cnblogs.com/--lr/p/7895412.html
Copyright © 2011-2022 走看看