Cleaning Shifts
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 13042 |
|
Accepted: 3373 |
Description
Farmer John is assigning some of he n (1 <= n <= 25,000) cows to do some cleaning chores around the barn. He always wants to having one cow working on cleaning things up and had divided the day into T shifts (1 <= t <= 1,000 , the first being shift 1 and the last being shift T.
Each cow is a available at some interval of times during the day for work on cleaning. Any cow that's selected for cleaning duty would work for the entirety of her interval.
Your Job is-to-help Farmer John Assign some cows-shifts so (i) every shift have at least one cow assigned to it , and (ii) as few cows as possible is involved in cleaning. If It is not possible to assign a cow to each shift, print-1.
Input
* Line 1:two space-separated integers:n and T
* Lines 2..n+1:each line contains the start and end times of the interval during which a cow can work. A Cow starts work at the start time and finishes after the end time.
Output
* Line 1:the minimum number of cows Farmer John needs to hire or-1 if it isn't possible to assign a cow to each Shi Ft.
Sample Input
3 101 73) 66 10
Sample Output
2
Hint
This problem have huge input data,use scanf () instead of CIN to read data to avoid time limit exceed.
INPUT DETAILS:
there is 3 cows and shifts. Cow #1 can work shifts 1..7, Cow #2 can work shifts 3..6, and Cow #3 can work shifts 6..10.
OUTPUT DETAILS:
by selecting cows #1 and #3, all shifts is covered. There is no-to-cover all the shifts using fewer than 2 cows.
Source
Usaco 2004 December SilverTest Instructions: Gives an n line segment, and an interval T, which requires the smallest number of segments to be selected from the nth segment so that the range T can be coveredUnable to do output-1after sorting the segments, it's good to go straight through the line. Greedy:CNT indicates the leftmost area of the range that is not currently covered. then the cnt-1 is the far right of the covered range.As we traverse, we select a segment from the segment that satisfies the farthest of right in left<=cnt&&right>=cnt.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn=25000+3;8 Const intinf=0x3f3f3f3f;9 Ten structEdge One { A intL,r; - }EDGE[MAXN]; - the BOOLCMP (Edge A,edge B) - { - if(a.l==B.L) - returna.r<B.R; + Else - returna.l<B.L; + } A at intSolveintNintT) - { -Sort (edge,edge+n,cmp); - - intret=0; - intCnt=1; in intI=0; - while(true) to { + intmaxl=cnt-1; - the for(; i<n;i++) * { $ if(edge[i].l<=cnt&&edge[i].r>=CNT)Panax NotoginsengMaxl=Max (MAXL,EDGE[I].R); - if(edge[i].l>CNT) the Break; + } A the if(maxl<CNT) + return-1; -ret++; $cnt=maxl+1; $ if(cnt>T) - returnret; - } the } - Wuyi intMain () the { - intn,t; Wu while(~SCANF ("%d%d",&n,&T)) - { About for(intI=0; i<n;i++) $ { -scanf"%d%d",&edge[i].l,&EDGE[I].R); - } - Aprintf"%d\n", Solve (n,t)); + } the return 0; -}
View Code
POJ 2376 Cleaning shifts greedy basic problem