L - 贪心 基础

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.

The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack one of the people, and perhaps bite off a finger or two, but in general, the people tolerated the geese as a minor nuisance.

One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called on his knights to slay the dragon and save the kingdom.

The knights explained: "To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon‘s heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must be at least as tall as the diameter of the head. The knights‘ union demands that for chopping off a head, a knight must be paid a wage equal to one gold coin for each centimetre of the knight‘s height."

Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying the dragon. As one of the advisors, your job was to help the king. You took it very seriously: if you failed, you and the whole kingdom would be burnt to a crisp!

Input

The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the number n of heads that the dragon has, and the number m of knights in the kingdom. The next n lines each contain an integer, and give the diameters of the dragon‘s heads, in centimetres. The following m lines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.

The last test case is followed by a line containing:

0 0

Output

For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:

Loowater is doomed!

Sample Input

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

Sample Output

11
Loowater is doomed!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN  20006
#define INF 1000000009
/*
花费尽量少:要求用能砍掉的最矮的骑士即可
*/
int head[MAXN], n, m;
vector<int> knight;
int main()
{
    while (scanf("%d%d", &n, &m), n + m)
    {
        knight.clear();
        for (int i = 0; i < n; i++)
            scanf("%d", &head[i]);
        int tmp;
        for (int i = 0; i < m; i++)
        {
            scanf("%d", &tmp);
            knight.push_back(tmp);
        }
        sort(knight.begin(), knight.end());
        int ans = 0;
        vector<int>::iterator it;
        int t;
        for ( t = 0; t < n; t++)
        {
            it = lower_bound(knight.begin(), knight.end(),head[t]);
            if (knight.empty()||it == knight.end())
                break;
            else
            {
                ans += *it;
                knight.erase(it);
            }
        }
        if (t!=n)
            printf("Loowater is doomed!\n");
        else
            printf("%d\n", ans);
    }
}
时间: 2024-10-02 17:17:14

L - 贪心 基础的相关文章

L贪心基础

<span style="color:#330099;">/* L - 贪心 基础 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Description Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The sh

poj2709 贪心基础

D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description 杂货店出售一种由N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),组成的颜料套装.你现在需要使用这N种颜料:不但如此,你还需要一定数量的灰色颜料.杂货店从来不出售灰色颜料——

hdu 1009 贪心基础题

B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse contai

Problem L: 搜索基础之马走日

Problem L: 搜索基础之马走日 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 134  Solved: 91[Submit][Status][Web Board] Description 马在中国象棋以日字形规则移动. 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. Input 第一行为整数T(T < 10),表示测试数据组数.每一组测试数据包含一

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

贪心基础入门讲解一——完美字符串

约翰认为字符串的完美度等于它里面所有字母的完美度之和.每个字母的完美度可以由你来分配,不同字母的完美度不同,分别对应一个1-26之间的整数. 约翰不在乎字母大小写.(也就是说字母F和f)的完美度相同.给定一个字符串,输出它的最大可能的完美度.例如:dad,你可以将26分配给d,25分配给a,这样整个字符串完美度为77. 分析: 由排序不等式,出现次数最多的字母显然应该给26.所以这个题目变成了统计每种字母出现的次数了,然后按照出现次数从大到小,依次分配从高到低的权值.这就是最朴素的贪心思想. 输

K - 贪心 基础

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains Jii pounds of JavaBeans and requires Fii pounds of cat food. FatMous

贪心基础入门讲解五——任务执行顺序

分析: 本题可以抽象成,从一个整数开始,每次减去a,再加上b (a,b都是正数),要求每次操作都不产生负数. 针对本题a[i] = R[i], b[i] = R[i] – O[i],注意O[i] < R[i],我们有0<b[i]<a[i]. 所以尽管每次有减有加,但是加的没有减的多,总数还是在不断见效的.关键我们是要“最有利”的一种执行顺序.大家可以尝试多种贪心策略. 我们给出标准答案——按照b[i]不增的顺序排序,是最“有利”的. 为了定义“有利”,我们这样证明我们的结论: 如果对于b

贪心基础入门讲解二——活动安排问题

有若干个活动,第i个开始时间和结束时间是[Si,fi),只有一个教室,活动之间不能交叠,求最多安排多少个活动? 分析: 我们就是想提高教室地利用率,尽可能多地安排活动.考虑容易想到的几种贪心策略: (1) 开始最早的活动优先,目标是想尽早结束活动,让出教室.然而, 这个显然不行,因为最早的活动可能很长,影响我们进行后面的活动.例如活动开始和结束时间分别为[0, 100), [1,2) ,[2, 3), [3, 4),[4,5],安排[0,100)的这个活动之后,其他活动无法安排,可是最优解是安排