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\),你可 ...
随机推荐
- 一文彻底搞清 Gradle 依赖【转】
来源:曾是放牛娃 www.jianshu.com/p/59fd653a54d2 转自:https://mp.weixin.qq.com/s?__biz=MzA3MDMyMjkzNg==&mid ...
- Sth about Educational DP Contest
Contest Website : atcoder.jp/contests/dp \[\begin{array}{c|C|c|c} TaskNum & TaskName & Statu ...
- 用 区间判断(if)来猜价格的高低
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int price = 150; 6 int gue ...
- Netty:简单使用
Netty是什么东西 Netty是一个封装很好的异步事件驱动框架,让我们快速的部署服务端和客户端的网络应用,进行异步IO通信. 1.什么是IO通信IO就是input 和 output,是一种在两台主机 ...
- 关于c++ STL map 和 unordered_map 的效率的对比测试
本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...
- iNeuOS工业互网平台,在纸业领域的成功应用案例
目 录 1. 项目背景... 2 2. 项目基本情况... 3 3. 概念解释... 5 1. 项目背景 最终用户是全国第5大纸业集团之一,年浆纸产能40 ...
- 01-ADB命令获取包名、界面名
命令代码 adb shell dumpsys window |findstr mFocusedApp
- Spring中的@Transactional必须要了解的概念
spring中的@Transactional基于动态代理的机制,提供了一种透明的事务管理机制,方便快捷解决在开发中碰到的问题. 一般使用是通过如下代码对方法或接口或类注释: 1 @Transactio ...
- null的坑 和 比较运算符、相等运算符的隐式转换问题 (在javascript中,null>=0 为真,null<=0 为真,null==0却为假,null到底是什么?)
null在关系运算中的坑 & 关系运算符的隐式转换问题 注意: 比较运算符 和 相等运算符 的 ECMAscript 语法实现不同. 比较运算符 和 相等运算符 对数据进行了隐式转换, 相当于 ...
- 在JavaScript中安全访问嵌套对象
大多数情况下,当我们使用JavaScript时,我们将处理嵌套对象,并且通常我们需要安全地访问最里面的嵌套值. 比如: const user = { id: 101, email: 'jack@dev ...