UVA11292 Dragon of Loowater 基础题 排序

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

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

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

感谢@ACdreamer 提供的翻译
输入输出样例
输入 #1

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

输出 #1

11
Loowater is doomed!

  • A[]为龙头数组
  • B[]为骑士数组
  • A[]B[]排序,从小到大贪即可
	while((read(n, m)), (n+m)) {
		for(int i=1; i<=n; i++) read(a[i]);
		for(int i=1; i<=m; i++) read(b[i]);
		sort(a+1, a+1+n);
		sort(b+1, b+1+m);
		int i = 1, j = 1, cnt = 0, sum = 0;
		while(i <= n && j <= m) {
			if(a[i] <= b[j]) 
				sum += b[j], i ++, j ++, cnt ++;
			else 
				j ++;
		}
		if(cnt == n) 
			printf("%d\n", sum);
		else 
			printf("Loowater is doomed!\n");
	}

完整代码

#define debug
#ifdef debug
#include <time.h>
#endif

#include <math.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define MAXN ((int)1e5 + 7)
#define ll long long
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for (int i = lef; i <= rig; i++)
#define forj(lef, rig) for (int j = lef; j <= rig; j++)
#define fork(lef, rig) for (int k = lef; k <= rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...)                             \
    do {                                       \
        cout << "\033[31;1m " << #x << " -> "; \
        err(x);                                \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template <typename T, typename... A>
void err(T a, A... x) {
    cout << a << ' ';
    err(x...);
}

namespace FastIO {

char print_f[105];
void read() {}
void print() { putchar('\n'); }

template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
    x = 0;
    char ch = getchar();
    ll f = 1;
    while (!isdigit(ch)) {
        if (ch == '-') f *= -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    x *= f;
    read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
    ll p3 = -1;
    if (x < 0) putchar('-'), x = -x;
    do {
        print_f[++p3] = x % 10 + 48;
    } while (x /= 10);
    while (p3 >= 0) putchar(print_f[p3--]);
    putchar(' ');
    print(oth...);
}
}  // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, a[MAXN], b[MAXN];

signed main() {
#ifdef debug
    freopen("test", "r", stdin);
    clock_t stime = clock();
#endif
	while((read(n, m)), (n+m)) {
		for(int i=1; i<=n; i++) read(a[i]);
		for(int i=1; i<=m; i++) read(b[i]);
		sort(a+1, a+1+n);
		sort(b+1, b+1+m);
		int i = 1, j = 1, cnt = 0, sum = 0;
		while(i <= n && j <= m) {
			if(a[i] <= b[j]) 
				sum += b[j], i ++, j ++, cnt ++;
			else 
				j ++;
		}
		if(cnt == n) 
			printf("%d\n", sum);
		else 
			printf("Loowater is doomed!\n");
	}


#ifdef debug
    clock_t etime = clock();
    printf("rum time: %lf 秒\n", (double)(etime - stime) / CLOCKS_PER_SEC);
#endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值