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): 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.

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
 

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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值