\(\text{Solution}\)

其实不想写(因为不好写。。。

所以贴贴 \(\text{Solution}\)

然而就关于这题而言讲得也不太清楚

可撤销贪心就是维护当前状态的最优解,同时考虑以后的反悔

顺着题解,本题在 \(k<val\) 时必然换一种购买方式

而当 \(k\ge val\) 时,有两种方式,一种可能使当前解更优,但另一种却可以留下两次免费获取的机会

所以令 \(res=2val-k\),压入堆中,对应当前解更优

而 \(res\) 以后可以被弹出,更改之前的决策,在当前免费取两次进而获得当前更优解

然而贴 \(\text{Code}\) 的原因是本人手打了个堆(几千年以来的壮举

\(\text{Code}\)

#include <cstdio>
#include <iostream>
#include <algorithm>
#define IN inline
using namespace std;
typedef long long ll; const int N = 5e5 + 5;
int n, a[N], b[N], c[N], d[N]; IN void read(int &x) {
x = 0; char ch = getchar(); int f = 1;
for(; !isdigit(ch); f = (ch == '-' ? -1 : f), ch = getchar());
for(; isdigit(ch); x = (x<<3)+(x<<1)+(ch^48), ch = getchar());
x *= f;
} struct Heap {
int d[N], siz;
IN Heap() {siz = 0;}
IN void Up(int x) {while (x > 1 && d[x] < d[x >> 1]) swap(d[x], d[x >> 1]), x >>= 1;}
IN void Down(int x) {
int y = (x << 1);
while ((y <= siz && d[x] > d[y]) || (y < siz && d[x] > d[y + 1])) {
if (y < siz && d[y] > d[y + 1]) ++y;
swap(d[x], d[y]), x = y, y = (x << 1);
}
}
IN int size() {return siz;}
IN int top() {return d[1];}
IN void pop() {d[1] = d[siz--], Down(1);}
IN void push(int x) {d[++siz] = x, Up(siz);}
}Q; int main() {
read(n);
ll ans = 0;
for(int i = 1; i <= n; i++) read(b[i]), ans += b[i];
sort(b + 1, b + n + 1), reverse(b + 1, b + n + 1);
int m = 0;
for(int i = 1, j; i <= n; i = j + 1) {
j = i;
while (j < n && b[j + 1] == b[i]) ++j;
a[++m] = b[i], c[m] = j - i + 1;
}
int num = 0;
for(int i = 1; i <= m; num += c[i++]) {
int rest = min(num - Q.size() * 2, c[i]), cnt = 0;
for(int j = 1; j <= rest; j++) d[++cnt] = a[i];
rest = c[i] - rest;
for(int j = 1; j <= rest; j += 2) {
if (!Q.size()) break;
int k = Q.top(); Q.pop();
if (k < a[i]) {d[++cnt] = a[i]; if (j < rest) d[++cnt] = a[i];}
else {d[++cnt] = k; if (j < rest) d[++cnt] = a[i] * 2 - k;}
}
for(int j = 1; j <= cnt; j++) if (d[j] >= 0) Q.push(d[j]);
}
while (Q.size()) ans -= Q.top(), Q.pop();
printf("%lld\n", ans);
}

CF335F Buy One, Get One Free的更多相关文章

  1. CF335F Buy One, Get One Free 贪心

    题意: \(n\)个物品,每个物品有一个价格,买一个高价格的物品,可以选择免费得到一个价格严格低于这个物品的物品.求得到\(n\)个物品的最小代价. 题解: 神仙贪心-- 题目要求求出最小代价,相当于 ...

  2. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. Best Time to Buy and Sell Stock1,2,3,4

    找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...

  8. [LeetCode] Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 4 Best Time to Buy and Sell Stock III_Leetcode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 第1章-Spring的模块与应用场景

    目录 一.Spring模块 1. 核心模块 2. AOP模块 3. 消息模块 4. 数据访问模块 5. Web模块 6. 测试模块 二.集成功能 1. 目标原则 2. 支持组件 三.应用场景 1. 典 ...

  2. vuex环境配置及使用

    vuex环境搭建 1.下载vuex,如果你是开发Vue2,请下载vuex3版本 npm i vuex@3 2.搭建vuex的环境配置 ​ 创建 src/state/index.js 文件 //导入Vu ...

  3. 设计链表-LeetCode707 基础题

    LeetCode链接:https://leetcode.cn/problems/design-linked-list/ 题目:设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属 ...

  4. js day04 综合案例秒数计算

    <script>         //用户输入总秒数         let second = +prompt('请输入总秒数:')         //计算时分秒         fun ...

  5. static_cast和dynamic_cast

    C++的强制类型转换,除了继承自C语言的写法((目标类型)表达式)之外,还新增了4个关键字,分别是:static_cast.dynamic_cast.const_cast和reinterpret_ca ...

  6. 13-flask博客项目之restful api详解2-使用

    13-flask博客项目之restful api详解1-概念 13-flask博客项目之restful api详解1-概念 Flask-RESTful学习网站 英文:https://flask-res ...

  7. 【nginx】代理设置Host

    旧文章从语雀迁移过来,原日期为2021-02-18 nginx 的 proxy 模块使我们经常会用到的模块之一,比如我们常用的 nginx 反向代理. 反向代理我们一般有这么几行配置代码: locat ...

  8. 【Markdown编辑器】语法规则

    一.Markdown介绍及工具推荐 1.介绍 Markdown是一种轻量级标记语言,它以纯文本形式(易读.易写.易更改)编写文档,并最终以HTML格式发布.Markdown也可以理解为将以MARKDO ...

  9. [常用工具] OpenCV获取网络摄像头实时视频流

    所需要硬件及软件环境: python 3/OpenCV3.4 or C++11/OpenCV3.4 1 RTSP协议 RTSP (Real Time Streaming Protocol),是一种语法 ...

  10. 基于云基础设施快速部署 RocketMQ 5.0 集群

    本文作者:蔡高扬,Apache RocketMQ Committer, 阿里云智能技术专家. 背景 上图左侧为 RocketMQ 4.x版本集群,属于非切换架构.NameServer 作为无状态节点可 ...