传送门

可以发现如果我们最终选择的物品集合已经确定,就很好求了

\(\sum k*t+\sum b \geqslant s\) ,二分即可

但现在我们无法确定该选哪些物品

因此我们只需要check一下0时刻是否符合条件,如果不符合则进行二分。

注意check的时候我们只需要找出最大的 \(m\) 个即可

有点玄学。

证一下它有单调性:

因为保证有解,令存在一个解为时刻 \(t\)

那么此时存在一个 \(\sum k*t+\sum b \geqslant s\)

考虑时刻 \(t+1\),发现多了个 \(\sum k\)

若 \(\sum k > 0\) ,可以二分

若 \(\sum k \leqslant 0\) ,0时刻一定更优,不必二分

  • 有空复习下nth_element的使用

Code:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1000010
#define ll long long
#define reg register int
//#define int long long char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline ll read() {
ll ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
} int n, m; ll s;
ll k[N], b[N];
const double eps=1e-8; namespace force{
ll ans=(ll)(1e18);
void solve() {
int lim=1<<n;
double k1, b1, s1=s, t;
for (reg s=1,s2,cnt; s<lim; ++s) {
k1=0; b1=0; cnt=0; s2=s;
do {s2&=(s2-1); ++cnt;} while (s2);
if (cnt>m) continue;
for (reg i=0; i<n; ++i) if (s&(1<<i))
k1+=k[i+1], b1+=b[i+1];
t=(s1-b1)/k1;
//cout<<"t: "<<t<<' '<<bitset<5>(s)<<endl;
if (ceil(t)>=-eps && k1*ceil(t)+b1>=s1-eps) ans=min(ans, (ll)ceil(t));
if (floor(t)>=-eps && k1*floor(t)+b1>=s1-eps) ans=min(ans, (ll)floor(t));
}
printf("%lld\n", ans);
exit(0);
}
} namespace task1{
ll tem[N];
inline bool cmp(ll a, ll b) {return a>b;}
bool check(ll t) {
for (reg i=1; i<=n; ++i) tem[i]=k[i]*t+b[i];
sort(tem+1, tem+n+1, cmp);
ll sum=0;
for (reg i=1; i<=m; ++i)
if ((sum+=tem[i])>=s) return 1;
return 0;
}
void solve() {
ll l=0, r=(ll)(1e9), mid;
while (l<=r) {
mid=(l+r)>>1;
if (!check(mid)) l=mid+1;
else r=mid-1;
}
printf("%lld\n", l);
exit(0);
}
} namespace task{
ll tem[N];
inline bool cmp(ll a, ll b) {return a>b;}
bool check(ll t) {
//cout<<"check "<<t<<endl;
for (reg i=1; i<=n; ++i) tem[i]=k[i]*t+b[i];
nth_element(tem+1, tem+m, tem+n+1, cmp);
//cout<<"tem: "; for (int i=1; i<=n; ++i) cout<<tem[i]<<' '; cout<<endl;
ll sum=0;
for (reg i=1; i<=m; ++i) if (tem[i]>0 && (sum+=tem[i])>=s) return 1;
return 0;
}
void solve() {
for (int i=1; i<=n; ++i) tem[i]=b[i];
sort(tem+1, tem+n+1, cmp);
ll sum=0;
for (reg i=1; i<=m; ++i) if ((sum+=tem[i])>=s) {puts("0"); exit(0);}
ll l=0, r=(ll)(1e9), mid;
while (l<=r) {
mid=(l+r)>>1;
if (!check(mid)) l=mid+1;
else r=mid-1;
}
printf("%lld\n", l);
exit(0);
}
} signed main()
{
bool geq=1, leq=1;
n=read(); m=read(); s=read();
for (int i=1; i<=n; ++i) {
k[i]=read(); b[i]=read();
if (b[i]>=s) {puts("0"); return 0;}
if (k[i]>0) leq=0;
else if (k[i]<0) geq=0;
}
//if (n<=20) force::solve();
//else if (geq) task1::solve();
//else if (leq) {puts("0"); return 0;}
task::solve(); return 0;
}

