题目链接:

http://poj.org/problem?id=3614

Sunscreen

Time Limit: 1000MS
Memory Limit: 65536K
#### 问题描述
> to avoid unsightly burns while tanning, each of the c (1 ≤ c ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. cow i has a minimum and maximum spf rating (1 ≤ minspfi ≤ 1,000; minspfi ≤ maxspfi ≤ 1,000) that will work. if the spf rating is too low, the cow suffers sunburn; if the spf rating is too high, the cow doesn't tan at all........
> the cows have a picnic basket with l (1 ≤ l ≤ 2500) bottles of sunscreen lotion, each bottle i with an spf rating spfi (1 ≤ spfi ≤ 1,000). lotion bottle i can cover coveri cows with lotion. a cow may lotion from only one bottle.
> what is the maximum number of cows that can protect themselves while tanning given the available lotions?

输入

  • Line 1: Two space-separated integers: C and L
  • Lines 2..C+1: Line i describes cow i's lotion requires with two integers: minSPFi and maxSPFi
  • Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

输出

A single line with an integer that is the maximum number of cows that can be protected while tanning

样例

sample input

3 2

3 10

2 5

1 5

6 2

4 1

sample output

2

题意

有c头牛晒太阳,每头牛都有一个能承受辐射的范围(min~max),现在有 l 种防晒霜,每种防晒霜都能将辐射值固定在spf,每种防晒霜都有一定的数量num。每头牛用最多一种防晒霜,问能满足多少头牛。

题解

贪心

对所有的牛按左端点排序,最所有的防晒霜按其spf值排序,然后从小到大枚举防晒霜,枚举到第i个防嗮霜的时候在所有可以满足的牛里面贪心选出一个右端点最小的牛(它最不可能用到后面的防嗮霜,所有他优先选)。

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<functional>
#define X first
#define Y second
#define mp make_pair
using namespace std; const int maxn = 2555;
int n, m; struct Node {
int x, y;
Node(int x,int y):x(x),y(y){}
bool operator < (const Node& tmp) const {
return y > tmp.y;
}
}; vector<pair<int, int> > cow, sc; int main() {
while (scanf("%d%d", &n, &m) == 2 && n) {
cow.clear(); sc.clear();
for (int i = 0; i < n; i++) {
int x, y; scanf("%d%d", &x, &y);
cow.push_back(mp(x, y));
}
for (int i = 0; i < m; i++) {
int x, y; scanf("%d%d", &x, &y);
sc.push_back(mp(x, y));
}
sort(cow.begin(), cow.end());
sort(sc.begin(), sc.end());
int ans = 0,st=0;
priority_queue<Node> pq;
for (int i = 0; i < m; i++) {
while (st < n&&cow[st].X <= sc[i].X) {
pq.push(Node(cow[st].X, cow[st].Y));
st++;
}
while (!pq.empty() && sc[i].Y) {
if (sc[i].X <= pq.top().y) {
sc[i].Y--;
ans++;
}
pq.pop();
}
}
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. ubuntu下avd创建和文件传输
  2. Python 基礎 - 集合的使用
  3. IPv6实验准备
  4. ThinkPHP 3.2.3 文件上传时间目录问题
  5. Java 学习之路 之 泛型方法
  6. LoadRunner 脚本学习 -- 动态储存方式和静态储存方式
  7. 十进制转二进制and位运算符
  8. python中的__init__ 、__new__、__call__小结
  9. javascript中substring和substr方法
  10. eclipse调试的基本意义
  11. man命令重定向后有^H乱码问题
  12. Setup SSH and SVN on Windows Server
  13. CentOS克隆机器步骤,图文教程
  14. Java7后try语句的优化
  15. ES6+javaScript原型
  16. js 购物车的实现
  17. FP ABPPMGR表 其它常用存储过程
  18. 简易HashMap实现
  19. MHDD修复硬盘坏道
  20. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

热门文章

  1. 关于servlet是在什么时候初始化的个人总结
  2. 学习 AngularJS 第一天
  3. 济南学习 Day1 T3 pm
  4. 洛谷 P1195 口袋的天空
  5. 真正理解KMP算法
  6. Ubuntu14.04 开启MySQL的remote access
  7. Informix 物联网应用示例(转)
  8. TortoiseGit 安装和使用的图文教程
  9. 如何修改 Discuz 门户文章页默认视频大小
  10. Excel中的宏--VBA的简单例子