版本比较
P3826 [NOI2017] 蔬菜 - 特殊性质2
正文
增加 19 行删除 19 行
2 行未改动34567878910111248 行未改动61626364656566676869707172727374757677787879808182838485868787888990919293939495969797989910010110210210310410510610610710810911011111111211311411511511611711811912012112212312412412512612712712812913013113113213313413413513613713827 行未改动1661671681691701701711721731743 行未改动17817918018118218218318418518618718818919019119219219319419519621 行未改动2 行未改动## 题目分析(特殊性质 2)
当所有 $x_i = 0$ 时,蔬菜**不会变质**。问题简化为:
> 有 $n$ 种蔬菜,第 $i$ 种有 $c_i$ 个单位。 > 卖第一个单位获得 $a_i + s_i$ 收益,后续每个单位获得 $a_i$ 收益。 > 有 $n$ 种蔬菜,第 $i$ 种有 $c_i$ 个单位。> 卖第一个单位获得 $a_i + s_i$ 收益,后续每个单位获得 $a_i$ 收益。> 每天最多卖 $m$ 个单位,问销售 $p$ 天最多获得多少收益。
## My Idea由于蔬菜不会变质(**“优质”**保鲜膜),我们只需**选择收益最高的若干单位**来卖,而不需要考虑时间顺序。48 行未改动};
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;}```
27 行未改动 队列:`[(3,菜1,2), (2,菜2,7)]`3. 取 `(3,菜1,2)` → 卖1个,收益+3,剩余1个 队列:`[(3,菜1,1), (2,菜2,7)]`
第1天收益:$7+6+3=16$ 第1天收益:$7+6+3=16$`ans[1]=16`
---
3 行未改动 队列:`[(2,菜2,7)]`2. 取 `(2,菜2,7)` → 卖2个,收益+4,剩余5个 队列:`[(2,菜2,5)]`
第2天收益:$16+3+4=23$ 第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$ 第3天收益:$23+6=29$`ans[3]=29`
---
21 行未改动