Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18217 | Accepted: 4636 |
Description
Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.
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.
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
* 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.
* 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 is not possible to assign a cow to each shift.
Sample Input
3 10 1 7 3 6 6 10
Sample Output
2
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
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.
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
有n个小区间和一个从1到t的大区间。
给出n个小区间的起止点,问最少用几个可以覆盖大区间,如果无解输出-1
按小区间左端点从小到大排序,每次贪心选择就行。
注意:只要每个整数点被覆盖就行,例如1 2,3 4,5 10这三个区间就可以覆盖1-10
discuss里的各种数据都过了,不知为何就是WAWAWA。
最后换成了另外一种不排序,每次从左端点已被覆盖(或者左端点挨着已被覆盖点)的区间中找最远右端点覆盖的写法,AC
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 "); 31 return 0; 32 } 33 now=ed+1;ans++; 34 if(ed>=t)break; 35 } 36 if(ed>=t)printf("%d ",ans); 37 else printf("-1 "); 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 ",ans); 74 else printf("-1 "); 75 return 0; 76 }*/