日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 2376】Cleaning Shifts (贪心)

發(fā)布時(shí)間:2023/12/10 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2376】Cleaning Shifts (贪心) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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 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.

題目大意:

? ??農(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 麻豆福利视频 | 精品日本一区二区三区 | 91精品国产91久久久久久黑人 | 亚洲第一视频在线 | 国产精品一区二区三区不卡 | 欧美亚洲另类在线 | 在线播放无码后入内射少妇 | 国产网红av| 天天夜夜草 | 欧美色亚洲色 | 影音先锋中文字幕一区 | 91高清在线| 国产97在线 | 亚洲 | 免费91看片| 亚洲国产精品美女 | 日本美女高潮 | 蜜乳av一区| 欧美丰满老熟妇aaaa片 | 久久高清| 天堂中文资源在线 | 日韩视频在线一区二区 | 777久久久精品一区二区三区 | 最新中文在线视频 | 宅男视频在线免费观看 | av一级久久 | 日本一区二区免费视频 | 美女aaa | 国产免费a| 亚洲区中文字幕 | 欧美色图在线播放 | 国产精品免费在线 | 亚洲精品乱码久久久久久 | 欧美一a一片一级一片 | 风间由美一区 | 国产精品成人一区二区三区 | 久久99精品久久久 | 天天干视频在线观看 | 国产精品xxx视频 | 少妇性l交大片免费观看 | 天天综合网久久 | 日本中文字幕精品 | 亚洲精华国产精华精华液网站 | 成人第一页 | 久久久久久久女国产乱让韩 | 天天视频污 | 窝窝午夜精品一区二区 | 校园激情亚洲 | 免费看a级片 | 美女一二区 | 91精品一区 | 成人午夜视频在线 | 一区二区三区欧美日韩 | 91理论片| 欧美视频在线观看一区二区三区 | 欧美成人精品一区二区免费看片 | 91免费国产在线观看 | 素人女裸体 | av日韩一区 | 日本免费福利视频 | 日本老小玩hd老少配 | 日本午夜电影 | 五级毛片| 国产又粗又长又黄的视频 | www.av在线免费观看 | 久久国产精品无码网站 | 理论片一区 | 欧美在线小视频 | 欧美综合在线观看 | 色一情一乱一乱一区91av | 国产乱子伦视频一区二区三区 | 国产极品一区 | 国产人妖一区二区三区 | 国产精品久久精品三级 | 午夜亚洲福利 | 欧美性生活精品 | 伊人久久麻豆 | 日韩欧美一区二 | 日本不卡二区 | 人与禽一级全黄 | 欧美乱大交xxxxx春色视频 | 亚洲精品久久久狠狠狠爱 | 国产日韩精品一区二区 | 亚洲av成人无码久久精品 | 自拍偷拍第一页 | 波多野吉衣中文字幕 | 西西人体做爰大胆gogo | 国产免费黄色网址 | 337p嫩模大胆色肉噜噜噜 | 另类天堂av| 国产精品欧美一区二区 | 天堂色区 | 精品午夜久久久 | 日韩少妇裸体做爰视频 | 一卡二卡三卡四卡五卡 | 精品美女 | 国产微拍精品一区 | 中国黄色一级视频 | 午夜久久影院 | 在线观看小视频 |