题面: [NOI2017]蔬菜

题解:

  首先每天蔬菜会变质这点并不好处理,我们考虑让时间倒流,从后向前处理,这样的话就相当于每天都会得到一定量的蔬菜。

  这样做有什么好处呢?

  我们可以发现一个性质:如果从后向前贪心卖菜,那么因为现在可以卖的菜,以后一定还可以卖(因为变成了得到菜),因此贪心就是对的了。

  因此我们用堆维护一下,从后向前贪心的卖菜,每次优先卖价格高的,第一次卖的菜价格要加上奖励的贡献,并且只能先卖一个,因为卖完这一个之后的同种菜没有奖励了,相当于贡献有变化。

  这样向前一直贪到第一天,于是我们就得到了卖1 ~ 100000天的最高收入。

  那么已知1到100000的最高收入,如何利用之前的决策信息快速的得知1到99999的最高收入呢?

  我们发现,这2者之间唯一的差别就是少买了最多m个蔬菜。

  那么我们已知我们已经卖了have个蔬菜,已知1到now这么多天最多卖$m * now$个蔬菜,那么如果$have > m * now$,我们就要舍弃部分蔬菜使得$have <= m * now$成立。

  用堆维护,每次撤销卖价最低的蔬菜即可,注意如果撤销的这个蔬菜是剩下的最后一个了,那么代价要加上奖励的代价。

  为什么这样是对的呢?

  因为如果这个菜是在后面被卖出的话,前面肯定也可以卖,所以如果我们撤销了某天卖出的蔬菜,可以看做在这天后面卖出的蔬菜被挤上来顶替了这个被撤销的蔬菜,于是代价就会始终等价于撤销了最后一天卖出的蔬菜。

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 100100
#define LL long long int n, m, k, can;//存下浪费了多少的卖菜名额
const int maxn = ;
LL ans[AC], last[AC], sum[AC], d[AC], have[AC], v[AC], s[AC], c[AC];
int sta[AC], top;
//每一天的答案,每种菜最后一天出现是在哪一天,那天有多少这种菜,d表示以后每天增加多少
//have表示这种菜现在卖出了多少,a表示卖菜的基础收益,s表示额外收益,c表示初始库存
int Head[AC], Next[AC], date[AC], tot;
struct node{
LL v;
int id;
}; struct cmp2{//小根堆
bool operator () (node a, node b){ return a.v > b.v;}
}; struct cmp{//大根堆
bool operator () (node a, node b){return a.v < b.v;}
}; priority_queue<node, vector<node>, cmp> q;
priority_queue<node, vector<node>, cmp2> q2; inline int read(){
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline void add(int f, int w){
date[++ tot] = w, Next[tot] = Head[f], Head[f] = tot;
} void pre()
{
n = read(), m = read(), k = read();
for(R i = ; i <= n; i ++)
{
v[i] = read(), s[i] = read(), c[i] = read(), d[i] = read();
if(d[i])
{
last[i] = c[i] / d[i] + , sum[i] = c[i] - (c[i] / d[i]) * d[i];
if(!sum[i]) sum[i] = d[i], last[i] --;
add(last[i], i);//把这个菜挂在第一次出现的地方
}
else last[i] = maxn, sum[i] = c[i], add(maxn, i);
}
} void get()//从后向前贪心,小根堆维护撤销
{
for(R i = ; i <= n; i ++)//把卖出去的菜压入栈中
if(have[i] == ) q2.push((node){v[i] + s[i], i});
else if(have[i]) q2.push((node){v[i], i});
for(R i = maxn - ; i; i --)//向前撤销
{
ans[i] = ans[i + ];//初始状态
LL y = m;
if(i * m >= can) continue;
else y = can - i * m, can -= y;
while(y && !q2.empty())
{
node x = q2.top();
if(have[x.id] == ) ans[i] -= x.v, q2.pop(), -- y;//如果只剩一个了就要弹出去了
else
{
if(have[x.id] > y)//如果反正取不完的话就随便取,否则就要注意不能取完了
{
have[x.id] -= y, ans[i] -= x.v * y, y = ;
if(have[x.id] == )
q2.pop(), x.v += s[x.id], q2.push(x);
}
else
{
ans[i] -= x.v * (have[x.id] - ), y -= (have[x.id] - );
have[x.id] = , q2.pop(), x.v += s[x.id], q2.push(x);
}
}
}
}
} void build()//从后向前贪心,大根堆维护取值
{
for(R i = maxn; i; i --)
{
for(R j = Head[i]; j; j = Next[j])
{
int x = date[j];
q.push((node){v[x] + s[x], x});
}
while(top) q.push((node){v[sta[top]], sta[top]}), -- top;//放回去
LL y = m;
while(y && !q.empty())
{
node x = q.top();
if(!have[x.id])
{
q.pop(), ans[maxn] += x.v, x.v -= s[x.id];
have[x.id] = , -- y, q.push(x);
}
else
{
LL tmp = (last[x.id] - i) * d[x.id] + sum[x.id] - have[x.id];//获取这个菜还剩多少
if(tmp >= y) have[x.id] += y, ans[maxn] += x.v * y, y = ;//还有就不用pop了
else
{
y -= tmp, have[x.id] += tmp, q.pop();
ans[maxn] += x.v * tmp;
if(d[x.id]) sta[++top] = x.id;
}
}
}
can += m - y;
}
} void work()
{
for(R i = ; i <= k; i ++)
printf("%lld\n", ans[read()]);
} int main()
{
// freopen("in.in", "r", stdin);
pre();
build();//先获取最后一个的值
get();//再倒退出整个数组
work();
// fclose(stdin);
return ;
}
05-11 20:23