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 ...
随机推荐
- 坑人的 try catch finally
一直以为这样可以关闭 fs, 其实不行 static void Main(string[] args) { FileStream fs = null; try { fs = new FileStrea ...
- 解决eclipse打开报错:failed to create the java virtual ma
在Eclipse安装目录下找到:eclipse.ini 将如下参数改为: --launcher.XXMaxPermSize 128M ------------------------------- 说 ...
- 【转】System.DateTime.Now.ToString()的一些用法
C#中的日期处理函数 //2007年4月24日 this.TextBox6.Text = System.DateTime.Now.ToString("D"); ...
- Extension Methods
Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...
- 安卓 unit 测试与 instrument 测试的代码共享
假如你有一款安卓应用,其包含一系列测试类,其中一部分是unit 测试(位于 src/test),其余为instrument 测试(位于 src/androidTest). 那么问题来了:你有一些想在所 ...
- hdu 4586 Play the Dice
思路:设期望值为s,前m个是再来一次机会,则有 s=(a[1]+s)/n+(a[2]+s)/n+……+(a[m]+s)/n+a[m+1]/n…… 化简:(n-m)s=sum 当sum=0时,为0: 当 ...
- linux设置和查看环境变量的方法
1. 显示环境变量HOME $ echo $HOME /home/redbooks 2. 设置一个新的环境变量hello $ export HELLO="Hello!" ...
- Oracle日期函数
Oracle日期函数用于对Oracle数据库中的日期及时间进行处理. (1)ADD_MONTHS Oracle日期函数返回一个具有与所提供日期相差月份的日期,函数中给出了未来或以前的月份数.语法如下: ...
- [DLX]HDOJ4069 Squiggly Sudoku
题意:有9*9的格子 每个格子 由五部分组成:上(16).右(32).下(64).左(128).和该格的数值(0~9) 若上下左右有分割格子的线 就加上相应的数, 该格的数值若为0,则是未知 1~9 ...
- 360抢票 网站维护中 && 你的登录被踢了!
本来在超市犹豫到底该买哪种暖手袋,犹豫了差不多半个多小时,还没决定好,一看时间还有8分钟到10点,遂狂奔回寝室抢票. 结果,360抢票被12306秒了—— 猜测原因是12306的验证码改了(变成动态的 ...