UVa 7146 Defeat the Enemy(贪心)

题目链接: 传送门

Defeat the Enemy

Time Limit: 3000MS     Memory Limit: 32768 KB

Description

Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others.One day, there is another tribe become their target. The strong tribe has decide to terminate them!!!There are m villages in the other tribe. Each village contains a troop with attack power EAttacki,and defense power EDefensei. Our tribe has n troops to attack the enemy. Each troop also has theattack power Attacki, and defense power Defensei. We can use at most one troop to attack one enemy village and a troop can only be used to attack only one enemy village. Even if a troop survives an attack, it can’t be used again in another attack. The battle between 2 troops are really simple. The troops use their attack power to attack against the other troop simultaneously. If a troop’s defense power is less than or equal to the other troop’s attack power, it will be destroyed. It’s possible that both troops survive or destroy.The main target of our tribe is to destroy all the enemy troops. Also, our tribe would like to have most number of troops survive in this war.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case start with 2 numbers n and m, the number of our troops and the number of enemy villages. n lines follow,each with Attacki and Defensei, the attack power and defense power of our troops. The next m lines describe the enemy troops. Each line consist of EAttacki and EDefensei, the attack power and defense power of enemy troops.

Output

For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (starting from 1) and y is the max number of survive troops of our tribe. If it‘s impossible to destroy all enemy troops, output ‘-1’ instead.

Limits:

1 ≤ T ≤ 100,
1 ≤ n, m ≤ 105,
1 ≤ Attacki, Defensei, EAttacki, EDefensei ≤ 109,

Sample Input

2
3 2
5 7
7 3
1 2
4 4
2 2
2 1
3 4
1 10
5 6

Sample Output

Case #1: 3
Case #2: -1

解题思路:

将我方战斗力从大到小排,敌方防御力从大到小排 然后每次把我方的战斗力大于敌方的防御力的战士的防御力加入到multiset中 在集合中查找比敌方攻击力大的最小的防御值,如存在,则用这个干掉敌人,自己能幸存,否则,用能干掉敌方的防御力最小的士兵,同归于尽

#include<iostream>
#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
using namespace std;

struct Node{
	int first,second;
};

bool cmp1(Node x,Node y)
{
	return x.first > y.first;
}

bool cmp2(Node x,Node y)
{
	return x.second > y.second;
}

int main()
{
	int T,Case = 1;
	scanf("%d",&T);
	while (T--)
	{
		int N,M,kill = 0;
		bool flag = true;
		Node our[100005],you[100005];
		memset(our,0,sizeof(our));
		memset(you,0,sizeof(you));
		multiset<int>s;
		multiset<int>::iterator it;
		scanf("%d%d",&N,&M);
		for (int i = 0;i < N;i++)
		{
			scanf("%d%d",&our[i].first,&our[i].second);
		}
		for (int i = 0;i < M;i++)
		{
			scanf("%d%d",&you[i].first,&you[i].second);
		}
		sort(our,our+N,cmp1);
		sort(you,you+M,cmp2);
		int j = 0;
		for (int i = 0;i < M;i++)
		{
			while (j < N && our[j].first >= you[i].second)
			{
				s.insert(our[j++].second);
			}
			if (s.empty())
			{
				flag = false;
				break;
			}
			it = s.upper_bound(you[i].first);
			if (it == s.end())
			{
				it = s.begin();
			}
			if (*it <= you[i].first)
			{
				kill++;
			}
			s.erase(it);
		}
		printf("Case #%d: %d\n",Case++,flag?(N-kill):-1);
	}
	return 0;
}
posted @   zxzhang  阅读(390)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航