【Educational Codeforces Round 33】 D. Credit Card (贪心)

本文介绍了一个关于信用卡收支管理的问题及解决方法。主要内容包括如何确保信用卡余额在规定限制内,并在特定检查日保持非负数。通过算法实现了最小次数的存款操作以满足条件。
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 nd (1 ≤ n ≤ 1051 ≤ 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


#include <bits/stdc++.h>
using namespace std;
int a[100001], pre[100001], maxsuf[100001];
int main(){
	int n, d;
	scanf("%d %d", &n, &d);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &a[i]);
	}
	pre[0] = 0;
	for(int i = 1; i <= n; ++i){
		pre[i] = pre[i - 1] + a[i];
	}
	maxsuf[n] = pre[n];
	for(int i = n - 1; i >= 1; --i){
		maxsuf[i] = max(maxsuf[i + 1], pre[i]);
	}
	long long add = 0;
	int ans = 0;
	for(int i = 1; i <= n; ++i){
		if(a[i] == 0){
			if(add + pre[i] < 0){
				add += d - (maxsuf[i] + add);
				ans++;
			}
			if(add + pre[i] < 0){
				printf("-1\n");
				return 0;
			}
		}
		else{
			if(add + pre[i] > d){
				printf("-1\n");
				return 0;
			}
		}
	}
	printf("%d\n", ans);
}

/*
题意:
一张信用卡,白天主人可以去银行存钱,随意存多少钱,但必须是正整数,且保证账户金额
不超过d,晚上的时候会对信用卡金额做一些变化,如果是a[i]是0则保证此时信用卡金额
必须大于等于0。问主人最少需要去充多少钱,如果出现违反规则的情况输出-1。

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


### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参与并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值