SP10286 DOTAA - DOTA HEROES 题解

SP10286 DOTAA - DOTA HEROES 题解

题目描述:

Problem Description:

Defence Of The Ancients(DOTA) is one of the most addictive online multiplayer games. There are n heroes in our team and our motto is to conquer the opponent’s empire. To safeguard their empire, the opponents had constructed m towers on the path. If one or more heroes get into the sight of a tower, then the tower does D amount of damage to one of those heroes at that instant (i.e. one of the heroes’ health decreases by D). Any hero will die if his health H <=0. Once a tower attacks one of the heroes at that instant, all of those at that instant get out of its sight. Find whether all of the heroes in our team can reach the opponent’s empire alive.

Input Specification:

The first line consists of one integer t representing the number of test cases. For each test case, the first line consists of three integers n, m and D, the number of heroes, number of towers and the amount of Damage respectively. The next n lines consist of an integer representing the health of respective hero.

Output Specification:

Just a word “YES” if we can reach the opponent’s empire alive, else “NO”.

Input Constraints:

1 <= t <= 500
1 <= n <= 500
1 <= m <= n
1 <= D, H <= 20000

Sample Input:


3
6 3 400
500
500
500
500
500
500

6 5 400
800
800
801
200
200
200

6 3 400
401
401
400
200
400
200

Sample Output:

YES
NO
NO

Explanation of test case 1:

One of the possible solutions is
First, three of the heroes can goes together. One of them receives 400 damage from the first tower and all of them cross it. Then while crossing the next tower, one of the heroes who is at 500 health gets 400 damage and all of them cross it. Then the third hero receives the damage when crossing the last tower. Similarly the other 3 heroes can reach the opponent’s base together without dying.

题面翻译:

在一场战争中,你是其中一方。你拥有 n n n 个英雄,每个英雄拥有 a i a_i ai 的血量。敌方拥有 m m m 个英雄,你可以用一个血量大于 k k k 的英雄去干掉他,并使你的英雄的血量减少 k k k。请问我方是否能打败敌方。

算法:暴力枚举

每个英雄拥有 a i a_i ai 的血量,只有血量 a i > k a_i > k ai>k 时才能击败一个敌人,因此当 a i ≤ k a_i \le k aik 时,不能攻击敌军。

因此考虑用 ceil 函数,其作用是将小数向上取整。

则每名英雄能攻击对方的数量为 ⌈ a i k ⌉ − 1 \left\lceil\frac{a_i}{k}\right\rceil - 1 kai1

因为当 a i ≤ k a_i \le k aik 时, a i ÷ k a_i \div k ai÷k 向上取整是 1 1 1。则能把 ≤ 1 \le 1 1 这部分舍去,符合题意。

代码:

#include
using namespace std;
#define ll long long
ll T,n,m,k,cnt;
double x;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>T;
    while(T--){
        cin>>n>>m>>k;
        cnt=0;
        for(int i=1;i<=n;i++){
            cin>>x;
            cnt+=ceil(x/k)-1;//之所以用 ceil 而不是 floor 是因为英雄血量要大于 k 时才能打对方,避免 ai==k 的情况 
        }
        if(cnt>=m) cout<<"YES\n";
        else cout<<"NO\n";
    } 
    return 0;
}

你可能感兴趣的:(题解,c++,算法)