BZOJ 2802 Poi2012 Warehouse Store 堆+貪心

來源:互聯網
上載者:User

標籤:bzoj   bzoj2802   堆   貪心   

題目大意:有n天,早上進貨中午賣,可以選擇賣或者不賣,問最多可以賣出多少人

首先貪心的思想是如果當前能賣就賣

但是這樣不一定是最優的

比如說我第一天來一個人把所有的庫存都買走了 然後後面基本沒有補給 後面的人都餓死了

因此我們維護一個大根堆來記錄我們都賣出了多少份

如果有一個人買不到 我們去大根堆裡尋找有沒有買的比他多的 如果有 把之前的人取消 賣給這個人

這樣雖然不能增加答案 但是可以增加庫存

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 250100using namespace std;typedef pair<int,int> abcd;int n,a[M],b[M],ans[M];long long stock;namespace Priority_Heap{abcd heap[M];int top;void Insert(abcd x){heap[++top]=x;int t=top;while(t>1){if(heap[t]>heap[t>>1])swap(heap[t],heap[t>>1]),t>>=1;elsebreak;}}void Pop(){heap[1]=heap[top--];int t=2;while(t<=top){if(t<top&&heap[t+1]>heap[t])++t;if(heap[t]>heap[t>>1])swap(heap[t],heap[t>>1]),t<<=1;elsebreak;}}}int main(){int i;cin>>n;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)scanf("%d",&b[i]);for(i=1;i<=n;i++){stock+=a[i];if(stock>=b[i])stock-=b[i],Priority_Heap::Insert(abcd(b[i],i));else{if(Priority_Heap::heap[1].first<b[i])continue;stock+=Priority_Heap::heap[1].first;stock-=b[i];Priority_Heap::Pop();Priority_Heap::Insert(abcd(b[i],i));}}cout<<Priority_Heap::top<<endl;for(i=1;i<=Priority_Heap::top;i++)ans[++ans[0]]=Priority_Heap::heap[i].second;sort(ans+1,ans+ans[0]+1);for(i=1;i<=ans[0];i++)printf("%d%c",ans[i],i==ans[0]?'\n':' ');return 0;}


BZOJ 2802 Poi2012 Warehouse Store 堆+貪心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.