hdu 5281 Senior's Gun(贪心)(思维)
2016-01-20 17:53
447 查看
Senior's Gun
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1608 Accepted Submission(s): 558
Problem Description
Xuejiejie is a beautiful and charming sharpshooter.
She often carries n
guns, and every gun has an attack power a[i].
One day, Xuejiejie goes outside and comes across m
monsters, and every monster has a defensive power
b[j].
Xuejiejie can use the gun i
to kill the monster j,
which satisfies b[j]≤a[i],
and then she will get a[i]−b[j]
bonus .
Remember that every gun can be used to kill at most one monster, and obviously every monster can be killed at most once.
Xuejiejie wants to gain most of the bonus. It's no need for her to kill all monsters.
Input
In the first line there is an integer
T,
indicates the number of test cases.
In each case:
The first line contains two integers n,
m.
The second line contains n
integers, which means every gun's attack power.
The third line contains m
integers, which mean every monster's defensive power.
1≤n,m≤100000,
−109≤a[i],b[j]≤109。
Output
For each test case, output one integer which means the maximum of the bonus Xuejiejie could gain.
Sample Input
1 2 2 2 3 2 2
Sample Output
1
虽然是稍微有点水的贪心题目,但是看看我的博客里边相关贪心的题解实在是少的可怜,这里就写出来供大家一起探讨。
题意还是比较好理解的,这里有一个小小的坑点:枪的数量,和怪物的数量,不一定是相等的,所以我们这里要特殊处理一下。
思路还是很好想通的,比田忌赛马相关问题简单了不少。没有那样复杂,我们这里直接拿最牛逼的枪干最差的怪物就行了:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int gun[100010]; int xue[101010]; int main() { int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { scanf("%d",&gun[i]); } for(int i=0;i<m;i++) { scanf("%d",&xue[i]); } sort(gun,gun+n); reverse(gun,gun+n);//反向处理的方法之一~ sort(xue,xue+m); long long int sum=0; int minn=min(n,m); for(int i=0;i<minn;i++) { if(gun[i]>xue[i]) sum+=gun[i]-xue[i]; } printf("%lld\n",sum); } }
相关文章推荐
- moo 7 Servlet Config / Servlet Context
- iOS post请求图片Base64编码上传
- Environment Tree(环境树)
- 关于listview自定义滚动条和背景
- 第一篇文章。留个标记。
- android开发步步为营之91:自定义AlertDialog
- Python动态监控日志的内容
- 安装grafana
- Python算法题----取出最长回文子串
- 1002:谁拿了最多奖学金
- zookeeper运维参考文档
- codeforces 613A Peter and Snow Blower
- XML和Json格式的比较
- 嵌入式程序员面试问题集锦
- Android 带输入框圆角Dialog--EditDialog
- ServiceStack.OrmLite 笔记10-group having 分页orderby等
- 图片的动画
- 首页焦点图
- 每个ios开发者都应该知道Top 10 Swift三方库
- 在WinServer2008下安装SQLServer2014