CF335F Buy One, Get One Free
\(\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的更多相关文章
- CF335F Buy One, Get One Free 贪心
题意: \(n\)个物品,每个物品有一个价格,买一个高价格的物品,可以选择免费得到一个价格严格低于这个物品的物品.求得到\(n\)个物品的最小代价. 题解: 神仙贪心-- 题目要求求出最小代价,相当于 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Best Time to Buy and Sell Stock1,2,3,4
找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- 关于解决scapy.error.Scapy_Exception: tcpdump is not available. Cannot use filter !报错
解决办法 sudo apt install tcpdump 后续 我特意没写到我的 arp 攻击那篇文章里面,就是为了水一片文章
- 记一次windows10电脑连上wifi无法上网的解决问题
前言 今天下午同学的电脑能连上wifi,但是却上不了互联网 开始思考 首先想到的肯定是WiFi驱动问题,但是wifi能连上大部分的原因可能就不是驱动问题了,为了彻底排除这个因素,我用到了我的usb网卡 ...
- DTSE Tech Talk 第13期:Serverless凭什么被誉为未来云计算范式?
摘要:在未来,云上交付模式会逐步从Serverful为主转向Serverless为主. 本文分享自华为云社区<DTSE Tech Talk 第13期:Serverless凭什么被誉为未来云计算范 ...
- 【JUC】信号量Semaphore详解
欢迎关注专栏[JAVA并发] 欢迎关注个人公众号-- JAVA旭阳 前言 大家应该都用过synchronized 关键字加锁,用来保证某个时刻只允许一个线程运行.那么如果控制某个时刻允许指定数量的线程 ...
- JavaScript入门⑩-ES6归纳总结
JavaScript入门系列目录 JavaScript入门①-基础知识筑基 JavaScript入门②-函数(1)基础{浅出} JavaScript入门③-函数(2)原理{深入}执行上下文 JavaS ...
- Django视图层/FBV与CBV/jsonResponse对象/form表单上传文件/request对象获取文件
目录 虚拟环境 视图层views 三板斧 jsonResponse对象 form如何上传文件/request对象获取文件 request对象方法 FBV与CBV CBV源码解析 虚拟环境 每创建一个虚 ...
- MySQL字符编码、存储引擎、严格模式、字段类型之浮点 字符串 枚举与集合 日期类型
目录 字符编码与配置文件 数据路储存引擎 创建表的完整语法 字段类型之整型 严格模式 字段类型之浮点型 字段类型之字符串类型 数字的含义 字段类型之枚举与集合 字段类型之日期类型 字符编码与配置文件 ...
- 解决Win10更新后远程桌面提示CredSSP加密Oracle修正的问题
1.以管理员身份打开cmd,运行命令提示符:2.执行REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\Cre ...
- [0x12] 132.小组队列
题意 link(more:UVA540) 简化题意:对 \(n\) 个小组排队,每个小组有至多 \(m\) 个成员(每个成员有唯一编号 \(x\)),当一个人来到队伍时,如果队中有同组成员,直接插入其 ...
- Zookeeper详解(02) - zookeeper安装部署-单机模式-集群模式
Zookeeper详解(02) - zookeeper安装部署-单机模式-集群模式 安装包下载 官网首页:https://zookeeper.apache.org/ 历史版本下载地址:http://a ...