uva 11292 - Dragon of Loowater

简介: 点击打开链接uva 11292 思路:贪心 分析: 1 能力强的骑士开价高是合理的,但是如果排去砍一个很弱的头那就是浪费人才了,所以呢我们把骑士的能力值和头的直径进行排序,然后一个砍一个这样就可以了,最后判断是否把所有的头全部砍完即可 ...

点击打开链接uva 11292


思路:贪心
分析:
1 能力强的骑士开价高是合理的,但是如果排去砍一个很弱的头那就是浪费人才了,所以呢我们把骑士的能力值和头的直径进行排序,然后一个砍一个这样就可以了,最后判断是否把所有的头全部砍完即可

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 20010;
int d[MAXN] , a[MAXN];
int n , m;

int solve(){
    int pos = 0 , ans = 0;
    if(m < n)
      return ans;
    sort(d , d+n);
    sort(a , a+m);
    for(int i = 0 ; i < m ; i++){//枚举每个人
       if(a[i] >= d[pos]){
         ans += a[i];
         pos++;
       }
       if(pos == n)
         return ans;
    }
    return 0;                  
}

int main(){
    while(scanf("%d%d" , &n , &m) && n+m){
        for(int i = 0 ; i < n ; i++)
            scanf("%d" , &d[i]);
        for(int i = 0 ; i < m ; i++)
            scanf("%d" , &a[i]);
        int ans = solve();
        if(!ans)
           printf("Loowater is doomed!\n");
        else
           printf("%d\n" , ans);
    }
    return 0;
}



目录
相关文章
UVA10474 大理石在哪儿 Where is the Marble?
UVA10474 大理石在哪儿 Where is the Marble?
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
HDU-3635,Dragon Balls(有点难度的并查集。。。)
HDU-3635,Dragon Balls(有点难度的并查集。。。)
UVa 11292 - Dragon of Loowater(排序贪心)
UVa 11292 - Dragon of Loowater(排序贪心)
77 0
|
存储 算法 测试技术
|
人工智能 Java C++
UVA - 10474 Where is the Marble
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them.
1372 0
|
BI 人工智能
UVA 11292 Dragon of Loowater(简单贪心)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.
1075 0