Solution -「CTSC 2018」「洛谷 P4602」混合果汁
\(\mathcal{Description}\)
Link.
\(n\) 种果汁,第 \(i\) 种美味度为 \(d_i\),每升价格 \(p_i\),一共 \(l_i\) 升。\(m\) 组询问,给定花费上限 \(g\) 和果汁需求量 \(L\),求混合多种果汁以满足要求时,所用果汁最小美味度的最大值。
\(n,m,p_i\le10^5\)。
\(\mathcal{Solution}\)
最小值最大,显然二分。
需要 check:能否用美味度不小于 \(mid\) 的果汁混合出 \(L\) 升,使得价格不超过 \(g\)。
没有美味度的限制,贪心地用单价更低的果汁就好啦!
回归到原问题,以按美味度降序排列后的果汁编号为版本轴建主席树,树是以单价为下标的权值线段树。外层二分出 \(mid\),再在以 \(mid\) 为根的树上走,贪心地购买果汁(先买左子树,不够再去右子树)。
就完了 qwq。复杂度 \(\mathcal O(n\log^2n)\)。
\(\mathcal{Code}\)
#include <cstdio>
#include <algorithm>
const int MAXN = 1e5;
int n, m, root[MAXN + 5];
typedef long long LL;
inline LL rint () {
LL x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
}
struct Juice {
int d, p, l;
inline void read () { d = rint (), p = rint (), l = rint (); }
inline bool operator < ( const Juice t ) const { return d > t.d; }
} juice[MAXN + 5];
struct PersistentSegmentTree {
static const int MAXND = MAXN * 40;
int cntnd, ch[MAXND + 5][2];
LL sum[MAXND + 5], prc[MAXND + 5];
inline void build ( int& rt, const int l, const int r ) {
rt = ++ cntnd;
if ( l == r ) return ;
int mid = l + r >> 1;
build ( ch[rt][0], l, mid ), build ( ch[rt][1], mid + 1, r );
}
inline void pushup ( const int rt ) {
sum[rt] = sum[ch[rt][0]] + sum[ch[rt][1]];
prc[rt] = prc[ch[rt][0]] + prc[ch[rt][1]];
}
inline void insert ( int& rt, const int l, const int r, const int p, const int v ) {
int old = rt, mid = l + r >> 1; rt = ++ cntnd;
ch[rt][0] = ch[old][0], ch[rt][1] = ch[old][1], sum[rt] = sum[old], prc[rt] = prc[old];
if ( l == r ) return sum[rt] += v, prc[rt] += 1ll * v * p, void ();
if ( p <= mid ) insert ( ch[rt][0], l, mid, p, v );
else insert ( ch[rt][1], mid + 1, r, p, v );
pushup ( rt );
}
inline LL buy ( const int rt, const int l, const int r, LL money, LL need ) {
if ( sum[rt] < need || money < 0 ) return -1;
if ( l == r ) return need * l <= money ? need * l : -1;
int mid = l + r >> 1;
if ( sum[ch[rt][0]] >= need ) return buy ( ch[rt][0], l, mid, money, need );
else {
LL t = buy ( ch[rt][1], mid + 1, r, money - prc[ch[rt][0]], need - sum[ch[rt][0]] );
return ~ t ? t + prc[ch[rt][0]] : -1;
}
}
} pst;
int main () {
n = rint (), m = rint ();
int mxp = 0;
for ( int i = 1; i <= n; ++ i ) {
juice[i].read ();
if ( mxp < juice[i].p ) mxp = juice[i].p;
}
std::sort ( juice + 1, juice + n + 1 );
pst.build ( root[0], 1, mxp );
for ( int i = 1; i <= n; ++ i ) {
pst.insert ( root[i] = root[i - 1], 1, mxp, juice[i].p, juice[i].l );
}
for ( LL g, L; m --; ) {
g = rint (), L = rint ();
int l = 1, r = n;
LL ans = -1, tmp;
while ( l <= r ) {
int mid = l + r >> 1;
if ( ~ pst.buy ( root[mid], 1, mxp, g, L ) ) r = ( ans = mid ) - 1;
else l = mid + 1;
}
printf ( "%d\n", ~ ans ? juice[ans].d : -1 );
}
return 0;
}
Solution -「CTSC 2018」「洛谷 P4602」混合果汁的更多相关文章
- LOJ 2555 & 洛谷 P4602 [CTSC2018]混合果汁(二分+主席树)
LOJ 题目链接 & 洛谷题目链接 题意:商店里有 \(n\) 杯果汁,第 \(i\) 杯果汁有美味度 \(d_i\),单价为 \(p_i\) 元/升.最多可以添加 \(l_i\) 升.有 \ ...
- 洛谷P4602 [CTSC2018]混合果汁(主席树)
题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 nn 种果汁,编号为 0,1,\cdots,n-10,1,⋯,n−1 . ii 号果汁的美味度是 d_idi ,每升价格为 p_ipi ...
- [洛谷P4602] CTSC2018 混合果汁
问题描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 n 种果汁,编号为 0, 1, 2, . . . , n − 1.i 号果汁的美味度是 di,每升价格为 pi.小 R 在制作混合果汁时 ...
- 「区间DP」「洛谷P1043」数字游戏
「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...
- Solution -「JSOI 2019」「洛谷 P5334」节日庆典
\(\mathscr{Description}\) Link. 给定字符串 \(S\),求 \(S\) 的每个前缀的最小表示法起始下标(若有多个,取最小的). \(|S|\le3\time ...
- Solution -「洛谷 P4372」Out of Sorts P
\(\mathcal{Description}\) OurOJ & 洛谷 P4372(几乎一致) 设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...
- Solution -「POI 2010」「洛谷 P3511」MOS-Bridges
\(\mathcal{Description}\) Link.(洛谷上这翻译真的一言难尽呐. 给定一个 \(n\) 个点 \(m\) 条边的无向图,一条边 \((u,v,a,b)\) 表示从 ...
- Solution -「APIO 2016」「洛谷 P3643」划艇
\(\mathcal{Description}\) Link & 双倍经验. 给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...
- 「洛谷4197」「BZOJ3545」peak【线段树合并】
题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...
随机推荐
- Win10如何更改C:\Users\下的用户名
详细操作步骤博文原址 : https://blog.csdn.net/wls666/article/details/103334152 但是,改完后会出现报错 这是微软应用商城出现问题 ,每次开机 ...
- gitlab新增ssh
https://blog.csdn.net/u011925641/article/details/79897517
- STM32寄存器深入分析
可能很多刚开始学习STM32的小伙伴都有一个疑惑,创建项目时会需要很多头文件,导致学习过程中很难明白那些头文件的作用,虽然知道头文件都是对寄存器的封装,但是怎么封装的就不知道了.这里我以led灯为试验 ...
- Visual Studio 2015 MFC之Button颜色变化-断点调试(Debug)
软件开发,对自己的程序进行调试很重要,本次文章在上一边随笔的基础上,介绍一下Button控件做显示灯的用法,Button控件的添加和变量设置等可以参考下面的的链接:Visaul Studio 2015 ...
- IDEA2020.1破解
IDEA2020.1破解 安装 下载idea idea官方下载地址:https://www.jetbrains.com/webstorm/download/other.html 下载破解插件 链接:h ...
- /etc/crontab和crontab -e的区别
(1) /etc/crontab是系统级别的crontab,系统的设置等,这种方法只有root用户能用 crontab -e是用户级的crontab,会被写到 /var/spool/cron 目录下, ...
- 源码安装gitlab
GitLab服务构成 GitLab由以下服务构成: nginx:静态Web服务器 gitlab-shell:用于处理Git命令和修改authorized keys列表 gitlab-workhor ...
- python 小兵(6)函数进阶
阅读目录 函数参数-动态参数 名称空间 函数的嵌套 gloabal.nonlocal 回到顶部 函数参数-动态参数 之前我们说过传参,如果我们在传参数的时候不很清楚有哪些的时候,或者说给一个函数传了很 ...
- APschedule定时任务
APScheduler是Python的一个定时任务框架,可以很方便的满足用户定时执行或者周期执行任务的需求, 它提供了基于日期date.固定时间间隔interval .以及类似于Linux上的定时任务 ...
- 初识 oracle!
/** * 一.oracle的简介? * 1.是一个关系型数据库,强大! * * 软件名 开发商 用途 * * oracle oracle 专门的软件公司 收费!1.连接的用户数,2.服务器的cpu的 ...