Description

Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales (1≤N≤100,0
00). The iith haybale has a certain flavor Fi (1≤Fi≤10^9) and a certain spiciness Si(1≤Si≤10^9).
The meal will consist of a single course, being a contiguous interval containing one or more consecu
tive haybales (Farmer John cannot change the order of the haybales). The total flavor of the meal is
 the sum of the flavors in the interval. The spiciness of the meal is the maximum spiciness of all h
aybales in the interval.Farmer John would like to determine the minimum spiciness his single-course 
meal could achieve, given that it must have a total flavor of at least MM (1≤M≤10^18).
给长度为n<=1e5的两个序列f[], s[] <= 1e9和一个long long M。
如果[1, n] 的一个子区间[a, b]满足 f[a]+f[a+1]+..+f[b] >= M, 我们就称[a, b]合法
,一个合法区间[a, b]的值为max(s[a], s[a+1], ..., s[b])。
让你求出可能的合法区间最小值为多少。
 

Input

The first line contains the integers N and M, 
the number of haybales and the minimum total flavor the meal must have, respectively. 
The next N lines describe the N haybales with two integers per line, 
first the flavor F and then the spiciness S.
 

Output

Please output the minimum spiciness in a single course meal that satisfies the minimum flavor requirement. 
There will always be at least one single-course meal that satisfies the flavor requirement.
 

Sample Input

5 10
4 10
6 15
3 5
4 9
3 6

Sample Output

9

Solution

开始敲了要2kb的线段树

然后过了之后幡然醒悟发现只要二分就好了

于是敲了一个代码量很短的二分,还跑快了一倍多

线段树做法

因为一段区间的值是一个$max$,所以我们只需要关心这个$max$

于是可以排序一下,按照每个点的权值来插入线段树里面

用线段树维护最大子段和

每次插入后查一下整棵树的最大子段和有没有超过$m$,超过直接输出当前插入的数的权值就行了

维护最大子段和的线段树的$pushup$写的是真的恶心

#include <cstdio>
#include <cstring>
#include <algorithm> #define N 100010
#define inf 0x3f3f3f3f
#define ll long long
#define lc ( rt << 1 )
#define rc ( rt << 1 | 1 ) using namespace std ; int n ;
ll m ;
struct node {
int s , f , id ;
} a[ N ] ;
struct tree {
int l , r ;
ll ls , rs , sum ;
bool flag ;
} t[ N << ] ; bool cmp( node a , node b ) {
return a.s < b.s ;
} void build( int l , int r , int rt ) {
t[ rt ].l = l ; t[ rt ].r = r ;
t[ rt ].ls = t[ rt ].rs = t[ rt ].sum = t[ rt ].flag = ;
if( l == r ) return ;
int mid = ( l + r ) >> ;
build( l , mid , lc ) ;
build( mid + , r , rc ) ;
} void pushup( int rt ) {
if( t[ lc ].flag && t[ rc ].flag ) {
t[ rt ].flag = ;
t[ rt ].sum = t[ rt ].ls = t[ rt ].rs = t[ lc ].sum + t[ rc ].sum ;
return ;
}
t[ rt ].ls = t[ lc ].ls ;
t[ rt ].rs = t[ rc ].rs ;
if( t[ lc ].flag ) t[ rt ].ls += t[ rc ].ls ;
if( t[ rc ].flag ) t[ rt ].rs += t[ lc ].rs ;
t[ rt ].sum = max( t[ lc ].sum , t[ rc ].sum ) ;
t[ rt ].sum = max( t[ rt ].sum , t[ lc ].rs + t[ rc ].ls ) ;
} void upd( int L , int val , int rt ) {
int l = t[ rt ].l , r = t[ rt ].r , mid = ( l + r ) >> ;
if( l == r ) {
t[ rt ].ls = t[ rt ].rs = t[ rt ].sum = val ;
t[ rt ].flag = ;
return ;
}
if( L <= mid ) upd( L , val , lc ) ;
else upd( L , val , rc ) ;
pushup( rt ) ;
} int main() {
scanf( "%d%lld" , &n , &m ) ;
for( int i = ; i <= n ; i ++ ) {
scanf( "%d%d" , &a[ i ].f , &a[ i ].s ) ;
a[ i ].id = i ;
}
sort( a + , a + n + , cmp ) ;
build( , n , ) ;
for( int i = ; i <= n ; i ++ ) {
upd( a[ i ].id , a[ i ].f , ) ;
if( t[ ].sum >= m ) {
printf( "%d\n" , a[ i ].s ) ;
return ;
}
}
}

二分做法

其实可以二分这个最大值(即答案)

