\(\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」混合果汁的更多相关文章

  1. LOJ 2555 & 洛谷 P4602 [CTSC2018]混合果汁(二分+主席树)

    LOJ 题目链接 & 洛谷题目链接 题意:商店里有 \(n\) 杯果汁,第 \(i\) 杯果汁有美味度 \(d_i\),单价为 \(p_i\) 元/升.最多可以添加 \(l_i\) 升.有 \ ...

  2. 洛谷P4602 [CTSC2018]混合果汁(主席树)

    题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 nn 种果汁,编号为 0,1,\cdots,n-10,1,⋯,n−1 . ii 号果汁的美味度是 d_idi​ ,每升价格为 p_ipi ...

  3. [洛谷P4602] CTSC2018 混合果汁

    问题描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 n 种果汁,编号为 0, 1, 2, . . . , n − 1.i 号果汁的美味度是 di,每升价格为 pi.小 R 在制作混合果汁时 ...

  4. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  5. Solution -「JSOI 2019」「洛谷 P5334」节日庆典

    \(\mathscr{Description}\)   Link.   给定字符串 \(S\),求 \(S\) 的每个前缀的最小表示法起始下标(若有多个,取最小的).   \(|S|\le3\time ...

  6. Solution -「洛谷 P4372」Out of Sorts P

    \(\mathcal{Description}\)   OurOJ & 洛谷 P4372(几乎一致)   设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...

  7. Solution -「POI 2010」「洛谷 P3511」MOS-Bridges

    \(\mathcal{Description}\)   Link.(洛谷上这翻译真的一言难尽呐.   给定一个 \(n\) 个点 \(m\) 条边的无向图,一条边 \((u,v,a,b)\) 表示从 ...

  8. Solution -「APIO 2016」「洛谷 P3643」划艇

    \(\mathcal{Description}\)   Link & 双倍经验.   给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...

  9. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

随机推荐

  1. 利用python绘制分析路易斯安那州巴吞鲁日市的人口密度格局

    前言 数据来源于王法辉教授的GIS和数量方法,以后有空,我会利用python来实现里面的案例,这里向王法辉教授致敬. 绘制普查人口密度格局 使用属性查询提取区边界 import numpy as np ...

  2. SYCOJ#1077、01字符串

    题目-01字符串 (shiyancang.cn) 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 ...

  3. Git 的基本命令的使用

    1.获得Git仓库(克隆一份代码到本地仓库) git clone url 2.更新本地的代码 git pull 3.查看本地修改的文件 git status 4.将本地的修改加到stage中 git ...

  4. 机器学习&恶意代码检测简介

    Malware detection 目录 可执行文件简介 检测方法概述 资源及参考文献 可执行文件简介 ELF(Executable Linkable Format) linux下的可执行文件格式,按 ...

  5. 《剑指offer》面试题68 - II. 二叉树的最近公共祖先

    问题描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q ...

  6. 顺序表-Go语言实现

    简单理解就是数组: 优缺点及使用场景 优点: 随机访问,在O(1)时间内找到第i个元素: 数据表中的数据是连续存放的,因此只要知道数据表中第一个元素的地址,那么后面的数据元素的地址就可以马上算出来. ...

  7. Solon Web 开发,十四、与Spring、Jsr330的常用注解对比

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  8. winform GDI+ 抗锯齿

    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

  9. 【刷题-LeetCode】190 Reverse Bits

    Reverse Bits Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 0000001010010100000 ...

  10. golang中如何将json文件解析成结构体

    package tool import ( "bufio" "encoding/json" "fmt" "os" ) t ...