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 ...
随机推荐
- 实验吧CTF题库-编程(部分)
百米 3秒提交答案,数字是随机变化的 利用Python脚本解题 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import ...
- ghld data format
%CTF: 1.00%FileType: PROF strp "VelocityProfile"%PROFSpec: 1.00 2006 00 00%Manufacturer: C ...
- linux命令-du查看占用磁盘空间大小
格式 df -h 查看磁盘分区情况 du /etc 目录文件大小都列出来 单位是k最后一行是总和 du -m 单位是m 小于1m写成1m du -h 单位人性化显示k/m du -sh /etc 查看 ...
- Python的安装以及路径的设置(python的下载地址:www.python.org)
在有的Python版本中在安装时,我们的可以再安装时选择Python路径的自动配备 在选择python的安装程序的时候,我们尽量选择python的2.版本,因为随着Python的更新,Python的数 ...
- 我积累的Java实用代码
1.解压zip文件 /** * 解压输入的zip流,Java默认的解压只能处理UTF-8编码的文件或者目录名,否则会报MALFORMED异常 * * @param is 输入流 * @param ou ...
- 无人驾驶——4.控制之MPC模型预测控制
源自:<无人驾驶无人驾驶车辆模型预测控制>——龚建伟 参考:https://wenku.baidu.com/view/8e4633d519e8b8f67c1cb9fa.html 0.车辆模 ...
- 【摘自张宴的"实战:Nginx"】nginx配置
user nobody;worker_processes 2; #error_log logs/error.log;error_log logs/error.log notice;#error_log ...
- keystone组件
引: 什么是keystone 为何要有keystone keystone的功能 keystone概念详解 keystone与openstack其他组件关系 k ...
- loj10102 旅游航道
传送门 分析 一道喜闻乐见的求桥的板子题. 代码 #include<iostream> #include<cstdio> #include<cstring> #in ...
- 718C Sasha and Array
传送门 题目 Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be ...