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颗 ...
随机推荐
- Java 常用方法
java 图片上传from表单必加属性:enctype="multipart/form-data" 1.获取字符串的长度 : length() 2 .判断字符串的前缀或后缀与已知字 ...
- 一个print的简单错误
原因是python 2.X 可以print 内容 而3.X 取消了这种写法 改成了print(内容)
- 【BZOJ】3835: [Poi2014]Supercomputer
题意 \(n(1 \le 1000000)\)个点的有根树,\(1\)号点为根,\(q(1 \le 1000000)\)次询问,每次给一个\(k\),每一次可以选择\(k\)个未访问的点,且父亲是访问 ...
- hud 5876 2016 ACM/ICPC Asia Regional Dalian Online
题意:给一个图 给定一个点s 求补图中s点到达各个点的最短路 思路:从s点开始bfs 在图中与s点有连接的都是在补图中不能直接到达的点 反之在补图中都是可以直接到达的点 由此bfs ((( 诡异的写法 ...
- BS中的 data:image/png;base64
举个图片的例子: 网页中一张图片可以这样显示: <img src="http://mail.163.com/images/x.png" /> 也可以这样显示: < ...
- 2016huasacm暑假集训训练三 D - Invitation Cards
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/D 题意:一张个向图,求从点1开始到其他各点的最短路权值和加上从其他各点到点1的最短 ...
- thinkphp2
- 【emWin】例程八:绘制位图
实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1bpeMYpp 密码:wgtp 实验现象:
- 【iCore3应用开发平台】发布 iCore3 应用开发平台使用说明
PDF下载地址:http://pan.baidu.com/s/1c2ca2lU
- selenium项目的实战经验
以前学习selenium,最接近项目的经验就是写了百度首页和自己开发的一个小网站的脚本,当时觉得差不多可以了.然而这次项目实战才发现还是学到不少知识,毕竟这个网站的专业程度远超过我自己写的,而且复杂程 ...