题意翻译

在数轴上有 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 ;
}

P3105 [USACO14OPEN]公平的摄影Fair Photography的更多相关文章

  1. P3105 [USACO14OPEN]公平的摄影(正解是乱搞,我却二分了)(+二分答案总结)

    照例化简题意: 给定一个01区间,可以把0改成1,问其中最长的01数量相等的区间长度. 额很容易想到前缀和,把w弄成1,h弄成-1,然后求前缀和,然后乱搞就行了. 但是一直不太会乱搞的我却直接想到了二 ...

  2. BZOJ3540: [Usaco2014 Open]Fair Photography

    3540: [Usaco2014 Open]Fair Photography Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 29 ...

  3. RabbitMQ简单应用の公平分发(fair dipatch)

    公平分发(fair dipatch)和轮询分发其实基本一致,只是每次分发的机制变了,由原来的平均分配到现在每次只处理一条消息 1.MQ连接工厂类Connection package com.mmr.r ...

  4. bzoj 3540: [Usaco2014 Open]Fair Photography

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

  5. [BZOJ3535][Usaco2014 Open]Fair Photography

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

  6. RabbitMQ学习第二记:工作队列的两种分发方式,轮询分发(Round-robin)和 公平分发(Fair dispatch)

    1.什么是RabbitMQ工作队列 我们在应用程序使用消息系统时,一般情况下生产者往队列里插入数据时速度是比较快的,但是消费者消费数据往往涉及到一些业务逻辑处理导致速度跟不上生产者生产数据.因此如果一 ...

  7. 解题:USACO14OPEN Fair Photography

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

  8. Fair Photography

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

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

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

随机推荐

  1. Vert.x Web之Router

    Vert.x Web 是一系列用于基于 Vert.x 构建 Web 应用的构建模块. Vert.x Web 的大多数特性被实现为了处理器(Handler),因此您随时可以实现您自己的处理器.我们预计随 ...

  2. 内存泄露检测工具Valgrind

    内存泄露简介 什么是内存泄漏 内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因,程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果. 内存泄 ...

  3. 5.cookie每个参数的意义和作用以及工作原理?

    cookie主要参数有: (1)expires 过期时间 (2)path cookie存放路径 (3)domain 域名 同域名下可访问 (4)Set-Cookie: name cookie名称

  4. 我真的不想再用 JPA 了

    在开发者的圈子里,没当说到一种技术好或者不好,都会引发激烈或者不激烈的争论,直到一个开发者出来说 PHP 是世界上最好的语言,大家伙儿才会纷纷退去继续写代码. 今天说 JPA 的问题不是想引发什么讨论 ...

  5. 花果山第一届猿类分级考试实录--Talk is cheap,Show me the code

    本故事纯属虚构,如有雷同,纯属巧合! 故事背景 悟空师徒4人取经回来后,因不耐收到管教,就回到了花果山,带领一帮猴子猴孙逍遥自在的过日子,奈何因在阎王殿里将生死薄中的猴子猴孙的名字都划去了,猴子猴孙是 ...

  6. Docker在IDEA中的使用以及如何部署到服务器

    IDEA中实现一键部署到服务器 点击运行自动部署到服务器: 服务器上安装docker 1,添加yum源 # yum install epel-release –y# yum clean all# yu ...

  7. NIO入门-----01

    package com.sico.pck01_nio; import java.nio.ByteBuffer; import org.junit.Test; /**  * @author Sico   ...

  8. python excel to mysql

    import sys import xlrd import pymysql import math import json from collections import OrderedDict # ...

  9. 无法将类型为“System.Xml.XmlComment”的对象强制转换为类型“System.Xml.XmlElement”

    今天开发C#项目时,有一个需要读取XML的功能点.编码过程中遇到了如标题所示的异常,如下图所示: 查询官网后得知XmlComment是注释节点的类型,如下图所示: 于是得出结论,使用XmlDocume ...

  10. 我对android handle更新UI 的一些理解

    1.handle可以方便快捷地管理子线程对主线程UI 的更新, 2.如果不用handle,当多个子线程同时请求更新UI 时,UI更新操作就无法进行