UVa 11292 The Dragon of Loowater 勇者斗恶龙
你的王国里有一条n个头的恶龙,你希望雇佣一些骑士把它杀死(也就是砍掉所有的头)。村里有m个骑士可以雇佣,一个能力值为 x 的骑士可以砍掉恶龙一个直径不超过 x 的头,且需要支付 x 个金币。如何雇佣骑士才能砍掉恶龙所有的头,并且支付最小的金币?注意,一个骑士只能砍一个头并且仅能被雇佣1次
因为要保证用的钱最少,所以先把骑士按照能力值从小到大进行排序。然后从最小的开始一个一个进行匹配。在进行匹配的时候又出现一个问题,那就是每个骑士只能雇佣一次。这里有2个处理方法,第一个是开一个数组用来标记该骑士是否被雇佣。另外一个就是,将龙头也按照从小到大进行排序,从小龙头和能力值低的其实开始匹配,并保留匹配到的位置。
拿下面这组数据进行模拟:
2 3
8
5
7
8
4
将数据排序之后如下:
龙头:5 8
骑士:4 7 8
第一轮匹配,从第一个头和第一个骑士开始。发现第一个骑士不能够砍掉第一个头。再匹配第一个头和第二个骑士,发现可以砍掉第一个头,所以雇佣这个骑士。
第二轮匹配,将第二个头和第三个骑士开始匹配(因为,第一个骑士连 <= 当前龙头的龙头都不能砍掉,就更不能砍掉后面的龙头),发现可以砍掉第二个头,所以雇佣这个骑士。
最后输出的结果为:
15
这道题目是一个非常简单的 排序 + 贪心。
附AC代码:
1: #include <stdio.h>
2: #include <math.h>
3: #include <iostream>
4: #include <cstdarg>
5: #include <algorithm>
6: #include <string.h>
7: #include <stdlib.h>
8: #include <string>
9: #include <list>
10: #include <vector>
11: #include <map>
12: #define LL long long
13: #define M(a) memset(a, 0, sizeof(a))
14: using namespace std;
15:
16: void Clean(int count, ...)
17: {
18: va_list arg_ptr;
19: va_start (arg_ptr, count);
20: for (int i = 0; i < count; i++)
21: M(va_arg(arg_ptr, int*));
22: va_end(arg_ptr);
23: }
24:
25: int d[20009], p[20009];
26:
27: int main()
28: {
29: int n, m, kill, pay;
30: while (~scanf("%d%d", &n, &m) && (n || m))
31: {
32: Clean(2, d, p);
33: for (int i = 1; i <= n; i++)
34: scanf("%d", &d[i]);
35: for (int i = 1; i <= m; i++)
36: scanf("%d", &p[i]);
37: sort(d + 1, d + n + 1);
38: sort(p + 1, p + m + 1);
39: kill = pay = 0;
40: for (int i = 1, j = 1; i <= n && j <= m; i++)
41: {
42: while (d[i] > p[j]) j++;
43: if (p[j] >= d[i]) kill += 1, pay += p[j++];
44: }
45: if (kill >= n) printf("%d\n", pay);
46: else puts("Loowater is doomed!");
47: }
48: return 0;
49: }
UVa 11292 The Dragon of Loowater 勇者斗恶龙的更多相关文章
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- UVA 11292 - The Dragon of Loowater (water)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sh ...
- uva 11292 The Dragon of Loowater(贪心)
题目大意: 你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头).村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币.如何雇佣骑士才 ...
- UVa 11292 The Dragon of Loowater 【贪心】
题意:有一条有n个头的恶龙,有m个骑士去砍掉它们的头,每个骑士可以砍直径不超过x的头,问怎样雇佣骑士,使花的钱最少 把头的直径从小到大排序,骑士的能力值也从小到大排序,再一个一个地去砍头 #inclu ...
- Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)
---恢复内容开始--- 题目: Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major ...
- 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 ...
- cogs 1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292]
1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292] ★ 输入文件:DragonUVa.in 输出文件:DragonUVa.out 简单对比时间 ...
随机推荐
- 【转载】关于typedef的用法总结
不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一些 ...
- C# 连接Oracle数据库
最近项目要用Oracle数据库,之前没搞过,近2天遇到好多问题,现在总结一下,做个备份. 一.关于Oracle安装 1.服务器端 从Oracle官网下载文件,file1和file2,解压之后安装就行了 ...
- GCC 静态库和动态库
转自GCC 静态库和动态库 //hello.c #include void print_hello() { printf("HelloWorld "); } //main.c #i ...
- java 几种常见的定时器
今天闲着没事就总结了一下在java中常用的几种定时器. 主要有3种java.util.Timer, ScheduledExecutorService和quartz 一.Timer 举个简单例子.每隔5 ...
- CentOS 6.4 搭建SVN服务器
SVN作为新一代代码版本管理工具,有很多优点,管理方便,逻辑明确,安全性高,代码一致性高.SVN数据存储有两种方式,BDB(事务安全表类型)和FSFS(一种不需要数据库的存储系统),为了避免在服务器连 ...
- WCF入门(七)——异常处理1
首先以一个简单的例子演示一下远程调用发生异常的结果: 服务器端代码如下: [ServiceContract] public interface IService1 { [OperationContra ...
- How do you design object oriented projects?
what are things you do during the high level design phase (before you begin programming) to determin ...
- Linqer工具
这些天写Linq挺烦人的,就上网搜搜可有什么好的sql转Linq的工具,咦,马上就看上了Linqer. 哈哈,介绍一下使用方法吧: 官方下载网站:http://sqltolinq.com/downlo ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- 利用python 获取 windows 组策略
工作中有时候会有这种需求: 1. 自动配置组策略的安全基线,这个东西不用你自己写了,微软有这个工具,Microsoft Security Compliance Manager,你可以在下面的地址去下载 ...