UVa-11292 Dragon of Loowater (贪心)
来源:互联网 发布:尚硅谷java视频教程 编辑:程序博客网 时间:2024/05/17 08:54
题目链接
题意:你的王国里一条n个头的恶龙,你希望雇佣一些骑士砍掉他的所有头,村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币,如何雇用骑士才能砍掉恶龙的所有头,且需要支付的金币最少?注意,一个骑士只能砍一个头(且不能被雇用两次)
输入:第一行为正整数n和m
接下来n行,每行为一个整数,即恶龙每个头的直径
接下来m行,每行为一个整数,即每个骑士的能力。
思路:贪心,首先我们要明确 为了砍掉所有头且花费金币最少。 我们就需要对这n个头的每一个,选出一个骑士与其对应,不妨把骑士的能力和恶龙的头都按升序排列。
对每一个头,我们从小到大选出一个骑士,且选出一个以后,前面的骑士都不需要考虑了(要么能力不足,要么已经被雇用过了)
设置一个标记cnt,当选出一个骑士时,cnt+1
综上,当结束时,如果cnt为n,那么就说明我们选出了n个骑士,也就是砍掉了恶龙的所有头
反之,若cnt!=n 那么就说明选不出这样的骑士。
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int knight[20000 + 5];int dragon[20000 + 5];int main(){int n ,m;while(~scanf("%d %d",&n,&m)){if(n==0 && m==0)break;int ans = 0,cnt=0; // ans是需要支付的金币,cnt是当前选出的骑士数。 for(int i = 0;i<n;i++){scanf("%d",&dragon[i]); }sort(dragon,dragon+n); //排序 for(int i = 0;i<m;i++){scanf("%d",&knight[i]);}sort(knight,knight+m); //排序 int k = 0;//当前考虑第几个骑士,前面的都不需要考虑了,因为要么能力不足,要么已经被雇用过了 for(int i = 0;i<n;i++){while(k < m && knight[k] < dragon[i]) //当前骑士能力不足时需要继续选 k++;if(k==m) //所有骑士都考虑完了 要跳出循环 break;cnt++;//选出了一个骑士 ans+=knight[k];k++;//当前骑士已被选择,考虑下一个骑士。 }if(cnt!=n){printf("Loowater is doomed!\n");}elseprintf("%d\n",ans);}}
阅读全文
0 0
- uva 11292 - Dragon of Loowater(贪心)
- uva - 11292 - Dragon of Loowater(贪心)
- UVa 11292 - Dragon of Loowater(贪心)
- UVa 11292 - Dragon of Loowater[贪心]
- UVA - 11292 Dragon of Loowater (贪心)
- UVA 11292 DRAGON OF LOOWATER 【贪心】
- UVa Dragon of Loowater 11292 (贪心)
- UVa 11292 - Dragon of Loowater(排序贪心)
- UVa - 11292 - The Dragon of Loowater ( 贪心 )
- UVA 11292 Dragon of Loowater(贪心)
- UVA 11292 Dragon of Loowater 贪心 .
- UVa - 11292 - Dragon of Loowater ( 排序 + 贪心 )
- UVA.11292 Dragon of Loowater (贪心)
- UVa 11292 Dragon of Loowater 贪心+排序
- |UVA 11292|贪心|Dragon of Loowater
- UVa 11292 Dragon of Loowater(排序+贪心)
- UVa-11292 Dragon of Loowater (贪心)
- Uva 11292 Dragon of Loowater (贪心)
- JSTL格式化日期
- PDF任务该怎样在ABBYY FineReader 中定义
- 第四周项目三 单链表应用
- WIFI模块ESP8266的使用指南(客户端和服务器两种模式建立)
- Android学习笔记9---Handler
- UVa-11292 Dragon of Loowater (贪心)
- 前端框架vue.js系列(1):基础及语法
- DPDK多线程初步解析
- 数据库——关于Oracle中[ ]通配符的使用
- DLT645-2007 规约 电表 报文解析
- [Leetcode] 405. Convert a Number to Hexadecimal 解题报告
- 保存成功后局部刷新
- JAVA试题:输出字符串中连续两个相同子串
- ROS hector slam