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

思路:按防御力从大到小考虑敌方的军队由我们哪只军队去摧毁,对每个敌方军队,维护我方军队可以摧毁它的集合,用S表示,从大到小考虑保证了S更容易维护,只需要记录防御力就够了,不需要大量的删除操作。我们需要选择S中防御力大于当前敌方军队且最小的军队,那么这个军队是可以摧毁敌方军队而自己不被摧毁的。如果这样的军队不存在,那么直接用S中防御力最小的去和敌方军队同归于尽,因为这样做到了自己损失最小。如果中间某个时刻S为空了,则说明我方派不出摧毁当前敌方军队的军队了。

(忠告:维护集合时先想想是用set呢还是multiset,否则又该怀疑数据有问题了。。。)

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma comment(linker, "/STACK:10240000")
#include <bits/stdc++.h>
using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii; #ifndef ONLINE_JUDGE
namespace Debug {
void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
/* -------------------------------------------------------------------------------- */ const int maxn = 1e5 + ; multiset<int> s;
int n, m;
pii a[maxn], b[maxn]; void work() {
int ans = n - m;
s.clear();
int now = n - ;
for (int i = m - ; i >= ; i --) {
while (now >= && a[now].X >= b[i].X) s.insert(a[now --].Y);
if (!s.size()) {
puts("-1");
return ;
}
multiset<int>::iterator iter = s.upper_bound(b[i].Y);
if (iter == s.end()) s.erase(s.begin());
else {
ans ++;
s.erase(iter);
}
}
printf("%d\n", ans);
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T, cas = ;
cin >> T;
while (T --) {
printf("Case #%d: ", ++ cas);
cin >> n >> m;
for (int i = ; i < n; i ++) {
scanf("%d%d", &a[i].X, &a[i].Y);
}
for (int i = ; i < m; i ++) {
scanf("%d%d", &b[i].Y, &b[i].X);
}
if (n < m) puts("-1");
else {
sort(a, a + n);
sort(b, b + m);
work();
}
}
return ;
}

[uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心的更多相关文章

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

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

  2. [LA7139 Rotation(2014 shanghai onsite)]二维树状数组

    题意:有一个n*m的矩形,一辆车从左上角出发,沿一条路径走,路径是由矩形上每个单元格的边构成的,最后回到左上角,求车子在每个格子转过圈数的平方和. 思路:假设需要记录每个格子转的顺时针的圈数(为负表示 ...

  3. UVa 7146 Defeat the Enemy(贪心)

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

  4. UVA LIVE 7146 Defeat the Enemy

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

  5. UVALive 7146 Defeat The Enemy

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

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

  7. 2014 Shanghai Invitation Contest

    题目链接 http://acm.hdu.edu.cn/search.php?field=problem&key=2014%C9%CF%BA%A3%C8%AB%B9%FA%D1%FB%C7%EB ...

  8. Defeat the Enemy UVALive - 7146

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

  9. BZOJ 3671 NOI 2014 随机数生成器 贪心

    题目大意:实在是太难说明了,自己看pdf吧.. 思路:优先依照它说明的方法处理数组,然后为了让数列中尽可能多的出现小的数字,所以1是必需要出现的,这样才干使整个数列的排序后字典序最小. 我们思考,假设 ...

随机推荐

  1. Java 14 来势汹汹,这回让空指针无处遁形!!

    上篇:Java 14 之模式匹配,非常赞的一个新特性! 相信在坐的每一位 Java 程序员都遇到过空指针异常: NullPointerException(NPE),不甚其烦. 栈长之前也分享几篇避免空 ...

  2. python之pymysql库连接mysql实现增、删、改、查

    安装第三方库pymysql 命令行cmd下通过pip install pymysql进行安装,安装完成后自行pip list可查看对应的版本信息 建立连接 1 #导入pymysql库 2 import ...

  3. HTTP Request和Response

    一.Servlet 1:实现Servlet接口 servlet生命周期: init方法:tomcat启动时 调用此方法 service方法:访问servlet时默认执行此方法 destroy方法:to ...

  4. 基于tcp协议的套接字通信:远程执行命令

    要解决粘包问题: TCP:流式协议 特点: 1.数据流没有开头也没有结果,像水流一样 2.TCP协议有一个nagle算法, nagle算法会将数据量较小,并且时间间隔较短的数据合成一条数据发送, 这么 ...

  5. python爬虫——用selenium爬取京东商品信息

    1.先附上效果图(我偷懒只爬了4页)  2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...

  6. 通达OA-2017版本漏洞复现

    搭建环境 服务器 虚拟机系统版本:Windows Server 2016. 安装服务端 1.下载安装程序 这里我们下载的是2017版本的通达OA服务端: 2.安装程序 配置服务: 漏洞复现 1.任意文 ...

  7. js 函数对象的继承 inherit 带 插件完整解析版[helpers.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS的对象继承方法 需求分析: 1. ...

  8. Spring Boot 整合 Spring Security,用户登录慢

    场景 Spring Boot + Spring Security搭建一个Web项目. 临时用了inMemoryAuthentication. @EnableWebSecurity public cla ...

  9. 【集群实战】Rsync数据同步工具

    1. Rsync介绍 1.1 什么是Rsync? Rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具.Rsync软件适用于unix/linux/windows ...

  10. 【Linux常见命令】paste命令

    paste - merge lines of files paste 命令用于合并文件的列. paste 指令会把每个文件以列对列的方式,一列列地加以合并. 语法: paste [OPTION]... ...