Merchant
\(get\)二分新用法。
每道题都有答案范围提示,以前只是以为是用来提示用什么类型输出的。
从来没想过直接用它来二分。
这道题真的刷新了我的认知啊。。。。。。
整道题的复杂度是\(O(nlog1e9)\)。
具体做法是,先\(check\)一下0时刻满不满足要求,如果不满足再进行二分。
关于他为什么可以二分:
可以知道,价值是时间的一次函数,对于任意一个选中的物品的集合,我们将他们\(k、b\)加和,就可以得到对应的斜率与截距,并且,我们只关心同一个\(t\)下的最大值,画出图像,可以发现他是单调增/单调减/单谷函数。
两种单调函数显然可以二分。
单谷函数当左边可以满足要求时,一定会在\(check0\)时就得出答案。
能够二分时,满足要求的点一定在右边,所以当\(check\)为\(false\)时,直接缩下界即可。
进行\(check\)时先用二分到的\(t\)计算出价值,然后将前\(m\)个最大的加和,看是不是大于\(s\)。
Code
#include<bits/stdc++.h>
using namespace std;
namespace STD
{
#define rr register
typedef long long ll;
const int N=1e6+4;
ll n,m,s;
ll w[N];
struct node{ll k,b;}a[N];
ll read()
{
rr ll x_read=0,y_read=1;
rr char c_read=getchar();
while(c_read<'0'||c_read>'9')
{
if(c_read=='-') y_read=-1;
c_read=getchar();
}
while(c_read<='9'&&c_read>='0')
{
x_read=(x_read<<3)+(x_read<<1)+(c_read^48);
c_read=getchar();
}
return x_read*y_read;
}
bool cmp(const ll a_,const ll b_){return a_>b_;}
bool check(int t)
{
for(rr int i=1;i<=n;i++)
w[i]=a[i].k*t+a[i].b;
nth_element(w+1,w+1+m,w+1+n,cmp);
ll sum=0;
for(rr int i=1;i<=m;i++)
{
if(w[i]<0) continue;
if(sum+w[i]>=s) return 1;
sum+=w[i];
}
return 0;
}
};
using namespace STD;
int main()
{
n=read(),m=read(),s=read();
for(rr int i=1;i<=n;i++)
a[i].k=read(),a[i].b=read();
if(check(0))
{
cout<<0<<'\n';
return 0;
}
int l=1,r=1e9;
while(l<r)
{
int mid=(l+r)>>1;
if(!check(mid)) l=mid+1;
else r=mid;
}
printf("%d\n",l);
}
2021-08-09 20:34:26 星期一
Merchant的更多相关文章
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
- POJ 3278 The merchant
传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there i ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...
- ThoughtWorks Merchant's Guide To The Galaxy
ThoughtWorks笔试题之Merchant's Guide To The Galaxy解析 一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天H ...
- [POJ 3728]The merchant
Description There are N cities in a country, and there is one and only one simple path between each ...
- opencart3图片Google Merchant Center验证通过不了的解决方法
最近在做一个opencart项目,有对接Google Merchant Center,但是一直提示产品图片验证无法通过,ytkah看了一下图片路径,/image/cache/catalog/demo/ ...
- 如何查看卖家ID (Merchant ID) 亚马逊哪里找?
如何查看卖家ID (Merchant ID) 亚马逊哪里找? 如何查看卖家ID (Merchant ID) 亚马逊哪里找? 1. 找到想要获取ID的卖家,点击店铺名(跟卖的卖家会收在”Other Se ...
- poj3728The merchant 【倍增】【LCA】
There are N cities in a country, and there is one and only one simple path between each pair of citi ...
- POJ 3728 The merchant(LCA+DP)
The merchant Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- 雅礼集训 Day6 T1 Merchant 解题报告
Merchant 题目描述 有\(n\)个物品,第\(i\)个物品有两个属性\(k_i,b_i\),表示它在时刻\(x\)的价值为\(k_i\times x+b_i\). 当前处于时刻\(0\),你可 ...
随机推荐
- @FeignClient常用属性
@FeignClient(name = "gateway-test", value = "gateway-test", url = "localhos ...
- 关于win7+cenos 7双系统安装
---恢复内容开始--- 1,cenos 0 7制作U盘启动 制作工具 http://pan.baidu.com/s/1nv9lpmp 镜像自备 2,安装centos 7 释放磁盘空间,如:20G.用 ...
- 『go成长之路』 defer 作用、典型用法以及多个defer调用顺序,附加defer避坑点,拿来吧你
预习内容 defer 的作用有哪些? 多个 defer 的执行顺序是怎样的? defer,return,函数返回值 三者之间的执行顺序 defer的作用 go中的defer是延迟函数,一般是用于释放资 ...
- 小白学习vue第五天-第二弹(全局局部、父子、注册语法糖,script/template抽离模板)
全局组件: 就是注册的位置在实例对象的外面 并且可以多个实例对象使用 而局部: 就是在实例对象的内部注册 父组件和子组件的关系 子组件就是在另一个组件里面注册的组件 组件注册语法糖: 就不用Vue.e ...
- lerna 常用命令
lerna 介绍 lerna 处理机构 固定模式(fixed) 所有包是统一的版本号,每次升级,所有包版本统一更新,不管这个包内容改变与否 具体体现在,lerna 的配置文件 lerna.json 中 ...
- Linux命令(三)vim编辑器的常用命令
.subTitle { background: rgba(51, 153, 0, 0.53); border-bottom: 1px solid rgba(0, 102, 0, 1); border- ...
- Notes about WindowPadX
WindowPadX乃一Autohotkey脚本,具有强大的单/多显示器窗口排布能力且易于配置.有了它,那些Pro版收费的.需要安装的DisplayFusion, MultiMon TaskBar, ...
- 关于解决numpy使用sklearn时的警告问题
关于解决numpy使用sklearn时的警告问题 在使用的时候,出现提示 :219: RuntimeWarning: numpy.ufunc size changed, may indicate bi ...
- 将JDK默认编码设置为UTF-8
此博文非原创:参考小兵qwer https://blog.csdn.net/xiaobing_122613/article/details/70209716 只是想留下对自己有用的东西,同时帮助更 ...
- NOIP 模拟 $24\; \rm graph$
题解 \(by\;zj\varphi\) 首先一个点能否选择的条件是 \(dis_{1,x}+dis_{x,n}=dis_{1,n}\) 正解是计算一条道路上的所有为 \(-1\) 边的选择范围,是个 ...