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 ...
随机推荐
- php数组和正则表达式的替换拆分匹配所有
正则表达式 $s = "a1s2d3f1g5f";//echo preg_replace("/\d/","#",$s); //替换 //$ ...
- JMeter脚本录制
1.1. 使用第三方录制方式或代理录制方式(建议) 第三方采用:http://www.badboy.com.au/ 通过badboy来录制,录制后另存为jmx格式即可. 操作步骤: a.打开badb ...
- 集合-TreeSet-Comparable
Student类:name.age属性 1 package com.bjpowernode.test01_set2_Comparable; /* * T: type */ public class S ...
- 轻量级ORM框架 QX_Frame.Bantina(一、框架简介)
轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...
- 接口测试之soapUI(WebService)
一.WebService介绍 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术. 1)XML+X ...
- 自制简易Linux系统
l 要求: 裁剪一个装载有网卡驱动可以上网并且使用到init的一个小系统 l 准备: 虚拟机,centos6.4 l 步骤: 一.在宿主机上添加一块硬盘,并为其安装grub 1. 在宿主机上添加一块硬 ...
- 【Hadoop】执行start-dfs.sh出错
问题1:hadoop2.7.3部署警告: Unable to load native-hadoop library for your platform 解决办法: 1.编辑hadoop-env.sh ...
- Servlet工作原理分析
最近在看<Java Web技术内幕>的Servlet工作原理,有点深奥和难以理解,于是乎,想通过一个简单的Demo将书上的思路理一遍,对Servlet有个更透彻更深的了解. Servlet ...
- idea 远程调试
Idea 远程在线测试 描述:在window下开发,部署到Linux服务器上,往往会遇到在windows下正常运行,在Linux服务器下异常,这是需要本地调试远程代码: 操作步骤: 一.代码已知 保证 ...
- java多线程sleep和wait方法的区别
分别创建了三个类,一个测试类,两个线程类实现Runnable接口. 当有notify()唤醒线程时,执行的结果如下: 当把TestSleepaWait.class.notify();语句注释后,即没有 ...