Senior's Gun
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1068 Accepted Submission(s): 384
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
有n把枪 m个敌人 每把枪有一个攻击值a[i] 每个敌人有个防御值b[i]
如果a[i]>b[i]可以获得a[i]-b[i]的奖励 求奖励值总和的最大值
要想让获得的奖励总和最大 就要确保在a[i]>b[i]的情况下 a[i]的值尽可能大 b[i]的值尽量小
所以就是排序 找到a[i]>=b[i]的临界点
二分的方法:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100100
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
ll n,m;
ll a[MAXN],b[MAXN];
bool judge(ll mid)
{
for(ll i=1;i<=mid;i++)
{
if(b[i]>a[i]) return 0;
}
return 1;
}
bool cmp(ll x,ll y)
{
return x>y;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(ll i=1;i<=m;i++)
scanf("%lld",&b[i]);
sort(a+1,a+n+1,cmp);
sort(b+1,b+m+1);
// for(int i=1;i<=n;i++)
// cout<<a[i]<<endl;
ll l=0,r=min(n,m)+1;
// cout<<r<<endl;
ll mid;
while(l<r-1)
{
// bug;
mid=(l+r)/2;
if(judge(mid)) l=mid;
else r=mid;
}
ll ans=0;
for(ll i=1;i<=l;i++)
ans+=(a[i]-b[i]);
printf("%lld\n",ans);
}
return 0;
}
暴力的方法:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100100
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
ll n,m;
ll a[MAXN],b[MAXN];
bool cmp(ll x,ll y)
{
return x>y;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(ll i=1;i<=m;i++)
scanf("%lld",&b[i]);
sort(a+1,a+n+1,cmp);
sort(b+1,b+m+1);
ll x=1,y=1;
ll ans=0;
while(x<=n&&y<=m&&a[x]>=b[y])
{
ans+=(a[x]-b[y]);
x++; y++;
}
printf("%lld\n",ans);
}
return 0;
}