怪物补刀贪心算法

Posted ACM算法日常

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怪物补刀贪心算法相关的知识,希望对你有一定的参考价值。

D. Fight with Monsters

There are n monsters standing in a row numbered from 1 to n. The i-th monster has hi health points (hp). You have your attack power equal to a hp and your opponent has his attack power equal to b hp.
n只怪兽站成一排,从1到n。第i只怪兽有 的血量,你的攻击力为a,对手的攻击力为b。

You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 0.
你和你的竞争者需要对抗这些怪兽,从第一只怪兽开始,只有击败后才能移动到第二只,依次往后。怪兽的hp为0时死亡。

The fight with a monster happens in turns.
轮流攻击怪兽。

You hit the monster by a hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
只有你攻击后怪物死亡,才能给你增加一分。

Your opponent hits the monster by b hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster.
你的竞争者击败怪物的话都不加分。

You have some secret technique to force your opponent to skip his turn. You can use this technique at most k times in total (for example, if there are two monsters and k=4, then you can use the technique 2 times on the first monster and 1 time on the second monster, but not 2 times on the first monster and 3 times on the second monster).
你有一种能力,能够让你的对手跳过一回合。你最多能够使用k次。

Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.
你最多能拿多少分呢?

Input

The first line of the input contains four integers n,a,b and k (1≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.

The second line of the input contains n integers h1,h2,…,hn (1≤hi≤109), where hi is the health points of the i-th monster.

Output

Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

Examples

input
6 2 3 3
7 10 50 12 1 8

output
5

input
1 1 100 99
100

output
1

input
7 4 2 1
1 3 5 4 2 7 6

output
6

解题思路:

由于是轮流共计,我先手,每一个回合下来我和竞争者造成a+b点伤害。

比如怪物血量30,我的3,竞争者7,最重要的是最后一个回合的情况,显然这里经过2个回合后,怪物血量还剩10,我如果要拿分,出手后还剩7点血,那么必须使用3次特殊能力,才能击败怪物。

对于每一只怪物,先是通过取余操作到达最后一回合。

求出最后一回合怪物的血量x。

计算需要的特殊能力(x-a)/a次数s。

显然,需要从小到大排序次数s,优先选择消耗小的怪物使用技能。

源代码:C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve()
{
vector<ll> v;
ll n, a, b, k, s, h, d, i, len, ans = 0;

// 怪物数量,自己的攻击力,竞争者的攻击力,特殊能力使用次数
cin >> n >> a >> b >> k;
s = a + b;

while (n--)
{
// 怪物血量
cin >> h;
// 至少留一点血,去掉重复的s
d = (h - 1) % s + 1;
// 如果剩余的d小于a,那么下一次出手肯定能拿分
if (d <= a)
ans++;
else
v.push_back(d - a);
}

// 从小到大排序
sort(v.begin(), v.end());

for (i = 0; i < v.size(); i++)
{
//向上取整
d = (v[i] + a - 1) / a;
// 消耗特殊能力
if (k >= d)
{
k -= d;
ans++;
}
}

cout << ans << '\n';
}

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();

return 0;
}


温馨提示




点赞的时候,请宠溺一点


以上是关于怪物补刀贪心算法的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1334C - Circle of Monsters(差值取前缀和 / 贪心)

Educational Codeforces Round 76 (Rated for Div. 2) - D. Yet Another Monster Killing Problem(贪心)(示例代码

CF1257DYet Another Monster Killing Problem贪心

贪心算法:划分字母区间

763. 划分字母区间-贪心算法

[bzoj3709][PA2014]Bohater_贪心