• HDU 4415


    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个人。

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

    解题思路:

      贪心;

      如果能杀有剑的:

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

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

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

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

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

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

      如果不能杀有剑的:

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

      综上:

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

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

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

      

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 #define N 100010
     5 struct enemy{int a,b;}t[N];
     6 bool cmp(enemy x,enemy y){ return x.a<y.a;}
     7 int main(){
     8     int T,n,m,cas=1;
     9     scanf("%d",&T);
    10     while(T--){
    11         int i,cost=0,num=0,k=-1;
    12         scanf("%d%d",&n,&m);
    13         for(i=0;i<n;i++) scanf("%d%d",&t[i].a,&t[i].b);
    14         sort(t,t+n,cmp);
    15         for(i=0;i<n;i++)if(t[i].b) break;//找cost_min,b!=0的人
    16         if(t[i].a<=m){
    17             cost+=t[k=i].a;
    18             for(num++,i=0;i<n;i++) num+=t[i].b;//死一个有剑,有剑的全死 ,剑全拿上,剑杀人数固定了 
    19         } if(num>=n){printf("Case %d: %d %d
    ",cas++,n,cost); continue;}
    20         for(i=0;i<n&&t[i].a+cost<=m&&num!=n;i++)if(i!=k)cost+=t[i].a,num++;
    21         printf("Case %d: %d %d
    ",cas++,num,cost);
    22     }return 0;
    23 }
    我自倾杯,君且随意
  • 相关阅读:
    Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 倒序并查集
    hdu 5573 Binary Tree 构造
    hdu 5514 Frogs 容斥思想+gcd 银牌题
    hdu 5536 Chip Factory 字典树+bitset 铜牌题
    LA 7263 Today Is a Rainy Day bfs+暴力 银牌题
    hdu 5834 Magic boy Bi Luo with his excited tree 树形dp+转移
    hdu 5869 Different GCD Subarray Query BIT+GCD 2016ICPC 大连网络赛
    LA 7043 International Collegiate Routing Contest 路由表 字典树离散化+bitset 银牌题
    校验注解工具类
    java-创建对象实例
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5159157.html
Copyright © 2020-2023  润新知