hdu 5281 Senior's Gun(贪心)

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.
 

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.

1n,m100000 109a[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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值