uva11292

你的王国里有一条n个头的恶龙,你希望雇佣一些骑士把它杀死(即砍掉所有头)。村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币。如何雇佣骑士才能砍掉龙的所有头,且需要支付的金币最少?注意,一个骑士只能砍一个头。(且不能被雇佣两次)。 输入格式

输入包含多组数据。每组数据的第一行为正整数n和m(1<=n,m<=20000);以下n行每行为一个整数,即恶龙每个头的直径;以下m行每行为一个整数,即每个骑士的能力。输入结束标志为n=m=0。 输出格式

对于每组数据,输出最小花费。如果无解,输出“Loowater is doomed!”。
#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 20007;
int n, m;
int a[maxn], b[maxn];
int main()
{
    
    
	while (cin >> n >> m && n && m) {
    
    
		for (int i = 0; i < n; i++) {
    
    
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
    
    
			cin >> b[i];
		}
		sort(a, a + n);
		sort(b, b + m);
		int first = 0, second = 0,ans = 0;
		while (first < n) {
    
    
			bool ok = false;
			while (second < m) {
    
    
				if (a[first] <= b[second]) {
    
    
					ans += b[second++];
					ok = true;
					break;
				}
				second++;
			}
			if (ok)first++;
			else break;
		}
		if (first == n) cout << ans << endl;
		else cout << "Loowater is doomed!" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/seanbill/article/details/121523429