UVA11292-The Dragon of Loowater

题意:给出龙的头数和头的数值,勇士的人数和需要用的钱,求出国王话费最少的钱才能将龙杀死,也可能杀不死

思路:先将两个数组排序,然后进行比较就可以了


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
int main(){

	int n, m;
	while(scanf("%d %d", &n, &m) && n && m){
		int arr1[20005], arr2[20005];		
		for(int i = 0; i < n; i++)				
			scanf("%d", &arr1[i]);	
		for(int i = 0; i < m; i++)	
			scanf("%d", &arr2[i]);	
		if(n > m)	
		{
			printf("Loowater is doomed!\n");	
			continue;	
		}	
		
		sort(arr1, arr1 + n);	
		sort(arr2, arr2 + m);	
		if(arr1[n - 1] > arr2[m - 1])	
		{
			printf("Loowater is doomed!\n");	
			continue;	
		}	
		int sum = 0, k = 0, i;	
		for(i = 0; i < n; i++)	
		{					
				if(k >= m)	
					break;			
				while(k < m && arr1[i] > arr2[k])	
					k++;	
				if(k >= m)			
					break;	
				sum += arr2[k];					
			 	k++;	
		}	
		if(i < n)	
			printf("Loowater is doomed!\n");	
		else
			printf("%d\n", sum);
	}

	return 0;
}














你可能感兴趣的:(UVA11292-The Dragon of Loowater)