BZOJ3540: [Usaco2014 Open]Fair Photography
3540: [Usaco2014 Open]Fair Photography
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 72 Solved: 29
[Submit][Status]
Description
FJ's N cows (2 <= N <= 100,000) are standing at various positions along a long one-dimensional fence. The ith cow is standing at position x_i (an integer in the range 0...1,000,000,000) and is either a plain white cow or a spotted cow. No two cows occupy the same position, and there is at least one white cow. FJ wants to take a photo of a contiguous interval of cows for the county fair, but in fairness to his different cows, he wants to ensure there are equal numbers of white and spotted cows in the photo. FJ wants to determine the maximum size of such a fair photo, where the size of a photo is the difference between the maximum and minimum positions of the cows in the photo. To give himself an even better chance of taking a larger photo, FJ has with him a bucket of paint that he can use to paint spots on an arbitrary subset of his white cows of his choosing, effectively turning them into spotted cows. Please determine the largest size of a fair photo FJ can take, given that FJ has the option of painting some of his white cows (of course, he does not need to paint any of the white cows if he decides this is better).
Input
* Line 1: The integer N.
* Lines 2..1+N: Line i+1 contains x_i and either W (for a white cow) or S (for a spotted cow).
Output
* Line 1: The maximum size of a fair photo FJ can take, after possibly painting some of his white cows to make them spotted.
Sample Input
8 W
11 S
3 W
10 W
5 S
INPUT DETAILS: There are 5 cows. One of them is a white cow at position 8, and so on.
Sample Output
OUTPUT DETAILS: FJ takes a photo of the cows from positions 3 to positions 10. There are 4 cows in this range -- 3 white and 1 spotted -- so he needs to paint one of the white cows to make it spotted.
HINT
Source
这个是经典题吧。。。把0看成-1
如果不考虑修改那么用last[x]记录前缀和为x的第一个位置
然后扫一遍
加上修改就是。。。
设当前位置pos,前缀和为x,则pos-last[x],pos-last[x+2]...都能更新答案
则在这之前
for(int i=2*n;i>=0;i--)
last[i]=min(last[i+2],last[i]);
我的写法和hzwer有点不一样,不知道哪里写萎了。。。
代码:mine
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 250000+5
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct rec{int x,y;}a[maxn];
int n,s[maxn],f[][maxn];
inline bool cmp(rec a,rec b)
{
return a.x<b.x;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)
{
a[i].x=read();
char ch=' ';
while(ch!='S'&&ch!='W')ch=getchar();
a[i].y=ch=='W'?:-;
}
sort(a+,a+n+,cmp);
memset(f,,sizeof(f));
f[][n]=;
for1(i,n)
{
s[i]=s[i-]+a[i].y;
f[i&][s[i]+n]=min(f[i&][s[i]+n],i);
}
for0(i,)
for1(j,n+n)
{
f[i][j]=min(f[i][j-],f[i][j]);
}
int ans=;
for1(i,n)
{
ans=max(ans,a[i].x-a[f[i&][s[i]+n]+].x);
}
printf("%d\n",ans);
return ;
}
代码:hzwer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define pa pair<int,int>
#define inf 1000000000
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,now;
int last[];
struct data{int pos,v;}a[];
inline bool operator<(data a,data b)
{
return a.pos<b.pos;
}
int main()
{
n=read();
for(int i=;i<=n;i++)
{
char ch[];
a[i].pos=read();
scanf("%s",ch);
if(ch[]=='W')a[i].v=-;
else a[i].v=;
}
sort(a+,a+n+);
memset(last,,sizeof(last));
int sum=n;
last[sum]=a[].pos;
for(int i=;i<n;i++)
{
sum+=a[i].v;
last[sum]=min(last[sum],a[i+].pos);
}
for(int i=*n;i>=;i--)
last[i]=min(last[i+],last[i]);
int ans=;sum=n;
for(int i=;i<=n;i++)
{
sum+=a[i].v;
ans=max(ans,a[i].pos-last[sum]);
}
printf("%d",ans);
return ;
}
之所以可以像hzwer这样写是因为位置的奇偶性不同,前缀和的奇偶性肯定不同。
挖坑。
BZOJ3540: [Usaco2014 Open]Fair Photography的更多相关文章
- bzoj 3540: [Usaco2014 Open]Fair Photography
3540: [Usaco2014 Open]Fair Photography Description FJ's N cows (2 <= N <= 100,000) are standin ...
- [BZOJ3535][Usaco2014 Open]Fair Photography
[BZOJ3535][Usaco2014 Open]Fair Photography 试题描述 FJ's N cows (1 <= N <= 100,000) are standing a ...
- [Usaco2014 Open]Gold Fair Photography(hash)
最近做了usaco2014 open的金组,果然美帝的题还是没有太简单啊QAQ,被每年的月赛骗了QAQ 不过话说官方题解真心棒(虽然英文的啃得好艰难,我英语渣你们别鄙视我= =),标程超级优美QAQ ...
- P3105 [USACO14OPEN]公平的摄影Fair Photography
题意翻译 在数轴上有 NNN 头牛,第 iii 头牛位于 xi(0≤xi≤109)x_i\:(0\le x_i\le 10^9)xi(0≤xi≤109) .没有两头牛位于同一位置. 有两种牛:白牛 ...
- Fair Photography
题目大意: 给出直线上N个点的位置和颜色(0或1),求最大的区间,使得区间内0的个数大于等于1的个数且0的个数减去1的个数为偶数. 解题过程: 1.先贴个lsdsjy大牛的线段树的做法:http:// ...
- 解题:USACO14OPEN Fair Photography
题面 有点像JRY的那道序列题,大概是统计题的经典套路? 先说无修改的:将白奶牛记为$-1$,花奶牛记为$1$,然后做前缀和统计某个前缀和$sum$第一次出现的位置,之后再出现就统计答案.对于修改(将 ...
- USACO 2014 US Open Fair Photography /// 技巧
题目大意: 给定n头奶牛 给定n头奶头所在位置和品种 品种只有G H两种 求一段区间的长度 要求区间内包含的品种满足各品种的数量相同 将一个品种的值设为1 另一个设为-1 假设 i<j 而 1~ ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- html rowspan colspan代码示例
<html> <body> <table border="1"> <tr> <td rowspan="4" ...
- sp_MSforeachtable使用方法 查看库中所有表的空间大小
sp_MSforeachtable使用方法 1)说明系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程,从ms sql 6.5开始.存放在S ...
- POJ 2080 Calendar(很水的模拟)
刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...
- hdu 4465 Candy
题解: 由题意得 需要运用: C(m,n)=exp(logC(m,n)) f[]=; ; i<=; i++) f[i]=f[i-]+log(i*1.0); double logC(int m,i ...
- Sina App Engine(SAE)入门教程(2)-Mysql使用
如果你还没有SAE的账号,请在http://sae.sina.com.cn 注册新用户.具体的注册流程请参见:Sina App Engine(SAE)入门教程(1)在常规的环境下,我们可以通过http ...
- Shell最多支持多少个参数
本文转自:http://www.jb51.net/article/56548.htm 这篇文章主要介绍了Shell最多支持多少个参数?本文是对Shell最多可以输入多少个参数的一篇测试文章,需要的 ...
- ipc$漏洞利用实例
一.什么是IPC$ IPC$(Internet Process Connection)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道, 通过提供可信任的用户名和口令 ...
- Android Andbase应用开发框架
[运行说明]运行AndbaseDemo需要将文件中的Andbase库Add进demo中.1.andbase中包含了大量的开发常用手段.如网络下载,多线程与线程池的管理,数据库ORM,图片缓存管理,图片 ...
- 9、JPA_映射双向一对一的关联关系
双向一对一的关联关系 举例说明:经理Manager和部门Department是双向一对一关联关系.则Manager实体类中有Department实体对象的引用,反之亦然. 其实体属性定义如下: Lis ...
- Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)
1.操作基本数据类型的流 1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...