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 ...
随机推荐
- Delphi Android 询问框
Delphi Android 询问框 http://community.embarcadero.com/blogs/entry/xe7-dialog-box-methods-support-anony ...
- TypeError: 'append' called on an object that does not implement interface FormData 解决方法
使用ajax提交form表单时,$("formId").serialize()不能提交type="file"类型的input,这个时候可以选择使用FormDat ...
- 用sass的minix定义一些代码片段,且可传参数
/** *@module功能 *@description生成全屏方法 *@method fullscreen *@version 1.7.0 *@param{Integer}$z-index 指定层叠 ...
- Web Api2 中线程的使用
System.Threading.Thread th = new System.Threading.Thread(方法名); th.IsBackground = true; th.Start(); 上 ...
- mysql 打印随机数
select rand(); 这样取出来的数据是类似这样的: 0.5389902438400223 要几位自己取几位: 取得方法类似 select substr(concat("000000 ...
- 人工智能一之TensorFlow环境配置
1.安装pip:sudo apt-get install python-pip python-dev 2.定义仅支持CPU的python2.7环境下TensorFlow安装包地址:export TF_ ...
- Mac系统下MySql下载MySQL5.7及详细安装流程
一.在浏览器当中输入以下地址 https://dev.mysql.com/downloads/mysql/ 二.进入以下界面:直接点击下面位置 ,选择跳过登录 点过这后直接下载. 三.下载完成后 ...
- 装饰器,装饰器多参数的使用(*arg, **kwargs),装饰器的调用顺序
一.#1.执行outer函数,并且将其下面的函数名,当作参数 #2.将outer的返回值重新赋值给f1 = outer的返回值 #3.新f1 = inner #4.func = 原f1 #!/usr/ ...
- 【总结整理】js获取css的属性(内部,外部,内嵌(写在tag中))
在JS中需要获取某个元素的宽高或者是绝对定位的位置信息,通常我们会这么写: var elemWidth = elem.style.width; console.log(elemWidth); //(空 ...
- Arduino 002 --- 在Ubuntu(Linux) 中搭建Arduino开发环境
在Ubuntu/Linux 中搭建Arduino开发环境 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 需要安装的Arduino的版本:Arduino 1.6.11(最新版本) ...