首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >怪物补刀贪心算法

怪物补刀贪心算法

作者头像
ACM算法日常
发布2020-02-17 11:06:36
发布2020-02-17 11:06:36
66400
代码可运行
举报
文章被收录于专栏:ACM算法日常ACM算法日常
运行总次数:0
代码可运行

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++

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}

温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注ACM算法日常

点赞的时候,请宠溺一点

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-02-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • D. Fight with Monsters
  • Input
  • Output
  • Examples
  • 解题思路:
  • 源代码:C++
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档