【CodeForces】893 - D Credit Card (贪心)

本文探讨了一道关于信用卡操作的算法题目,涉及到的操作包括增加金额、支出和查询余额。重点在于如何在不使余额为负且不超过上限的情况下,确定最少去银行充值次数的贪心算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

题目读了好久。。
题意:
有一个人新办了一张信用卡,初始账户上没有钱。给出两个数字q,d。
q是操作次数。
操作分为三种:

  1. ai a i 为正值时表示信用卡增加了 ai a i 元。
  2. ai a i 为负值时表示信用卡支出了 ai a i 元。
  3. ai a i 为0时,进行一次查询,查询时信用卡上的钱不能是负值。

在每次操作前,这个人可以选择去银行充钱,但是无论时充钱还是怎么样,银行卡上的钱一定不能超过d,如果超过就输出-1。
问:此人最少需要去几次银行?

这道题应该可以看出来是一道贪心的题目。主要突破点就是充钱的范围是一个区间,但是这一定会有一个最大值和最小值,我们时刻更新最大值和最小值,如果某一时刻,最小值都大于d了,那么就说明无论如何此时卡上的钱都会大于d,那么就是输出-1。

最重要的贪心思想就体现在mmax和mmin的更新处。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
    int n,d,x,res = 0;
    scanf("%d %d",&n,&d);
    int mmax = 0,mmin = 0;
    while(n--){
        scanf("%d",&x);
        if(x == 0){
            if(mmax < 0){
                mmax = d;
                res++;
            }
            mmin = max(0,mmin);    //只有最大值跟新时res需要自增,因为mmin不可能大于mmax,所以说在之前充钱时,存在某种情况使得此时mmin为0。
        }
        mmax += x;
        mmin += x;
        mmax = min(mmax,d);
        if(mmin > d){
            puts("-1");
            return 0;
        }
    }
    printf("%d\n", res);
    return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值