题目链接

题目

见链接。

题解

知识点:贪心,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. 基于html+javascript开发的base64解码工具

    base64在线解码工具可以帮助你将Base64编码的字符串解码为原始的文本或数据. 预览入口 以下是一个简单的base64在线解码工具的示例: html <!DOCTYPE html> ...

  2. spring启动流程 (6完结) springmvc启动流程

    SpringMVC的启动入口在SpringServletContainerInitializer类,它是ServletContainerInitializer实现类(Servlet3.0新特性).在实 ...

  3. [转帖]grafana自定义告警模版

     发表于 2022-03-16  更新于 2023-03-03 因 grafana 告警信息太多无用数据,容易干扰查看例如使用 企业微信告警消息如下太多无用Labels 例如 endpoint,job ...

  4. [转帖]redis缓存命中率介绍

    缓存命中率的介绍 命中:可以直接通过缓存获取到需要的数据. 不命中:无法直接通过缓存获取到想要的数据,需要再次查询数据库或者执行其它的操作.原因可能是由于缓存中根本不存在,或者缓存已经过期. 通常来讲 ...

  5. [转帖]DBWR与LGWR的写入机制

    https://www.jianshu.com/p/6c87cb6cd320 读与写是每个数据库提供的最基本的功能.当数据库中出现第一个进程时,总免不了要将数据从磁盘上加载到内存中,一次数据库的物理I ...

  6. [转帖]《Linux性能优化实战》笔记(24)—— 动态追踪 DTrace

    使用 perf 对系统内核线程进行分析时,内核线程依然还在正常运行中,所以这种方法也被称为动态追踪技术.动态追踪技术通过探针机制来采集内核或者应用程序的运行信息,从而可以不用修改内核和应用程序的代码就 ...

  7. [转帖]Redis 使用指南:深度解析 info 命令

    https://www.cnblogs.com/hwpaas/p/9442410.html Redis 是一个使用  ANSI C 编写的开源.基于内存.可选持久性的键值对存储数据库,被广泛应用于大型 ...

  8. java浅拷贝BeanUtils.copyProperties引发的RPC异常 | 京东物流技术团队

    背景 近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到 人在地铁上,bug从咚咚来~ 没有任何修改的服务接口,抛出异常: ...

  9. 【dp,建模】AGC032D Rotation Sort

    Problem Link 有一个长为 \(n\) 的排列 \(p\),给定 \(A,B\),你每次可以做以下两种操作之一: 选取 \(l,r\),将 \(p[l:r]\) 循环右移,代价为 \(A\) ...

  10. vue中jsx

    //item.vue 文件如下 <template> <div> <h1 v-if="id===1"> <slot></slo ...