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.
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 。
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
///大二贪心第一题
贪心思想,没想到直接开数组,用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;
}
#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;
}