日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 一区二区乱子伦在线播放 | 国产一区二区视频在线观看 | 久久字幕 | 女主播在线观看 | 麻豆私人影院 | 男女黄床上色视频免费的软件 | 毛片在线网 | 亚洲天堂三区 | 在线国产精品视频 | 老牛嫩草二区三区观影体验 | 韩国福利一区 | 男插女av | 日韩两性视频 | 亚洲手机av | 久久久久人妻精品一区二区三区 | 日本加勒比一区二区 | 人人看av| 国产精品一区二区免费在线观看 | 青娱乐97 | 亚洲视频www| 国产精品久久国产 | 四虎精品一区二区三区 | 成年人视频免费在线观看 | 日日干日日摸 | 亚洲电影中文字幕 | 翔田千里在线播放 | 麻豆精品在线播放 | 丰满人妻一区二区 | 婷婷午夜精品久久久久久性色av | 九一精品在线 | 毛色毛片| 国产精品色在线 | 中文字幕中文字幕 | 奇米网一区二区 | 国产女女| 91毛片视频 | 激情影院内射美女 | 天天综合网在线 | 精品国产a线一区二区三区东京热 | 久久瑟瑟 | 国产激情对白 | 国产精品白浆一区二小说 | 欧洲精品久久一区二区 | 久久久久久久999 | 黄色欧美在线 | 青青操在线视频 | yjizz视频| 国产天堂精品 | 丰满少妇一级片 | 免费一级特黄3大片视频 | 日本人妻不卡一区二区三区中文字幕 | 又黄又爽无遮挡 | 5566色 | www.国产麻豆| 日韩不卡视频在线观看 | 亚洲高清精品视频 | 丝袜人妻一区二区三区 | 免费看日批 | 亚洲熟女少妇一区 | 亚洲色图3p| 激情aaa | 国产一久久 | 国产精品丝袜视频无码一区69 | 亚洲美女性生活视频 | 91在线观看| 亚洲一区观看 | 女人下面流白浆的视频 | 岳狂躁岳丰满少妇大叫 | 成人免费黄 | 久久桃色| 日韩一级| 久久精品一区二区免费播放 | 欧美精品亚洲精品 | 蜜臀在线一区二区三区 | 天堂中文字幕av | 秋霞午夜| 国产网红在线观看 | 成人精品免费在线观看 | 五月天婷婷基地 | 一级黄色片视频 | 91爱国产| 在线看黄色网 | 台湾佬成人中文网222vvv | 在线黄色av | 与子敌伦刺激对白播放的优点 | 国产精品成人免费看片 | 亚洲精品乱码久久久久久麻豆不卡 | 蜜桃av一区二区三区 | 干美女av| 亚洲色图综合在线 | 四虎影视在线播放 | 久久伊人婷婷 | 污污污污污污www网站免费 | 欧美日韩国产传媒 | 日本视频中文字幕 | 国产亚洲制服欧洲高清一区 | 黄色成年人 | 欧美综合久久 | 朝桐光一区二区三区 |