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 ...
随机推荐
- jsp中一些重要的问题
jsp [1] 简介 > HTML - HTML擅长显示一个静态的网页,但是不能调用Java程序. > Servlet - Servlet擅长调用Java程序和后台进行交互,但是它不擅长显 ...
- Linux环境变量配置的三个方法--/etc/profile,~/.bashrc,shell
[环境配置的原因] 在windows系统下,很多软件的安装都需要设置环境变量,比如安装JAVA JDK.如果不安装环境变量,在非软件安装的目录下运行javac命令,将会报告"找不到文件&qu ...
- 【Ubuntu16]】ufw
Usage: ufw COMMAND Commands: enable enables the firewall 开启ufw防火墙 disable disables the firewall 禁用防火 ...
- Java 开发环境配置(Windows篇)
window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloa ...
- Java web轻量级开发面试教程读书笔记:数据库方面,如何准备面试
如果在面试或与资深人事交流的过程中,你能有效合理地展示出本章所给出的一些知识点,那么对你的评价就会是"对数据库有深入了解",甚至能加上"有设计数据表的经验",即 ...
- DOM 对象方法
DOM 对象方法 这里提供一些您将在本教程中学到的常用方法: 方法 描述 getElementById() 返回带有指定 ID 的元素. getElementsByTagName() 返回包含带有指定 ...
- 说说我和QQ和她的故事
这篇文章纯属感慨而写, 没有教学性质,额我是Jyang 2780217151是我现在用的QQ,然而这并不是我要说的 大概在11年还是12年的时候,我玩这些所谓的黑客圈子,以前的网名我就不说了. 我记得 ...
- ios 初体验< UISegmentedControl 分段控件>
小知识: 数组快速创建 @[@"",@"",@"",@"".......],字典快速创建方法:@{@"&q ...
- [js高手之路]Vue2.0基于vue-cli+webpack同级组件之间的通信教程
我们接着上文继续,本文我们讲解兄弟组件的通信,项目结构还是跟上文一样. 在src/assets目录下建立文件EventHandler.js,该文件的作用在于给同级组件之间传递事件 EventHandl ...
- 三,ESP8266 SPI
重点是说SPI通信协议,,,, 不要害怕协议因为协议是人规定的,,刚好我也是人......规定的协议既然能成为规范让所有人所接受,那么必然有它的优势和优点,必然值得学习,, 害怕协议的人是因为当初碰到 ...