UVA11292 Dragon of Loowate 模拟

题意翻译

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

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

对于每组数据,输出最小花费。如果无解,输出“Loowater is doomed!”。

感谢@ACdreamer 提供的翻译
题目描述

PDF

输入输出格式
输入格式:

输出格式:

输入输出样例
输入样例#1: 复制

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

输出样例#1: 复制

11
Loowater is doomed!


排序然后 O( N )扫一遍即可,签到题难度

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 2147480000
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-7
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

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

int main()
{
	//ios::sync_with_stdio(false);
	while (cin >> n >> m && n || m) {
		ms(a); ms(b);
		for (int i = 1; i <= n; i++)rdint(a[i]);
		for (int i = 1; i <= m; i++)rdint(b[i]);
		if (m < n) {
			cout << "Loowater is doomed!" << endl; continue;
		}
		else {
			sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m);
			int i, j;
			int tot = 0;
			i = 1; j = 1;
			while (i <= n && j <= m) {
				if (a[i] <= b[j]) {
					tot += b[j]; i++; j++;
				}
				else {
					while (a[i] > b[j])j++;
				}
			}
			if (j > m&&i < n) {
				cout << "Loowater is doomed!" << endl;
			}
			else {
				cout << tot << endl;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/83031764