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;
}
UVa 7146 Defeat the Enemy(贪心)的更多相关文章
- UVA LIVE 7146 Defeat the Enemy
这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...
- UVALive 7146 Defeat The Enemy
Defeat The Enemy Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Long long ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- I - Defeat the Enemy UVALive - 7146 二分 + 贪心
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Defeat the Enemy UVALive - 7146
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...
- [uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心
题意:我方n个军队和敌方m个军队进行一对一的对战,每个军队都有一个攻击力和防御力,只要攻击力不小于对方就可以将对方摧毁.问在能完全摧毁敌方的基础上最多能有多少军队不被摧毁. 思路:按防御力从大到小考虑 ...
- UVA 11729 - Commando War(贪心 相邻交换法)
Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...
- UVA 11292-Dragon of Loowater (贪心)
Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...
随机推荐
- U-boot与linux的关系
基本上没有啥关系,U-boot的话你也知道,说白了就像是Dos工具箱,本身算是个精简的Linux系统了,主要是负责硬件的初始化和引导,本身带有一些工具,作为引导程序,常作为嵌入式设备的引导.当真正的系 ...
- 理解Android虚拟机体系结构
1 什么是Dalvik虚拟机 Dalvik是Google公司自己设计用于Android平台的Java虚拟机,它是Android平台的重要组成部分,支持dex格式(Dalvik Executable)的 ...
- Code Review 五问五答
Code Review 是什么? Code Review即代码审查,程序猿相互审核对方的代码. Code Review能获得什么好处? 提高代码可维护性 你写的代码不再只有编译器看了,你得写出审核人能 ...
- ModernUI教程:如何从MUI样式中派生自定义样式
下面的步骤用来说明怎么样去创建一个基于MUI的自定义样式.让我们创建一个字体颜色显示为红色的按钮样式. 可视化显示如下: 因为我们并没有明确生命继承自MUI风格,它还是采用WPF的默认风格.我们需要设 ...
- 优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案
简介 本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发 ...
- [BZOJ 1997][HNOI2010]Planar(2-SAT)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1997 分析: 考虑每条边是在圈子里面还是圈子外面 所以就变成了2-SAT判定问题了= ...
- yii2组件之下拉框带搜索功能(yii-select2)
简单的小功能,但是用起来还是蛮爽的.分享出来让更多的人有更快的开发效率,开开心心快乐编程. 如果你还没有使用过composer,你可就out了,看我的教程分享,composer简直就是必备神奇有木有. ...
- C++命名规范
1.1 类型名 首字母大写,末尾加_T.如: class TnppCoverageArea_T{…}; 1.2 1.2 变量和函数名 变量和函数名中首字母小写,其后每个英文单词的第一个字母 ...
- 分析函数——keep(dense_rank first/last)
来源于:http://blog.itpub.net/28929558/viewspace-1182183/ 销售表:SQL> select * from criss_sales where de ...
- 如何使用国内源部署Ceph?
由于网络方面的原因,Ceph的部署经常受到干扰,通常为了加速部署,基本上大家都是将Ceph的源同步到本地进行安装.根据Ceph中国社区的统计,当前已经有国内的网站定期将Ceph安装源同步,极大的方便了 ...