传送门:http://poj.org/problem?id=3190

Stall Reservations

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 9173 Accepted: 3208 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.


解题心得:

  1. 题意就是每个牛在一段时间内要产奶,每个奶牛在产奶的时候必须给它安排一间屋子,问最少需要多少间屋子,以及给每个奶牛安排产奶的屋子是第几间。
  2. 也就是一个思维题 ,先把奶牛用起始时间拍个序,用一个优先队列1来放所有的屋子编号,再用一个优先队列2来记录每个奶牛产奶结束的时间,遇到一个奶牛开始产奶,就看优先队列2中在当前时间是否有奶牛已经结束产奶了,如果已经结束产奶,就将安排给那个产奶的奶牛屋子还回队列1当中,然后从队列1中找一个编号最小的屋子给当前的奶牛。优先队列1中出现的房间号数字最大的就是最少需要安排的房间数。

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 5e4+100;
struct COW {
int s,e,num;
bool operator <(const COW & a) const {
return a.e < e;
}
}cow[maxn];
int n; bool cmp(COW a,COW b) {
if(a.s == b.s)
return a.e < b.e;
return a.s < b.s;
} void init() {
for(int i=1;i<=n;i++) {
scanf("%d%d",&cow[i].s,&cow[i].e);
cow[i].num = i;
}
sort(cow+1,cow+n+1,cmp);
} int ans[maxn];
void get_ans() {
int Max_num = -1;
priority_queue <int,vector<int>,greater <int> > qu;
priority_queue <COW> qu2;
for(int i=1;i<=maxn;i++)
qu.push(i);
for(int i=1;i<=n;i++) {
COW now = cow[i];
while(!qu2.empty() && qu2.top().e < now.s) {
qu.push(ans[qu2.top().num]);
qu2.pop();
}
ans[now.num] = qu.top();
if(qu.top() > Max_num)
Max_num = qu.top();
qu.pop();
qu2.push(cow[i]);
}
printf("%d\n",Max_num);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
} int main() {
scanf("%d",&n);
init();
get_ans();
return 0;
}

POJ:3190-Stall Reservations的更多相关文章

  1. poj 3190 Stall Reservations

    http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  2. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  3. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  4. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  5. POJ 3190 Stall Reservations【贪心】

    POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...

  6. POJ -3190 Stall Reservations (贪心+优先队列)

    http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...

  7. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  8. POJ 3190 Stall Reservations 【贪心 优先队列】

    题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...

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

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

随机推荐

  1. switch 和 if...else if 的区别

     为什么很多人用  if...else..if   而不使用   switch 1,if...else...if 只是单纯地一个接一个比较:if...else可能每个条件都计算一遍: 2,switch ...

  2. (C#) 线程之 AutoResetEvent, EventHandle.

    AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...

  3. django orm 时间字段讲解

    创建django的model时,有DateTimeField.DateField和TimeField三种类型可以用来创建日期字段,其值分别对应着datetime().date().time()三中对象 ...

  4. ansible测试环境

    ip user sudo_user port usage 192.168.48.81 ansible root 29922 nagios & ansible control 192.168.4 ...

  5. DOM对象和js对象以及jQuery对象的区别

    DOM对象和js对象以及jQuery对象的区别 DOM对象和js对象以及jQuery对象的区别 一.DOM对象 文档对象模型简称DOM,是W3C组织推荐的处理可扩展置标语言的标准编程接口. DOM实际 ...

  6. python3线程介绍01(如何启动和调用线程)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...

  7. TP5.1:facade静态代理

    THINKPHP中有很多很多的facade静态代理,这些静态代理的位置在:thinkphp\think\facade文件夹下 1.在app\commom中新建一个文件,名为:Test.php,表示被代 ...

  8. chromedp自动启动为headless模式

    // Command click is a chromedp example demonstrating how to use a selector to // click on an element ...

  9. Illegal access:this web application instance has been stopped already

    七月 23, 2014 2:34:35 下午 org.apache.catalina.loader.WebappClassLoader loadClass信息: Illegal access: thi ...

  10. ASP .NET CORE 读取配置文件的方法

    老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...