【POJ - 2376】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.
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 10Sample Output
2Hint
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.
題目大意:
? ??農(nóng)夫有N頭?!,F(xiàn)在他想讓一些牛去做家務(wù)。然后他把一天分成T個(gè)時(shí)間點(diǎn),也就是一天的時(shí)間點(diǎn)是區(qū)間[1,T]。他想要任何一個(gè)時(shí)間點(diǎn)都有牛在做家務(wù)?,F(xiàn)在給出每頭牛的工作時(shí)間,問(wèn)你能否用最少的牛滿(mǎn)足他的要求,即用最少的時(shí)間段覆蓋掉這一天([1,T])。如果不能滿(mǎn)足則輸出-1,否則輸出最少的牛數(shù)量。
分析:貪心題。題目意思很明顯是求最少的區(qū)間覆蓋掉大區(qū)間。先對(duì)這些時(shí)間段排好序,這個(gè)排序應(yīng)該是沒(méi)什么問(wèn)題的。然后呢,第一頭??隙ㄒx,就從這頭牛開(kāi)始,選取下一頭牛。下一頭牛怎么選取呢?即在滿(mǎn)足條件的牛里面(注意:滿(mǎn)足條件的牛是只要開(kāi)始工作時(shí)間 start>=cow[0].y+1 即可),選取右邊界值最大的那個(gè),因?yàn)檫@樣子能夠覆蓋掉最多的時(shí)間段。以此類(lèi)推,故貪心法求之。
解題報(bào)告:
? ? ? ? ? ?代碼太多,已經(jīng)分不清了。
AC代碼1:
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; struct co{int beg;int end; }; bool cmp(co a,co b){if(a.beg==b.beg) {return a.end<b.end;}return a.beg<b.beg; } int main() {int n,t;cin >> n>> t;co cow[25005];//若定義在全局變量 即 struct中 那么會(huì)有自動(dòng)補(bǔ)全功能存在 如果這樣定義 則沒(méi)有 for(int i=1;i<=n;i++){scanf("%d%d",&cow[i].beg,&cow[i].end);}int cur=0;int low;int pos=1;int ans=0;sort(cow+1,cow+n+1,cmp);while(cur<=t){ //行嗎 low=cur+1;int i; // for(i=pos;cow[i].beg<=low&&cow[i].end>=low;i++){//這樣寫(xiě)不可以!!只能i<=n 然后再for里面用if來(lái)判斷是否結(jié)束虛幻 或者繼續(xù)循環(huán) 這樣更好一些 // cur=max(cur,cow[i].end); // } // pos=i;for(i=pos;i<=n;i++){if(cow[i].beg<=low&&cow[i].end>=low){cur=max(cur,cow[i].end);}else if (cow[i].beg>low) {pos=i;break;} }if(low>cur) {break;}else {++ans;}}if(cur>=t) cout << ans << endl;else cout << -1 << endl;return 0 ;}?
?
AC代碼2:
#include<iostream> #include<cstdio> #include<algorithm>const int MAX= 25000 + 5; typedef long long ll; struct co{int start;int end; } cow[MAX]; //ll a[] bool cmp( const co & a1,const co & a2){if(a1.start==a2.start) {return a1.end > a2.end;}return a1.start<a2.start; } using namespace std; int main() {int n,t;int cur=0;scanf("%d%d",&n,&t);int sum=0;for(int i=0;i<n;i++){scanf("%d%d",&cow[i].start,&cow[i].end);} sort(cow,cow+n,cmp);int po=0;//從第po頭牛開(kāi)始找 // int maxx=0;//其實(shí)就是cur while(cur<t){//如果當(dāng)前的最大值還是小于t的話(huà) //int flag=0;int min=cur+1;for(int i=po;i<n;i++) {if(cow[i].start<=min&&cow[i].end>=min){//不能用cur也不能cur+1 !! 因?yàn)閏ur不能變 但是這樣cur就變了! // flag=1;cur=max(cur,cow[i].end);//maxx=max(maxx,cow[i].end);} else if(cow[i].start>min){// flag=1;po=i;//下一次從第i個(gè)開(kāi)始找break; }}// if(flag==0) break;if(min>cur) break;else ++sum;}if(cur>=t) cout << sum << endl;else cout << -1<<endl;return 0 ;}AC代碼3:
#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; struct A { int left,right; }; int cmp(const A &a,const A &b) { if(a.left==b.left) return a.right<b.right; return a.left <b.left; } int main() { A a[25005],b[25005]; int m,n,c; scanf("%d%d",&n,&c);for(int i=0; i<n; i++) { scanf("%d%d",&a[i].left,&a[i].right); } sort(a,a+n,cmp); /*for(int i=0;i<n;i++) cout << a[i].left <<" "<< a[i].right<<endl;*/ int max=0; int sum=0; int top=0; while(max<c) { int min=max+1; for(int i=top; i<n; i++) if(a[i].left<=min&&a[i].right>=min) max=max>a[i].right?max:a[i].right; else if(a[i].left>min) { top=i; break; } if(min>max) break; else sum++; } if(max>=c) cout << sum <<endl; else cout << "-1\n"; return 0; }?AC代碼4:
#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; struct A { int left,right; }; int cmp(const A &a,const A &b) { if(a.left==b.left) return a.right<b.right; return a.left <b.left; } int main() { A a[25005],b[25005]; int m,n,c; scanf("%d%d",&n,&c);for(int i=0; i<n; i++) { scanf("%d%d",&a[i].left,&a[i].right); } sort(a,a+n,cmp); /*for(int i=0;i<n;i++) cout << a[i].left <<" "<< a[i].right<<endl;*/ int max=0; int sum=0; int top=0; while(max<c) { int min=max+1; for(int i=top; i<n; i++) if(a[i].left<=min&&a[i].right>=min) max=max>a[i].right?max:a[i].right; else if(a[i].left>min) { top=i; break; } if(min>max) break; else sum++; } if(max>=c) cout << sum <<endl; else cout << "-1\n"; return 0; }?
總結(jié)
以上是生活随笔為你收集整理的【POJ - 2376】Cleaning Shifts (贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 新一代iPad mini曝光:供应量称配
- 下一篇: 曝6.7英寸版iPhone 14要改名: