BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案
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
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 << 2 ] ; 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 = 0 ; if( l == r ) return ; int mid = ( l + r ) >> 1 ; build( l , mid , lc ) ; build( mid + 1 , r , rc ) ; } void pushup( int rt ) { if( t[ lc ].flag && t[ rc ].flag ) { t[ rt ].flag = 1 ; 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 ) >> 1 ; if( l == r ) { t[ rt ].ls = t[ rt ].rs = t[ rt ].sum = val ; t[ rt ].flag = 1 ; 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 = 1 ; i <= n ; i ++ ) { scanf( "%d%d" , &a[ i ].f , &a[ i ].s ) ; a[ i ].id = i ; } sort( a + 1 , a + n + 1 , cmp ) ; build( 1 , n , 1 ) ; for( int i = 1 ; i <= n ; i ++ ) { upd( a[ i ].id , a[ i ].f , 1 ) ; if( t[ 1 ].sum >= m ) { printf( "%d\n" , a[ i ].s ) ; return 0 ; } } }
二分做法
其实可以二分这个最大值(即答案)
然后每次$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 = 0 ; for( int i = 1 ; i <= n ; i ++ ) { if( b[ i ] > x ) { sum = 0 ; continue ; } sum += a[ i ] ; if( sum >= m ) return 1 ; } return 0 ; } int main() { scanf( "%d%lld" , &n , &m ) ; for( int i = 1 ; i <= n ; i ++ ) { scanf( "%d%d" , &a[ i ] , &b[ i ] ) ; } int l = 0 , r = 1e9+1 , ans = 0 ; while( l <= r ) { int mid = ( l + r ) >> 1 ; if( check( mid ) ) ans = mid , r = mid - 1 ; else l = mid + 1 ; } printf( "%d\n" , ans ) ; }