bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 — 线段树

 

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

Time Limit: 10 Sec  Memory Limit: 64 MB

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

瞎搞了个线段树,据说神犇用的时间戳

#include<cstdio>
#define M 1000010
inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
inline int max(int a,int b){return a>b?a:b;}
struct ljn{int l,r,maxx,lazy;}tr[4*M];
int n,x,y;
void make(int l,int r,int p)
{
    tr[p].l=l,tr[p].r=r,tr[p].lazy=0;tr[p].maxx=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    make(l,mid,p<<1);
    make(mid+1,r,p<<1|1);
}
void pd(int p)
{
    int lz=tr[p].lazy;
    tr[p<<1].lazy+=lz;
    tr[p<<1|1].lazy+=lz;
    tr[p<<1].maxx+=lz;
    tr[p<<1|1].maxx+=lz;
    tr[p].lazy=0;
}
void xg(int l,int r,int c,int p)
{
    if(tr[p].l==l&&tr[p].r==r)
    {
        tr[p].lazy+=c;
        tr[p].maxx+=c;
        return;
    }
    pd(p);
    int mid=(tr[p].l+tr[p].r)>>1;
    if(mid>=r) xg(l,r,c,p<<1);
    else if(mid<l) xg(l,r,c,p<<1|1);
    else 
    {
        xg(l,mid,c,p<<1);
        xg(mid+1,r,c,p<<1|1);
    }
    tr[p].maxx=max(tr[p<<1].maxx,tr[p<<1|1].maxx);
}
int main()
{
    n=read();
    make(1,1000000,1);
    for(int i=0;i<n;i++)
    {
        x=read();y=read();
        xg(x,y,1,1);
    }
    printf("%d\n",tr[1].maxx);
}

另附第二种做法

#include<cstdio>
#define M 1000000
inline int max(int x,int y){return x>y?x:y;}
inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int n,a,b,t[M+10],ans,wei;
int main()
{
    n=read();
    while(n--)
    {
        a=read();b=read();
        t[a]++;
        t[b+1]--;
        wei=max(b,wei);
    }
    for(int i=1;i<=wei;i++)
    {
        t[i]=t[i-1]+t[i];
        ans=max(ans,t[i]);
    }
    printf("%d\n",ans);
}

 

评论

  • Gipsy_DAnger 回复

    为什么线段树呢,静态的

发表评论

衫小寨 出品