BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

线段树..

--------------------------------------------------------------------------------------

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#define rep( i , n ) for( int i = 0 ; i < n ; i++ )

#define clr( x , c ) memset( x , c , sizeof( x ) )

#define L( x ) ( x << 1 )

#define R( x ) ( L( x ) ^ 1 )

#define LC( x ) tree[ L( x ) ]

#define RC( x ) tree[ R( x ) ]

#define mid( l , r ) ( ( l + r ) >> 1 )

using namespace std;

const int n = 1000000;

struct Node {

int l , r;

int Max , add;

Node() : Max( 0 ) , add( 0 ) { }

};

Node tree[ n << 2 ];

void maintain( int x ) {

Node &o = tree[ x ];

o.Max = 0;

if( o.r > o.l )

o.Max = max( LC( x ).Max , RC( x ).Max );

o.Max += o.add;

}

int L , R;

void update( int x ) {

Node &o = tree[ x ];

if( L <= o.l && o.r <= R )

o.add += 1;

else {

int m = mid( o.l , o.r );

if( L <= m ) update( L( x ) );

if( m < R ) update( R( x ) );

}

maintain( x );

}

void build( int x , int l , int r ) {

Node &o = tree[ x ];

o.l = l , o.r = r;

if( l == r )

return ;

int m = mid( l , r );

build( L( x ) , l , m );

build( R( x ) , m + 1 , r );

}

int main() {

// freopen( "test.in" , "r" , stdin );

int m;

cin >> m;

build( 1 , 1 , n );

while( m-- ) {

scanf( "%d%d" , &L , &R );

update( 1 );

}

printf( "%d\n" , tree[ 1 ].Max );

return 0;

}

--------------------------------------------------------------------------------------

1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 587  Solved: 327
[Submit][Status][Discuss]

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

有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求

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

OUTPUT DETAILS:

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.

HINT

不妨试下这个数据,对于按结束点SORT,再GREEDY的做法 1 3 5 7 6 9 10 11 8 12 4 13 正确的输出应该是3

Source

Silver

时间: 2024-08-08 01:24:08

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )的相关文章

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553  Solved: 307[Submit][Status] Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some preci

【差分】BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚

显而易见的是我们要求的时间段重叠最多的次数. 用线段树也可以,不过既然是学习和练习前缀和与差分那么就用差分的思想. 在num[a]的位置+1 在num[y+1]位置-1 表示[a,b]区间有一个时间段. 最后统计一下num[i]最多的次数(i是1~n) /************************************************************** Problem: 1651 User: LYFer Language: C++ Result: Accepted Ti

bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每次查看队首的first是否比当前牛的起始时间早,是则弹出队首记录当前牛的答案,再把新的pair放进去,否则增加牛棚,同样要塞进队里 #include<iostream> #include<cstdio> #include<algorithm> #include<que

bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚*

bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚 题意: 有N头牛,每头牛有个喝水时间段,这段时间它将专用一个棚.现在给出每头牛的喝水时间段,问至少要多少个棚才能满足它们的要求.n≤50000,时刻≤1000000. 题解: 时间段左端点对应的sum元素++,右端点+1对应的sum元素--,最后从左到右加一遍就可以得到每个时刻的喝水牛数,找个最大值就可以了. 代码: 1 #include <cstdio> 2 #include <cstring

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边界 : dp( i , i ) = V[ i ] * n -------------------------------------------------------------------------------------------- #include<cstdio> #include&l

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows

题目 1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 234  Solved: 185[Submit][Status] Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ se

[BZOJ 1018] [SHOI2008] 堵塞的交通traffic 【线段树维护联通性】

题目链接:BZOJ - 1018 题目分析 这道题就说明了刷题少,比赛就容易跪..SDOI Round1 Day2 T3 就是与这道题类似的..然而我并没有做过这道题.. 这道题是线段树维护联通性的经典模型. 我们线段树的一个节点表示一个区间的联通性,有 6 个 bool 值,表示这个区间的 4 个角上的点之间的联通性. 然后用两个子区间的联通性和两个子区间之间的连边情况合并出整个区间的联通性. 修改某条边时,先在边的数组中修改,然后从这条边所在的点的线段树叶子开始向上 Update . 询问两

bzoj 2733 永无乡 - 并查集 - 线段树

永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛.如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的.现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥.Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输

bzoj 1594: [Usaco2008 Jan]猜数游戏——二分+线段树

Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在牛棚后面摆N(1 <= N<= 1,000,000)堆干草,每堆有若干捆,并且没有哪两堆中的草一样多.所有草堆排成一条直线,从左到右依次按1..N编号,每堆中草的捆数在1..1,000,000,000之间. 然后,游戏开始.另一头参与游戏的奶牛会问那头摆干草的奶牛 Q(1 <= Q <= 25,000)个问题,问题的格式如下: 编号为Ql..Q