【贪心+二分】Silver Fox vs Monster

AtCoder Beginner Contest 153 F题 Silver Fox vs Monster传送门

这道题当时没一点思路,后来搜了几篇博客看了好久后给补了出来

题目描述

Silver Fox is fighting with N monsters.
The monsters are standing in a row, and we can assume them to be standing on a number line. The
i-th monster, standing at the coordinate Xi, has the health of Hi.
Silver Fox can use bombs to attack the monsters. Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x−D and x+D (inclusive) by A. There is no way other than bombs to decrease the monster’s health.
Silver Fox wins when all the monsters’ healths become 0 or below.
Find the minimum number of bombs needed to win.
Constraints
1 ≤ N ≤ 2 × 1 0 5 1≤N≤2×10^5 1N2×105
· 0 ≤ D ≤ 1 0 9 0≤D≤10^9 0D109
· 1 ≤ A ≤ 1 0 9 1≤A≤10^9 1A109
· 0 ≤ X i ≤ 1 0 9 0≤Xi≤10^9 0Xi109
· 1 ≤ H i ≤ 1 0 9 1≤Hi≤10^9 1Hi109
· X i   a r e   d i s t i n c t X_i\ are\ distinct Xi are distinct.
· A l l   v a l u e s   i n   i n p u t   a r e   i n t e g e r s . All\ values\ in\ input\ are\ integers. All values in input are integers.

输入

Input is given from Standard Input in the following format:
N D A
X 1 X_1 X1 H 1 H_1 H1
:
X N X_N XN H N H_N HN

输出

Print the minimum number of bombs needed to win.

样例输入 Copy

【样例1】
3 3 2
1 2
5 4
9 2
【样例2】
9 4 1
1 5
2 4
3 3
4 2
5 1
6 2
7 3
8 4
9 5
【样例3】
3 0 1
300000000 1000000000
100000000 1000000000
200000000 1000000000

样例输出 Copy

【样例1】
2
【样例2】
5
【样例3】
3000000000

提示

样例1解释
First, let us use a bomb at the coordinate 4 to decrease the first and second monsters’ health by 2.
Then, use a bomb at the coordinate 6 to decrease the second and third monsters’ health by 2.
Now, all the monsters’ healths are 0. We cannot make all the monsters’ health drop to 0 or below with just one bomb.
样例2解释
We should use five bombs at the coordinate 5.
样例3解释
Watch out for overflow.

题目大意:有多个小怪物在X轴上,每个小怪物都有自己的血量和坐标位置,银狐想要消灭所有的小怪物,他只能投放炸弹进行攻击,每个炸弹的攻击范围是 x-a到x+a,问银狐最少可以投放多少个炸弹可以把所有的小怪物都给消灭了

解题思路:先按照每个小怪物的位置从小到大进行排序,由近及远的消灭小怪物,做到不遗漏任何一个小怪,同时使每个炸弹的攻击效率最大,看大图样例 -->
在这里插入图片描述AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct monster{
    ll x,h;
}m[200010];
bool cmp(monster t1,monster t2)
{
    return t1.x<t2.x;
}
ll q[200010];///表示受到前面炮弹攻击的次数
int main()
{
    ll n,d,a,i;
    scanf("%lld%lld%lld",&n,&d,&a);
    for(i=1;i<=n;i++)
    {
        scanf("%lld%lld",&m[i].x,&m[i].h);
    }
    d*=2;
    sort(m+1,m+n+1,cmp);
    ll ans=0;
    for(i=1;i<=n;i++)
    {
        q[i]+=q[i-1];
        m[i].h-=q[i]*a;
        if(m[i].h<=0) continue;

        ll count1=0;
        if(m[i].h%a==0)///上取整
            count1=m[i].h/a;
        else count1=m[i].h/a+1;
        ans+=count1;
        q[i]+=count1;
        ll L=i,R=n,mid;
        ll cnt=m[i].x+d;
        while(L<=R)///处理炮弹在m[i].x这个位置时能攻击到到的最大的位置
        {
            mid=(L+R)/2;
            if(m[mid].x<=cnt)
                L=mid+1;
            else R=mid-1;
        }
        q[L]-=count1;
    }
    printf("%lld",ans);
    return 0;
}

参照博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值