Educational Codeforces Round 33 (Rated for Div. 2) D题 【贪心:前缀和+后缀最值好题】
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题 【贪心:前缀和+后缀最值好题】的更多相关文章
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi的的幂为kkk,则这个 ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Educational Codeforces Round 33 (Rated for Div. 2) 题解
A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [翻译] 深入浅出Go语言调度器:第一部分 - 系统调度器
目录 译者序 序 介绍 系统调度器 执行指令 Figure 1 Listing 1 Listing 2 Listing 3 线程状态 任务侧重 上下文切换 少即是多 寻找平衡 缓存行 Figure 2 ...
- (十五)springMvc 拦截器
文章目录 定义拦截器 接口中三个方法 配置拦截器 多个拦截器的规则 定义拦截器 springMvc 中定义拦截器只需要实现一个接口 org.springframework.web.servlet.Ha ...
- 并不对劲的CSP-S2019
day1 对题的第一印象: t1:颇有"小凯的疑惑"之风(赛后发现确实如此,因为最好写的正解也可以直接输出) t2:log方会被卡吧?好像倍增一个log?(赛后发现有很好写的线性做 ...
- Path.Combine(
// 获取程序的基目录. var dir1 = System.AppDomain.CurrentDomain.BaseDirectory; // 获取模块的完整路径. var dir2 = Syste ...
- 怎样查看Nginx版本号
方法一: 使用 nginx -v nginx -v 方法二: 使用 nginx -V nginx -V 注意: nginx -V 显示的是: 版本号 / 编译器版本 / 配置参数
- UEditor编辑器
1.UEditor编辑器官网:http://ueditor.baidu.com/website/ 2.下载文件:选择 1.4.3.3 .Net版本 UTF-8板 3.建一个ueditor文件夹,将下 ...
- JDialog
JDialog继承Dialog,Dialog继承Window,所以可以用setLocationRelativeTo(Component c)来实现Dialog的显示,当c为空时,直接显示在屏幕前,为组 ...
- 使用百度echarts仿雪球分时图(二)
上一章简单的介绍了一下分时图的构成,其实就是折线图跟柱状图的组成.本来这章打算是把分时图做完,然后再写一章来进行美化和总结,但是仔细观察了一下,发现其实东西还是有点多的.分时图的图表做完后,还要去美化 ...
- react——css样式
1.行内样式: 两个大括号包着.第一个大括号表示里面写js,第二个大括号里面是样式对象 2.传对象 将对象和结构分离,直接写一个大括号,里面写对象 3.将所有的样式对象合并成一个大对象,直接点 以上样 ...
- dedecms:限制栏目列表生成的最大页数防止被采集
dedecms:限制栏目列表生成的最大页数防止被采集 如果您的网站数据量较大,列表很多的话甚至达到上千页,生成列表时就特别耗费时间,这个缺点可以被优化掉:网站好不容易建起来,担心网站内容被采集走,如果 ...