题目链接

题目

见链接。

题解

知识点:贪心,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的更多相关文章

  1. UVa 7146 Defeat the Enemy(贪心)

    题目链接: 传送门 Defeat the Enemy Time Limit: 3000MS     Memory Limit: 32768 KB Description Long long ago t ...

  2. UVA LIVE 7146 Defeat the Enemy

    这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...

  3. UVALive 7146 Defeat The Enemy

    Defeat The Enemy Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Long long ...

  4. 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. ...

  5. Defeat the Enemy UVALive - 7146

      Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...

  6. I - Defeat the Enemy UVALive - 7146 二分 + 贪心

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. [uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心

    题意:我方n个军队和敌方m个军队进行一对一的对战,每个军队都有一个攻击力和防御力,只要攻击力不小于对方就可以将对方摧毁.问在能完全摧毁敌方的基础上最多能有多少军队不被摧毁. 思路:按防御力从大到小考虑 ...

  8. 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, ...

  9. Codeforces 240E. Road Repairs 最小树形图+输出路径

    最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...

  10. CF240E Road Repairs(最小树形图-记录路径)

    A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 ...

随机推荐

  1. zookeeper 集群环境搭建及集群选举及数据同步机制

    本文为博主原创,未经允许不得转载: 目录: 1. 分别创建3个data目录用于存储各节点数据 2. 编写myid文件 3. 编写配置文件     4.分别启动 5.分别查看状态 6. 检查集群复制情况 ...

  2. idea开发常用快捷键总结

    转载请注明出处: idea提供了很多的快捷键,但在开发过程中并发全用,只是常用部分快捷键,在这里总结一下,总结的不全,有好的快捷键可在评论里补充下,提前谢各位 由于很早之前用的eclipse或spri ...

  3. 小技巧:WIndows快速创建文件夹

    快速创建文件夹的技巧 1.首先创建文本文档将扩展名更改为.bt,mkdir.bat 2.写入创建文件夹的代码 md 文件夹1 文件夹2 文件夹3 pause 3.双击执行mkdir.bat

  4. .net core 3.0 获取 IServiceProvider 实例

    .net core 3.0后,获取IServiceProvider需要绕点弯路 首先,新建一个类: public class MyServiceProviderFactory : IServicePr ...

  5. [转帖]TPC-C 、TPC-H和TPC-DS区别

    https://zhuanlan.zhihu.com/p/339886289 针对数据库不同的使用场景TPC组织发布了多项测试标准. TPC-C: TPC Benchmark C于1992年7月获得批 ...

  6. [转帖]058、集群优化之PD

    PD调度基本概念 调度流程 调度中还有这还缺来了merge,例如合并空region. store: 基本信息,容量,剩余空间,读写流量等 region: 范围,副本分布,副本状态,数据量,读写流量等 ...

  7. [转帖]关于 AREX

    https://arextest.github.io/website/zh-Hans/docs/intro/ AREX 介绍​ 背景​ 对于一个初上线的简单服务,只需通过常规的自动化测试加上人工即可解 ...

  8. [转帖]发布即巅峰!万字长文:Java性能调优六大工具:MAT内存分析工具

    jianshu.com/p/4ed3dd8b7b83 MAT是MemoryAnalyzerTool的简称,它是一款功能强大的Java堆内存分析器,可以用于查找内存泄漏以及查看内存消耗情况.MAT是 基 ...

  9. Linux与Windows系统字符集的简要学习

    背景 最近同事反馈公司的产品再更新了mysql-8.0.31的驱动jar包后部分功能报错. 问题核心原因 研发这边石磊老师已经找到了. 结论是Mysql8.0.26之后的数据库驱动好像会识别操作系统的 ...

  10. Redis 的简单学习与整理

    Redis 的简单学习与整理 背景 最近一直进行Redis性能调优和高可用的课题 但是不管什么课题,第一步应该是学习与使用redis 之前总结过 rdb 与 rdr 分析 键值对等内容. 但是发现想要 ...