Battle—— 贪心 + 优先队列

Battle

Description

You’re Zhu Rengong, a formidable hero. After a number of challenging missions, you are finally facing the final Boss – a black dragon called Heilong. Due to his overwhelming power, you have to plan your actions carefully.

You have H1 hit points (HP) at the beginning of the battle, and Heilong has H2. Heilong attacks you each time you do an action, and deal Ai points of damage in the i-th round. You have three kinds of actions.

Attack. You attack Heilong, and do x points of damage.
Defend. You focus on avoiding the attack of Heilong. This action nullifies Heilong’s attack in this round.
Heal. You heal yourself, adding y hit points to yourself. There is no upper bound on your HP.
If anyone’s HP drop below or equal to 0, he dies. If you can’t kill the dragon within N rounds, you will also die. So you need to know how many rounds you need to kill the black dragon. If you cannot kill him, you will have to calculate the maximal damage you can do on Heilong.

Input

The first line contains five integers, N, x, y, H1, H2. 1 ≤ N ≤ 105, 1 ≤ x,y ≤ 104, 1 ≤ H1,H2 ≤ 109. Then follow N lines, the ith line contains one integer Ai-1. 1 ≤ Ai-1 ≤ 104.

Output

If you can kill Heilong, the first line of your output should be “Win”. Otherwise “Lose”.
The second line contains the number of rounds you need to kill Heilong if the first line is “Win”, or the maximal damage you can do if the first line is “Lose”.

Sample Input

Sample Input 1

4 1 1 3 3
1
10
1
10

Sample Input 2

4 1 1000 1 4
1
10
1
1

Sample Output

Sample Output 1

Win
4

Sample Output 2

Lose
3

Hint

In Sample 1, you have to defend in the 2nd round, othewise you will die.
In Sample 2, you heal yourself in the first round, and keep attacking until N rounds expires.

题意: 开始你有H1的血量,敌人有H2的血量,你一次可以对敌人造成x点伤害,每次回血可以回复y,回血没有上限。每回合你都有三种选择:

  1. 攻击,对敌人造成x点伤害
  2. 防御,抵挡敌人这回合对你的伤害
  3. 回血,回复y点血量
    敌人每回合都对你进行攻击,每次攻击造成的伤害是 a[i] 。问:你能否再n轮之前打败敌人,若能,输出打败敌人所进行的轮数,若不能,输出你最多能坚持多少轮。

题解: 一直和它刚,当自己没血的时候( H 1 ≤ 0 H1 \le 0 H10),从前面进行过的回合中撤销一次进攻,撤销的这次进攻必须得是可以抵挡敌人最多的伤害或者是可以让自己回复最多的生命。这里我们可以把抵挡一次攻击看成是回复a[i]的生命值,每回合都将a[i]和y中的最大值存入优先队列,当没血了就从优先队列里面取出第一个元素给自己回血。

c++ AC 代码

#include
#include
#include

int main()
{
    int n,x,y,h1,h2;
    scanf("%d%d%d%d%d",&n,&x,&y,&h1,&h2);
    int a[n+1];
    for (int i = 1; i <= n; i++)
        scanf("%d",a+i);

    int round=0,cnt=0;
    int maxcnt = 0;
    bool flag = 0;
    std::priority_queue<int> que;
    for (int i = 1; i <= n; i++)
    {
        cnt++;
        if(cnt*x >= h2)
        {
            round = i;   // 把它杀死了
            flag = 1;
            break;
        }

        que.push(std::max(a[i],y));
        maxcnt = std::max(cnt,maxcnt);
        h1 -= a[i];

        while(h1<=0 && !que.empty())
        {
            h1 += que.top(); que.pop();
            cnt--;
        }
    }

    if(flag)
    {
        puts("Win");
        printf("%d\n",round);
    }
    else
    {
        puts("Lose");
        printf("%d\n",maxcnt*x);
    }
        
    return 0;
}

你可能感兴趣的:(贪心)