UVa 11292:Dragon of Loowater(水题)

来源:互联网 发布:js对象参数 编辑:程序博客网 时间:2024/06/01 10:33

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=2267

题意:你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头)。村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币。如何雇佣骑士才能砍掉恶龙的所有头,且需要支付的金币最少?注意,一个骑士只能砍一个头(且不能被雇佣两次)。(本段摘自《算法竞赛入门经典(训练指南)》

分析:
水题。直接排序搞就好了。

代码:

#include <iostream>#include <fstream>#include <algorithm>#include <cmath>#include <cctype>#include <string>using namespace std;const int maxn = 20000 + 5, INF = 2000005;int n, m, ans, cnt;int a[maxn], b[maxn];int main(){    while (~scanf("%d%d", &n, &m), n || m)    {        for (int i = 0; i < n; ++i)            scanf("%d", &a[i]);        for (int i = 0; i < m; ++i)            scanf("%d", &b[i]);        if (n > m)            printf("Loowater is doomed!\n");        else        {            sort(a, a + n);            sort(b, b + m);            cnt = 0;            ans = 0;            for (int i = 0; i < m; ++i)                if (b[i] >= a[cnt])                {                    ++cnt;                    ans += b[i];                    if (cnt == n)                        break;                }            if (cnt == n)                printf("%d\n", ans);            else                printf("Loowater is doomed!\n");        }    }    return 0;}
0 0
原创粉丝点击