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 简单对比时间 ...
随机推荐
- [转载]关于C++ const 的全面总结
C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...
- MySQL主从关系设置(转)
来源:LAMP兄弟连 作者:李恺 http://***/php/bencandy.php?fid=70&id=635 要做MySQL主从关系的设置,那么就得有两台MySQL主机.所以在开始之前 ...
- iOS 沙盒购买,弹出“需要验证”,“继续登录”的问题?
点击购买后,能弹出 确认购买的对话框, 您想以xxx的价格买一个xxx吗? [environment:sandbox] 点击确认购买后,弹出"需要验证" 点击继续,输入密码后.竟然 ...
- jquery 父页面 子页面 同级页面 调用
项目中用到 代码 ,先 展示 ,以便以后用到,直接看下就会明白. var li_divs = parent.$("#servicelist")[0].contentWindow. ...
- Application.persistentDataPath 的一个小坑
打包之前在Android的Player Setting里面选择WriteAccess (写入访问) Internal Only:表示Application.persistentDataPath的路径是 ...
- Unity3D研究院之IOS全自动打包生成ipa
接着上一篇文章, 自动生成framework,这篇文章我把shell自动化打包ipa整理了一下,希望大家喜欢,嘿嘿.. 建议大家先看一下上一篇文章.http://www.xuanyusong.com/ ...
- Thread的第三天学习
线程互斥 public void method1() { synchronized(this) { ... } } 等同于 public synchronized void method1() { ...
- 套题T8&T9
A - 8球胜负(eight) Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submi ...
- MySQL错误代码大全【转载】
B.1. 服务器错误代码和消息 服务器错误信息来自下述源文件: 错误消息信息列在share/errmsg.txt文件中."%d"和"%s"分别代表编号和字符串, ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...