题目链接

题目

见链接。

题解

知识点:贪心,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. Win10 高效语音输入方案:听写功能 win + H

    win + H 打开听写功能,即可打开语音输入,帮你快速糊完文档 (队友亲测好用)

  2. 如何与chatgpt共存

    作为程序员,专注于创造性劳动,而把重复性劳动任务交给chatgpt,要成为 需求 和 chatgpt的桥梁. 人工智能比如chatgpt越来越强,提问能力是人类的天赋,提问能力更为重要.

  3. 基于python的视频点播网站(python+django+vue开发的视频点播网站-视频管理系统)

    演示地址 前台地址: http://video.gitapp.cn 后台地址:http://video.gitapp.cn/admin 后台管理帐号: 用户名:admin123 密码:admin123 ...

  4. 【MCU】单片机如何检测市电通断?(应用甚广~)

    [来源]https://mp.weixin.qq.com/s/TQKtEbxS8WSo3D1MecdMIw

  5. java - for循环 排序数组 - 求数组最小值

    主要是利用静态变量存储 public class Bubble2 { static int minNumber; public static void main(String[] args) { in ...

  6. [转帖]如何监控Redis性能指标(译)

    Redis给人的印象是简单.很快,但是不代表它不需要关注它的性能指标,此文简单地介绍了一部分Redis性能指标.翻译过程中加入了自己延伸的一些疑问信息,仍然还有一些东西没有完全弄明白.原文中Metri ...

  7. [转帖]OpenSSL版本历史

    OpenSSL版本历史 新闻日志 这是所有 OpenSSL 公告的简洁日志.它们几乎是发布通知. 日期物品 2021 年 7 月 29 日OpenSSL 3.0 的 Beta 2 现已推出.这是一个候 ...

  8. [转帖]TiUP Cluster 命令合集

    https://docs.pingcap.com/zh/tidb/stable/tiup-component-cluster TiUP Cluster 是 TiUP 提供的使用 Golang 编写的集 ...

  9. 计划任务方式定期获取jvm dump的方法

    说明 产品最近有一些问题,想着能够每隔一段时间抓取一下dump文件. 需求 可以定期抓取, 需要注意磁盘空间的使用. 实现方法 定时任务使用 crontab 计划任务来做 预定义获取jvm dump的 ...

  10. VOP 消息仓库演进之路|如何设计一个亿级企业消息平台

    作者:京东零售 李孟冬 VOP作为京东企业业务对外的API对接采购供应链解决方案平台,一直致力于从企业采购数字化领域出发,发挥京东数智化供应链能力,通过产业链上下游耦合与链接,有效助力企业客户的成本优 ...