[USACO06FEB]摊位预订Stall Reservations

题目描述

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 periodAn assignment of cows to these stalls over timeMany answers are correct for each test dataset; a program will grade your answer.

约翰的N(l<N< 50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当 然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段B.显然,约翰必须开发一个调控系统来决定每头奶牛应该被安排到哪个牛棚去挤 奶,因为奶牛们显然不希望在挤奶时被其它奶牛看见.

约翰希望你帮他计算一下:如果要满足奶牛们的要求,并且每天每头奶牛都要被挤过奶,至少需要多少牛棚 •每头牛应该在哪个牛棚被挤奶。如果有多种答案,你只需任意一种即可。

输入输出格式

输入格式:

Line 1: A single integer, N

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

输出格式:

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.

输入输出样例

输入样例#1:

5

1 10

2 4

3 6

5 8

4 7

输出样例#1:

4

1

2

3

2

4

说明

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.

思路比较简单的贪心题,小技巧比较多。

按照开始吃草的时间排序,用一个小根堆维护每个序栏最后一头牛吃草的时间,把最早吃完的牛放在堆顶。本人太蒻不会priority_queue的骚操作,所以只能用堆了......

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
int read()
{
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*w;
}
int n,cnt;
int a[50010];
struct node{
int l,r,id,s;
}f[50010];
bool cmp1(node p,node q)
{
if(p.l==q.l) return p.r<q.r;
return p.l<q.l;
}
bool cmp2(node p,node q)
{
return p.id<q.id;
}
void push(int k)
{
int now,next;
a[++cnt]=k;
now=cnt;
while(now>1)
{
next=(now>>1);
if(f[a[now]].r>=f[a[next]].r) break;
swap(a[now],a[next]);
now=next;
}
}
void delet()
{
int now,next;
a[1]=a[cnt--];
now=1;
while(now*2<=cnt)
{
next=now*2;
if(next+1<=cnt&&f[a[next+1]].r<f[a[next]].r) next++;
if(f[a[now]].r<=f[a[next]].r) break;
swap(a[now],a[next]);
now=next;
}
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
f[i].l=read();f[i].r=read();
f[i].id=i;
}
sort(f+1,f+1+n,cmp1);
f[1].s=1;
int num=1;
push(1);
for(int i=2;i<=n;i++)
{
int d=f[a[1]].r;
if(f[i].l>d)
{
f[i].s=f[a[1]].s;
delet();
push(i);
}
else if(f[i].l<=d)
{
num++;
f[i].s=num;
push(i);
}
}
sort(f+1,f+1+n,cmp2);
cout<<num<<endl;
for(int i=1;i<=n;i++)
{
cout<<f[i].s<<endl;
}
}

[USACO06FEB]摊位预订Stall Reservations(贪心)的更多相关文章

  1. 洛谷P2859 [USACO06FEB]摊位预订Stall Reservations

    P2859 [USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They a ...

  2. bzoj1651 / P2859 [USACO06FEB]摊位预订Stall Reservations

    P2859 [USACO06FEB]摊位预订Stall Reservations 维护一个按右端点从小到大的优先队列 蓝后把数据按左端点从小到大排序,顺序枚举. 每次把比右端点比枚举线段左端点小的数据 ...

  3. 题解 P2859 【[USACO06FEB]摊位预订Stall Reservations】

    题目链接: https://www.luogu.org/problemnew/show/P2859 思路: 首先大家会想到这是典型的贪心,类似区间覆盖问题的思路,我们要将每段时间的左端点从小到大排序, ...

  4. [USACO06FEB] Stall Reservations 贪心

    [USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...

  5. POJ 3190 Stall Reservations贪心

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

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

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

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

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

  8. [POJ3197]Stall Reservations (贪心)

    题意 (来自洛谷) 约翰的N(l<N< 50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当 然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段 ...

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

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

随机推荐

  1. [luogu] P3809 【模板】后缀排序 (SA)

    板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...

  2. nginx配置虚拟主机-端口号区分/域名区分

    Nginx实现虚拟机 可以实现在同一台服务运行多个网站,而且网站之间互相不干扰.同一个服务器可能有一个ip,网站需要使用80端口.网站的域名不同. 区分不同的网站有三种方式:ip区分.端口区分.域名区 ...

  3. (3.1)狄泰软件学院C++课程学习剖析二

    深度剖析C++第二部分 1.通过对象名能够访问public成员变量.每个对象的成员变量都是专属的,成员变量不能够在对象之间共享. 2.需求:统计在程序运行期间某个类的对象数目,保证程序的安全性(不能使 ...

  4. git错误处理

    1.今天 当我  执行  git add  somefile 的时候,出现 如下 错误: If no other git process is currently running, this prob ...

  5. php面试专题---16、MySQL创建高性能索引考点

    php面试专题---16.MySQL创建高性能索引考点 一.总结 一句话总结: 注意:只写精品 1.索引的基础? 类似书籍的目录:索引类似于书籍的目录,要想找到一本书的某个特定主题,需要先查找书的目录 ...

  6. loj6259「CodePlus 2017 12 月赛」白金元首与独舞

    分析 我们将没连的点连向周围四个点 其余的按照给定的方向连 我们将所有连出去的位置统一连到0点上 再以0作为树根 于是就将问题转化为了有向图内向树计数 代码 #include<iostream& ...

  7. 130、TensorFlow操作多个计算图

    # Programming with multiple graphs # 当训练一个模型的时候一个常用的方式就是使用一个图来训练你的模型 # 另一个图来评价和计算训练的效果 # 在许多情况下前向计算和 ...

  8. 测开之路八十五:python处理csv文件

    写入csv文件 一:写入字典 二:写入普通数据 读取: 第一种:普通读取 第二种:读取csv并用namedtuple映射列名,类似于使用类的实例 第三种:字典形式 import csvfrom col ...

  9. 测开之路七十三:用kafka实现消息队列之环境搭建

    一:装java环境,确保java能正确调用 kafka下载地址:http://kafka.apache.org/downloads 下载并解压kafka: 新建两个文件夹,用于存放zookeeper和 ...

  10. MQ基础知识学习

    之前听人提起了MQ协议,我就去稍微了解了一下什么是MQ,和MQ的一些基础性的知识. 什么是MQ呢? 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据 ...