poj3646

简单题

View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

#define maxn 20005

int n, m;
int head[maxn], knight[maxn];

void input()
{
for (int i = 0; i < n; i++)
scanf("%d", &head[i]);
for (int i = 0; i < m; i++)
scanf("%d", &knight[i]);
}

int work()
{
int j = 0;
int ret = 0;
for (int i = 0; i < n; i++)
{
while (j < m && knight[j] < head[i])
j++;
if (j >= m)
return -1;
ret += knight[j];
j++;
}
return ret;
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d%d", &n, &m), n | m)
{
input();
sort(head, head + n);
sort(knight, knight + m);
int ans = work();
if (ans == -1)
printf("Loowater is doomed!\n");
else
printf("%d\n", ans);
}
return 0;
}

原文地址:https://www.cnblogs.com/rainydays/p/2206503.html