HDU——5281 Senior's Gun
2015-09-17 20:47
316 查看
Senior's Gun
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1346 Accepted Submission(s): 476
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
Source
BestCoder Round #47 ($)
///大二贪心第一题
贪心思想,没想到直接开数组,用sort排序竟然能水过。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<iomanip>
#include<numeric>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <sstream> //基于字符串的流
#include <utility> //STL 通用模板类
#include <complex.h> //复数处理
#include <fenv.h> //浮点环境
#include <inttypes.h> //整数格式转换
#include <stdbool.h> //布尔环境
#include <stdint.h> //整型环境
#include <tgmath.h> //通用类型数学宏
#define L(a,b,c) for(int a = b;a >= c;a --)
#define M(a,b,c) for(int a = b;a < c;a ++)
#define N(a,b) memset(a,b,sizeof(a));
const int MAX=100000000;
const int MIN=-MAX;
typedef long long LL;
typedef int T;
typedef double D;
typedef char C;
using namespace std;
T n,m,a[100010],b[100010];
int cmp(int x,int y)
{
return x>y;
}
int main()
{
T t;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>n>>m;
M(i,0,n)
scanf("%d",&a[i]);
sort(a,a+n,cmp); ///对a数组从大到小排序,
M(i,0,m)
scanf("%d",&b[i]);
sort(b,b+m);
LL sum=0;
M(i,0,n) ///由题意可以看出来,n的值一定小于等于m,所以只需要对a数组进行讨论
{
///贪心思想
if(a[i]>b[i]&&i<m)
sum+=(LL)(a[i]-b[i]);
}
printf("%lld\n",sum);
}
return 0;
}
相关文章推荐
- 漫谈程序员(七)公司真实上线项目开发日志--你懂得
- Android关于JSON解析
- sed高级使用
- 第104讲:通过案例解析Akka中的Actor的不同类型的Constructor学习笔记
- jaxp解析器 使用DOM对象来解析XML
- 安卓的生命周期(初学)
- linux找不到动态链接库 .so文件的解决方法
- Jmeter代理录制脚本
- C学习之指针强化
- Java基础知识强化23:Java中数据类型转换(面试题)
- hdu1874
- HDU1213——Farm Irrigation(并查集)
- BZOJ 1133 [POI2009]Kon DP
- 有意练习--Rails RESTful(一)
- Linux 内核的测试和调试
- JS插件之——bootstrap-suggest.js
- 中国剩余定理
- Python的assert
- ios 爱行华夏 技术支持
- 单向电表模块焊接完成