POJ 3614:Sunscreen 贪心+优先队列
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5378 | Accepted: 1864 |
Description
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?
Input
* 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
Output
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头牛,每头牛有自己的承受阳光的最小值和最大值。现在有L瓶防晒霜,能够保持,一定数额的阳光。。。。然后还有一定的数量。问最多有多少头牛是被保护着的。
对牛进行排序,承受阳光小的放前面。
对防晒霜进行排序,保持阳光小的放在前面。
对每一种防晒液进行遍历,然后把能塞进来的牛即牛的最小值<防晒霜的值,都塞到优先队列中来。然后优先队列往外面是按照牛的最大值的递增顺序,先往外面弹小的,然后往外面弹大的。这样贪心就能够保证如果成功是最优的结果,失败了的话后面的防晒霜也不可能符合规格了。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#pragma warning(disable:4996)
using namespace std; struct no1 {
int min_v;
int max_v;
}cow[2505],lotion[2505]; int num_c, num_l; bool cmp2(no1 x, no1 y)
{
if (x.min_v == y.min_v)
{
return x.max_v < y.max_v;
}
else
{
return x.min_v < y.min_v;
}
} class cmp
{
public:
bool operator()(int x, int y)
{
return x > y;
}
}; priority_queue<int, vector<int>, cmp>qu; int main()
{
int i,j,ans;
scanf("%d%d", &num_c, &num_l); for (i = 1; i <= num_c; i++)
{
scanf("%d%d", &cow[i].min_v, &cow[i].max_v);
}
for (i = 1; i <= num_l; i++)
{
scanf("%d%d", &lotion[i].min_v, &lotion[i].max_v);
}
sort(cow + 1, cow + num_c + 1, cmp2);
sort(lotion + 1, lotion + num_l + 1, cmp2); j = 1;
ans = 0; for (i = 1; i <= num_l;i++)
{
while (j <= num_c && cow[j].min_v <= lotion[i].min_v)
{
qu.push(cow[j].max_v);
j++;
}
while (qu.size() != 0 && lotion[i].max_v != 0)
{
int x = qu.top();
qu.pop();
if (x < lotion[i].min_v)
{
continue;
}
else
{
ans++;
lotion[i].max_v--;
}
}
}
cout << ans << endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3614:Sunscreen 贪心+优先队列的更多相关文章
- poj -3614 Sunscreen(贪心 + 优先队列)
http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...
- POJ 3614 Sunscreen 贪心
题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...
- 优先队列:POJ No 3614 Sunscreen 贪心
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6410 Accepted: 2239 Descrip ...
- POJ 3614 Sunscreen 优先队列 贪心
题意 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值,太大就晒伤了,太小奶牛没感觉. 而刚开始的阳光的强度非常大,奶牛都承受不住 ...
- 【POJ 3614 Sunscreen】贪心 优先级队列
题目链接:http://poj.org/problem?id=3614 题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max]:有L瓶防晒液,每瓶有自己的spf值和容量(能供几头 ...
- Sunscreen POJ - 3614(贪心)
To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with s ...
- POJ 3614 Sunscreen (优先队列)
题意:奶牛美容:有C头奶牛日光浴,每头奶牛分别需要minSPF_i和maxSPF_i单位强度之间的阳光.现有L种防晒霜,分别能使阳光强度稳定为SPF_i,其瓶数为cover_i.求最多满足多少头奶牛 ...
- POJ 3614 Sunscreen(贪心,区间单点匹配)
把牛的SPF看作一个区间,防晒霜看作点.一个点可以匹配C[i]次,问最大匹配数.可以用图论做. 也可以贪心.贪心的思想是,把区间和点排序以后,考虑最左边的点,加入和这个点相交的区间, 并排除出界的区间 ...
- poj 3614 Sunscreen
...
随机推荐
- Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换
今天处理了一个Bug,创建历史数据时脚本函数的起始时间不赋值或者赋0值时,计算引擎推给历史库的UTC时间为-288000000000,一开始以为是bug,经过分析后发现不赋值默认给起始时间赋0值,而此 ...
- ffmpeg使用C语言sdk实现抽取视频中的视频数据
主要使用函数 特征码:Start code 解码的一些视频参数,分辨率和帧率:SPS/PPS ffmpeg获取SPS/PPS:codec->extradata 实例 #include <s ...
- 当你输入一个url链接发生了什么?
先看一张图 打开手机和pad的QQ 发送消息给QQ好友,此时发送了一个请求给服务器,服务器接受到后反馈给手机和pad发送消息成功,然后QQ好友就可以接受到消息了 电脑浏览器输入百度 打开台式电脑或者笔 ...
- vue dialog每次打开会展示上一次数据(转载)
原文地址: (https://www.jianshu.com/p/82b6681d0768) 在dialog外套一层div,div中以v-if来控制组件el-dialog的出现与否,每次弹出el-di ...
- 【剑指Offer面试编程题】题目1384:二维数组中的查找--九度OJ
题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 输入: 输入可能包含 ...
- 《Java周边》IDEA 创建Gradle子项目
1. 创建Gradle项目 新建项目选择左侧gradle,右侧选择自己jdk版本,勾选java项目,点击Next 写上GroupId组名ArtifactId项目名后点击Next 这里选择使用本地gra ...
- markdown基本语法教程
标题 一级标题 二级标题 三级标题 以此类推,总共六级标题,建议在警号后面加一个空格,这是最标准的markdown语法 列表 在markdown下: 列表的显示只需要在文字前加上-.+或*即可变为无序 ...
- 新见Java数据类型_需了解
LinkedList<T>.LinkedList.poll() 先给出结论:pop 与 poll 都是取出 LinkedList 的第一个元素,并将该元素删除,等效于:removeFirs ...
- springboot打包的问题可执行jar和不可执行jar
具体解释可以参看:https://www.cnblogs.com/liaojie970/p/9007577.html 如果只是想要依赖那么可以将springboot自带的打包插件换掉就可以了,换为如下 ...
- Day10 - 灾难 HYSBZ - 2815
Description 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过生 ...