BZOJ1651Stall Reservations 專用牛棚

1651: [Usaco2006 Feb]Stall Reservations 專用牛棚
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 698 Solved: 391
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
線段樹裸題。。
可以發現一個神奇的規律。。
覆蓋次數最多的點就是答案。。
於是區間修改+區間查詢最大值水過。。
還有就是查詢區間是1-1000000
附上本蒟蒻的代碼:

#include<cstdio>
#include<climits>
#include<iostream>
using namespace std;
int n,a[50001],delta[4000001];
struct kx
{
    int value;
}node[4000001];

int read()
{
    int w=0,c=1; char ch=getchar();
    while (ch<'0' || ch>'9')
      {
        if (ch=='-') c=-1;
        ch=getchar();
      }
    while (ch>='0' && ch<='9')
      w=w*10+ch-'0',ch=getchar();
    return w*c;
}

void update(int s)
{
    node[s].value=max(node[s*2].value,node[s*2+1].value);
}

void paint(int s,int z,int l,int r)
{
    node[s].value+=z,delta[s]+=z;
}

void pushdown(int s,int l,int r)
{
    int mid=(l+r)/2;
    paint(s*2,delta[s],l,mid),paint(s*2+1,delta[s],mid+1,r),delta[s]=0;
}

void insert(int s,int l,int r,int x,int y,int z)
{
    int mid=(l+r)/2;
    if (x<=l && y>=r)
      {
        paint(s,z,l,r);
        return;
      }
    pushdown(s,l,r);
    if (x<=mid) insert(s*2,l,mid,x,y,z);
    if (y>mid) insert(s*2+1,mid+1,r,x,y,z);
    update(s);
}

int query(int s,int l,int r,int x,int y)
{
    int mid=(l+r)/2,ans;
    if (x<=l && y>=r) return node[s].value;
    pushdown(s,l,r);
    if (x<=mid) ans=query(s*2,l,mid,x,y);
    else ans=-INT_MAX;
    if (y>mid) ans=max(ans,query(s*2+1,mid+1,r,x,y));
    update(s);
    return ans;
}

int main()
{
    int i,x,y;
    n=read();
    for (i=1;i<=n;i++)
      {
        x=read(),y=read();
        insert(1,1,1000000,x,y,1);
      }
    printf("%d\n",query(1,1,1000000,1,1000000));
    return 0;
}

AC100紀念。。
都流傳着AC100表白的人生成就。。
然而我早就體驗過三次。。
什麼??你說表白多了有經驗??
我覺得我還不如安安靜靜地當個0級小怪物。。
離省選、APIO、CTSC不遠了。。
然而還是沒有什麼動力。。
無論是OI還是文化課。。
儘管在學校裏有kxj。。
不知道這樣的狀態會持續多久。。
做一道題總是安靜不下來。。
不能認真地去思考。。
在學校裏不管是文理科上課都很懶散。。
晚上在宿舍裏就頹廢。。
這不是個好徵兆。。
現在僅僅是高一。。還有機會挽救。。
其實還是比較希望kxj能看到這篇。。
畢竟Ta目前是我比較大的動力。。
儘管說着別想多。。其實自己已經想多。。
其實你的心意我都懂。。
走一步看一步吧。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章