• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
UVa 11292 Dragon of Loowater (水题,排序)

题意:有n个条龙,在雇佣勇士去杀,每个勇士能力值为x,只能杀死头的直径y小于或等于自己能力值的龙,只能被雇佣一次,并且你要给x赏金,求最少的赏金。

析:很简单么,很明显,能力值高的杀直径大的,低的杀直径小的。所以我们先对勇士能力值从小到大排序,然后对龙的直径从小到大排序,

然后扫一遍即可,如某个勇士杀不龙,就可以跳过,扫到最后,如果杀完了就结束,输出费用,否则就是杀不完。

代码如下:

#include <iostream>
#include <cstdio>
#include <climits>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>

using namespace std;
const int maxn = 20000 + 10;

int a[maxn], b[maxn];

int main(){
    int n, m;
    while(scanf("%d %d", &n, &m) && m && n){
        for(int i = 0; i < n; ++i)    scanf("%d", &a[i]);
        for(int i = 0; i < m; ++i)    scanf("%d", &b[i]);

        sort(a, a+n);  sort(b, b+m);
        if(n > m){  printf("Loowater is doomed!\n");  continue;   }//勇士太少,直接结束

        int cnt = 0, cost = 0;
        for(int i = 0; i < m; ++i)
            if(b[i] >= a[cnt]){
                cost += b[i];//记下费用
                if(++cnt == n)  break;//龙杀完了,提前退出
            }
        if(cnt < n)  printf("Loowater is doomed!\n");
        else printf("%d\n", cost);
    }
    return 0;
}

 

posted on 2016-05-31 22:42  dwtfukgv  阅读(163)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3