Bzoj 2802 Poi2012 Warehouse Store heap + greedy

Source: Internet
Author: User

The main topic: There are N days, in the morning to buy noon sell, you can choose to sell or not to sell, ask the most can sell how many people

The first greedy idea is to sell if you can sell it now.

But it's not necessarily optimal.

For example, on my first day, I bought all my inventory, and then there were basically no supplies back there, starving to death.

So we maintain a big pile to record how many copies we've sold.

If there's one person who can't buy us, we go to the big pile and look for a lot more than him. If there was a cancellation before the sale of the person

This may not increase the answer but can increase inventory

#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] &GT;HEAP[T&GT;&GT;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::P op (); 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); (i=1;i<=ans[0];i++) printf ("%d%c", ans[i],i==ans[0]? ' \ n ': '); return 0;}


Bzoj 2802 Poi2012 Warehouse Store heap + greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.