【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales Silver 二分
【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales (Silver)
Description
Input
Output
Sample Input
8 1
1 4
3 8
12 15
20 20
Sample Output
题解:当我又一次看到了这道熟悉的题,想起了几年前狂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 二分的更多相关文章
- 【BZOJ4099】Trapped in the Haybales Gold STL
[BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...
- 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分
题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- SDWC补题计划
2018的寒假去了SD的冬令营,因为一班二班难度悬殊,对我很不友好,几乎什么也没学会,但是我把两个班的课件都存了下来,现在慢慢把两个班的例题以及课后题都补一补(毕竟冬令营的钱不能白花). 这些题目横跨 ...
- POJ 3662
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4591 Accepted: 1693 D ...
- POJ 3273
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12122 Accepted: 4932 ...
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- BZOJ——1620: [Usaco2008 Nov]Time Management 时间管理
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 920 Solved: 569[Submit][Status][Discuss] Description ...
随机推荐
- 记录日志框架:log4net使用
一.log4net简介 Log4net是Apache下一个开放源码的项目,我们可以控制日志信息的输出目的地.Log4net中定义了多种日志信息输出模式.在做项目的时候最头疼的是在程序发布到正式环境之后 ...
- EasyUI的treegrid组件动态加载数据问题的解决办法
http://www.jquerycn.cn/a_3455 —————————————————————————————————————————————————————————————————————— ...
- js学习笔记10----字符串的基本操作
1.字符串的基本操作如下: 定义字符串: var str = "Hello World!" 字符串的基本操作如下: str.length-----返回字符串长度,这里返回12 st ...
- javascript实现记录文本框内文字个数
最近在做一个项目中遇到这样一个问题,要对文本框中用户输入的文字进行记数,在下面显示出来,因为我们做的是一个短信发送平台,现在我们国家的短信服务,如果你的信息超过了70个字符,短信就会按二条给你下发.所 ...
- [oracle] 系统权限管理
1 利用超级用户连接到数据库登录 sqlplus / as sysdba or slqplus SYS/SYSPWD as sysdba 这两个命令的效果是一样的,都是以sysdba的身份使得SYS管 ...
- thikphp 简单的接口骨架
//get id 获取内容,调用xml方法 public function get(){ $id = $_GET['id'];//接收id $User = M('user'); //$val-> ...
- selenium测试(Java)-- 键盘事件(七)
1 package com.test.key; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.Keys; 5 impo ...
- 【转】【Asp.Net】ASP.Net Response.ContentType 详细列表
不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式. 代码如: <% response.ContentType =&quo ...
- bootstrap -- css -- 辅助类
文本 文本颜色 如果文本是个链接,则鼠标移动到链接文本上的时候,文本会变暗 .text-muted:灰色 .text-primary:浅蓝色 .text-success:绿色 .text-info:深 ...
- Unity3d之Shader开发介绍
Shader是为渲染管线中的特定处理阶段提供算法的一段代码.shader是伴随着可编程渲染管线出现的,它的出现使得游戏开发者可以对渲染过程加以控制,拥有更大的创作空间,因此Shader的出现可以看作是 ...