HDU 4415 - Assassin’s Creed

Problem Description
Ezio Auditore is a great master as an assassin. Now he has prowled in the enemies’ base successfully. He finds that the only weapon he can use is his cuff sword and the sword has durability m. There are n enemies he wants to kill and killing each enemy needs Ai durability. Every time Ezio kills an enemy he can use the enemy’s sword to kill any other Bi enemies without wasting his cuff sword’s durability. Then the enemy’s sword will break. As a master, Ezio always want to do things perfectly. He decides to kill as many enemies as he can using the minimum durability cost. 
Input
The first line contains an integer T, the number of test cases.
For each test case:
The first line contains two integers, above mentioned n and m (1<=n<=10^5, 1<=m<=10^9).
Next n lines, each line contains two integers Ai, Bi. (0<=Ai<=10^9, 0<=Bi<=10).
 

Output

For each case, output "Case X: " (X is the case number starting from 1) followed by the number of the enemies Ezio can kill and the minimum durability cost.
Sample Input
2
3 5
4 1
5 1
7 7
2 1
2 2
4 0
 
Sample Output
Case 1: 3 4 Case 2: 0 0
 
大致题意:
  用m点耐久度去杀人,杀死一个人需要Ai耐久度,但是获得的武器可以额外再杀死Bi个人。

  题目要求的首先是杀人最多,其次是消耗耐久度最少。

解题思路:

  贪心;

  如果能杀有剑的:

    先杀一个有剑的,有剑都能被杀死了;

    拿到所有剑后,剑的数量是固定的,那么用剑杀死的人数也是固定的,杀谁都一样;

    既然用剑杀谁都一样,那么不用剑就杀耗耐久最小的;

    所以按耗耐久由小到大排序,一个个杀过去到杀不动为止。

    如果这里面有有剑的怎么办呢? 没什么关系,他自己被耗耐久杀死了,那原本杀他的剑可以杀别人,用剑杀的人数都固定了;

    如果这么面没有有剑的怎么办呢?先杀有剑的耗耐久最小的咯;

  如果不能杀有剑的:

    还是按耐久由小到大排序,一个个杀过去到杀不动为止。

  综上:

    如果能杀有剑,那把有剑的耗耐久最小的杀掉,答案要加上剑的数量

    如果有剑的都不能杀,就不杀咯:

    剩余耐久肯定是依次杀耗耐久最小的,注意第一个杀的有剑的可能里面,那就要跳过;

  

 #include <cstdio>
#include <algorithm>
using namespace std;
#define N 100010
struct enemy{int a,b;}t[N];
bool cmp(enemy x,enemy y){ return x.a<y.a;}
int main(){
int T,n,m,cas=;
scanf("%d",&T);
while(T--){
int i,cost=,num=,k=-;
scanf("%d%d",&n,&m);
for(i=;i<n;i++) scanf("%d%d",&t[i].a,&t[i].b);
sort(t,t+n,cmp);
for(i=;i<n;i++)if(t[i].b) break;//找cost_min,b!=0的人
if(t[i].a<=m){
cost+=t[k=i].a;
for(num++,i=;i<n;i++) num+=t[i].b;//死一个有剑,有剑的全死 ,剑全拿上,剑杀人数固定了
} if(num>=n){printf("Case %d: %d %d\n",cas++,n,cost); continue;}
for(i=;i<n&&t[i].a+cost<=m&&num!=n;i++)if(i!=k)cost+=t[i].a,num++;
printf("Case %d: %d %d\n",cas++,num,cost);
}return ;
}
上一篇:如何让你的 React Native 应用在键盘弹出时优雅地响应


下一篇:为学Android,我看了这些书