题意翻译

在数轴上有 NNN 头牛,第 iii 头牛位于 xi(0≤xi≤109)x_i\:(0\le x_i\le 10^9)xi​(0≤xi​≤109) 。没有两头牛位于同一位置。
有两种牛:白牛和花斑牛。保证至少有一头白牛。你可以把白牛涂成花斑牛,不限数量,不限哪只。
找一段尽量长的区间,使得区间的两端点均有一头牛,且区间中白牛与花斑牛的数量相等。试求区间长度。

感谢 @Planet6174 的翻译

题目描述

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

输入输出格式

输入格式:

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

输出格式:

* Line 1: The maximum size of a fair photo FJ can take, after possibly painting some of his white cows to make them spotted.

输入输出样例

输入样例#1:
复制

5
8 W
11 S
3 W
10 W
5 S
输出样例#1: 复制

7

说明

There are 5 cows. One of them is a white cow at position 8, and so on.

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.

题解:

  发现这个题目本质上是找一个区间,使得(白牛的数量-花牛的数量)%2==0,所以想到前缀和,记sum1为白牛的前缀和,sum2为花牛的前缀和。区间i,j合法只有区间设k=(sum1[i]-sum1[j-1])-(sum2[j-1]-sum2[j-1])。必须k>=0且k%2==0,给式子变一下形,就是k=sum1[i]-sum2[i]-(sum2[j-1]-sum1[j-1])。分类讨论sum1[i]-sum2[i]的奇偶性质,把sum2[j-1]-sum1[j-1]丢到两棵线段树里,维护sum2[j-1]-sum1[j-1]的最大值就可以判断区间何不合法,因为要枚举右端点,时间复杂度nlogn.

代码:

  

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 104000
#define ll long long
#define RG register
using namespace std; struct tree{
int l,r,maxx;
}a[][MAXN*]; struct cow{
int pl,co;
void read(){
char x;
scanf("%d%c%c",&pl,&x,&x);
if(x=='W') co=;
else co=;
}
}b[MAXN]; int sum1[MAXN],sum2[MAXN];
int n; inline bool cmp(cow x,cow y){
if(x.pl<y.pl) return ;
return ;
} int check(int x){
if(abs(x)%==) return ;
else return ;
} int query(int xv,int l,int r,int id,int x){
int L=a[id][xv].l,R=a[id][xv].r,mid=(L+R)/;
if(L==R) return L;
if(a[id][xv*].maxx+x>=) return query(xv*,l,mid,id,x);
else if(a[id][xv*+].maxx+x>=) return query(xv*+,mid+,r,id,x);
else return ;
} void insert(int xv,int ps,int zhi,int id){
int l=a[id][xv].l,r=a[id][xv].r,mid=(l+r)/;
if(l==r){
a[id][xv].maxx=zhi;
return;
}
if(ps<=mid) insert(xv*,ps,zhi,id);
else insert(xv*+,ps,zhi,id);
a[id][xv].maxx=max(a[id][xv*].maxx,a[id][xv*+].maxx);
} void work(){
int ans=;
for(int i=;i<=n;i++){
int hh=sum1[i]-sum2[i],l=;int id=check(hh);
if((sum1[i]-sum2[i]+sum2[]-sum1[])%==&&sum1[i]-sum2[i]+sum2[]-sum1[]>=)
ans=max(ans,b[i].pl-b[].pl);
if(hh+a[id][].maxx>=)
l=query(,,i,id,hh);
if(l)
ans=max(ans,b[i].pl-b[l+].pl);
hh=sum2[i]-sum1[i];id=check(hh);
insert(,i,sum2[i]-sum1[i],id);
}
printf("%d",ans);
} void build(int id,int l,int r){
if(l==r){
a[][id].l=a[][id].l=l,a[][id].r=a[][id].r=r;
a[][id].maxx=a[][id].maxx=-(<<);
return;
}
a[][id].maxx=a[][id].maxx=-(<<);
a[][id].l=a[][id].l=l,a[][id].r=a[][id].r=r;
int mid=(l+r)/;
build(id*,l,mid),build(id*+,mid+,r);
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) b[i].read();
sort(b+,b+n+,cmp);
for(int i=;i<=n;i++) if(b[i].co==) sum1[i]=sum1[i-]+,sum2[i]=sum2[i-]; else sum2[i]=sum2[i-]+,sum1[i]=sum1[i-];
build(,,n);
work();
return ;
}

最新文章

  1. Oracle一个事务中的Insert和Update执行顺序
  2. JAVA深入研究——Method的Invoke方法。
  3. ue4 UE4Editor.lib找不到
  4. zju3430
  5. Java中的守护线程和非守护线程(转载)
  6. C++-多重继承的注意点
  7. 05-UIKit绘图演练
  8. HDU 5710 Digit-Sum (构造)
  9. CentOS7安装Puppet+GitLab+Bind
  10. java IO和NIO的场景选择
  11. 绑定事件导致发送多个ajax请求的问题
  12. greatis很不错,出售源代码
  13. POJ 1324 Holedox Moving 搜索
  14. hdu_2296_Ring(AC自动机+DP)
  15. ReactiveX--响应式编程の相关概念 浅析
  16. 安卓高级4 第三方库SlidingMenu的使用
  17. MongoDB 小记
  18. Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)
  19. jqGrid后台交互样例
  20. pycharm 配置使用

热门文章

  1. 【Spring】对持久层技术的整合
  2. 基于SSM后台管理系统/人事管理系统
  3. python里面的xlrd模块详解
  4. 04.Django基础四之模板系统
  5. python里的while循环和if循环
  6. git报错,远程克隆和更新不下来解决方法
  7. 从零开始使用 Webpack 搭建 Vue 开发环境
  8. Unity-遇到的问题小总结
  9. Anaconda基本认识
  10. FreeSql 导航属性的联级保存功能