Question
1651: [usaco Feb] stall reservations dedicated cowshed 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 .. B (1 <= A <= B <= 1,000,000), where des both times A and B. obviusly, 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 stallrequired in the barn so that each cow can have her private milking period * an assignment of cows to these stils over time
There are nheaded cows, and each ox has a time to drink water. During this time, it will be dedicated to a stall. Now it gives the time period for each ox to drink water and asks how many stall should be required to meet their requirements.
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 stallthe 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 input5
1 10
2 4
3 6
5 8
4 7
Sample output4
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 stils are possible. Hint
Try this data. For sort by end point, then greedy's Practice 1 3 5 7 6 9 10 11 8 12 4 13 correct output should be 3
Question
We can use the timestamp to calculate the maximum number of timestamps at the same time.
Code
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: [usaco Feb] stall reservations dedicated Cowshed