zoukankan      html  css  js  c++  java
  • E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题

    E. Superhero Battle
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.

    Each round has the same scenario. It is described by a sequence of nn numbers: d1,d2,,dnd1,d2,…,dn (106di106−106≤di≤106). The ii-th element means that monster's hp (hit points) changes by the value didi during the ii-th minute of each round. Formally, if before the ii-th minute of a round the monster's hp is hh, then after the ii-th minute it changes to h:=h+dih:=h+di.

    The monster's initial hp is HH. It means that before the battle the monster has HH hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 00. Print -1 if the battle continues infinitely.

    Input

    The first line contains two integers HH and nn (1H10121≤H≤1012, 1n21051≤n≤2⋅105). The second line contains the sequence of integers d1,d2,,dnd1,d2,…,dn (106di106−106≤di≤106), where didi is the value to change monster's hp in the ii-th minute of a round.

    Output

    Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer kk such that kk is the first minute after which the monster is dead.

    Examples
    input
    Copy
    1000 6
    -100 -200 -300 125 77 -4
    
    output
    Copy
    9
    
    input
    Copy
    1000000000000 5
    -1 0 0 0 0
    
    output
    Copy
    4999999999996
    
    input
    Copy
    10 4
    -3 -6 5 4
    
    output
    Copy
    -1




    这个题目不难,但是细节比较多,我觉得我做的很自闭,因为WA好多次,然后我就自闭了。
    上网查了查题解,就是要考虑一次循环之后可以减少最多的值,这个要优先考虑,然后就是去找有多少个整次的循环。


    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <iostream>
    #include <vector>
    #include <queue>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int maxn = 2e5 + 100;
    ll a[maxn];
    
    int main()
    {
        int flag=0,mark;
        ll h, n,sum=0,mi=inf;
        cin >> h >> n;
        ll g = h;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld", &a[i]);
            sum += a[i];
            mi = min(mi, sum);
            g += a[i];
            if(g<=0&&!flag)
            {
                mark = i;
                flag = 1;
            }
        }
        if(flag)
        {
            printf("%d
    ", mark);
            return 0;
        }
        if (sum >= 0)
        {
            printf("-1
    ");
            return 0;
        }
        sum = -sum;
        h += mi;
        ll ans = h / sum * n;
        ll cur = h % sum - mi;
        while(cur>0)
        {
            for(int i=1;i<=n;i++)
            {
                cur += a[i];
                ans++;
                if (cur <= 0) break;
            }
            //printf("cur=%d
    ", cur);
        }
        printf("%lld
    ", ans);
        return 0;
    }





  • 相关阅读:
    Window安装Redis并设置为开机启动
    大佬为你揭秘微信支付的系统架构,你想知道的都在这里了
    想要设计自己的微服务?看这篇文章就对了
    破局人工智能:构建AI,与腾讯云一起探索语音应用场景
    巧用机器学习定位云服务器故障
    重磅发布 | 黑镜调查:深渊背后的真相之「DDoS 威胁与黑灰产业调查报告」
    5分钟内看懂机器学习和深度学习的区别
    如何防范和应对Redis勒索,腾讯云教你出招
    Redis勒索事件爆发,如何避免从删库到跑路?
    作为一个编程新手,我再也不怕Flink迷了我的眼!
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10645708.html
Copyright © 2011-2022 走看看