贪心Cleaning shifts(需重做)
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.
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.
OutputLine 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.
我写的,不对,超时
#include<iostream> #include<algorithm> using namespace std; struct Cow{ int begin; int end; }; int n,t,MAX=0,MIN=1000001; bool cmp(Cow a,Cow b){ if(a.begin==b.begin)return a.end>b.end; else return a.begin<b.begin; } int Search(Cow *c){ int i,num=0; int m=MAX; for(i=0;i<n;i++){ if(c[i].begin<=m+1&&c[i].end>MAX){ MAX=c[i].end;num=i; } } return num; } int main(){ while(cin>>n>>t){ Cow *c=new Cow[n]; for(int i=0;i<n;i++){ cin>>c[i].begin>>c[i].end; } sort(c,c+n,cmp); int cnt=1; MAX=c[0].end; MIN=c[0].begin; while(1){ int num=Search(c);//找到下一头牛的标号 if(num==0)break;//表示没找到更大范围的 cnt++; } if(MAX!=t||MIN!=1)cout<<-1<<endl; else cout<<cnt<<endl; } return 0; } /* 4 12 1 7 3 9 8 12 2 5 3 10 1 7 3 6 6 10 5 100 1 24 67 89 15 48 48 68 89 100 */
正确版本
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int maxn = 26000; int n, t; struct cow { int l, r; }a[maxn]; bool cmp(cow a, cow b) { if (a.l != b.l) return a.l < b.l; else return a.r > b.r; } int main() { while (scanf("%d%d", &n, &t) != EOF) { bool fl, fr; fl = false; fr = false; for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].l, &a[i].r); if (a[i].l == 1) fl = true; if (a[i].r == t) fr = true; } if (!fl || !fr) printf("-1\n"); else { sort(a, a + n, cmp); int ans = 1; int r = a[0].r; int mmax = a[0].r; int j = 0; while (1) { while (j + 1 < n && a[j + 1].l <= r + 1) { j++; if (a[j].r > mmax) mmax = a[j].r; } if (mmax != r) { ans++; r = mmax; } else { if (j == n - 1)break; else { ans = -1; break; } } } printf("%d\n", ans); } } return 0; }