HDU-5281

Senior‘s Gun

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 842    Accepted Submission(s): 309

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 ($)

/**
    题意:xuejiejie有n把枪,每把枪的威慑力是mmap[i],现在有m个master,每个master
          的攻击性是temp[i] ,现在xuejiejie得到的bonus值为mmap[i] - temp[i]
          现在要求bonus的值最大化
    做法:暴力
**/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 100000 + 10
#define INF 0x7fffffff
long long  mmap[maxn];
long long  temp[maxn];
int main()
{
//#ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&mmap[i]);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%lld",&temp[i]);
        }
        sort(mmap,mmap+n);
        sort(temp,temp+m);
        long long sum = 0;
        long long ans = 0;
        int p = 0;
        for(int i=n-1;i>=0;i--)
        {
            if(p>=m) break;
            if(mmap[i] >= temp[p])
            {
               ans += mmap[i] - temp[p];
               sum = max(sum,ans);
               p++;
            }
            else
            {
                p++;
                i++;
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}
时间: 2024-12-27 22:24:05

HDU-5281的相关文章

hdu 5281 Senior&#39;s Gun

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5281 Senior's Gun 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

HDU 5281 Senior&amp;#39;s Gun 杀怪

题意:给出n把枪和m个怪.每把枪有一个攻击力,每一个怪有一个防御力.假设某把枪的攻击力不小于某个怪的防御力则能将怪秒杀,否则无法杀死.一把枪最多仅仅能杀一个怪,不能用多把枪杀同一个怪.每杀一次怪能够得到枪的攻击力减去怪的防御力的的分数. 求得分的最大值. 贪心.首先我们考虑这样一种情况:用攻击力为A的枪杀防御力为a的怪,攻击力为B的枪杀防御力为b的怪.则得分为A - a + B - b. 最好还是设A ≤ B,则有a ≤ A ≤ B.b ≤ B.假设有A < b,那么我们考虑用B杀a.而不使用A

HDU 5281 Senior&#39;s Gun 杀怪

题意:给出n把枪和m个怪.每把枪有一个攻击力,每个怪有一个防御力.如果某把枪的攻击力不小于某个怪的防御力则能将怪秒杀,否则无法杀死.一把枪最多只能杀一个怪,不能用多把枪杀同一个怪.每杀一次怪可以得到枪的攻击力减去怪的防御力的的分数.求得分的最大值. 贪心.首先我们考虑这样一种情况:用攻击力为A的枪杀防御力为a的怪,攻击力为B的枪杀防御力为b的怪.则得分为A - a + B - b. 不妨设A ≤ B,则有a ≤ A ≤ B,b ≤ B.如果有A < b,那么我们考虑用B杀a,而不使用A枪也不杀b

HDU 5281 BestCoder Round #47 1002:Senior&#39;s Gun

Senior's Gun Accepts: 235 Submissions: 977 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 学姐姐是一个酷酷的枪手. 她常常会随身携带n把枪,每把枪有一个攻击力a[i]. 有一天她遇到了m只怪兽,每只怪兽有一个防御力b[j].现在她决定用手中的枪消灭这些怪兽. 学姐姐可以用第i把枪消灭第j只怪兽当且仅当b[j]≤a[i],同时她会获

HDU 5281【贪心】

用最牛逼的枪打最弱的怪物,先给枪和怪物排个序. #include <iostream> #include <queue> #include <algorithm> #include<cstring> #include<cstdio> typedef long long ll; using namespace std; #define MAX 100000+10 ll gun[MAX]; ll monster[MAX]; int n, m; int

Bestcoder Round 47 &amp;&amp; 48

1.Senior's Array(hdu 5280) 题目大意:给出大小为N的数组和P,求将数组中的某个元素替换为P后的最大连续子段和.N<=1000 题解: 1.送分题,比赛的时候只想到枚举替换的元素然后贪心找最大连续子段和.时间复杂度O(N2) 2.实际上有更好的做法.类似线段树的合并,枚举替换元素的时候 可以把左右两边的答案合并.F[i]表示以i结尾,G[i]表示以i开头的最优解.合并的时候只要考虑把中间的P用起来,即 A[x]+max(0,F[x-1])+max(0,G[x+1]) .

hdu 1052(田忌赛马 贪心算法,sort排序)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18155    Accepted Submission(s): 5281 Problem Description Here is a famous story in Chinese history. "That was about

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往