P3826 [NOI2017] 蔬菜 - 特殊性质2

· · 个人记录

[NOI2017] 蔬菜 - 特殊性质 2 题解

题目分析(特殊性质 2)

当所有 x_i = 0 时,蔬菜不会变质。问题简化为:

有 n 种蔬菜,第 i 种有 c_i 个单位。
卖第一个单位获得 a_i + s_i 收益,后续每个单位获得 a_i 收益。
每天最多卖 m 个单位,问销售 p 天最多获得多少收益。

My Idea

由于蔬菜不会变质(“优质”保鲜膜),我们只需选择收益最高的若干单位来卖,而不需要考虑时间顺序。

贪心策略:将所有可能的单位按收益从高到低排序,取前 p \times m 个。

关键点:

Good Algorithm

数据结构设计

用优先队列维护当前可卖的单位,包含:

算法步骤

  1. 初始化:将每种蔬菜的特殊单位加入优先队列
  2. 模拟销售:
    • 每天从队列取收益最高的单位
    • 如果是特殊单位(第一次卖该蔬菜),卖完后将 c_i - 1 个普通单位加入队列
    • 批量处理多个收益相同的单位
  3. 预处理答案:计算销售 1 到 \max p 天的最大收益
  4. 回答查询:O(1) 输出

Check Time

Show You My Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N=1e5+10;
int n,m,k;
LL a[N],s[N],c[N],x[N],ans[N];

struct Node{
    LL val;     // 单位收益
    int id;     // 蔬菜编号
    LL cnt;     // 剩余数量
    bool operator<(const Node& t)const{
        return val<t.val;  // 大根堆,收益高的优先
    }
};

int main(){
    scanf("%d%d%d",&n,&m,&k);

    // 检查是否所有x_i=0
    bool spec2=true;
    for(int i=1;i<=n;i++){
        scanf("%lld%lld%lld%lld",&a[i],&s[i],&c[i],&x[i]);
        if(x[i]!=0) spec2=false;
    }

    // 不符合特性2直接输出0
    if(!spec2){
        for(int i=1;i<=k;i++) scanf("%*d"),printf("0\n");
        return 0;
    }

    // 读查询,找最大天数
    vector<int> qry;
    int maxp=0;
    for(int i=1;i<=k;i++){
        int p; scanf("%d",&p);
        qry.push_back(p);
        maxp=max(maxp,p);
    }

    // 初始化优先队列:所有蔬菜的特殊单位
    priority_queue<Node> pq;
    for(int i=1;i<=n;i++){
        if(c[i]>0) pq.push({a[i]+s[i],i,1});
    }

    LL sum=0;                    // 累计总收益
    vector<Node> tmp;            // 暂存今天没卖完的
    LL today;                    // 今天已卖数量

    // 预处理所有天数答案
    for(int day=1;day<=maxp;day++){
        tmp.clear();
        today=0;

        // 今天卖m个单位
        while(today<m && !pq.empty()){
            Node cur=pq.top(); pq.pop();

            // 批量卖:卖min(剩余需求, 该组数量)个
            LL sell_cnt=min((LL)(m-today),cur.cnt);
            sum+=cur.val*sell_cnt;
            today+=sell_cnt;

            // 更新剩余数量
            cur.cnt-=sell_cnt;
            if(cur.cnt>0) tmp.push_back(cur);

            // 特殊单位卖完的处理
            if(cur.val==a[cur.id]+s[cur.id] && cur.cnt==0){
                // 加入该蔬菜的所有普通单位
                if(c[cur.id]-1>0){
                    pq.push({a[cur.id],cur.id,c[cur.id]-1});
                }
            }
        }

        // 今天没卖完的放回队列
        for(auto& cur:tmp) pq.push(cur);

        // 记录第day天的答案
        ans[day]=sum;
    }

    // 输出所有查询
    for(auto p:qry) printf("%lld\n",ans[p]);

    return 0;
}

样例模拟

符合特性2的示例

输入:

2 3 2
3 3 3 0    # 蔬菜1:a=3,s=3,c=3,x=0
2 5 8 0    # 蔬菜2:a=2,s=5,c=8,x=0
1          # 查询:p=1
3          # 查询:p=3

模拟过程

初始化队列:

第1天(卖3单位):

  1. 取 (7,菜2,1) → 卖1个,收益+7
    • 特殊单位卖完,加入普通单位:8-1=7个收益2 队列:[(6,菜1,1), (2,菜2,7)]
  2. 取 (6,菜1,1) → 卖1个,收益+6
    • 特殊单位卖完,加入普通单位:3-1=2个收益3 队列:[(3,菜1,2), (2,菜2,7)]
  3. 取 (3,菜1,2) → 卖1个,收益+3,剩余1个 队列:[(3,菜1,1), (2,菜2,7)]

第1天收益:7+6+3=16
ans[1]=16

第2天(卖3单位): 队列:[(3,菜1,1), (2,菜2,7)]

  1. 取 (3,菜1,1) → 卖1个,收益+3,卖完 队列:[(2,菜2,7)]
  2. 取 (2,菜2,7) → 卖2个,收益+4,剩余5个 队列:[(2,菜2,5)]

第2天收益:16+3+4=23
ans[2]=23

第3天(卖3单位): 队列:[(2,菜2,5)] 取 (2,菜2,5) → 卖3个,收益+6,剩余2个 队列:[(2,菜2,2)]

第3天收益:23+6=29
ans[3]=29

查询输出:

正确性验证

手动验证最优解:

  1. 所有单位收益排序:7, 6, 3, 3, 2, 2, 2, 2, 2, 2, 2

算法正确。

记录

完结撒花!!!