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 ...
随机推荐
- 【SymmetricDS】实现新的数据库方言
2018-04-20 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/8890785.html 本文翻译自SymmetricDS官方文档 I ...
- oracle DCL-(grant、revoke )
1.授权GRANT <权限列表> to <user_name>; 2.收回权限REVOKE <权限列表> from <user_name>
- MyBatis配置Setting详细说明
该表格转载自http://blog.csdn.net/summer_yuxia/article/details/53169227 setting是指定MyBatis的一些全局配置属性,这是MyBati ...
- C++——override
override关键字作用: 如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译.举例子说明 struct Base { virtual ...
- 使用R语言绘制图表
#========================================================#wolf moose graph version 20170616.R###Data ...
- utf8转unicode
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
- c#与Java事件定义的不同
C#: using System; using System.Collections.Generic; using System.Text; namespace Test1 { class Progr ...
- spring 中配置sessionFactory及用法
spring 中配置sessionFactory及用法 方法一: 1.在Spring的applicationContext.xml中配置bean <!-- 启用注解注入 --> ...
- Chrome Plugin Recommendation
1.AdBlock 拦截广告神器 2.IPBlade 变更IP地址,使你自由 3.JSON-handle 让接口返回的JSON数据更好看 4.Proxy SwitchyOmega 变更浏览器代理 5. ...
- 【Arcgis for android】相关教程收集自网络
请加入qq群:143501213 一起交流和学习 推荐博客: 张云飞VIR http://www.cnblogs.com/vir56k/tag/arcgis%20for%20android/ arcg ...