UVALive7146 Defeat the Enemy
题目
见链接。
题解
知识点:贪心,STL。
首先要保证我方军队能消灭对方军队才行,因此只要我们按攻击力从大到小排,对方按防御力从大到小排,从大到小遍历,用我方所有攻击力大于敌方目前防御力军队中的一个打,就能保证对方小的防御力不会先把我方大的攻击力用掉,导致对方大的防御力没有办法消灭,即能消灭就一定消灭,不能就一定不能返回 \(-1\)。
现在考虑保证我方被消灭最少,发现如果在我方攻击力大于对方防御力的军队里面选,一定优先选择防御力刚好大于对方攻击力的军队,这样就可以保证大的防御力能保留,小的防御力不会被消灭;但如果最大的防御力都小于等于对方攻击力,那一定选择防御力最小的军队打,因为既然都会同归于尽,那么就让大的防御力保留,把最小的防御力同归于尽,保证后面有足够的防御力。因此我们这时候需要一个容器能够排序,查询,删除,插入,可以有相同元素,那么就要选择 \(multiset\) 存储我方军队防御力。
于是,每次先把大于等于的军队放入多重集,如果没有则返回 \(-1\) 。然后查找一个刚好防御力大于其攻击力的军队打,否则就用防御力最小的打,然后军队存活数量减一。
时间复杂度 \(O((n+m) \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node {
int atk, def;
}a[100007], b[100007];
bool solve() {
int n, m;
cin >> n >> m;
for (int i = 0;i < n;i++)cin >> a[i].atk >> a[i].def;
for (int i = 0;i < m;i++)cin >> b[i].def >> b[i].atk;
sort(a, a + n, [&](node a, node b) {return a.atk > b.atk;});
sort(b, b + m, [&](node a, node b) {return a.def > b.def;});
int cnt = n;
multiset<int> ms;
for (int i = 0, j = 0;j < m;j++) {
while (i < n && a[i].atk >= b[j].def) ms.insert(a[i++].def);
if (ms.empty()) return false;
auto it = ms.upper_bound(b[j].atk);
if (it == ms.end()) {
ms.erase(ms.begin());
cnt--;
}
else ms.erase(it);
}
cout << cnt << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1, cnt = 1;
cin >> t;
while (t--) {
cout << "Case #" << cnt++ << ":";
if (!solve()) cout << -1 << '\n';
}
return 0;
}
UVALive7146 Defeat the Enemy的更多相关文章
- UVa 7146 Defeat the Enemy(贪心)
题目链接: 传送门 Defeat the Enemy Time Limit: 3000MS Memory Limit: 32768 KB Description Long long ago t ...
- 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. ...
- Defeat the Enemy UVALive - 7146
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...
- I - Defeat the Enemy UVALive - 7146 二分 + 贪心
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- [uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心
题意:我方n个军队和敌方m个军队进行一对一的对战,每个军队都有一个攻击力和防御力,只要攻击力不小于对方就可以将对方摧毁.问在能完全摧毁敌方的基础上最多能有多少军队不被摧毁. 思路:按防御力从大到小考虑 ...
- 25 Killer Actions to Boost Your Self-Confidence
25 Killer Actions to Boost Your Self-Confidence Once we believe in ourselves, we can risk curiosity, ...
- Codeforces 240E. Road Repairs 最小树形图+输出路径
最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...
- CF240E Road Repairs(最小树形图-记录路径)
A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 ...
随机推荐
- Idea 进行远程服务器debug操作
本文为博主原创,转载请注明出处: 很多时候为了定位服务器的问题,不方便定位时,采用idea 远程debug 服务器环境的服务进行问题定位,主要操作步骤如下: 1. 修改服务器服务的JVM 配置,开启远 ...
- 【TouchGFX 】使用 CubeMX 创建 TouchGFX 工程时 LCD 死活不显示
生成的代码死活无法让LCD显示,经两个晚上的分析验证是LTDC_CLK引脚速度设置为低速导致,经测试中速.高速.超高速都正常,真是冤,聊以此以示纪念
- 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.12.1)
一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...
- pgcacher 的简单学习
pgcacher 的简单学习 学习地址 https://github.com/rfyiamcool/pgcacher https://zhuanlan.zhihu.com/p/551833981 ht ...
- 快速部署minio的一个思路
快速部署minio的一个思路 背景 小型项目上希望能够快速部署一些中间件. 因为minio比较简单,想着快速一键部署. 加快工作效率. 这里将脚本和思路写下来, 其他应用可以一样进行. 思路 1. 下 ...
- [转帖]PD Config Learn the PD configuration file
The PD configuration file supports more options than command-line parameters. You can find the defau ...
- [转帖]CentOS-7-x86_64-Everything-2009 rpm包列表(CentOS7.9)
CentOS-7-x86_64-Everything-2009 rpm包列表(CentOS7.9) 共10073个文件 复制389-ds-base-1.3.10.2-6.el7.x86_64.rpm ...
- [转帖]MegaCli命令
MegaCli命令 设置jbod模式 1.3 LSI 9260/9261 raid卡配置 LSI 9260/9261 raid卡支持0.1.10.5.6.50.60 常用命令: 清除raid卡原有的配 ...
- 【转帖】查看mysql库大小,表大小,索引大小
https://www.cnblogs.com/lukcyjane/p/3849354.html 说明: 通过MySQL的 information_schema 数据库,可查询数据库中每个表占用的空间 ...
- [转帖]webpagetest 私有化部署
https://www.jianshu.com/p/83bd6b3473ae 介绍 webpagetest 是一款开源的 web 性能测试工具,开源地址,每个人都可在其公共实例上对自己的 web 应用 ...