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颗 ...
随机推荐
- jenkins和hudson---打酱油的日子
自动化构建:Jenkins起源于Hudson.Hudson在商业软件的路上继续前行,而Jenkins则作为开源软件,从hudson分支出来. 因此现在的jenkins和hudson非常类似,但是随着二 ...
- Python2 连接MySQL
先安装MySQL-python yum install -y MySQL-python 测试代码: # -*- coding: utf-8 -*- import os import MySQLdb i ...
- Python中的dict和set
1.dict定义: Python写一个dict如下: >>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} >>> d[' ...
- DEV 等待窗口
DevExpress.Utils.WaitDialogForm dlg = , )); System.Threading.Thread.Sleep(); dlg.Close();
- JS 点击弹出图片/ 仿QQ商城点击左右滚动幻灯片/ 相册模块,点击弹出图片,并左右滚动幻灯片
1, 点击弹出图片 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 【Alpha】Daily Scrum Meeting总结
一.项目预期计划和现实进展 项目预期计划 现实进展 登陆 完成 使用菜单 完成 查看自己的信息 完成(额外完成可修改) 完成能用的界面 完成(额外美化) 可以导入导出表格 导入表格完成,导出未完成 教 ...
- HDU2653 BFS+优先队列
Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/3 ...
- jQuery全屏滚动插件fullPage.js
github https://github.com/alvarotrigo/fullPage.js demo http://alvarotrigo.com/fullPage/ 脚手架 <link ...
- 20145215实验二 Java面向对象程序设计
一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...
- 安卓中級教程(6):annotation的基本用法
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...