BZOJ2802 [Poi2012]Warehouse Store 【贪心】

题目链接

BZOJ2802

题解

这样的问题通常逆序贪心
每个(A[i])只能用来满足后面的(B[i])
就用当前(A[i])不断提供给最小的(B[i])即可
用一个堆维护

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 250005,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
struct node{int v,id;};
inline bool operator < (const node& a,const node& b){
	return a.v > b.v;
}
priority_queue<node> q;
LL A[maxn],B[maxn];
int n,ans[maxn],ansi;
int main(){
	n = read();
	REP(i,n) A[i] = read();
	REP(i,n) B[i] = read();
	node u;
	for (int i = n; i; i--){
		q.push((node){B[i],i});
		while (A[i] && !q.empty()){
			u = q.top(); q.pop();
			if (A[i] >= u.v) ans[++ansi] = u.id,A[i] -= u.v;
			else u.v -= A[i],q.push(u),A[i] = 0;
		}
	}
	sort(ans + 1,ans + 1 + ansi);
	printf("%d
",ansi);
	REP(i,ansi) printf("%d ",ans[i]);
	return 0;
}

原文地址:https://www.cnblogs.com/Mychael/p/9237236.html