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] >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::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