Senior's Gun(贪心)(long long 与a[i] 如何scanf)

本文介绍了一个射击游戏场景,其中一名角色需要使用不同攻击力的枪支来对付拥有不同防御力的怪物,目标是最大化获取的奖励。文章探讨了如何通过算法找到最佳的攻击策略,包括枪支和怪物的匹配原则,以及实现这一策略的编程方法。
摘要由CSDN通过智能技术生成

Problem Description

Xuejiejie is a beautiful and charming sharpshooter.
她是神枪手
She often carries n guns, and every gun has an attack power a[i] .
她带着n个枪,每个枪有攻击力ai
One day, Xuejiejie goes outside and comes across m monsters, and every monster has a defensive power b[j] .
一天,面对m个怪兽,每个怪兽有保护力bj
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 .
他能用枪i去击杀怪兽j,满足 bj <= ai  他会拿到 ai - bj 金币
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.
T == 测试案例
In each case:
The first line contains two integers n , m .
n m
The second line contains n integers, which means every gun's attack power.
n个整数  == 攻击力
The third line contains m integers, which mean every monster's defensive power.
m个整数 == 防御值
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

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long hp[100001];
long long ad[100001];

bool compare(long long& a,long long& b) {
	return a > b;
}


int main() {

	int caseNum;
	cin >> caseNum;
	for (int caseNo = 1; caseNo <= caseNum; caseNo++) {
		int n , m; // n个ad  // m个hp
		cin >> n >> m;


		for (int i = 0; i <= n - 1; i++) {
			scanf("%lld",&ad[i]);
		}
		for (int i = 0; i <= m - 1; i++) {
			scanf("%lld",&hp[i]);
		}

		sort(ad, ad + n, compare); // 伤害从大到小
		sort(hp, hp + m); // hp从小到大

		long long sum = 0;
		int no = 0;
		for (int i = 0; i <= n - 1; i ++) {
			if (ad[i] >= hp[i] && i <= m - 1) {
				sum += ad[i] - hp[i];
			} else {
				break;
			}

		}

		cout << sum << endl;

	}


}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值