5142: [Usaco2017 Dec]Haybale Feast

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 182  Solved: 131
[Submit][Status][Discuss]

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

HINT

Source

思路:双指针,每次得到以i为结尾的最短的大于等于M的区间的最小值,更新答案即可。

(是一个水题,但是还有更优的解法:由于最小化最大值,我们可以想到二分或者排序, 我们按照b排序,然后求最小的b,满足a的最大连续区间和大于等于M,这里可以用线段树解决。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],b[maxn];
multiset<int>s;
int main()
{
int N,head=,ans=; ll M,res=;
scanf("%d%lld",&N,&M);
rep(i,,N) scanf("%d%d",&a[i],&b[i]),ans=max(ans,b[i]);
rep(i,,N){
res+=a[i]; s.insert(b[i]);
while(res-a[head]>=M){
res-=a[head]; s.erase(s.find(b[head])); head++;
}
if(res>=M) ans=min(ans,(*(--s.end())));
}
printf("%d\n",ans);
return ;
}

BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)的更多相关文章

  1. BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案

    Description Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales ...

  2. [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树

    Description Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1…N. T ...

  3. 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树

    [BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...

  4. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  5. [bzoj4391] [Usaco2015 dec]High Card Low Card 贪心 线段树

    ---题面--- 题解: 观察到以决策点为分界线,以点数大的赢为比较方式的游戏都是它的前缀,反之以点数小的赢为比较方式的都是它的后缀,也就是答案是由两段答案拼凑起来的. 如果不考虑判断胜负的条件的变化 ...

  6. [BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP

    链接 题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费 题解 先将区间排序 设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i ...

  7. BZOJ4777 [Usaco2017 Open]Switch Grass[最小生成树+权值线段树套平衡树]

    标题解法是吓人的. 图上修改询问,不好用数据结构操作.尝试转化为树来维护.发现(不要问怎么发现的)最小生成树在这里比较行得通,因为最近异色点对一定是相邻的(很好想),所以只要看最短的一条两端连着异色点 ...

  8. bzoj4393: [Usaco2015 Dec]Fruit Feast

    题意: T,A,B.T是上限.A和B可以随意吃但是不能超过T.有一次将吃的东西/2的机会.然后可以继续吃,不能超过T.问最多可以吃多少. =>我们先处理不能/2可以吃到哪些.然后弄个双指针扫一扫 ...

  9. 【BZOJ5138】[Usaco2017 Dec]Push a Box(强连通分量)

    [BZOJ5138][Usaco2017 Dec]Push a Box(强连通分量) 题面 BZOJ 洛谷 题解 这题是今天看到萝卜在做然后他一眼秒了,我太菜了不会做,所以就来做做. 首先看完题目,是 ...

随机推荐

  1. Faster-rcnn 配置方法

    Faster-rcnn 在Linux下的配置方法 感谢@邓学长 建立过程: (下载库的时候要按照库readme 进行操作) opencv 的包下载安装,安装教程 用git命令将这个库下载到本地 fas ...

  2. 生产消费模式:多线程读写队列ConcurrentQueue

    需求:现需要将多个数据源的数据导入到目标数据库,这是一个经典的生产消费应用的例子. 直接上代码,看下实现: // 初始化列队缓冲区 队列大小为100 IDataCollection<List&l ...

  3. Asp.net WebApi 配置 Swagger UI

    首先安装Swashbuckle.Core 然后添加swagger配置文件. [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), &q ...

  4. 使用httpclient提交表单数据加号(+)会被自动替换成空格的坑

    坑的场景: 今天使用httpclient-4.5.3版本,发送如下报文: { "idNo": "7+6+0+2ce722a546b39463bd62817fe57f8&q ...

  5. IE8下使用asp.net core mvc+jquery ajaxSubmit问题

    由于项目中一些特殊的地方使用了ajaxSubmit提交数据,但发现在IE8中出现问题,使用该方式提交数据后,无法返回提交结果,而是直接下载该方法名的一个文件,翻阅了园子,终于找到了最简单的解决办法,特 ...

  6. 初学Selenium遇到的那些坑

    一.遇到一个下拉选择框,可以点击继续选择: 所以click两次就可以了: 二.国际话问题 bdId.selectByIndex(index);//index位下拉框内容的下标,从0开始,  数组形式[ ...

  7. 设计模式---工厂方法C++实现

    工厂方法C++实现 1描述 定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类 2类图描述 3代码实现: class Product { protected: ...

  8. EasyUI validType属性

    /** * 包含easyui的扩展和常用的方法 * * @author * * @version 20120806 */ var wjc = $.extend({}, wjc);/* 定义全局对象,类 ...

  9. SPOJ 694 && SPOJ 705 (不重复子串个数:后缀数组)

    题意 给定一个字符串,求它的所有不重复子串的个数 思路 一个字符串的子串都必然是它的某个后缀的前缀.对于每一个sa[i]后缀,它的起始位置sa[i],那么它最多能得到该后缀长度个子串(n-sa[i]个 ...

  10. [转载]java获取word文档的条目化内容

    在开发Web办公系统或文档系统时,PageOffice组件是众所周知的在线处理微软word/ppt/excel文档的强大工具,它对WORD文档的各种处理在API层面进行了封装,屏蔽了Office VB ...