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.
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 the
attack 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.
题目大意:每个军队有一个攻击力和防御力,当一个军队的攻击力大于等于对方的防御力,就可以摧毁敌军(可以双方同时阵亡)。现给出我方和敌方所有军队的攻击力和防御力,问要击溃敌方所有军队,最多能保留多少的存活兵力(只能单挑)。
思路:贪心,可参考田忌赛马。
把我方按攻击力从大到小排序,把敌方按防御力从大到小排序。
遍历敌方的军队,设当前敌方军队为enemy。
要击溃enemy,首先我方派出军队的攻击力要大于等于enemy的防御力。如果我方能不损兵干掉对面,就贪心选择一个防御力最小的但又大于enemy的攻击力的军队。如果必须损兵,就贪心地选择一个防御力最小的军队。这个可以用multiset维护。
正确性就不证啦,不服你举个反例。
PS:比赛的时候吧multiset写成了set卡了几个小时呵呵呵呵……
代码(0.082S):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std; const int MAXN = ; struct Node {
int atk, def;
void read() {
scanf("%d%d", &atk, &def);
}
}; Node a[MAXN], b[MAXN];
int n, m, T; int solve() {
multiset<int> st;
int res = ;
for(int i = , j = ; j < m; ++j) {
while(i < n && a[i].atk >= b[j].def)
st.insert(a[i++].def);
if(st.empty()) return -;
auto it = st.upper_bound(b[j].atk);
if(it != st.end()) st.erase(it);
else st.erase(st.begin()), res++;
}
return n - res;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i) a[i].read();
for(int i = ; i < m; ++i) b[i].read();
sort(a, a + n, [](Node x, Node y) {
return x.atk > y.atk;
});
sort(b, b + m, [](Node x, Node y) {
return x.def > y.def;
});
printf("Case #%d: %d\n", t, solve());
}
}
UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)的更多相关文章
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7146 Defeat The Enemy
Defeat The Enemy Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Long long ...
- hdu5071 2014 Asia AnShan Regional Contest B Chat
模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...
- 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy
Galaxy Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...
随机推荐
- 一些Asp.Net面试题答案
工作时间长了总是用同样的一些东西 其他的有些生疏 闲来看看面试题练习一下: 题目出处嘛...aspnet-tests-for-juniors 转载请注明来源:http://www.cnblogs ...
- Android -- ImageView(控制图片的大小以及旋转的角度)
1.
- 【JAVA】 UIMnager
Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...
- Mysql创建新用户方法
1. CREATE USER 语法: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'dog'@'lo ...
- php正则获取html图片标签信息(采集图片)
php获取html图片标签信息(采集图片),实现图片采集及其他功能,带代码如下: <?php $str="<img src='./a.jpg'/>111111<img ...
- 初学者对于MVC架构模式学习与理解
理解MVC的工作原理,明白一个网页是如何显示出来的 之前一直盲目的在慕课上看视频,脑袋里想着要理解mvc,看了mvc相关的视频,看完之后就觉得空白白的,M,V,C各代表什么我知道,但是这个究竟有啥意思 ...
- hibernate关联关系笔记
Hibernate关联关系笔记 单向N:1 * 有连接表:在N方使用<join>/<many-to-one>.1方无需配置与之关联的持久化类. * 没有连接表:在N方使用& ...
- sql语句 之聚合函数
聚合分析 在访问数据库时,经常需要对表中的某列数据进行统计分析,如求其最大值.最小值.平均值等.所有这些针对表中一列或者多列数据的分析就称为聚合分析. 在SQL中,可以使用聚合函数快速实现数据的聚 ...
- MyBatis学习总结(五)——实现关联表查询(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013516.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数 ...
- 【Git学习笔记】撤销修改
工作区下的.git文件夹其实是Git的版本库,Git的版本库里存了很多东西,其中最重要的就是称为 stage 的暂存区,还有Git为我们自动创建的第一个分支 master ,以及指向master的一个 ...