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 简单对比时间 ...
随机推荐
- error: The shader requires a sampler in slot 0 which hasn't been set [XXXsampler]
About the sampler, you need to assign it to your pixelshader. m_d3dContext.Get()->PSSetSamplers(0 ...
- Nginx配置一个自签名的SSL证书
http://www.liaoxuefeng.com/article/0014189023237367e8d42829de24b6eaf893ca47df4fb5e000 要保证Web浏览器到服务器的 ...
- [LeetCode]Link List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Spring 3.0: Unable to locate Spring NamespaceHandler for XML schema namespace
被这个问题折磨着很久:参考: http://have23.iteye.com/blog/1340777 (cfx 与 spring 整合的时候出现的问题: org.springframework.be ...
- *[topcoder]TheMatrix
http://community.topcoder.com/stat?c=problem_statement&pm=13035 矩阵棋盘的题,比较典型.可以定两条column夹住,然后上下扫, ...
- 【nginx运维基础(7)】常用PHP开源程序的NginxRewrite示例
在写伪静态的时候,可以先用一个打印$_GET的PHP文件来测试,并且一定注意浏览器缓存,另外正则里如果有"{}",正则要用双引号包起来 dedecms location / { r ...
- Linux中断(interrupt)子系统
Linux中断(interrupt)子系统之一:中断系统基本原理 Linux中断(interrupt)子系统之二:arch相关的硬件封装层 Linux中断(interrupt)子系统之三:中断流控处理 ...
- Sina App Engine(SAE)入门教程(7)- Storage使用
参考阅读 sae storage api 文档 Storage 说明文档 Storage 大文件上传说明 storage是什么? 因为sae禁用了代码环境的本地读写,但是在网站运行的过程中,必定会出现 ...
- android从应用到驱动之—camera(2)---cameraHAL的实现
本文是camera系列博客,上一篇是: android从应用到驱动之-camera(1)---程序调用流程 本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这 ...
- netty websocket协议开发
websocket的好处我们就不用多说了,就是用于解决长连接.服务推送等需要的一种技术. 以下我们来看一个例子: package com.ming.netty.http.websocket; impo ...