zoukankan      html  css  js  c++  java
  • 训练赛(1---5)D

    一、题目

    Description

    Sometimes you have to try fighting even though you know that your enemy is very powerful than you. Your hero with initial health H is about to fight against a venomous enemy who has a poisonous value of P. The enemy's poison deals i*P damage at it's ith attacking chance(i>=1). The hero dies when his health becomes <=0. After enemy's attack, if the hero survives, he heals himself with a health of A by using his skills. Then the enemy gets the chance again and the cycle continues till the hero dies. Find the survival time of the hero. You can safely assume that the hero is mortal.

    Example Scenario:

    Initial Health(H) = 10, Poison (P) = 2, Heal value(A) = 1

    At time 1, enemy does 1*2 damage reducing the hero's health to 8

    At time 2, hero heals himself by 1 increasing his health to 9

    At time 3, enemy does 2*2 damage reducing the hero's health to 5

    At time 4, hero heals himself by 1 increasing his health to 6

    At time 5, enemy does 3*2 damage and kill the hero.

    The hero survived 5 units of time.

    Input:

    The first line consists of an integer t, the number of test cases. For each test case there is a line with 3 integers H, P and A.

    Output:

    For each test case, find the survival time of the hero.

    Input Constraints:

    1<=t<=10^6

    1<=H<=10^6

    1<=P<=10^6

    0<=A<P

    Sample Input:

    3

    3 7 2

    81 4 1

    87 8 4

    Sample Output:

    1

    13

    9

    二、程序代码

    #include <cstdio>
    typedef long long LL;
    int H,P,A;
    bool judge(int n)
    {
        LL ans = (LL)(n+1)*n/2*P-(LL)(n-1)*A-H;
        if(ans >= 0) return 1;
        return 0;
    }
    
    int main()
    {
        int kase;
        scanf("%d",&kase);
        while(kase--)
        {
            scanf("%d%d%d",&H,&P,&A);
            int l = 1,r = 1000000,ans = l;
            while(l <= r)
            {
                int mid = (l+r)>>1;
                if(judge(mid))  r = mid-1,ans = mid;
                else    l = mid+1;
            }
            printf("%d
    ",ans*2-1);
        }
        return 0;
    }

    三、心得体会

      做这道题时一直把它当做暴力的题来做,一直超时。

      用二分就好啦~~

      直接二分英雄死亡的时间,然后计算判断即可~~~

  • 相关阅读:
    程其襄实变函数与泛函分析课件
    谢惠民答案
    谢惠民 数学分析习题课讲义 答案
    谢惠民数学分析习题课讲义下册参考解答
    重磅! 谢惠民下册参考解答已经全部完成, 共 473 页!
    各大高校考研试题参考解答目录2020/06/21版
    Jenkins Pipeline审批
    Zabbix监控DHCP作用域(json格式数据)
    MDT通过UserExit.vbs调用PowerShell脚本获取变量
    MDT通过PowerShell脚本自定义变量(自定义计算机名)
  • 原文地址:https://www.cnblogs.com/fightfor/p/3905183.html
Copyright © 2011-2022 走看看