[bzoj2802][Poi2012]Warehouse Store_贪心_堆

Warehouse Store bzoj-2802 Poi-2012

题目大意:一家商店的连续n天内,每一天会进货$a_i$个,有且只有一个客人回来买$b_i$个,问至多满足多少人。

注释:$1\le n \le 2.5\cdot 10^5$。


想法:又往dp上想了.....

然后一顿推,无果。

查了题解,发现贪心好难.....

首先,我们先搞清楚我们要维护什么?我们要维护两个最值,分别是前i天能最多能满足多少人和满足这么多人的情况下最多剩多少钱。

那么,对于第一个。假设我们已经维护好了前i-1天剩的最多的钱数$sum$,如果$sum+a_i>=b_i$,那么这一定是前i+1天可能的最多的人数。

对于第二个最值,我们需要想网络流一样的反悔策略。这样,我们请出priority_queue

如果当前能满足,就直接$sum+=a_i-b_i$了事。

如果之前的最多钱数都不能满足,那么我们想更新钱数,所以堆内维护的是按b的小根堆,如果当前的b比堆顶的b小,直接替换即可。

最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 250010 
using namespace std;
typedef long long ll;
int a[maxn],b[maxn],c[maxn];
priority_queue<pair<int,int> > q;
int n,m,ans,cnt;
ll sum=0;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n;i++)
	{
		sum+=a[i];
		if(sum>=b[i]) ans++,sum-=b[i],q.push(make_pair(b[i],i));
		else if(!q.empty())
		{
			
			int t=q.top().first;
			if(t>b[i])
			{
				q.pop();
				sum-=b[i];sum+=t;
				q.push(make_pair(b[i],i));
			}
		}
	}
	printf("%d\n",ans);
	while(!q.empty()) c[++cnt]=q.top().second,q.pop();
	sort(c+1,c+cnt+1);
	for(int i=1;i<=ans;i++) printf("%d ",c[i]);
	puts("");
	return 0;
}

小结:贪心蒸的难... ...

posted @ 2018-07-31 21:17  JZYshuraK_彧  阅读(252)  评论(0编辑  收藏  举报