POJ 3190 Stall Reservations (优先队列)C++
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 7646 | Accepted: 2710 | Special Judge | ||
Description
Help FJ by determining:
- The minimum number of stalls required in the barn so that each cow can have her private milking period
- An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.
Input
Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output
Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
Sample Input
5
1 10
2 4
3 6
5 8
4 7
Sample Output
4
1
2
3
2
4
Hint
Here's a graphical schedule for this output:
Time 1 2 3 4 5 6 7 8 9 10
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int l,r,n;
bool operator <(const node &b)const
{
return r>b.r||(r==b.r&&l>b.l);
}
}a[50005];
priority_queue <node> q;
int u[50005];
bool cmp(node a,node b)
{ return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
int ans=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].n=i;
}
sort(a+1,a+n+1,cmp);
q.push(a[1]);
u[a[1].n]=1; for(int i=2;i<=n;i++)
{
if(!q.empty()&&q.top().r<a[i].l){
u[a[i].n]=u[q.top().n];
q.pop();
}
else{
ans++;
u[a[i].n]=ans;
}
q.push(a[i]);
}
printf("%d\n",ans);
for(int i =1;i<=n;i++){
printf("%d\n",u[i]);
}
while(!q.empty()) // 尤其重要,排空队列
q.pop();
}
}
POJ 3190 Stall Reservations (优先队列)C++的更多相关文章
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ -3190 Stall Reservations (贪心+优先队列)
http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- POJ 3190 Stall Reservations 【贪心 优先队列】
题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...
- Stall Reservations POJ - 3190 (贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11002 Accepted: 38 ...
- POJ:3190-Stall Reservations
传送门:http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- 会话控制cookie和session
Cookie Cookie简介 HTTP是无状态协议,服务器不能记录浏览器的访问状态,也就是说服务器不能区分中两次请求是否由一个客户端发出.这样的设计严重阻碍的Web程序的设计.如:在我们进行网购时, ...
- Tomcat修改端口
当在服务器上运行报错说端口已占用的时候,需要修改Tomcat的端口,步骤如下: 一.找到Tomcat的安装目录 二.进入conf目录,找到server.xml文件,记事本打开 此时文件中有三个地方可以 ...
- NodeJS入门简介
NodeJS入门简介 二.模块 在Node.js中,以模块为单位划分所有功能,并且提供了一个完整的模块加载机制,这时的我们可以将应用程序划分为各个不同的部分. const http = require ...
- 带你走进SAP项目实施过程——前言(0)
欢迎关注博主的微信公众号,每天提供原创的SAP技术和项目管理新资讯! 一直很想写一些关于SAP项目管理以及实施过程的系列文章,讲述企业SAP项目从立项开始到启动,再到实施过程,直到最后的上线及总结.我 ...
- Springboot+resteasy定时任务
定时任务 需求:按每日统计点赞数.评论数.热度的增加量(不是现有量) 1.每天零点执行:首先遍历出user的统计字段 然后插入到新创建的表中. 2.每天一点执行:根据时间段将两表的数据相减创建增量字段 ...
- Web初学-Web应用细节
一.web应用程序简介 WEB应用程序指供浏览器访问的程序,通常也简称为web应用. 一个web应用由多个静态web资源和动态web资源组成,如: html.css.js文件 Jsp文件.java程序 ...
- 如何利用Jmeter做代理录制脚本
如果对于这个Jmeter不太会使用的童鞋,可以去参考其他的教学文档,本文只提供Jmeter的代理使用. 第一步:添加线程组
- python进阶学习(三)
本节通过SQLite了解数据库操作 ------------------------- 数据库支持 使用简单的纯文本只能实现有退限的功能,所需要引入数据库,完成更强大的功能,本节使用的简单数据库SQL ...
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻 ...
- Ibatis insert语句插入null引发的错误
公司使用的orm框架为ibatis,其中默认的insert语句一直都是这样写的: <insert id="insert" parameterClass="activ ...