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. kubernetes实战(二十五):kubeadm 安装 高可用 k8s v1.13.x

    1.系统环境 使用kubeadm安装高可用k8s v.13.x较为简单,相比以往的版本省去了很多步骤. kubeadm安装高可用k8s v.11 和 v1.12 点我 主机信息 主机名 IP地址 说明 ...

  2. Netty 使用经验总结(一)

    1: Netty 4的线程模型转变 在Netty 3的时候,upstream是在IO线程里执行的,而downstream是在业务线程里执行的.比如netty从网络读取一个包传递给你的handler的时 ...

  3. Jsoup爬虫解析

    需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...

  4. UIPageViewController基本使用

    UIPageViewController基本使用 @interface ViewController ()<UIPageViewControllerDelegate,UIPageViewCont ...

  5. (转)Elasticsearch聚合初探——metric篇

    前言 ES中的聚合被分为两大类:Metric度量和bucket桶(原谅我英语差,找不到合适的词语.....就用单词来说吧!).说的通俗点,metric很像SQL中的avg.max.min等方法,而bu ...

  6. pyrhon3与mysql:查、更、删49

    import pymysql conn = pymysql.connect(host=',db='jodb1',port=3307,charset='utf8') # 172.31.10.225 # ...

  7. postman返回参数的截取

    同事在使用postman接口测试的时候,遇到这么一个问题,在一个参数里面,返回了一个类似数组的参数,如下: 然后现在需要把数组里面的两个参数分别保存到环境变量里面: 个人的想法是通过截取的方式进行数组 ...

  8. testng入门教程11 TestNG运行JUnit测试

    现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...

  9. Log4net 日志传到 graylog监控

    graylog是java的一个日志监控插件.存储用的是mongoDB,效率还是挺高的.不过嘛,文档太少了,安装和配置都很不容易. 官网:http://www.graylog.org/ 在graylog ...

  10. ASP.NET Post方式提交

    public static string SendMsg(string fxPhone, string fxPassword, string toPhone, string msg) { try { ...