Senior's Gun(贪心)

Senior's Gun

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


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
BestCoder Round #47 ($)
 


解题思路:贪心,每次都用剩余的最大的枪的能量减去剩余最小怪物的能量。注意不能用cin,cout,不然这题会超时

还有答案要用long  long  ,不然会WA,因为 1n,m100000 ,   举个极端例子,a[0]=a[1]=....=a[99999]=1e9,  b[0]=b[1]=....=a[99999]=0, 此时答案为

100000*1e9,而int型约为21亿,肯定WA

下面附上各种数据类型取值范围

unsigned   int   0~4294967295   
int   2147483648~2147483647 
unsigned long 0~4294967295
long   2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define lim 100000+10
bool cmp(int a, int b)
{
    return a>b;
}

int a[lim], b[lim];
int main()
{
    int n, m, T;
    int i, j;
    int len;
    scanf("%d", &T);
    while(T--)
    {
        long long sum=0;
        scanf("%d%d", &n, &m);
        len=min(n, m);
        for(i=0;i<n;i++)
            scanf("%d", &a[i]);
        for(i=0;i<m;i++)
            scanf("%d", &b[i]);
        sort(a,a+n,cmp);
        sort(b,b+m);
        for(i=0;i<len;i++)
        {
            if(a[i]>b[i])
                sum+=(a[i]-b[i]);
            else
                break;
        }
        printf("%I64d\n", sum);
    }
    return 0;
}


109a[i],b[j]109

你可能感兴趣的:(贪心)