Atcoder ABC390E Vitamin Balance 题解 [ 绿 ] [ 背包 ] [ 二分 ]
Vitamin Balance:比较板的背包。
思路
一个 dp 数组里同时存三种食物的最大维他命显然不可行,因为一种食物维他命最多不代表其他维他命也同样多,而最终的价值取决于维他命最少的那个,所以这种思路不可行。
因此我们考虑对每一种食物计算给它 \(x\) 的体积下最大的维他命数量,这个显然可以背包去做。然后因为要求三个里的最小值最大,所以这个最小值显然有单调性,考虑二分最小值,在二分 check 的内部再套一个二分,找出第一个维他命大于等于这个维他命最小值的体积,然后全部加在一起,判断是否小于等于总体积即可完成 check。
时间复杂度 \(O(nx+\log V \log x)\)。其中 \(V\) 为答案的值域。
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (p<<1)
#define rc ((p<<1)|1)
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using pi=pair<int,int>;
int n,m;
ll dp[5][5005];
vector<pi>a[5];
void do_dp(int id)
{
for(auto x:a[id])
{
int w=x.fi,v=x.se;
for(int j=m;j-v>=0;j--)
{
dp[id][j]=max(dp[id][j],dp[id][j-v]+w);
}
}
}
bool check(ll mn)
{
int ans1=(lower_bound(dp[1]+1,dp[1]+m+1,mn)-dp[1]);
int ans2=(lower_bound(dp[2]+1,dp[2]+m+1,mn)-dp[2]);
int ans3=(lower_bound(dp[3]+1,dp[3]+m+1,mn)-dp[3]);
if(ans1+ans2+ans3<=m)return 1;
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
int v,ax,c;
cin>>v>>ax>>c;
a[v].push_back({ax,c});
}
do_dp(1);
do_dp(2);
do_dp(3);
ll l=0,r=2e9,mid;
while(l<r)
{
mid=(l+r+1)>>1;
if(check(mid))l=mid;
else r=mid-1;
}
cout<<l;
return 0;
}
Atcoder ABC390E Vitamin Balance 题解 [ 绿 ] [ 背包 ] [ 二分 ]的更多相关文章
- AtCoder ExaWizards 2019 简要题解
AtCoder ExaWizards 2019 简要题解 Tags:题解 link:https://atcoder.jp/contests/exawizards2019 很水的一场ARC啊,随随便便就 ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 184 题解
AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...
- Inviting Friends(hdu3244 && zoj3187)完全背包+二分
Inviting Friends Time Limit: 1 Second Memory Limit: 32768 KB You want to hold a birthday party, invi ...
- 【LeetCode题解】530_二分搜索树的最小绝对值差
目录 [LeetCode题解]530_二分搜索树的最小绝对值差 描述 方法一.中序遍历二分搜索树 思路 Java 代码 Python 代码 [LeetCode题解]530_二分搜索树的最小绝对值差 描 ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 172 题解
AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...
- AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...
随机推荐
- docker之基本命令(2)
上一篇介绍了一下,docker使用的有些基础命令.这次再来深入的了解一下较为复杂的docker命令 管理指令 说明 docker container 用于管理容器 docker image 用于 ...
- PythonDay5Advance
PythonDay5Advance 函数和模块 main函数要有,用户自己选择要做的功能,根据选择调用不同的函数 用户注册的信息需要使用一个文件存储,登录需要判断用户是否存在,密码是否正确 注册的时候 ...
- C# 获取系统盘符
1.使用.net管理对象(引入System.Management) public static List<string> getDisk() { WqlObjectQuery wmique ...
- 拥抱云原生,数据湖加速器 GooseFS 助力 Fluid 数据缓存实现
01 前言 数据湖加速器 GooseFS 是由腾讯云推出的高性能.高可用.弹性的分布式缓存方案.依靠对象存储(Cloud Object Storage,COS)作为数据湖存储底座的成本优势,为数据湖 ...
- Flutter之GetX之路由管理
GetX之路由管理 GetX有一套完整的路由管理,并且不需要context上下文,API非常简洁 直接导航 导航到新的页面 Get.to(NextScreen()); 返回,此方法可以用于关闭Snac ...
- IOS获取蓝牙状态
IOS获取蓝牙状态 监听蓝牙状态 在Link Binaries With Libraries中添加CoreBluetooto.framework 创建CBCentralManager对象 为了避免每次 ...
- GraphQL Part V: 字段,参数和变量
字段 我们对字段已经有了好的起点,我们在 HelloWorldQuery 中有两个字段:hello 和 world.他们都是单值字段. 现在我们可以扩展应用来支持复杂类型.例如,我们想象一下,我们在创 ...
- K8S deployment 重启的三种方法
一般重启deployment,常规操作是删掉对应的pod, 但如果有多个副本集的话,一个个删很麻烦.除了删除pod,还可以: 方案一: 加上环境变量kubectl patch deploy <d ...
- Windows 记录开机后应用启动慢的问题
最近大屏产品经常报一些开机启动的问题,工厂反馈厂测软件有些模块测试不通过,家里开发测试均发现Launcher等软件首次启动需要加载10多秒. 经过小伙伴们排查,发现是刷母盘后首次开机问题概率比较大.使 ...
- Qt开发经验小技巧146-150
Qt中自带的很多控件,其实都是由一堆基础控件(QLabel.QPushButton等)组成的,比如日历面板 QCalendarWidget 就是 QToolButton+QSpinBox+QTable ...