UVA 7146 Defeat The Enemy (贪心)

题意:我方有n个士兵,对方有m个士兵,每个士兵有自己的攻击力和防御力,要派士兵过去把对方的士兵都干掉。当两个士兵相遇时,如果A的攻击力不低于B的防御力,则B就死掉了,同理如果B的攻击力不低于A的防御力,A就死掉了,可以发生两个士兵同时死亡的情况。一个士兵只能参加一次作战(即最多只能干掉对方一个敌人)。问我方能否干掉对方所有的敌人,如果能,我方最多能剩下多少士兵存活。

分析:贪心题!!还学了一波multiset(允许重复),upper_bound()返回一个比给定值大的数的位置(要用到迭代器)对我方的士兵按照攻击从大到小的顺序排序,对敌方的士兵按照防御从大到小的顺序排序。然后扫描的对方的士兵,每次把我方所有能干掉这个敌人的士兵都加到集合里面去,然后看看有没有士兵的防御力高于敌方的攻击力,如果有则派防御力高于敌方的攻击力中防御力最低的士兵去,如果没有则派防御力最低的士兵去。

            multimap提供的操作列出如下:
begin() 返回指向第一个元素的迭代器 
clear() 删除所有元素 
count() 返回一个元素出现的次数 
empty() 如果multimap为空则返回真 
end() 返回一个指向multimap末尾的迭代器 
equal_range() 返回指向元素的key为指定值的迭代器对 
erase() 删除元素 
find() 查找元素 
get_allocator() 返回multimap的配置器 
insert() 插入元素 
key_comp() 返回比较key的函数 
lower_bound() 返回键值>=给定元素的第一个位置 
max_size() 返回可以容纳的最大元素个数 
rbegin() 返回一个指向mulitmap尾部的逆向迭代器 
rend() 返回一个指向multimap头部的逆向迭代器 
size() 返回multimap中元素的个数 
swap() 交换两个multimaps 
upper_bound() 返回键值>给定元素的第一个位置 
value_comp() 返回比较元素value的函数 

Sample Input

22 35 2

Sample Output

Case 1: Yes-1 10 1Case 2: No
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
#define maxn 100005

struct elem
{
	int att,def;
} me[maxn],oth[maxn];
multiset<int> s;
multiset<int>::iterator it;
bool cmpm(elem a,elem b)
{
	return a.att>b.att;
}
bool cmpo(elem a,elem b)
{
	return a.def>b.def;
}
int main()
{
	int t,n,m;
	scanf("%d",&t);
	int cas=0;
	while(t--)
	{
		cas++;
		//printf("Case #%d: ",cas);
		scanf("%d%d",&n,&m);
		int i;
		for(i=0;i<n;i++) scanf("%d%d",&me[i].att,&me[i].def);
		for(i=0;i<m;i++) scanf("%d%d",&oth[i].att,&oth[i].def);
		if(n<m)
		{
			printf("Case #%d: -1\n",cas);continue;
		}
		sort(me,me+n,cmpm);
		sort(oth,oth+m,cmpo);
		s.clear();
		int flag=0,j=0;
		int summ=n,sumo=m;
		for(i=0;i<m;i++)
		{
			while(j<n&&me[j].att>=oth[i].def)
			{
				s.insert(me[j].def);
				j++;
			}
			if(s.empty()){
				flag=1;break;
			}
			int k;
			sumo--;
			it=s.upper_bound(oth[i].att);
			if(it==s.end()) {
				it=s.begin();summ--;
			}
	    	s.erase(it); 
		}
		if(sumo!=0||flag) printf("Case #%d: -1\n",cas);
		else printf("Case #%d: %d\n",cas,summ);
	}
	return 0;
}


             

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Mongol Empire was one of the largest empires in history, stretching from Eastern Europe to Asia. It was founded by Genghis Khan in 1206 and lasted until the mid-14th century. The empire was known for its military prowess, innovative tactics, and the use of terror to intimidate its enemies. Genghis Khan was born in 1162 in the region that is now Mongolia. He was born into a family of nomads and grew up herding sheep and hunting. At the age of 16, he was captured by a rival tribe and enslaved. He escaped and later began to unite the various tribes of the region under his leadership. In 1206, he was declared the supreme ruler of the Mongol Empire. Under Genghis Khan's leadership, the Mongol Empire began to expand rapidly. Genghis Khan was a brilliant military strategist and his army was known for its speed and mobility. The Mongols were also skilled horsemen and archers, and they were able to defeat much larger armies using innovative tactics such as feigned retreats and surprise attacks. After Genghis Khan's death in 1227, his empire was divided among his four sons. Over the next few decades, the Mongol Empire continued to expand under the leadership of Genghis Khan's descendants. They conquered China, Central Asia, and parts of Eastern Europe. The Mongols were known for their brutality and the use of terror to intimidate their enemies. They were also tolerant of other religions and cultures, and they allowed conquered peoples to retain their own customs and traditions. One of the most famous Mongol leaders was Kublai Khan, who ruled China from 1279 to 1294. He founded the Yuan Dynasty, which was the first foreign dynasty to rule China. Kublai Khan was a patron of the arts and literature, and he encouraged the development of new technologies such as paper money and gunpowder. He also sent emissaries to Europe, including Marco Polo, who wrote about his travels in China. The Mongol Empire began to decline in the mid-14th century. The empire had become too large to be effectively governed, and there were internal divisions and conflicts among the ruling families. The Black Death, which swept through Europe and Asia in the mid-14th century, also had a devastating impact on the Mongol Empire. By the end of the 14th century, the Mongol Empire had disintegrated into several smaller states. Despite its reputation for brutality, the Mongol Empire had a significant impact on world history. The Mongols were responsible for the spread of ideas, technologies, and cultures across Eurasia. They also played a role in the development of international trade and commerce. The Mongol Empire was a significant force in world history, and its legacy can still be felt today.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值