HDU 5281 Senior & #39; s Gun, hdu5281

Source: Internet
Author: User

HDU 5281 Senior's Gun, hdu5281
Senior's GunTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 616 Accepted Submission (s): 241


Problem DescriptionXuejiejie 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 sans SS 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.
InputIn 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 .
OutputFor each test case, output one integer which means the maximum of the bonus Xuejiejie cocould gain.
Sample Input

12 22 32 2
 
Sample Output
1
 
SourceBestCoder Round #47 ($)

Question:

There are n guns and m monsters, each of which has an energy value. Each monster has a energy consumption value. Now I use n to shoot monsters, each gun can only be used once, and a monster can only be used once. When I put a gun on j, only the monster can get the energy value as a [I]-B [j], if a [I]> = B [j] is required, the gun can be used up, the monster can be used up, and the maximum amount of energy can be obtained.
Problem-solving: If k is used to shoot k monsters, each gun is the highest energy value, and each monster consumes the least energy.
<pre name="code" class="cpp">#include<stdio.h>#include<algorithm>using namespace std;const int N = 100005;bool cmp(int a,int b){    return a>b;}__int64 ans,a[N],b[N];int main(){        int T,n,m;    scanf("%d",&T);    while(T--){        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++)            scanf("%I64d",&a[i]);        for(int i=0; i<m; i++)            scanf("%I64d",&b[i]);        sort(a,a+n,cmp);        sort(b,b+m);        int k=0;        ans=-(1<<29);        while(k<n&&k<m){            if(k!=0){                a[k]+=a[k-1];                b[k]+=b[k-1];            }            if(ans<a[k]-b[k])                ans=a[k]-b[k];            k++;        }        printf("%I64d\n",ans);    }}


 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.