【贪心】Stall Reservations(luogu 2859/poj 3190)
生活随笔
收集整理的這篇文章主要介紹了
【贪心】Stall Reservations(luogu 2859/poj 3190)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Stall Reservations
luogu 2859
poj 3190
題目大意:
有n頭牛,每頭牛都有自己的擠奶時間,擠奶時間內每頭牛用一個奶棚,現在問最少需要多少個奶棚
輸入樣例
5 1 10 2 4 3 6 5 8 4 7輸出樣例
4 1 2 3 2 4數據范圍
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
解題思路:
直接貪心,當有位時就進去,否則開一個,但這樣o(n2)o(n^2)o(n2)會TLETLETLE,所以我們用STL堆來求空的,使時間復雜度優化到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)//看看是否重復{d.pop();a[i].s=h.s;//記錄牛棚的編號d.push(a[i]);//入堆}else{a[i].s=++ans;//新建一個牛棚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); }總結
以上是生活随笔為你收集整理的【贪心】Stall Reservations(luogu 2859/poj 3190)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 花呗临时额度是什么意思 是怎么解释的
- 下一篇: 【结论】棋盘(jzoj 2297)