D. 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.

Input

-   - 

Output


Input

-  

Output

-

思路:首先由于每次充钱我们只需要保证账户金额不超过d就可以无限充钱,那么我们不会因为检查时金额为负数而输出-1,因为我们一定有能力在检查的那天白天把金额充值到正数。现在的问题是,任何一天的白天金额不能超过d,且希望充值次数最少。所以在每次必须充值的时候,我们尽量多充一些,充多少由后面的前缀最大值定。假设未来的最大前缀是f[j],那么此时最多只能充d - f[j]。(参考博客)

AC代码:

#include<bits/stdc++.h>

using namespace std;
#define N 1250000
int arr[N];
int sum[N];// 前缀和
int f[N];//从后面跑出的最大值
int main(){
int n,d;
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
scanf("%d",&arr[i]);
sum[]=;
for(int i=;i<=n;i++)
sum[i]=sum[i-]+arr[i];// 前缀和
f[n]=sum[n];
for(int i=n-;i>=;i--)
f[i]=max(f[i+],sum[i]);// 最大值
int ans=;// 需要增加的钱 的数目
int res=;// 需要增加的钱 的金额
for(int i=;i<=n;i++){
if(!arr[i]){
if(sum[i]+res<){ // 给钱的范围 --(不能超过接下来的最大值)
res+=d-(f[i]+res);
ans++;
}
if(res+sum[i]<){ // 如果钱还是为负数。则不符合
puts("-1");
return ;
}
}else{
if(res+sum[i]>d){//
puts("-1");
return ;
}
}
}
printf("%d\n",ans);
return ;
}

 

Educational Codeforces Round 33 (Rated for Div. 2) D题 【贪心:前缀和+后缀最值好题】的更多相关文章

  1. Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays

    题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi​的的幂为kkk,则这个 ...

  2. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

  3. Educational Codeforces Round 33 (Rated for Div. 2) 题解

    A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...

  4. Educational Codeforces Round 33 (Rated for Div. 2)A-F

    总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...

  5. Educational Codeforces Round 33 (Rated for Div. 2) D. Credit Card

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

  6. Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】

    C. Rumor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  7. Educational Codeforces Round 33 (Rated for Div. 2) B. Beautiful Divisors【进制思维/打表】

    B. Beautiful Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. Educational Codeforces Round 33 (Rated for Div. 2) A. Chess For Three【模拟/逻辑推理】

    A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Educational Codeforces Round 33 (Rated for Div. 2)

    A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. (四)mybatis 的主键返回

    目录 文章目录 自增主键(LAST_INSERT_ID()) 非自增主键(UUID() ) 自增主键(LAST_INSERT_ID()) 在映射关系文件中配置 <!--插入用户--> &l ...

  2. POJ1631_高深DP

    按照那个图形研究比较了一会, 居然发现是最长上升子序列问题, 这个是真的牛逼!! 只不过是题目没有说的那么直白!

  3. 【Python基础】13_Python中的PASS

    pass关键字的使用 在程序分支中,如果不想立刻执行该分支,可使用pass占位符,pass不表示任何含义,仅保证程序不会报错. 如: action_str = input("请选择希望执行的 ...

  4. 这里除了安全,什么都不会发生!Docker镜像P2P加速之路

    1.1      问题: 在使用Docker运行容器化应用时,宿主机通常先要从Registry服务(如Docker Hub)下载相应的镜像(image).这种镜像机制在开发环境中使用还是很有效的,团队 ...

  5. Delphi cxpagecontrol融合窗体

    功能说明: 一.在需要融合的每个窗体加一句 initialization RegisterClasses([TFrmDataDict]); //类名 二.cxpagecontrol融合窗体,在调用时 ...

  6. Unity Button延迟功能

    有时候Button点下去不是要求立即反应的,而是先有个特别短的动画,再反应. 实现: 继承Button,然后重写一下OnPointerClick,利用协程来延迟. using System.Colle ...

  7. C# 使用Emit实现动态AOP框架 (二)

    目  录 C# 使用Emit实现动态AOP框架 (一) C# 使用Emit实现动态AOP框架 (二) C# 使用Emit实现动态AOP框架 (三) C# 使用Emit实现动态AOP框架 进阶篇之异常处 ...

  8. 处理python错误问题

    ------------恢复内容开始------------ 调试过程中遇到的问题 (1)爬取首页源码出现中文乱码 解决方案: 将网页编码强制转换成gbk,并去除解决乱码问题的三行代码. (2)程序运 ...

  9. VS Code 运行 JavaScript 文件时出现“node...”乱码或错误

    1.错误图片: 2.如果是中文乱码的话,可以到设置里边把「Auto Guess Encoding」这一项勾起来. 3.如果不是这个原因,可能是因为没安装 Node.js 和配置 Node.js 环境, ...

  10. MySQL间隙锁问题

    间隙锁(Gap Lock):锁加在不存在的空闲空间,可以是两个索引记录之间,也可能是第一个索引记录之前或最后一个索引之后的空间. 最近用户反馈说系统老是出现insert时,等待超时了,最后发现是ins ...