解题思路:
正解是从最后一天往前贪心……
假设我们已经知道了最优的可以卖出的所有蔬菜,那么当我们知道了第i天的答案,我们只要去掉m个价值最小的蔬菜,就能得到第i-1天的答案。
所以我们现在只要求出最后一天的答案以及最优的可以卖出的所有蔬菜。
由于题目有要求,所以把每种蔬菜拆成两份,前c-1个价值为a,最后一个价值为a+s,按价值放入优先队列中,依次取出来从变质的最后一天开始放,放入还能放的地方,并记录一下放入的蔬菜价值好处理答案。
还能放的地方可以用并查集维护,记录连续的空区间,直接跳到下一个可以放的地方。
时间复杂度为 O(m∗max(p)∗logn∗α(n)) O ( m ∗ m a x ( p ) ∗ l o g n ∗ α ( n ) )
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
if(c=='-')c=getchar(),f=-1;
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int N=100005;
int n,m,k,mx,cnt,p[N],last[N];
ll ans[N],num[N],g[10*N];
struct node
{
int w,c,x;
node(){}
node(int _w,int _c,int _x):w(_w),c(_c),x(_x){}
inline friend bool operator < (const node &a,const node &b){return a.w<b.w;}
};
priority_queue<node>q;
inline int find_last(int x)
{
if(x>mx)return find_last(mx);
return last[x]==x?x:last[x]=find_last(last[x]);
}
int main()
{
//freopen("lx.in","r",stdin);
n=getint(),m=getint(),k=getint();
for(int i=1;i<=n;i++)
{
int a=getint(),s=getint(),c=getint(),x=getint();
if(c>1)q.push(node(a,c-1,x));
q.push(node(a+s,1,-1*(x?(c-1)/x+1:100000)));
}
for(int i=1;i<=k;i++)p[i]=getint(),mx=max(mx,p[i]);
for(int i=1;i<=mx;i++)last[i]=i,num[i]=m;
while(!q.empty())
{
node t=q.top();q.pop();
if(t.x<0)
{
int p=find_last(-t.x);
if(!p)continue;
num[p]--,g[++cnt]=t.w;
if(num[p]==0)last[p]=find_last(p-1);
ans[mx]+=t.w;
}
else
{
ll p,np,cur;
p=t.x?(t.c-1)/t.x+1:mx,cur=t.c-(p-1)*t.x;
np=find_last(p),cur+=(p-np)*t.x,p=np;
while(p>0)
{
if(!cur)break;
ll del=min(num[p],cur);
for(int i=1;i<=del;i++)ans[mx]+=g[++cnt]=t.w;
cur-=del,num[p]-=del;
if(!num[p])last[p]=find_last(p-1);
np=find_last(p-1),cur+=(p-np)*t.x,p=np;
}
}
}
sort(g+1,g+cnt+1);
int tot=mx*m,l=1;ll cur=ans[mx];
for(int i=mx-1;i;i--)
{
tot-=m;
while(cnt-l+1>tot)cur-=g[l++];
ans[i]=cur;
}
for(int i=1;i<=k;i++)printf("%lld\n",ans[p[i]]);
return 0;
}