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. div+css网页标准布局实例教程(三)

    前边两节学完后,前台工作基本上完成了,下边的任务该程序员添加程序了.为什么说是基本完成呢?因为要做的工作还很多,不但要把首页做出来,其它的列表页详细页等页面也得做.还要配合程序员把整个网站完成,这样才 ...

  2. nodejs(二)child_process模块

    1.child_process是Node.js的一个十分重要的模块,通过它可以实现创建多进程,以利用多核计算资源. child_process模块提供了四个创建子进程的函数,分别是spawn,exec ...

  3. js屏蔽f12键

    <script>            $(document).keydown(function(e) {                 if (e.keyCode == 123) {/ ...

  4. lua源代码学习(一)lua的c api外围实现

    工作后,整个人已经比較松懈了.尽管一直在看lua的源代码.可是一直是比較零碎的时间,没有系统的整理,所以还是收获不多.由于近期工作也不是非常忙了,就想整理下lua的源代码学习的笔记.加深下印象,并分享 ...

  5. UIStoryboard跳转界面

    /**1.创建Storyboard,加载Storyboard的名字,这里是自己创建的Storyboard的名字*/ UIStoryboard *storyboard = [UIStoryboard s ...

  6. PAT Radix[二分][进制转换][难]

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  7. [LeetCode] 557. Reverse Words in a String III_Easy tag: String

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  8. 神经网络前向后向传播(理论推导+代码) 单层神经网络相当于logistic regression

    建立神经网络的主要步骤是: 1. 定义模型结构(例如输入特征的数量) 2. 初始化模型的参数 3. 循环: # 3.1 计算当前损失(正向传播) # 3.2 计算当前梯度(反向传播) # 3.3 更新 ...

  9. iOS 网易彩票-5设置模块二

    产品推荐 产品推荐使用的是UICollectionView控件,UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视 ...

  10. python绘图之seaborn 笔记

    前段时间学习了梁斌老师的数据分析(升级版)第三讲<探索性数据分析及数据可视化>,由于之前一直比较忙没有来得及总结,趁今天是周末有点闲暇时间,整理一下笔记: 什么是seaborn Seabo ...