UVA 11292 - Dragon of Loowater

题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2267


直接贪心,从小到大排序,然后比较就ok了


代码如下:

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

int i,j;
const int N=20010;
typedef long long LL;

int a[N],b[N];
int n,m;

int main()
{
    while(scanf("%d%d",&n,&m)&&(n+m)!=0)
    {

        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=0;i<m;i++)
            scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+m);
        int sum=0,cur=0;
        for(i=0,j=0;i<n;i++)
        {
            while(b[j]<a[i]&&j<m)
            {
                j++;
            }
            if(j>=m)
                break;
            if(b[j]>=a[i])
            {
                cur++;
                sum+=b[j];
                j++;
            }
        }
        if(cur>=n)
            printf("%d\n",sum);
        else
            printf("Loowater is doomed!\n");
    }
    return 0;
}

/*
2 3
5
4
7
8
4
2 1
5
5
10
0 0
*/


你可能感兴趣的:(UVA 11292 - Dragon of Loowater)