CodeForces-1238B Kill'Em All


传送门:CodeForces - 1238B

题目描述

Ivan plays an old action game called Heretic. He’s stuck on one of the final levels of this game, so he needs some help with killing the monsters.

The main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into two parts; let’s assume that the point x=0 is where these parts meet.

The right part of the corridor is filled with n monsters — for each monster, its initial coordinate xi is given (and since all monsters are in the right part, every xi is positive).

The left part of the corridor is filled with crusher traps. If some monster enters the left part of the corridor or the origin (so, its current coordinate becomes less than or equal to 0), it gets instantly killed by a trap.

The main weapon Ivan uses to kill the monsters is the Phoenix Rod. It can launch a missile that explodes upon impact, obliterating every monster caught in the explosion and throwing all other monsters away from the epicenter. Formally, suppose that Ivan launches a missile so that it explodes in the point c. Then every monster is either killed by explosion or pushed away. Let some monster’s current coordinate be y, then:

if c=y, then the monster is killed;
if y<c, then the monster is pushed r units to the left, so its current coordinate becomes y−r;
if y>c, then the monster is pushed r units to the right, so its current coordinate becomes y+r.

Ivan is going to kill the monsters as follows: choose some integer point d and launch a missile into that point, then wait until it explodes and all the monsters which are pushed to the left part of the corridor are killed by crusher traps, then, if at least one monster is still alive, choose another integer point (probably the one that was already used) and launch a missile there, and so on.

What is the minimum number of missiles Ivan has to launch in order to kill all of the monsters? You may assume that every time Ivan fires the Phoenix Rod, he chooses the impact point optimally.

You have to answer q independent queries.

输入描述

The first line contains one integer $q$ $(1≤q≤10^5)$ — the number of queries.

The first line of each query contains two integers $n$ and $r$ $(1≤n,r≤10^5)$ — the number of enemies and the distance that the enemies are thrown away from the epicenter of the explosion.

The second line of each query contains $n$ integers $x_i$ $(1≤x_i≤10^5)$ — the initial positions of the monsters.

It is guaranteed that sum of all n over all queries does not exceed $10^5$.

输出描述

For each query print one integer — the minimum number of shots from the Phoenix Rod required to kill all monsters.

题意分析

最小次数就应该从最后开始打,只需要从最后开始遍历,判断它能否被推死就可以了!

样例输入

2
3 2
1 3 5
4 1
5 2 3 5

样例输出

2
2

AC代码

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

int a[100005];

int main() {
    IOS;
    int N;
    cin>>N;
    while(N--) {
        int n,r;
        cin>>n>>r;
        int i;
        for(i=0; i<n; i++) {
            cin>>a[i];
        }
        sort(a,a+n);
        int cnt=0;
        a[n]=-1;
        for(i=n-1; i>=0; i--) {
            if(a[i]==a[i+1])continue;
            if(a[i]-cnt*r<=0)break;
            cnt++;
        }
        cout<<cnt<<endl;
    }
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}