poj3190区间类贪心+优先队列
题意:每个奶牛产奶的时间为A到B,每个奶牛产奶时要占用一间房子,问n头奶牛产奶共需要多少房子,并输出每头奶牛用哪间房子
分析:这题就是一个裸的贪心,将奶牛按开始时间进行排序即可,但考虑一下数据范围,我们可以用一个优先队列来进行维护,在优先队列中我们按照奶牛的结束时间最小构造小顶堆,然后判断新进来的元素的开始时间是否比最小的结束时间大,若是,加入队列,修改队列,若不是,加入对列,并且计数加1
注意自定义类型的优先队列的维护方法。详见:http://blog.sina.com.cn/s/blog_4e5157120100vn7b.html
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=;
//注意自定义类型在优先队列中的用法
typedef struct P
{
int start,over,id;
friend bool operator<(P a,P b)
{
return a.over>b.over; //小顶堆
}
}P;
P point[maxn];
bool cmp(P a,P b)
{
return a.start<b.start;
}
int t[maxn];
int main()
{
int n;
while(cin>>n)
{
for(int i=;i<n;i++)
{
scanf("%d%d",&point[i].start,&point[i].over);
point[i].id=i;
}
memset(t,,sizeof(t));
sort(point,point+n,cmp);
priority_queue<P> que;
int r=; //统计个数
que.push(point[]);
t[point[].id]=++r;
for(int i=;i<n;i++)
{
P node=que.top();
if(node.over<point[i].start){
t[point[i].id]=t[node.id];
que.pop();
que.push(point[i]);
}
else{
t[point[i].id]=++r;
que.push(point[i]);
}
}
cout<<r<<endl;
for(int i=;i<n;i++)
printf("%d\n",t[i]);
}
return ;
}
poj3190区间类贪心+优先队列的更多相关文章
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- CodeForces 137C【贪心+优先队列】
这种区间的贪心好像都出"烂"了? 不过还是想写一下... 先按照区间左端点排序一下,然后搞个优先队列维护当前最小的右端点. #include <bits/stdc++.h&g ...
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 贪心+优先队列 HDOJ 5360 Hiking
题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
随机推荐
- ignite中的sql查询
ignite中进行sql查询需要对要查询的cache和字段进行配置,可以在xml中配置,也可以在代码中配置或进行注解,我用的是xml配置: <!-- 配置cache --> <pro ...
- Spotlights
Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- mysql 修改 添加 删除 表字段
添加表的字段 alter table 表名 add 字段名 字段的类型 例子: alter table table1 add transactor varchar(10) n ...
- libmysql.dll 找不到
在用C#开发的时候,需要连接MySQL ,系统提示 libmysql.dll 找不到模块. 我们可以找到 MySQL安装文件夹下的 C:\Program Files\MySQL\MySQL Ser ...
- sonar tomacat配置
最近在学习Sonar,配置了好几天,才搭建起来环境,为自己的学习能力感到汗颜,赶紧在此记录一下,所谓好记性不如烂笔头. 1.Sonar介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java ...
- lucene 中关于Store.YES 关于Store.NO的解释
总算搞明白 lucene 中关于Store.YES 关于Store.NO的解释了 一直对Lucene Store.YES不太理解,网上多数的说法是存储字段,NO为不存储. 这样的解释有点郁闷:字面意 ...
- java 创建一个File文件对象
Example10_1.java import java.io.*; public class Example10_1 { public static void main(String args[]) ...
- html5 之本地数据存储
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 cookie与webSt ...
- Issue 5158: Modal dialog present (UnexpectedAlertOpen) issue in IE (Similar issue like 3360)
https://code.google.com/p/selenium/issues/detail?id=5158 Reported by mailtopa...@gmail.com, Feb 13 ...
- PAT (Advanced Level) 1094. The Largest Generation (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...