【BZOJ】2802: [Poi2012]Warehouse Store(贪心)

http://www.lydsy.com/JudgeOnline/problem.php?id=2802

自己yy了一下。。。

每一次如果够那么就买。

如果不够,考虑之前买过的,如果之前买过的比当前花费的钱多,那么就去掉之前买的,变成买现在的。

如何证明?不知道QAQ

还有。。。注意开longlong。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const ll getint() { ll r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

const int N=250005;
priority_queue<pair<ll, int> > q;
ll a[N];
bool vis[N];
int main() {
	int n=getint(), ans=0;
	ll rest=0;
	for1(i, 1, n) read(a[i]);
	for1(i, 1, n) {
		ll b=getint();
		rest+=a[i];
		if(rest>=b) rest-=b, q.push(make_pair<ll, int>(b, i)), ++ans, vis[i]=1;
		else if(!q.empty()) {
			ll tp=q.top().first;
			if(tp>b) { rest+=tp; rest-=b; vis[i]=1; vis[q.top().second]=0; q.pop(); q.push(make_pair<ll, int>(b, i)); }
		}
	}
	print(ans); puts("");
	for1(i, 1, n) if(vis[i]) printf("%d ", i);
	return 0;
}

  

 


 

 

Description


有一家专卖一种商品的店,考虑连续的n天。
第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择满足顾客的要求,或是无视掉他。
如果要满足顾客的需求,就必须要有足够的库存。问最多能够满足多少个顾客的需求。

 

Input

第一行一个正整数n (n<=250,000)。
第二行n个整数A1,A2,...An (0<=Ai<=10^9)。
第三行n个整数B1,B2,...Bn (0<=Bi<=10^9)。

 

Output

第一行一个正整数k,表示最多能满足k个顾客的需求。
第二行k个依次递增的正整数X1,X2,...,Xk,表示在第X1,X2,...,Xk天分别满足顾客的需求。

Sample Input

6
2 2 1 2 1 0
1 2 2 3 4 4

Sample Output

3
1 2 4

HINT

 

Source

posted @ 2014-12-09 12:29  iwtwiioi  阅读(338)  评论(0编辑  收藏  举报