前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【题解】Gamer Hemose

【题解】Gamer Hemose

作者头像
MikeC
发布2022-09-21 15:01:35
2470
发布2022-09-21 15:01:35
举报
文章被收录于专栏:MikeC's Blog

题目描述

One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?

There is an Agent in Valorant, and he has n weapons. The ii -th weapon has a damage value a_i

, and the Agent will face an enemy whose health value is H .

The Agent will perform one or more moves until the enemy dies.

In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to 0 . However, not everything is so easy: the Agent can't choose the same weapon for 2 times in a row.

What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases t (1\le t\le 10^5). Description of the test cases follows.

The first line of each test case contains two integers n and H (2\le n\le 10^3,1\le H\le 10^9) — the number of available weapons and the initial health value of the enemy.

The second line of each test case contains n integers a_1,a_2,\dots,a_n (1\le a_i\le 10^9) — the damage values of the weapons.

It's guaranteed that the sum of n over all test cases doesn't exceed 2\cdot 10^5.

输出格式

For each test case, print a single integer — the minimum number of times that the Agent will have to use the weapons to kill the enemy.

输入输出样例

输入 #1

代码语言:javascript
复制
3
2 4
3 7
2 6
4 2
3 11
2 1 7

输出 #1

代码语言:javascript
复制
1
2
3

分析

显然,最优方案一定是交替使用伤害值最大的两种武器。所以我们可以对所有武器按伤害值排序,得到最大的两个伤害值 d_1d_2。然后得到可以交替使用两种武器攻击的次数是 h/(d_1+d_2)。接下来依 h-h/(d_1+d_2) 的大小再处理一下最后还需要再进行几次攻击即可。

时间复杂度 O(n \log n)

代码

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
int a[1001];
bool cmp(int a,int b){
    return a>b;
}
int t,n,h;
int main() {
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&h);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n,cmp);
        int sum=a[1]+a[2];
        int ans=(h/sum)*2;
        h-=(ans/2)*sum;
        if(h-a[1]>0){
            ans+=2;
        }else if(h!=0){
            ans+=1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=4tiqlv6p5maq

最后修改:2022 年 08 月 14 日 05 : 16 PM

© 允许规范转载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021 年 10 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入格式
  • 输出格式
  • 输入输出样例
    • 输入 #1
      • 输出 #1
      • 分析
      • 代码
      相关产品与服务
      云开发 CloudBase
      云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档