【贪心】Stall Reservations(luogu 2859/poj 3190)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【贪心】Stall Reservations(luogu 2859/poj 3190)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                Stall Reservations
luogu 2859
poj 3190
題目大意:
有n頭牛,每頭牛都有自己的擠奶時(shí)間,擠奶時(shí)間內(nèi)每頭牛用一個(gè)奶棚,現(xiàn)在問最少需要多少個(gè)奶棚
輸入樣例
5 1 10 2 4 3 6 5 8 4 7輸出樣例
4 1 2 3 2 4數(shù)據(jù)范圍
1?N?50,0001 \leqslant N \leqslant 50,0001?N?50,000
 1?A?B?1,000,0001 \leqslant A \leqslant B \leqslant 1,000,0001?A?B?1,000,000
解題思路:
直接貪心,當(dāng)有位時(shí)就進(jìn)去,否則開一個(gè),但這樣o(n2)o(n^2)o(n2)會(huì)TLETLETLE,所以我們用STL堆來求空的,使時(shí)間復(fù)雜度優(yōu)化到o(nlogn)o(n\ log_n)o(n?logn?)
代碼:
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,ans; struct rec {int s,bg,ed,num; }a[50500]; bool operator <(const rec &x,const rec &y){return x.ed>y.ed;}//改為小根堆 bool cmp(rec x,rec y){return x.bg<y.bg;} bool cmpp(rec x,rec y){return x.num<y.num;} int main() {scanf("%d",&n);for (int i=1;i<=n;++i){scanf("%d %d",&a[i].bg,&a[i].ed);a[i].num=i;}sort(a+1,a+1+n,cmp);priority_queue<rec>d;//定義堆a[1].s=1;d.push(a[1]);ans=1;for (int i=2;i<=n;++i){rec h=d.top();if (h.ed<a[i].bg)//看看是否重復(fù){d.pop();a[i].s=h.s;//記錄牛棚的編號(hào)d.push(a[i]);//入堆}else{a[i].s=++ans;//新建一個(gè)牛棚d.push(a[i]);}}sort(a+1,a+1+n,cmpp);//排序回原來的樣子printf("%d\n",ans);for (int i=1;i<=n;++i)printf("%d\n",a[i].s); }總結(jié)
以上是生活随笔為你收集整理的【贪心】Stall Reservations(luogu 2859/poj 3190)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 花呗临时额度是什么意思 是怎么解释的
 - 下一篇: biblatex中如何修改参考文献中日期