然后每次$check$的时候$O(n)$扫一遍,看看最大子段和(段内点权不包括大于这个二分的值)会不会大于$m$

答案是具有单调性的所以可以二分

跑的飞快

#include <cstdio>
#include <algorithm> using namespace std ; #define N 100010
#define ll long long int a[ N ] , b[ N ] ;
int n ;
ll m ; bool check( int x ) {
ll sum = ;
for( int i = ; i <= n ; i ++ ) {
if( b[ i ] > x ) {
sum = ;
continue ;
}
sum += a[ i ] ;
if( sum >= m ) return ;
}
return ;
} int main() {
scanf( "%d%lld" , &n , &m ) ;
for( int i = ; i <= n ; i ++ ) {
scanf( "%d%d" , &a[ i ] , &b[ i ] ) ;
}
int l = , r = 1e9+ , ans = ;
while( l <= r ) {
int mid = ( l + r ) >> ;
if( check( mid ) ) ans = mid , r = mid - ;
else l = mid + ;
}
printf( "%d\n" , ans ) ;
}

BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案的更多相关文章

  1. BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)

    5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 182  Solved: 131[ ...

  2. 【BZOJ4552】排序(线段树,二分答案)

    [BZOJ4552]排序(线段树,二分答案) 题面 BZOJ 题解 好神的题啊 直接排序我们做不到 怎么维护? 考虑一下,如果我们随便假设一个答案 怎么检验它是否成立? 把这个数设成\(1\),其他的 ...

  3. BZOJ4552 HEOI/TJOI2016 排序 线段树、二分答案

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4552 题意:给出一个$1$到$N$的全排列,对其进行$M$次排序,每次排序将区间$[l ...

  4. LOJ 2585 「APIO2018」新家 ——线段树分治+二分答案

    题目:https://loj.ac/problem/2585 算答案的时候要二分! 这样的话,就是对于询问位置 x ,二分出一个最小的 mid 使得 [ x-mid , x+mid ] 里包含所有种类 ...

  5. luoguP6619 [省选联考 2020 A/B 卷]冰火战士(线段树,二分)

    luoguP6619 [省选联考 2020 A/B 卷]冰火战士(线段树,二分) Luogu 题外话1: LN四个人切D1T2却只有三个人切D1T1 很神必 我是傻逼. 题外话2: 1e6的数据直接i ...

  6. 【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树

    [BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号 ...

  7. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  8. BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  9. BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库

    正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...

随机推荐

  1. Charles maplocal 时中文显示乱码问题

    用Charles对request进行Map Local后,app上看返回的中文是乱码? 是Map Local的文件编码有问题?是Charles设置有问题?是电脑环境有问题?哈哈,都不是 你是Andro ...

  2. 共享访问在.NET中的编程实现

    转载:http://blog.csdn.net/zhzuo/article/details/1732937 共享访问在.NET中的编程实现 发布日期:2007-08-08 | 更新日期:2009-03 ...

  3. git快速入门 push/clone/reset/merge/切换分支全都有

    本文介绍git快速入门,从安装/创建init / 发布push/版本回退reset / branch分支切换/合并分支merge 这些基本的操作都有涉及,方便新人快速入手,有需要的朋友mark一下.首 ...

  4. Vue 数据绑定语法

    数据绑定语法 Vue.js 的模板是基于 DOM 实现的.这意味着所有的 Vue.js 模板都是可解析的有效的 HTML,且通过一些特殊的特性做了增强.Vue 模板因而从根本上不同于基于字符串的模板, ...

  5. 教你在Android手机上使用全局代理

    前言:在Android上使用系统自带的代理,限制灰常大,仅支持系统自带的浏览器.这样像QQ.飞信.微博等这些单独的App都不能使用系统的代理.如何让所有软件都能正常代理呢?ProxyDroid这个软件 ...

  6. input标签file文件上传图片本地预览

    <input type="file" name="img-up" id="img-up" value="" /&g ...

  7. css定位浮动总结

    定位:定位在中间,放大缩小时也不会跑偏. position:absolute; top: 50%; left: 50%; margin: -270px 0 0 -455px; 解释:定位后,设百分比的 ...

  8. discuz $_G变量

    class.core.php中 global $_G;        $_G = array(            'uid' => 0,            'username' => ...

  9. linux 统计wc

    说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选项含义如下: - c 统计字节 ...

  10. [SQL入门级] 这篇咱'薄利多销',记录多一点

    这个系列的博文知识回顾sql知识作的记录,温故而知新.上一篇内容达不到知识分享的层级被移出园子首页,对不住各位看官,内容简单了些.下面咱就记录多一些的基础知识,薄利多销: 控制用户权限 • 创建用户 ...