• Codeforces Round #547 E. Superhero Battle(思维+模拟)


    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n 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 n numbers: d1,d2,…,dn (−106≤di≤106). The i-th element means that monster’s hp (hit points) changes by the value di during the i-th minute of each round. Formally, if before the i-th minute of a round the monster’s hp is h, then after the i-th minute it changes to h:=h+di.

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

    Input
    The first line contains two integers H and n (1≤H≤1012, 1≤n≤2⋅105). The second line contains the sequence of integers d1,d2,…,dn (−106≤di≤106), where di is the value to change monster’s hp in the i-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 k such that k is the first minute after which the monster is dead.

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

    题意:一直怪兽有血量H,给一个序列表示每轮怪兽每次收到的伤害和回复的血量,现在判断怪兽要经历多次操作会死亡。

    H值很大,暴力模拟是不可能了,因此做一些小优化,如果怪兽的血量能撑过每一轮攻击的最大伤害值,那么说明至少在这一轮他是不会死的,因此对于一个不会死的轮数,直接除过去就好了,求一个sum表示每轮遭到的伤害,求一个max表示每轮伤害的巅峰,能承受的一轮,减去sum,不能承受的一轮,即最后一轮,直接模拟怪兽收到的每次伤害即可。

    用(H-max)/ sum即怪兽能完整撑过的轮数,用轮数乘每轮操作次数即可得到操作总数,最后模拟每次操作,减到怪物H为0即可

    代码如下:

    #include<bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int maxn=2e5+7;
    int n;
    LL hp,a[maxn];
    int main()
    {
        LL sum=0,mx=0;
        bool flag=false;
        scanf("%lld%d",&hp,&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
            if(-sum>=hp)flag=true;
            if(-sum>mx)mx=-sum;
        }
        if(sum>=0&&!flag)printf("-1
    ");
        else
        {
            LL cnt=0;
            if(flag)
            {
                int i=0;
                while(hp>0)hp+=a[i++],cnt++;
                printf("%lld
    ",cnt);
                return 0;
            }
            cnt=(hp-mx)/abs(sum);
            hp-=cnt*abs(sum);
            if(!hp)hp=abs(sum);
            cnt*=n;
            int i=0;
            while(hp>0) hp+=a[i++],cnt++,i=i==n?0:i;
            printf("%lld
    ",cnt);
        }
    }
    
  • 相关阅读:
    「HAOI2015」「LuoguP3178」树上操作(树链剖分
    「LuoguP3865」 【模板】ST表 (线段树
    「LuoguP3384」【模板】树链剖分
    「网络流24题」「Codevs1237」 餐巾计划问题
    「LuoguP1799」 数列_NOI导刊2010提高(06)
    「咕咕网校
    「数论」逆元相关
    「SHOI2007」「Codevs2341」 善意的投票(最小割
    「BZOJ3438」小M的作物(最小割
    「NOIP2005」「Codevs1106」篝火晚会
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135659.html
Copyright © 2020-2023  润新知