记录编号 100018 评测结果 AAAAAAAAAA
题目名称 [UVa 11292] 勇者斗恶龙 最终得分 100
用户昵称 GravatarOI永别 是否通过 通过
代码语言 C++ 运行时间 0.017 s
提交时间 2014-05-02 19:01:54 内存使用 0.70 MiB
显示代码纯文本
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 20100
struct BST{
	int l,r,dat,ran;
}t[N];
int a[N];
int root = 0,size = 0;
int n,m;
inline void right(int & x){
	int tem = t[x].r;
	t[x].r = t[tem].l;
	t[tem].l = x;
	x = tem;
	return;
}

inline void left(int & x){
	int tem = t[x].l;
	t[x].l = t[tem].r;
	t[tem].r = x;
	x = tem;
	return;
}


void insert(int & x,int y){
	if (!x){
		x = ++ size;
		t[x].l = t[x].r = 0;
		t[x].dat = y;
		t[x].ran = rand();
		return;
	}
	else{
		if (t[x].dat > y){
			insert(t[x].l,y);
			if (t[x].ran > t[t[x].l].ran) left(x);
		}
		else{
			insert(t[x].r,y);
			if (t[x].ran > t[t[x].r].ran) right(x);
		}
	}
	return;
}

void del(int & x,int y){
	if (t[x].dat == y){
		if (t[x].l == 0 || t[x].r == 0){
			if (t[x].l == 0){
				x = t[x].r;
			}
			else x = t[x].l;
			return;
		}
		else{
			if (t[t[x].r].ran > t[t[x].l].ran){
				left(x);
				del(t[x].r,y);
			}
			else{
				right(x);
				del(t[x].l,y);
			}
		}
	}
	else{
		if (t[x].dat > y) del(t[x].l,y);
		else del(t[x].r,y);
	}
	return;
}

int askpre(int x,int y){
	if (!x) return -1;
	int ans = -1;
	if  (t[x].dat <= y) {
		ans = max(t[x].dat,askpre(t[x].r,y));
	}else ans = max(ans,askpre(t[x].l,y));
	return ans;
}


void work(){
	root = size = 0;
	memset(t,0,sizeof(t));
	int ans = 0;

	int x;
	for (int i = 1; i <= n;i ++)scanf("%d",&x),insert(root,x);
	for (int i = 1; i <= m; i++) scanf("%d",&a[i]);
	sort(a + 1,a + 1 + m);
	int ds = 0;
	for (int i = 1; i <= m; i++){
		int tem = askpre(root,a[i]);
		if (tem >= 0){
			ans += a[i];
			del(root,tem);
			ds++;
		}
	}
	if (!root)
		printf("%d\n",ans);
	else puts("Loowater is doomed");
	return;
}

int main(){
	freopen("DragonUVa.in","r",stdin);
	freopen("DragonUVa.out","w",stdout);
	while (scanf("%d%d",&n,&m) != EOF){
		if (!n) puts("0");
		else
		work();
	}
}