bzoj1637 [Usaco2007 Mar]Balanced Lineup
Description
Input
Output
Sample Input
0 11
1 10
1 25
1 12
1 4
0 13
1 22
Sample Output
11
输入说明
有7头牛,像这样在数轴上。
1 1 0 1 0 1 1
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
输出说明
牛 #1 (at 11), #4 (at 12), #6 (at 13), #7 (at 22) 组成一个平衡的最大的区间,大小为 22-11=11 个单位长度。
<-------- 平衡的 -------->
1 1 0 1 0 1 1
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
离散+差分序列搞
把01的标识改成1和-1,然后离散完用差分序列维护。这样搞只要找到最前面第一个前缀和和它相等的更新答案就好了
注意到差分的范围是-50000到50000,所以还要化负为正
#include<cstdio>
#include<algorithm>
using namespace std;
#define add 50010
struct dat{
int x,v;
}d[50010];
inline bool operator < (dat a,dat b){return a.x<b.x;}
inline int max(int a,int b)
{return a>b?a:b;}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,now=add,ans;
int last[100050];
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
d[i].v=read();d[i].x=read();
if (!d[i].v)d[i].v=-1;
}
sort(d+1,d+n+1);
for (int i=1;i<=n;i++)
{
now+=d[i].v;
if (!last[now])last[now]=d[i+1].x;
else ans=max(ans,d[i].x-last[now]);
}
printf("%d",ans);
}
bzoj1637 [Usaco2007 Mar]Balanced Lineup的更多相关文章
- 【差分+前缀和】BZOJ1637: [Usaco2007 Mar]Balanced Lineup
Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000 ...
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup
1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer John ...
- BZOJ 1637: [Usaco2007 Mar]Balanced Lineup( sort + 前缀和 )
将 0 变为 -1 , 则只需找区间和为 0 , 即前缀和相同的最长区间 , 记录一下每个前缀和出现的最早和最晚的位置 , 比较一下就 OK 了 --------------------------- ...
- 1637: [Usaco2007 Mar]Balanced Lineup
1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 393 Solved: 263[ ...
- 【BZOJ】1637: [Usaco2007 Mar]Balanced Lineup(前缀和+差分+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1637 很神思想.. 前缀和应用到了极点... 我们可以发现当数量一定时,这个区间最前边的牛的前边一个 ...
- BZOJ 1637 [Usaco2007 Mar]Balanced Lineup:前缀和 + 差分
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1637 题意: Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ...
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup【瞎搞】
我是怎么想出来的-- 把种族为0的都变成-1,按位置x排升序之后,s[i]表示种族前缀和,想要取(l,r)的话就要\( s[r]-s[l-1]==0 s[r]==s[l-1] \),用一个map存每个 ...
- BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 933 Solved: 56 ...
- BZOJ1636: [Usaco2007 Jan]Balanced Lineup
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 476 Solved: 345[ ...
随机推荐
- aop动态代理学习
学习,顺便做个demo,方便理解. A接口有c方法,类B实现A接口,原本应该是执行B类中的c方法,可现在不这样做: 我声明产生B类的代理类B',由它来冒充B类的“兄弟”并“实现”A接口, 对外界来说B ...
- hdu 5335 Walk Out(bfs+寻找路径)
Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...
- PHP文件目录copy
/**(2) PHP文件目录copy @param string $dirsrc 原目录名称字符串 @param string $dirto 目标目录名称字符串 */ function copyDir ...
- python高级编程之选择好名称:pepe8和命名最佳实践
# # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' # my_list=['a','b','c','d'] ...
- pyQt事件处理
Qt事件处理01 Qt处理事件的第二种方式:"重新实现QObject::event()函数",通过重新实现event()函数,可以在事件到达特定的事件处理器之前截获并处理他们.这种 ...
- Spark常用函数讲解之Action操作
摘要: RDD:弹性分布式数据集,是一种特殊集合 ‚ 支持多种来源 ‚ 有容错机制 ‚ 可以被缓存 ‚ 支持并行操作,一个RDD代表一个分区里的数据集RDD有两种操作算子: Trans ...
- C++空类中的默认函数
定义一个空的C++类,例如 class Empty { } 一个空的class在C++编译器处理过后就不再为空,编译器会自动地为我们声明一些member function,一般编译过去就相当于 cla ...
- UIScrollView 代理方法
在使用UIScrollView和它的子类UITableView时,有时需要在不同操作状态下,做不同的响应. 如何截获这些状态,如正在滚动,滚动停止等,使用UIScrollViewDelegate_Pr ...
- jQuery Moblile Demos学习记录Theming、Button、Icons图标,脑子真的不好使。
jQuery Moblile Demos学习记录Theming.Button.Icons图标,脑子真的不好使. 06. 二 / Jquery Mobile 前端 / 没有评论 本文来源于www.i ...
- entity framework 6 通用数据类
原文 http://blog.csdn.net/laokaizzz/article/details/25730813 public class BaseDAL { string strConn = ...