Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18217 | Accepted: 4636 |
Description
Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.
Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input
* 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
Sample Input
3 10 1 7 3 6 6 10
Sample Output
2
Hint
INPUT DETAILS:
There are 3 cows and 10 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 are covered. There is no way to cover all the shifts using fewer than 2 cows.
Source
1 /*by SilverN*/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 const int mxn=1000200; 9 int n,t; 10 int mx[mxn];//起点是mx[]的区间中,最远的右端点位置 11 int ans=0; 12 int main(){ 13 scanf("%d%d",&n,&t); 14 int a,b; 15 int i,j; 16 for(i=1;i<=n;i++){ 17 scanf("%d%d",&a,&b); 18 mx[a]=max(mx[a],b); 19 } 20 int last=1,now=1; 21 int ed; 22 while(now<=t){ //now是当前已经覆盖的长度 23 ed=0; 24 for(i=last;i<=now;i++){ 25 if(mx[i]>ed){ 26 ed=mx[i];last=i; 27 } 28 } 29 if(ed+1==now){ 30 printf("-1\n"); 31 return 0; 32 } 33 now=ed+1;ans++; 34 if(ed>=t)break; 35 } 36 if(ed>=t)printf("%d\n",ans); 37 else printf("-1\n"); 38 return 0; 39 } 40 /* 41 const int mxn=26000; 42 struct line{ 43 int l,r; 44 }a[mxn]; 45 int cmp(const line a,const line b){ 46 if(a.l!=b.l)return a.l<b.l; 47 return a.r<b.r; 48 } 49 int st,ed; 50 int n,t; 51 int main(){ 52 int i,j; 53 scanf("%d%d",&n,&t); 54 for(i=1;i<=n;i++) 55 scanf("%d%d",&a[i].l,&a[i].r); 56 sort(a+1,a+n+1,cmp); 57 ed=0; 58 int ans=0; 59 if(a[1].l<=1) 60 for(i=1;i<=n;i++){ 61 int nxt=0; 62 while(i+1<=n && a[i+1].l<=ed+1){ 63 i++; 64 nxt=max(nxt,a[i].r); 65 } 66 if(a[i].l<=ed+1) nxt=max(nxt,a[i].r); 67 if(nxt>ed){ 68 ed=nxt; 69 ans++; 70 } 71 if(ed>=t)break; 72 } 73 if(ed>=t)printf("%d\n",ans); 74 else printf("-1\n"); 75 return 0; 76 }*/