zoukankan      html  css  js  c++  java
  • 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 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

    题意:一个人有一个银行账号 , 他可以早上去银行存钱(可以存放任意数目) 晚上去银行对存钱的账号进行操作
       (有取钱、存钱、和查询三种操作),他希望在他每次查询银行账户的时候里面的钱不能为负数,而且在
       每一天里面银行账户里面的钱不能超过 d (限度)超过输出 -1 否则输出 最小的需要存钱的次数
    思路:由于可以早上充钱晚上操作,只要平常不超过 d ,则在查询时可以早上充钱 使账户余额不小于零 ,
    所以最小的充钱次数 就是查询时 账户余额小于零的 次数。 设置账户的最大值和最小值 , 在这个区间内的
    账户余额都是可以通过 改变上一次 的存钱数目得到的 , 则当最小值超过 d 输出 -1 , 最大值小于 0 存钱
    注意:需要不断维护 最小最大值 , 平常时维护最大值不超过 d , 查询时 维护 最小值 不小于 0
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std ;
    
    #define maxn 110000
    int n , d ;
    int num[maxn] ;
    
    
    int  main(){
    
        while(~scanf("%d %d" , &n , &d)){
    
            for(int i=1 ; i<=n ; i++){
                scanf("%d" , &num[i]) ;
            }
            int flag = 0 , result = 0;
            int la = 0 , ra = 0 ;
    
            for(int i=1 ; i<=n ; i++){
                if(num[i] == 0 ){//查询时 注意维护最小值 不小于 0 
                    if(ra < 0 ){
                        result ++ ;
                        la = 0 , ra = d ;
                    }else {
                        la = max(la , 0 ) ;
                    }
                }else{// 平常时 注意维护 最大值 不超过 d 
                    la += num[i] ;
                    ra += num[i] ;
                    ra = min(d , ra) ;
                    if(la>d){
                        flag = -1 ;
                        break ;
                    }
    
                }
    
            }
            if(flag == -1){
                printf("-1
    ") ;
            }else {
                printf("%d
    " , result) ;
            }
        }
        return 0 ;
    }
    View Code

  • 相关阅读:
    WannaMine4.0查杀方法
    kthrotlds(WatchDogs变种)查杀方法
    Powershell无文件挖矿查杀方法
    Myking&暗云III病毒查杀方法
    NAT ALG原理与应用
    Invoke-PSImage 使用简介
    Cobaltstrike 安装
    windows 创建、启动、删除、停止服务
    CobaltStrike 之 Attacks 菜单解析
    网络安全法(2016年11月7日发布 2017年6月1日起施行 主席令(第五十三号)公布)
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7928598.html
Copyright © 2011-2022 走看看