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. 在服务器 部署 asp.net core 报502.5的错

    HTTP Error 502.5 - Process Failure 如果 IIS 该安装的都装好了的话,那就需要安装一个.net core sdk,链接如下: https://www.microso ...

  2. Dropping water balloons (入门dp)

    2017-08-12 18:36:24 writer:pprp 最近刚刚接触动态规划,感觉状态的查找和转移自己很难想到,都是面向题解编程,但是一开始都是这样了,只有相信我可以独立自己解决动态规划这类问 ...

  3. Adobe HTTP Dynamic Streaming (HDS) manifest 文件 f4m bootstrap属性解析

    首先介绍一下应用背景,我们使用的很多浏览器都依赖Adobe Flash Player 播放视频文件.Flash Player是一个播放的客户端,客户端和服务端之间的流通信有几套实现的标准.包括这里介绍 ...

  4. gcc编译出错---make[5]: *** [s-attrtab] Killed

    内存不足导致的编译出错,解决方法是增加swapfile. root@ubuntu:home# swapon -s Filename    Type            Size    Used    ...

  5. GTS--阿里巴巴分布式事务全新解决方案

    现代IT应用中,服务化SOA作为主流的技术架构被广泛应用到各种信息系统.原来一个系统被分拆成若干个服务的集合,产生了跨服务调用的分布式事务问题.随着Dubbo.SpringCloud等微服务框架的流行 ...

  6. [Vue]vee-validate的使用——自定义校验规则及校验message

    1.安装vee-validate npm install vee-validate --save 2.main.js里引用vee-validate插件 import Vue from 'vue' im ...

  7. Git 常用命令和 Git Flow 梳理

    git 用 git 有一段时间了,之前没有详细地了解 git flow,导致协作过程中或多或少出现了一些头疼问题.最近静下心来理了下 git flow 的整个流程,再回头看开朗了不少,总结到这里.介绍 ...

  8. python 细节回顾

    #!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': # 第一个实例 if letter == 'h': continue ...

  9. 【zzuli-2276】跳一跳

    题目描述 今天跳跳去公园游玩,第一个游戏就难倒了跳跳,游戏规则是跳跳站在一个面积无限大的矩形土地上,开始时跳跳在左上角(即第一行第一列),每一次跳跳都可以选择一个右下方格子,并瞬间跳过去(如从下图中的 ...

  10. C# 打开TXT文件读取内容

    控制关键字,有些关键字发短信发不出来,比如(金融)需要转化为(金.融) 要求:读取敏感字的,并且替换掉 using : using System.Collections.Generic;using S ...