首页 > 代码库 > BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

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


题目


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

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 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 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


题解


这道题用时间戳的思路就可以了,我们统计同一时间最大的时间戳个数就是答案。


 

代码


 1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 using namespace std; 4  5 #define LL long long 6 #define Inf 2147483647 7 #define InfL 10000000000LL 8  9 inline int abs(int x){if (x<0) return -x;return x;}10 inline int abs(LL x){if (x<0) return -x;return x;}11 inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}12 inline void swap(LL &x,LL &y){LL tmp=x;x=y;y=tmp;}13 inline int remin(int a,int b){if (a<b) return a;return b;}14 inline int remax(int a,int b){if (a>b) return a;return b;}15 inline LL remin(LL a,LL b){if (a<b) return a;return b;}16 inline LL remax(LL a,LL b){if (a>b) return a;return b;}17 inline void read(int &x){x=0;int f=1;char ch=getchar();while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}x=x*f;}18 inline void read(LL &x){x=0;LL f=1;char ch=getchar();while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}x=x*f;}19 inline void read(int &x,int &y){read(x);read(y);}20 inline void read(LL &x,LL &y){read(x);read(y);}21 inline void read(int &x,int &y,int &z){read(x,y);read(z);}22 inline void read(int &x,int &y,int &n,int &m){read(x,y);read(n,m);}23 inline void read(LL &x,LL &y,LL &z){read(x,y);read(z);}24 inline void read(LL &x,LL &y,LL &n,LL &m){read(x,y);read(n,m);}25 const int Maxn=1000000;26 int n;27 int a,b;28 int t[Maxn+10]; 29 int main(){30     read(n);31     for (;n--;){32         read(a,b);33         t[a]++;34         t[b+1]--;35     }36     int Ans=0;37     for (int i=1;i<=Maxn;i++){38         t[i]=t[i-1]+t[i];39         Ans=remax(Ans,t[i]);40     }41     printf("%d\n",Ans);42     return 0;43 }
View Code

 


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