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 ...
随机推荐
- 【Oracle】Oracle读取RAW二进制类型并实现与十六进制的相互转换
1.十六进制转二进制 select HEXTORAW('7264B1CD0582734D8E27E0FBDA15B2A5') from dual; 2.二进制转十六进制 select AUUID_0, ...
- bug处理记录:Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512M; support was removed in 8.0
1. 报错: Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512M; support was removed ...
- <五>模板的完全特例化和非完全特例化
模板作为C++泛型编程的基础十分重要,其使得一份代码能用于处理多种数据类型.而有些时候,我们会希望对一些特定的数据类型执行不同的代码,这时就需要使用模板特例化(template specializat ...
- Python异步爬虫(aiohttp版)
异步协程不太了解的话可以去看我上篇博客:https://www.cnblogs.com/Red-Sun/p/16934843.html PS:本博客是个人笔记分享,不需要扫码加群或必须关注什么的(如果 ...
- Python 缩进语法的起源:上世纪 60-70 年代的大胆创意!
上个月,Python 之父 Guido van Rossum 在推特上转发了一篇文章<The Origins of Python>,引起了我的强烈兴趣. 众所周知,Guido 在 1989 ...
- PHP7.2 装mongodb 遇到的坑,完美解决!
公司要做QA安全测试,组长就丢了一个源码包给我,什么资料都无. 系统是个Laravel框架,源码都是从线上git下来.然后看了本地composer.json 没有生成vendor 第一步安装 comp ...
- week_9(推荐系统)
Andrew Ng 机器学习笔记 ---By Orangestar Week_9(推荐系统) 1. Problem Formulation 这节就仅仅简单地介绍了一下 推荐系统的应用和实例.完全可以略 ...
- git使用与代码托管
平时自己写的简单程序文件太多,可以放到代码托管的网站.比如国内的gitee.com, 好吧,只是把这个网站当网络云盘用了.在gitee网站上加上程序运行环境,使用文档,写好README.md使用介绍. ...
- C++可执行文件绝对路径获取与屏蔽VS安全检查
:前言 前几天写新项目需要用到程序所在绝对路径的时候,发现网上居然一点相关分享都没有. :_pgmptr 翻箱倒柜找出了几本教程,发现了一个叫_pgmptr的东西. 进去看了一下,在stdlib.h里 ...
- Young's theorem杨氏定理
杨氏定理 定理叙述 参考百度百科. Young's Theorem: Let \(f\) be a differentiable function of \(n\) variables. If eac ...