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 ...
随机推荐
- 第九章 Servlet工作原理解析(待续)
从 Servlet容器说起 创建 Servlet实例 Servlet体系结构 Servlet如何工作 Servlet中的Listener Filter如何工作 Servlet中的url-pattern
- javascript——常用函数
1.获取随机数: function GetRandomNum(n, m) { //n-m之间的随机数 return Math.floor(Math.random() * (m - n + 1) + n ...
- 控制器对应view生命周期
一.控制器view创建的六种方式 1.有没有同名xib创建2.通过 storyboard 创建3.有指定xib情况下创建4.有同名xib情况5.有同名去掉controll的情况6.loadveiw ...
- 【转】Sublime Text2中的快捷键一览表(Sublime 键盘快捷键大全 )
Sublime Text 提供了无比强大的快捷键阵容,如果能够在Coding的时候灵活的使用快捷键,将能够使得你的效率倍增,相信在不久的将来,Sublime Text将是你跨平台使用的最佳Coding ...
- CSS技巧: CSS隐藏文字的方法(CSS text-indent: -9999px;)
建站过过程中朋友喜欢把网站名称用H1表示,但从美观考虑,要用logo图片来代替h1,这时需要隐藏h1内的这段文字,但又不能对搜索引擎不友好,否则就失去了定义h1标签的意义. 在CSS中如何以图代字,找 ...
- 如何让DIALOG点击确定按钮之后由于数据不合法不关闭
public void SetDialogIsClose(DialogInterface pDialog, Boolean pisClose) { try { Field _Field = pDial ...
- 指定jdk编译或运行
set JAVA_HOME=D:\java\jdk8 set CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOMe%\lib\tools.jar; set Pat ...
- Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...
- matlab rand(3,5)
rand()函数在(0,1)上创建均匀分布的随机数的数组 >> rand(3,5) ans = 0.8147 0.9134 0.2785 0.9649 0.9572 0.9058 0.63 ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存
1. 2. 3.字符串