【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales (Silver)

Description

Farmer John has received a shipment of N large hay bales (1≤N≤100,000), and placed them at various locations along the road
connecting the barn with his house. Each bale j has a size Sj and a distinct position Pj giving its location along the one-dimensional
road. Bessie the cow is currently located at position B, where there is no hay bale.
Bessie the cow can move around freely along the road, even up to the position at which a bale is located, but she cannot cross
through this position. As an exception, if she runs in the same direction for D units of distance, she builds up enough speed to
break through and permanently eliminate any hay bale of size strictly less than D. Of course, after doing this, she might open up
more space to allow her to make a run at other hay bales, eliminating them as well.
FJ is currently re-painting his house and his barn, and wants to make sure Bessie cannot reach either one (cows and fresh paint
do not make a good combination!) Accordingly, FJ wants to make sure Bessie never breaks through the leftmost or rightmost hay
bale, so she stays effectively trapped within the hay bales. FJ has the ability to add hay to a single bale of his choosing to help
keep Bessie trapped. Please help him determine the minimum amount of extra size he needs to add to some bale to ensure Bessie
stays trapped.
农夫约翰将N(1<=N<=100000)堆干草放在了一条平直的公路上。第j堆干草的大小为Sj,坐标为Pj。奶牛贝茜位于一个没有干草堆的点B。
奶牛贝茜可以在路上自由移动,甚至可以走到某个干草堆上,但是不能穿过去。但是如果她朝同一个方向跑了D个单位的距离,那么她就有足够大的速度去击碎任何大小严格小于D的干草堆。当然,在这之后如果她继续朝着该方向前进,那么她的速度不会清零。
约翰可以指定某堆干草,并增大它的大小,他想知道他最少需要增大多少,才能把奶牛贝茜困住,或者根本不可能。

Input

The first line of input contains N as well as Bessie's initial position B. Each of the next N lines describes a bale, and
contains two integers giving its size and position. All sizes and positions are in the range 1…109.

Output

Print a single integer, giving the minimum amount of hay FJ needs to add to prevent Bessie from escaping.
Print -1 if it is impossible to prevent Bessie's escape.

Sample Input

5 7
8 1
1 4
3 8
12 15
20 20

Sample Output

4

题解:当我又一次看到了这道熟悉的题,想起了几年前狂WA不止的恐惧,我屏住呼吸,再一次点开了这道题目,就在这时,我突然震惊的发现——

  我TM看错题了!!!

好吧,这题说的是只能增大一个干草堆的大小,我以前一直认为是多个(也就是两个),并且还真的写出来了一种算法,拍极限数据都没问题!!

进入正题:

先讨论增大Bessie左边的干草堆的情况,我们枚举右边的干草堆j,设增大的大小为k,加高的干草堆编号为i,干草堆大小size,干草堆坐标x,容易列出方程

也就是size[i]+x[i]越大越好,前提x[i]不能太小

于是我们先处理一下size[i]+x[i]的最大值,然后二分x[i],然后更新答案就行了

增大Bessie右边的干草堆的情况也类似

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
const int maxn=100010;
int n,m,ans;
struct bale
{
int x,d;
}s[maxn];
int f[maxn];
set<int> s1,s2;
bool cmp(bale a,bale b)
{
return a.x<b.x;
}
int main()
{
scanf("%d%d",&n,&m);
ans=1<<30;
int i,j,l,r,mid;
for(i=1;i<=n;i++) scanf("%d%d",&s[i].d,&s[i].x);
s[++n].x=m;
sort(s+1,s+n+1,cmp);
for(i=1;i<=n;i++) if(s[i].x==m)
{
m=i;
break;
}
f[m]=-1<<30;
for(i=m-1;i>=1;i--) f[i]=max(f[i+1],s[i].x+s[i].d);
for(i=m+1;i<=n;i++) f[i]=max(f[i-1],s[i].d-s[i].x);
for(i=m+1;i<=n;i++)
{
l=1,r=m;
while(l<r)
{
mid=l+r>>1;
if(s[i].x-s[mid].x<=s[i].d) r=mid;
else l=mid+1;
}
if(r<m) ans=min(ans,max(0,s[i].x-f[r]));
}
for(i=1;i<m;i++)
{
l=m+1,r=n+1;
while(l<r)
{
mid=l+r>>1;
if(s[mid].x-s[i].x<=s[i].d) l=mid+1;
else r=mid;
}
if(l>m+1) ans=min(ans,max(0,-s[i].x-f[l-1]));
}
if(ans==1<<30) printf("-1");
else printf("%d",ans);
return 0;
}

