题意:每个奶牛产奶的时间为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区间类贪心+优先队列的更多相关文章

  1. poj3190 Stall Reservations (贪心+优先队列)

    Cleaning Shifts Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  2. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  3. CodeForces 137C【贪心+优先队列】

    这种区间的贪心好像都出"烂"了? 不过还是想写一下... 先按照区间左端点排序一下,然后搞个优先队列维护当前最小的右端点. #include <bits/stdc++.h&g ...

  4. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

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

  6. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  7. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  8. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

  9. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

随机推荐

  1. 查找List中的最大最小值

    以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值: import java.util.*; public class Main { pu ...

  2. Hibernate Session & Transaction详解

    Hibernate Session & Transaction详解 HIbernate中的Session Session是JAVA应用程序和Hibernate进行交互时使用的主要接口,它也是持 ...

  3. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  4. PAT1006

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  5. jq的事件对象的属性

    1.event.type() 该方法的作用是可以获取到时间的类型 $('a').click(function(){ alert(event.type);//获取事件类型 return  false;/ ...

  6. JavaScript数组函数unshift、shift、pop、push使用实例

    如何声明数组 s中数组的声明可以有几种方式声明 复制代码代码如下: var tmp = [];  // 简写模式var tmp = new Array(); // 直接new一个var tmp = A ...

  7. Bcdedit命令使用详解使用方法

    XP,WIN2003,VISTA,万indows,WIN2008多盘多系统多引导bcdedit的使用windows出了新系统vista,2008想赏赏鲜学习学习~~但又习惯于用旧的XP,2003,然而 ...

  8. 解读QML之二

    QML文档 QML文档是用QML语法组成的字符串.一个文档定义了一个QML对象类型.文档以”.qml”最为后缀,可以保存在本地和网络上,可以使用代码生成.一 个在文档中定义的对象类型的实例,也可以使用 ...

  9. 我的android学习脚步----------- Button 和监听器setonclicklistener

    最基本的学习,设置一个按钮并监听实现实时时刻显示 首先XML布局,在layout中的  activity_main.xml中拖一个Button按钮到相应位置 然后在xml文件中做修改 <Rela ...

  10. make[1]: *** [/workopenwrt/trunk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/stamp/.tools_install_nnnnn] Error 2 make[1]: Leaving directory `/work/openwrt/trunk' make: *** [world]

    主要原因是编译时未连上网,编译时需要下载些插件,连接网后,重启下系统再编译下.