【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右边的干草堆的情况也类似

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <set>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn=100010;
  8. int n,m,ans;
  9. struct bale
  10. {
  11. int x,d;
  12. }s[maxn];
  13. int f[maxn];
  14. set<int> s1,s2;
  15. bool cmp(bale a,bale b)
  16. {
  17. return a.x<b.x;
  18. }
  19. int main()
  20. {
  21. scanf("%d%d",&n,&m);
  22. ans=1<<30;
  23. int i,j,l,r,mid;
  24. for(i=1;i<=n;i++) scanf("%d%d",&s[i].d,&s[i].x);
  25. s[++n].x=m;
  26. sort(s+1,s+n+1,cmp);
  27. for(i=1;i<=n;i++) if(s[i].x==m)
  28. {
  29. m=i;
  30. break;
  31. }
  32. f[m]=-1<<30;
  33. for(i=m-1;i>=1;i--) f[i]=max(f[i+1],s[i].x+s[i].d);
  34. for(i=m+1;i<=n;i++) f[i]=max(f[i-1],s[i].d-s[i].x);
  35. for(i=m+1;i<=n;i++)
  36. {
  37. l=1,r=m;
  38. while(l<r)
  39. {
  40. mid=l+r>>1;
  41. if(s[i].x-s[mid].x<=s[i].d) r=mid;
  42. else l=mid+1;
  43. }
  44. if(r<m) ans=min(ans,max(0,s[i].x-f[r]));
  45. }
  46. for(i=1;i<m;i++)
  47. {
  48. l=m+1,r=n+1;
  49. while(l<r)
  50. {
  51. mid=l+r>>1;
  52. if(s[mid].x-s[i].x<=s[i].d) l=mid+1;
  53. else r=mid;
  54. }
  55. if(l>m+1) ans=min(ans,max(0,-s[i].x-f[l-1]));
  56. }
  57. if(ans==1<<30) printf("-1");
  58. else printf("%d",ans);
  59. return 0;
  60. }

【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. 检查 Linux 服务器性能

    如何用十条命令在一分钟内检查 Linux 服务器性能 如果你的Linux服务器突然负载暴增,报警短信快发爆你的手机,如何在最短时间内找出Linux性能问题所在?来看Netflix性能工程团队的这篇博文 ...

  2. shell两个数字的运算,一共三个变量

    #!/bin/bash #两个数运算的简单脚本 + ,一共三个参数 echo $# #对获取的参数以此判断是否包含[a-zA-Z]的东西,如果包含就退出.因为数字相加不是数字就是加减乘除 for i_ ...

  3. js类型转换 之 转字符串及布尔类型

    上一篇我们讲到了如何转数字类型,今天总结一下转字符串及布尔类型的方法: 转字符串方法主要有: toString(); String(); 具体的用法如下表格所示: 方法 例子 返回值 说明 toStr ...

  4. ASP.NET C# 获取当前日期 时间 年 月 日 时 分 秒

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  5. Windoows窗口程序一

    编写窗口程序的步骤: .定义WinMain入口函数 .定义窗口处理函数(处理消息)WindowProc .注册窗口类RegisterClass .创建窗口(在内存中创建窗口)CreateWindow ...

  6. solr学习2

    1:solr中的时间问题 solr中显示的时间默认会比我们本机时间少八个小时,因为时区不一样. 在solr的web页面查看会发现时间少八个小时. 但是使用java代码操作的时候是整成的的,所以在这只需 ...

  7. selenium测试(Java)-- 一组元素操作(十一)

    利用下面的例子来编写测试脚本 页面代码: <!DOCTYPE html> <html> <head> <meta http-equiv="conte ...

  8. Android startActivityForResult 回传数据

    一个activity打开新的activity,新的activity关闭之后,返回数据.原来的activity要接收返回的数据,在开启新的activity时,就需要调用startActivityForR ...

  9. linux -- 服务开机自启动

    好吧,最近需要用到开机启动服务,百度了一下,几乎都是一个版本,然后之间各种传递.我也抄个 ******************************************************* ...

  10. php -- 取日期

    1.获取当前时间方法date()很简单,这就是获取时间的方法, 格式为:date($format, $timestamp), format为格式 - 必需 timestamp为时间戳–可填参数. 比如 ...