题意:我方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. Python的6种方式实现单例模式

    单例模式是一个软件的设计模式,为了保证一个类,无论调用多少次产生的实例对象,都是指向同一个内存地址,仅仅只有一个实例(只有一个对象). 实现单例模式的手段有很多种,但总的原则是保证一个类只要实例化一个 ...

  2. [转+自]SSH工作原理

    SSH工作原理 熟悉Linux的人肯定都知道SSH.SSH是一种用于安全访问远程服务器的网络协议.它将客户端与服务端之间的消息通过加密保护起来,这样就无法被窃取或篡改了.那么它安全性是如何实现的呢? ...

  3. webpack之Loader

    我们知道webpack的优点之一就是专注于处理模块化的项目,能做到开箱即用,但同时这也是webpack的缺点,只能用于模块化开发的项目,例如:Vue,React,Angular.Webpack在进行打 ...

  4. 一个可能是世界上最全的 API 接口集合库开源项目

    对于程序员来说,为自己的程序选择一些合适的API并不是那么简单,有时候还会把你搞得够呛,今天猿妹要和大家分享一个开源项目,这个项目汇集了各种开发的api,涵盖了音乐.新闻.书籍.日历等,无论你是从事W ...

  5. centos 部署 vue项目

    安装Nodejs 下载安装包,可选择其他版本 node-v10.16.0-linux-x64.tar.xz 将下载文件上传至linux服务器并解压 tar -xvf node-v10.16.0-lin ...

  6. python学习23之标准库

    '''''''''标准库1.datetime 日期时间模块存在于Lib/datetime.py文件内'''from datetime import datetime,date,time #from d ...

  7. 如何在 Windows Event Log 中查找系统重启的信息

    事件ID:12 事件ID 13: 事件ID 41: 事件ID 6008: 事件ID 1074:事件ID 1074: ========================================== ...

  8. [Ubuntu ] Vim Error E492 - Not an editor command: PluginInstall

    git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/plugin/Vundle.vim https://stackoverflow.co ...

  9. java关于throw Exception的一个小秘密

    目录 简介 throw小诀窍 总结 java关于throw Exception的一个小秘密 简介 之前的文章我们讲到,在stream中处理异常,需要将checked exception转换为unche ...

  10. slow-log 和bin-log相关参数介绍

    1. slow-log show global status Slow_queries   --------慢查询的次数,即查询的时间超过long_query_time设置的时间(不能修改) 配置文件 ...