UVa 11292 勇者斗恶龙(The Dragon of Loowater)
有一条n个头的恶龙,现在有m个骑士可以雇佣去杀死他,一个能力值为x的勇士可以砍掉直径不超过x的头,而且需要支付x个金币。如何雇佣才能砍掉所有的头且支付最少的金币,注意一个勇士只能砍一个头,也只能被雇佣一次。
输入包含多组数据,每组数据第一行为正整数n,m(1<=n,m<=20000),以下n行每行为一个整数,就是恶龙的头的直径,以下m行每行为一个整数,就是每个骑士的能力值。输入结束标志位m=n=0。
输出格式对于每组数据输出最少花费,如果无解,则输出"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!
嗯...最小的花费,肯定会想到贪心的思想...当然这道题里有轻微的贪心思想。
思路:
将龙的头按照直径的长短从小到大排序,将勇士的能力值从小到大排序(为了不浪费勇士的能力值),然后进行比较...
鬼畜一——cur:
注意我们要记录龙已经被砍掉几个头,并且下一个要砍哪一个,所以用cur来记录,并且最后cur要和n进行比较,看看是否将龙所有的头都砍完了..
鬼畜二——输入:
输入与平常不同,只有输入到 0 0 时才会停止,所以要用一个while循环嵌套所有
大体过程:
输入 ------> 贪心思想的排序 --------> 比较 --------> 是否能将所有的头砍去 ---------> 根据情况输出
下面是AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; const int maxn = ; int j[maxn], d[maxn];
//j数组表示骑士的能力值
//d数组表示龙头的直径 int main(){
int n, m;
while(scanf("%d%d", &n, &m) == && n && m){//while循环进行输入,n,m不得为0
for(int i = ; i <= n; i++) scanf("%d", &d[i]);
for(int k = ; k <= m; k++) scanf("%d", &j[k]);
sort(d+, d+n+); //排序
sort(j+, j+m+);
int cost = , cur = ;//cost为最小花费,cur为鬼畜一
for(int i = ; i <= m; i++){
if(j[i] >= d[cur]){//进行比较
cost += j[i];
if(++cur == n) break;//将龙头全砍掉
}
}
if(cur < n) printf("Loowater is doomed!\n");//没砍完龙头
else printf("%d\n", cost);
}
return ;
}
UVa 11292 勇者斗恶龙(The Dragon of Loowater)的更多相关文章
- 勇者斗恶龙UVa11292 - Dragon of Loowater
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=s ...
- UVa 11292 勇者斗恶龙
https://vjudge.net/problem/UVA-11292 题意:有n条任意个头的恶龙,你希望雇一些其实把它杀死.一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币 ...
- uva 11292 Dragon of Loowater (勇者斗恶龙)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVA它11292 - Dragon of Loowater
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVA 11292 Dragon of Loowater(简单贪心)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- cogs 1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292]
1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292] ★ 输入文件:DragonUVa.in 输出文件:DragonUVa.out 简单对比时间 ...
- 算法 UVA 11292
***从今天开始自学算法. ***代码是用c++,所以顺便再自学一下c++ 例题1 勇者斗恶龙(The Dragon of Loowater, UVa 11292) 你的王国里有一条n个头的恶龙,你 ...
- The Dragon of Loowater
The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into ...
随机推荐
- 问题:oracle decode;结果:oracle中的decode的使用
oracle中的decode的使用 Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件 ...
- docker 笔记(3)第一个dockerfile
#vim Dockerfile FROM ubuntu RUN apt-get update && apt-get install -y vim #docker build -t ub ...
- java,js判断全角半角
function chkHalf(str){ for(var i=0;i { strCode=str.charCodeAt(i); if((strCode>65248)||(strCode==1 ...
- java多线程环境单例模式实现详解
Abstract 在开发中,如果某个实例的创建需要消耗很多系统资源,那么我们通常会使用惰性加载机制,也就是说只有当使用到这个实例的时候才会创建这个实例,这个好处在单例模式中得到了广泛应用.这个机制在s ...
- oracle行转列练习
----------------------第一题--------------------------- create table STUDENT_SCORE ( name ), subject ), ...
- Ajax02 什么是json、json语法、json的使用、利用jQuery实现ajax
目录 1什么是json 2json语法 3json的使用 4利用jQuery实现ajax编程 1 什么是json JavaScript Object Notation(JavaScript 对象表示法 ...
- Entity Framework Code-First(23):Entity Framework Power Tools
Entity Framework Power Tools: Entity Framework Power Tools (currently in beta 3) has been released. ...
- java IO的总结
1: fileChannel 没有bufferedreader快, bufferedreader 可设置缓冲大小和编码 2: bufferedreader 的readline 遇到回车也换行
- 网站下载器WebZip、Httrack及AWWWB.COM网站克隆器
动机 闲扯节点,可略读. 下载并试用这些软件并非是为了一己之私,模仿他人网站以图利.鉴于国内网络环境之艰苦,我等屌丝级半罐水程序员,纵有百度如诸葛大神万般协力相助,也似后主般无能不能解决工作和娱乐中 ...
- Java50道经典习题-程序26 求星期
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母.分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母.周一至周日的英文单词 ...