题解 Merchant的更多相关文章

  1. [NOIP10.6模拟赛]1.merchant题解--思维+二分

    题目链接: while(1)gugu(while(1)) 闲扯 考场上怕T2正解写挂其他两题没管只打了暴力,晚上发现这题思维挺妙的 同时想吐槽出题人似乎热衷卡常...我的巨大常数现在显露无疑QAQ 分 ...

  2. [最近公共祖先] POJ 3728 The merchant

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 Desc ...

  3. [POJ 3728]The merchant

    Description There are N cities in a country, and there is one and only one simple path between each ...

  4. poj3728The merchant 【倍增】【LCA】

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  5. APIO2017伪题解

    题目质量还是比较高的,只是当时澳大利亚方面出了一点问题?最后造成了区分度非常迷的局面. 纵观三道题,T1是披着交互外衣的提答题,T2是披着交互外衣的传统题,T3是一道据说近年来APIO最水的一道传统题 ...

  6. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  7. [CSP-S模拟测试]:Merchant(二分答案)

    题目描述 有$n$个物品,第$i$个物品有两个属性$k_i,b_i$,表示它在时刻$x$的价值为$k_i\times x+b_i$.当前处于时刻$0$,你可以选择不超过$m$个物品,使得存在某个整数时 ...

  8. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  9. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

随机推荐

  1. STM32笔记一

    1.脉冲宽度调制是(PWM):用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术,广泛应用在从测量.通信到功率控制与变换的许多领域中.一般用于直流电机调速. 2.外部中断:外部中断是单片机实 ...

  2. RabbitMQ入门教程 [转]

    1.引言 RabbitMQ--Rabbit Message Queue的简写,但不能仅仅理解其为消息队列,消息代理更合适.消息队列主要解决应用耦合,异步消息,流量削锋等问题.实现高性能,高可用,可伸缩 ...

  3. 入门Kubernetes-minikube本地k8s环境

    前言: 在上一篇 结尾中使用到了minikube方式来做k8s本地环境来学习k8s. 那么这篇先了解下minikube及使用 一.Minikube 简介 minikube 在 macOS.Linux ...

  4. 题解 SP3591 PATHEADS - Patting Heads

    类似桶排 先看有多少头奶牛抽出这个数 再看这个数的奶牛能拍多少人的头(别忘了-1,自己不能拍自己) 最后根据输入输出 110ms #include<bits/stdc++.h> using ...

  5. 【Azure 环境】Azure通知中心(Notification Hub)使用百度推送平台解说

    问题描述 在通知中心的页面中显示支持BaiDu,介绍一下支持的是百度(Baidu)的什么吗?Azure的这个功能在国内使用的时候是否可以保证国内安卓手机的信息送达率? 问题解答 通知中心的页面中的Ba ...

  6. CPU 几核

    1.设备管理器:打开"处理器",出现几个就是几核

  7. [刘阳Java]_Spring AOP入门_第7讲

    AOP技术个人认为是能够完善(改善)面向对象编程OOP.为什么这么说,我们得先从AOP的概念说起,然后通过一段简单的例子加以佐证.这样子大家就可以慢慢地了解AOP 1. AOP概念 AOP为Aspec ...

  8. 队列Queue:任务间的消息读写,安排起来~

    摘要:本文通过分析鸿蒙轻内核队列模块的源码,掌握队列使用上的差异. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列十三 消息队列Queue>,作者:zhushy . 队列(Queue)是 ...

  9. js问题记录

    1.aixos请求响应302重定向时无法获取返回数据, 解决方法:在请求头中添加  headers: { 'X-Requested-With': 'XMLHttpRequest' },

  10. ajax 提交序列化表单

    1.提交序列化表单+参数: var a = $.param({'address':address,'delivity':delivity,'payment':payment}) + '&' + ...