Stall Reservations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5469   Accepted: 1973   Special Judge

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

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

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have.

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

Explanation of the sample:

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.

 
题意:有N头牛,每头牛都有不同的进食时间段,求至少多少个牛棚,使得每个牛棚每个时间段最多只有一头牛在吃东西。
思路:
若每个牛棚的总区间为[A,B],每头牛的进食阶段分别为[begin_i,end_i](i=1,2,3....N),则先对每头牛的开始时间begin_i从小到大排序,先将第一头牛压入第一个stall中,之后的牛循环压入stall中,每次尽量将begin_i最小的牛先压入stall中,当前stall若装不下(由于后面的stall的end值越来越大,更装不下),则需要增加新的一个stall,所有的stall都存储在最小堆中(priority_queue),在堆中尽量让结束时间早的stall放在最上面。最后输出最小堆中stall的数量即可。
AC代码如下:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N_MAX = ;
struct section{
unsigned int begin;
unsigned int end;
unsigned int symbol;
bool operator <(const section &b)const {
return begin < b.begin;
}
}; struct stall {
unsigned int begin;
unsigned int end;
unsigned int symbol;
bool operator <(const stall&b)const{//return回来的是常量,习惯性的加const
return end > b.end;
}
};
section cow[N_MAX];
unsigned int result[N_MAX];//记录每一头牛在哪个stall
priority_queue<stall>que;//最小优先队列,stall中end小的放在堆的上面 inline void put_cow(const int &i,const bool &new_stall) {
stall s;
if (new_stall) {//需要存放在新的stall的情况
s.symbol = que.size() + ;//给新的stall贴上标号
}
else {
s.symbol = que.top().symbol;
que.pop();
}
s.end = cow[i].end;//压入牛后更新stall中end的值
result[cow[i].symbol] = s.symbol;//记录每一头牛放在什么stall中
que.push(s);
} int main() {
int N;
while (cin >> N) {
for (int i = ;i < N;i++) {
scanf("%d%d", &cow[i].begin, &cow[i].end);
cow[i].symbol = i;
}
sort(cow,cow+N);//开始时间小的牛排前面
put_cow(, true);
for (int i = ;i < N;i++) {
put_cow(i, cow[i].begin <= que.top().end);//当前牛的开始值是否小于end值最小的stall的end值
}
cout << que.size()<<endl;
for (int i = ;i < N;i++)
cout << result[i] << endl; }
return ;
}
 

poj3190 stall revertation的更多相关文章

  1. POJ3190 Stall Reservations 【贪婪】

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 111 ...

  2. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

  3. poj3190 Stall Reservations(贪心+STL)

    https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...

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

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

  5. POJ-3190 Stall Reservations---优先队列+贪心

    题目链接: https://vjudge.net/problem/POJ-3190 题目大意: 有N头奶牛,每头奶牛都会在[1,1000000]的时间区间内的子区间进行挤奶.挤奶的时候奶牛一定要单独放 ...

  6. poj3190 Stall Reservations

    我一开始想用线段树,但是发现还要记录每头cow所在的棚...... 无奈之下选择正解:贪心. 用priority_queue来维护所有牛棚中结束时间最早的那个牛棚,即可得出答案. 注意代码实现的细节. ...

  7. POJ3190 Stall Reservations 贪心

    这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...

  8. 《挑战程序设计竞赛》2.2 贪心法-区间 POJ2376 POJ1328 POJ3190

    POJ2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14585   Accepte ...

  9. 【POJ - 3190 】Stall Reservations(贪心+优先队列)

    Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...

随机推荐

  1. android视频播放心得体会

    android视频播放主要是两种方式1.系统封装好的videoplayer,有前进.后退.暂停/播放.拉动最基本的功能,够一般使用,操作办法也很简单,如果需要自定义程度高就需要用到第二种方法:Surf ...

  2. IOS 在Ipad 横屏 上使用UIImagePickerController

    转载前请注明来源:http://www.cnblogs.com/niit-soft-518/p/4381328.html 最近在写一个ipad的项目,该项目必须是横屏.进入正题,有一项功能是要调用系统 ...

  3. 【Android 应用开发】Activity 状态保存 OnSaveInstanceState參数解析

    作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38297083 一. 相关方法简单介绍 1. 状态保存方法演示 ...

  4. HubbleDotNet 学习之路

    1.创建后台任务实现自动同步更新表数据.打开工具后点击“management”选项卡,选择“task scheduler management”,在弹出的窗口中点击右侧的"add" ...

  5. LVS DR模型

    1,环境 VMWare10, CentOS6.3 2,LVS DR网络规划 所有机器都只需要一张网卡,给Director的eth0网卡起个别名eth0:1即VIP的值:给RealServer的lo网卡 ...

  6. Git链接到自己的Github(2)进阶使用

    接着上一篇的,从github clone下代码. 1.先查看当前开发分支 $ cat .git/HEAD ref: refs/heads/master 这里的master是默认分支. 2.查看当前状态 ...

  7. Apache Rewrite 服务器变量

    Apache提供给rewirte模块的环境变量大概分成5个类型. 第一部分: HTTP headers 部分参数 参数名称: HTTP_USER_AGENT 样例参考值: Mozilla/5.0 (W ...

  8. 路径(keyPath)、键值编码(KVC)和键值观察(KVO)

    键路径 在一个给定的实体中,同一个属性的所有值具有相同的数据类型. 键-值编码技术用于进行这样的查找—它是一种间接访问对象属性的机制. - 键路径是一个由用点作分隔符的键组成的字符串,用于指定一个连接 ...

  9. app卡顿问题检测--KMCGeigerCounter

    介绍: KMCGeigerCounter是一个iOS帧速计算器,像盖革计数器那样,当动画丢失一帧时它就记录一次.掉帧通常是不可见的,但是很难区分55fps和60fps之间的不同,而KMCGeigerC ...

  10. ubuntu nexus 安装

    今天公司组织学习使用linux系统搭建nexus maven私服中央仓库,在公司使用centos搭建了一个,回家又用ubuntu搭建一个,主要是为了能熟悉整个流程,现将主要过程总结如下:(PS:楼主是 ...