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颗 ...
随机推荐
- 一个print的简单错误
原因是python 2.X 可以print 内容 而3.X 取消了这种写法 改成了print(内容)
- [Leetcode] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Ubuntu14.04安装微软雅黑字体
1.首先获得一套“微软雅黑”字体库(自行百度),包含两个文件msyh.ttf(普通)、msyhbd.ttf(加粗);2.在/usr/share/fonts目录下建立一个子目录,例如win,命令如下: ...
- windows下读取磁盘扇区数据
在Windows下,磁盘可以看做是一个文件,其文件名为\\\\.\\PhysicalDriveX,其中X表示磁盘的编号,例如\\\\.\\PhysicalDrive0表示的是第0号磁盘,如果需要读取一 ...
- 【转发】关于Java性能的9个谬论
转载请注明出处,感谢大家的支持!本文来自优优码:http://www.uucode.net/201502/9%e4%b8%aa%e8%b0%ac%e8%ae%ba Java的性能有某种黑魔法之称.部分 ...
- java jdbc
1.java 的jdbc类包括,DriverManager,Connection Connection con = DriverManager.getConnection("jdbc:odb ...
- .net web 小基累
获取当前网站的根目录:HttpContext.Current.Request.PhysicalApplicationPath+“Content”
- SQL DEFAULT 约束
DEFAULT 约束用于向列中插入默认值. 如果没有规定其他的值,那么会将默认值添加到所有的新记录. 下面的 SQL 在 "Persons" 表创建时为 "City&qu ...
- Solr Python API : SolrCloudpy 与 Pysolr 的 对比
http://ae.yyuap.com/pages/viewpage.action?pageId=920314 SolrCloudpy文档:http://solrcloudpy.github.io/s ...
- 云计算CTO工作的具体内容(挺详细)
职位描述: 工作职责: 1.参与制定公司发展战略.年度经营计划和预算方案. 2.全面管理公司研发和技术支持工作,全面负责技术层面的整体运营,包括软件研发.项目实施.销售支持及技术管理: 3.规划公司的 ...