\(\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. Azure DevOps Server 用户组加入 Azure AD Domain Service 管理用户

    一,引言 今天我们继续讲解 Azure DevOps Server 的内容,对于管理用户组除了在 Azure DevOps Server 服务器上添加管理员方式外,还有没有其他方式,Azure Dev ...

  2. Golang Gorm 封装 分页查询 Where Order 查询

    说说为什么写Gorm,因为公司新项目需要,研究了下Go下的gorm.对于一个项目首先考虑的问题,就是封装一些常用的工具方法,例如多参数查询 where or Like 还有order by Limit ...

  3. 如何查看计算机的CPU信息

    CPU-Z是一款家喻户晓的CPU检测软件,是检测CPU使用程度极高的一款软件.它支持的CPU种类相当全面,软件的启动速度及检测速度都很快.另外,它还能检测主板和内存的相关信息,其中就有我们常用的内存双 ...

  4. 如何优化大场景实时渲染?HMS Core 3D Engine这么做

    在先前举办的华为开发者大会2022(HDC)上,华为通过3D数字溪村展示了自有3D引擎"HMS Core 3D Engine"(以下简称3D Engine)的强大能力.作为一款高性 ...

  5. js获取当前年月日时分

    function GetDate(){ var now = new Date(); var year = now.getFullYear(); //年 var month = now.getMonth ...

  6. JavaScript:函数:如何声明和调用函数?

    首先,理解什么是函数? 通俗的说,函数就是用大括号括起来的一组JS语句的集合体,是一个代码块,表达一种行为逻辑. 当我们调用函数的时候,我们就是在执行这一组JS语句. 然后,确定一点,在JS中,函数也 ...

  7. Cryptohack的Adrien's Signs解题思路

    题目如下: 输出的结果: 题目分析: 在原题的题目描述中并没有什么有用的消息,更多的信息是通过代码审计出来的.大致意思是,先把字节flag转换为二进制形式的字符串,然后判断字符串中每个字符,如果为1, ...

  8. MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(1)-后端项目框架搭建

    前言: 前面的四个章节我们主要讲解了MongoDB的相关基础知识,接下来我们就开始进入使用.NET7操作MongoDB开发一个ToDoList系统实战教程. MongoDB从入门到实战的相关教程 Mo ...

  9. Linux基础:ssh与scp

    登陆 登陆服务器 ssh user@hostname user: 用户名 hostname :IP地址或域名 第一次登陆会提示 The authenticity of host '123.57.47. ...

  10. [常用工具] PyAutoGUI使用教程

    PyAutoGUI使用教程 目录 PyAutoGUI使用教程 1 基础知识 2 一般函数 3 故障保险 4 鼠标函数 4.1 鼠标移动 4.2 鼠标拖动 4.3 鼠标单击 4.4 鼠标滚动 4.5 鼠 ...