UVA 11292

#include<stdio.h>
#include<stdlib.h>

int Dragon[20001],Knights[20001];

int cmp(const void *a,const void *b){
    return *(int *)a - *(int *)b;
}

int main ()
{
    int i,j,N,M,cost,index;
    //freopen("C:\\Users\\XIAOSI\\Desktop\\acm.txt","r",stdin);  
    while(scanf("%d %d",&N,&M) != EOF){
        if(N == 0 && M == 0){
            break;
        }
        
        cost = 0;
        index = 0;
        
        for(i = 0;i < N;i++){
            scanf("%d",&Dragon[i]);
        }
        
        for(i = 0;i < M;i++){
            scanf("%d",&Knights[i]);
        }
        
        qsort(Dragon,N,sizeof(int),cmp);
        qsort(Knights,M,sizeof(int),cmp);
        
        for(i = 0;i < M;i++){
            if(index >= N){
                break;
            }
            if(Knights[i] >= Dragon[index]){
                
                cost += Knights[i];
                index++;
            }
        }
        
        if(index >= N){
            printf("%d\n",cost);
        }
        else{
            printf("Loowater is doomed!\n");
        }
    }
    return 0;
}
View Code

自己做了很久都没AC,老是超时,可能我用的算法不行,所以就借鉴了一下别人的AC代码,看来我要更加努力了

posted on 2013-07-17 23:33  Forgiving  阅读(72)  评论(0)    收藏  举报