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).

在X的非负轴上有N个不在同一位置上的数,0或1.至少有1个0.
可以先任意把0染成1.
区间长度定义为,[L,R]中最右和最左的数的差的绝对值.
求一个最长区间,满足区间中所有数0和1的个数相同.
输出这个最长区间的长度.

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

5
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

7
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

题解:
碰上一道好题。想法和hzwer的一样:

这个是经典题吧。。。把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的更多相关文章

  1. bzoj 3540: [Usaco2014 Open]Fair Photography

    3540: [Usaco2014 Open]Fair Photography Description FJ's N cows (2 <= N <= 100,000) are standin ...

  2. [BZOJ3535][Usaco2014 Open]Fair Photography

    [BZOJ3535][Usaco2014 Open]Fair Photography 试题描述 FJ's N cows (1 <= N <= 100,000) are standing a ...

  3. [Usaco2014 Open]Gold Fair Photography(hash)

    最近做了usaco2014 open的金组,果然美帝的题还是没有太简单啊QAQ,被每年的月赛骗了QAQ 不过话说官方题解真心棒(虽然英文的啃得好艰难,我英语渣你们别鄙视我= =),标程超级优美QAQ ...

  4. P3105 [USACO14OPEN]公平的摄影Fair Photography

    题意翻译 在数轴上有 NNN 头牛,第 iii 头牛位于 xi(0≤xi≤109)x_i\:(0\le x_i\le 10^9)xi​(0≤xi​≤109) .没有两头牛位于同一位置. 有两种牛:白牛 ...

  5. Fair Photography

    题目大意: 给出直线上N个点的位置和颜色(0或1),求最大的区间,使得区间内0的个数大于等于1的个数且0的个数减去1的个数为偶数. 解题过程: 1.先贴个lsdsjy大牛的线段树的做法:http:// ...

  6. 解题:USACO14OPEN Fair Photography

    题面 有点像JRY的那道序列题,大概是统计题的经典套路? 先说无修改的:将白奶牛记为$-1$,花奶牛记为$1$,然后做前缀和统计某个前缀和$sum$第一次出现的位置,之后再出现就统计答案.对于修改(将 ...

  7. USACO 2014 US Open Fair Photography /// 技巧

    题目大意: 给定n头奶牛 给定n头奶头所在位置和品种 品种只有G H两种 求一段区间的长度 要求区间内包含的品种满足各品种的数量相同 将一个品种的值设为1 另一个设为-1 假设 i<j 而 1~ ...

  8. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. 【BZOJ】【3398】【USACO 2009 Feb】Bullcow 牡牛和牝牛

    组合计数/乘法逆元 排列组合求总方案数 这个可以用一个一维的动态规划解决: f[i][0]表示第i头牛是牝牛的方案数 f[i][1]表示第i头牛是牡牛的方案数 则转移为:f[i][0]=f[i-1][ ...

  2. [vijos 1770]大内密探

    描述 在古老的皇宫中,有N个房间以及N-1条双向通道,每条通道连接着两个不同的房间,所有的房间都能互相到达.皇宫中有许多的宝物,所以需要若干个大内密探来守护.一个房间被守护当切仅当该房间内有一名大内密 ...

  3. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. java基础知识回顾之javaIO类--内存操作流ByteArrayInputStream和ByteArrayOutputSteam(操作字节数组)

    直接看代码: package cn.itcast.io.p6.bytestream; import java.io.ByteArrayInputStream; import java.io.ByteA ...

  5. java基础知识回顾之---java String final类之intern方法

    public class StringObjectDemo { /** * @param args */ public static void main(String[] args) { String ...

  6. lintcode 中等题:subsets II 带重复元素的子集

    题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...

  7. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  8. 《HTTP权威指南》笔记

    http://blog.csdn.net/sunorry?viewmode=contents有些笔记 MIME 类型是一种文本标记,表示一种主要的对象类型和一个特定的子类型,中间由一条斜杠来分隔:te ...

  9. Unix编程之size_t、ssize_t

    http://blog.csdn.net/lalor/article/details/7426184 首先,我非常肯定以 及确定的告诉你ssize_t是有符号整型,在32位机器上等同与int,在64位 ...

  10. 老韩思考:一个卖豆腐的能转行IT吗? 你的卖点在哪里?

    前言: 我带过的学生很多,各行各业都有,泰牛程序员招生消息放出去后,还有一个在菜市场上卖豆腐的也看我的视频教程,决定转换IT行业,我想,北大毕业的可以卖猪肉,那么卖豆腐的为什么就不能从事IT行业呢?那 ...