\(\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. 安装mySql 出现 one more product requirements have not been satisified

    安装mySql 出现 one more product requirements have not been satisified 原因是缺少一些依赖环境. 在弹出的对话框中点击 否. 然后点击执行, ...

  2. windows安装grunt时提示不是内部或外部命令解决方案

    参考:https://www.cnblogs.com/hts-technology/p/8477258.html 安装windows安装elasticsearch-head时 不需要输入grunt s ...

  3. 【Java SE】Day02 数据类型转换、运算符、方法入门

    一.数据类型转换 1.自动转换 取值范围小在运算时会提升为取值范围大的类型 byte+int=int int+double=double 转换规则:byte.short.char-->int-- ...

  4. http 缓存 笔记

    http 缓存,有时候静态资源没更新的情况下,不需要每次都去服务器获取,减少资源的请求. Http 报文中与缓存相关的首部字段 1. 通用首部字段(就是请求报文和响应报文都能用上的字段) 2. 请求首 ...

  5. Django连接数据库(mysql)与Django ORM实操(增删改查) 前端页面

    目录 一:pycharm链接数据库(MySQL) 1.pycharm右侧Database工具栏 2.下载对应的驱动,填写MySQL数据库信息连接(当前为客户端) 3.pycharm连接MySQL数据库 ...

  6. docker registry(私库)搭建,使用,WEB可视化管理部署

    Docker Registry 是Docker官方一个镜像,可以用来储存和分发Docker镜像.目前比较流行的两个镜像私库是Docker Registry ,HarBor 其中HarBor最合适企业级 ...

  7. 单例模式及pickle序列化模块

    内容回顾 目录 内容回顾 单列模式实现的多种方式 pickle序列化模块 根据类名或对象名如何获取到类的字符串名 选课系统需求分析 功能提炼 选课系统架构设计 选课系统目录搭建 单列模式实现的多种方式 ...

  8. Young's theorem杨氏定理

    杨氏定理 定理叙述 参考百度百科. Young's Theorem: Let \(f\) be a differentiable function of \(n\) variables. If eac ...

  9. AspNetCore底层源码剖析(三)IOC

    title: AspNetCore底层源码剖析(三)IOC date: 2022-09-21 13:20:01 categories: 后端 tags: - .NET 介绍 每个 ASP.NET Co ...

  10. [Unity]限制一个值的大小(Clamp以及Mathf)

    如何限制一个物体的运动范围? 代码实例 public float xMin, xMax, zMin, zMax; rigidbody.position = new Vector3( Mathf.Cla ...