【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales Silver 二分的更多相关文章

  1. 【BZOJ4099】Trapped in the Haybales Gold STL

    [BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...

  2. 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分

    题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  5. SDWC补题计划

    2018的寒假去了SD的冬令营,因为一班二班难度悬殊,对我很不友好,几乎什么也没学会,但是我把两个班的课件都存了下来,现在慢慢把两个班的例题以及课后题都补一补(毕竟冬令营的钱不能白花). 这些题目横跨 ...

  6. POJ 3662

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4591   Accepted: 1693 D ...

  7. POJ 3273

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12122   Accepted: 4932 ...

  8. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  9. BZOJ——1620: [Usaco2008 Nov]Time Management 时间管理

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 920  Solved: 569[Submit][Status][Discuss] Description ...

随机推荐

  1. pip国内源

    pip install -i https://pypi.douban.com/simple pyqrcode

  2. Intellij Idea14 jstl标签的引入

    习惯了eclipse和myeclipse开发的我们总是依赖于系统的插件,而当我想当然的以为IntelliJ IDEA 的jstl 的使用应该和myeclispe一样,当时使用起来却到处碰壁,完全找不到 ...

  3. 在hibernate中查询单个对象的方法,get()、load()、

    查询单个对象可以直接通过Session对象来做到,其中session这个对象提过了2种获得单个对象的方法,一个是get方法和load方法,我去看这个两个方法的时候发现这两个方法的参数是一样的,使用方式 ...

  4. 【转】MFC WM_CTLCOLOR 消息

    WM_CTLCOLOR消息用来完成对EDIT, STATIC, BUTTON等控件设置背景和字体颜色, 其用法如下: 1.首先在自己需要设置界面的对话框上点击右键-->建立类向导-->加入 ...

  5. 第三百零六节,Django框架,models.py模块,数据库操作——创建表、数据类型、索引、admin后台,补充Django目录说明以及全局配置文件配置

    Django框架,models.py模块,数据库操作——创建表.数据类型.索引.admin后台,补充Django目录说明以及全局配置文件配置 数据库配置 django默认支持sqlite,mysql, ...

  6. 【转载】C# 快速高效率复制对象另一种方式 表达式树

    1.需求 在代码中经常会遇到需要把对象复制一遍,或者把属性名相同的值复制一遍. 比如: public class Student { public int Id { get; set; } publi ...

  7. bootstrap table使用指南

    Bootstrap table是国人开发的一款基于 Bootstrap 的 jQuery 表格插件,通过简单的设置,就可以拥有强大的单选.多选.排序.分页,以及编辑.导出.过滤(扩展)等等的功能. 目 ...

  8. git 分支的创建、合并、删除

          基本概念与命令 分支(branch):每次提交,Git都把提交的内容串成一条时间线,这条时间线就是一个分支 .   git 分支的创建 git branch branchName git ...

  9. jQuery Datatable 转载

    jQuery Datatable 实用简单实例 时间 2014-05-08 10:44:18  51CTO推荐博文 原文  http://sgyyz.blog.51cto.com/5069360/14 ...

  10. 关于直播学习笔记-003-nginx-rtmp、srs、vlc、obs

    服务器 1.nginx-rtmp:https://github.com/illuspas/nginx-rtmp-win32 2.srs:https://github.com/illuspas/srs- ...