bzoj 3540: [Usaco2014 Open]Fair Photography

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

可以先任意把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.

首先看到这里题目里面有0和1,又是要求一段区间。那么肯定是先把0给换成-1然后统计前缀和
如果没有0变成1的修改操作,那么直接记录当前位置前缀和的值出现的最早位置再减就可以了
然后考虑修改
一次修改使前缀和变化2(-1变成1),所以用last[x+2]来更新last[x]就可以了
一开始迷之把"S"打成"B"【Black和White】
结果调了好久
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct cow
{
	int x,t;
	bool operator <(cow y) const
	{
		return x<y.x;
	}
}a[200001];
struct save
{
	int s,l1,l2;
}s[200001];
int sx[300001];
int main()
{
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	int n;
	scanf("%d",&n);
	int i;
	string x;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i].x);
		cin>>x;
		if(x[0]=='S')
			a[i].t=1;
		else
			a[i].t=-1;
	}
	sort(a+1,a+1+n);
	for(i=1;i<=n;i++)
	{
		s[i].s=s[i-1].s+a[i].t;
		s[i].l1=a[i].x;
		s[i].l2=a[i+1].x;
	}
	s[0].l2=a[1].x;
	for(i=0;i<=n;i++)
		s[i].s+=100000;
	memset(sx,-1,sizeof(sx));
	int ans=0;
	for(i=0;i<n;i++)
	{
		if(sx[s[i].s]!=-1)
			//ans=max(s[i].l1-sx[s[i].s],ans);
			;
		else
			sx[s[i].s]=s[i].l2;
	}
	for(i=200000;i>=0;i--)
	{
		if(sx[i+2]!=-1&&sx[i]!=-1)
			sx[i]=min(sx[i+2],sx[i]);
		else if(sx[i+2]!=-1)
			sx[i]=sx[i+2];
	}
	for(i=1;i<=n;i++)
		if(sx[s[i].s]!=-1)
			ans=max(ans,a[i].x-sx[s[i].s]);
	printf("%d\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值