Educational Codeforces Round 33 (Rated for Div. 2)

来源:互联网 发布:军爷捏脸数据 编辑:程序博客网 时间:2024/06/05 17:07

D. Credit Card
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
Examples
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
//代码写的可读性比较差,解释一下。
//从左往右递推,首先我们可以将0与0之间分成区间来看待。
如果说[i,j]区间的区间和与i之前的区间可达到的和小于0的话
那么显然需要deposit,由于deposit有限制为d,但又不能确定
为d这是因为后面区间可能其和加上deposit的值会大于d,于是
需要另外讨论,即设置一个limit记录可以加上的值,这个值由
后面的区间的子区间的Max值不断进行缩小,另外如果i之前的区间
和大于0,显然也是需要记录的,这是因为递推到的当前区间和如果小于0,还需要判断之前区间和是否加上这个当前区间和足够大于0。

#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;#define INF 0x3f3f3f3fint limitl,limitr,x[100005],deposit,n,d,l;void dfs(){    int Max,b;    Max = b = 0;    for(;l < n;++l){        if(x[l] == 0){            break;        }        b += x[l];        Max = max(b,Max);    }    if(Max + limitl > d){        deposit = -1;        return;    }    if(l >= n)        return;    limitr = min(limitr,d - Max);    if(b < 0){        l++;        int t = abs(b);        if(limitl >= t){            limitl = limitl - t;            dfs();        }        else if(t > limitr){            limitl = 0;            limitr = d;            deposit++;            dfs();        }           else{            limitr = limitr - (t - limitl);            limitl = 0;            dfs();        }    }    else{        l++;        limitl = limitl + b;        dfs();    }}int main(){    while(~scanf("%d%d",&n,&d)){        for(int i = 0;i < n;++i){            scanf("%d",&x[i]);        }        deposit = l = 0;        int sum = 0;        while(l < n){            if(x[l] == 0){                if(sum < 0){                    limitr = d;                    limitl = 0;                    l++;                    deposit++;                    dfs();                    break;                }            }            else{                sum += x[l];                if(sum > d){                    deposit = -1;                    break;                }            }            l++;        }        cout << deposit << endl;    }    return 0;}
阅读全文
0 0
原创粉丝点击