题目描述

Farmer John's N cows (1 <= 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 has breed b_i (either 'G' for Guernsey or 'H' for Holstein).  No two cows occupy the same position.

FJ wants to take a photo of a contiguous interval of cows for the county fair, but we wants all of his breeds to be fairly represented in the photo.Therefore, he wants to ensure that, for whatever breeds are present in the photo, there is an equal number of each breed (for example, a photo with all Holsteins is ok, a photo with 27 lsteins and 27 Guernseys is ok, but a photo with 10 Holsteins and 9 Guernseys is not ok).

Help FJ take his fair photo by finding the maximum size of a photo that satisfies FJ's constraints.  The size of a photo is the difference between the maximum and
minimum positions of the cows in the photo.  It is possible that FJ could end up taking a photo of just a single cow, in which case this photo would have size zero.

PROBLEM NAME: fairphoto

FJ的N只牛(1 <= N <= 100,000)站在一排长长的栅栏前的不同位置。第i只牛站在位置xi(0...,1,000,000,000),且它的品种是bi(或者是G,或者是H)。任意的两只牛不会占着同一个位置。

FJ想为连续区间的牛照一张相片,使得照片上两品种的牛的数目是公平的。例如照片上所有的牛品种都是H,一照片上有27个G品种,27个H品种都是可以的,但是如果一照片上有10个H品种,9个G品种就不行。

请帮助FJ照一张公平的照片且照片尺寸最大。照片尺寸为照片中最大位置与最小位置差。如果最终照一张只包含一只牛的照片,那么这张照片的尺寸为0.

输入

* Line 1: The integer N.

* Lines 2..1+N: Line i+1 contains x_i and b_i.

输入的第1行为整数N

第2...i+1行,每行包含整数xi与bi

输出

* Line 1: A single integer indicating the maximum size of a fair   photo.

输出仅有一个整数,表示可以照一张公平的照片的最大尺寸。

样例输入

6
4 G
10 H
7 G
16 G
1 G
3 H

样例输出

7

提示

INPUT DETAILS:

There are six cows with breeds (from left to right) G, H, G, G, H, G.

OUTPUT DETAILS:

The largest fair photo Farmer John can take is of the middle 4 cows,containing 2 Holsteins and 2 Guernseys.

我们把G看成-1,H看成1,求前缀和

-1,0,-1,-2,-1,-2

很明显,如果[x,y]满足题目要求,那么b[x-1]=b[y](b前缀和数组)

我们存下每个前缀和值最早的出现位置,然后遍历整个前缀和数组,对于每一个值,查找它的最小位置,算出ans

注意:0的最小位置应该是0,为了避免数组越界(前缀和为负数),我们存起来的时候要+n

代码是我好久以前写的,所以可能有点丑(其实是模拟赛做到跟这差不多的突然想加博客)

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct cow{
int w;char c;
}ccow[];
bool cmp(cow a,cow b)
{
return a.w<b.w;
}
int hg[];
int big[];
int sta[];
int main()
{
memset(big,-,sizeof(big));memset(sta,-,sizeof(sta));
int n,MAX=-;
cin>>n;
for(int i=;i<=n;i++)cin>>ccow[i].w>>ccow[i].c;
sort(ccow+,ccow+n+,cmp);
hg[]=;
sta[n]=;char dgh=ccow[].c;
int len=;
for(int i=;i<=n;i++)
{
if(ccow[i].c==dgh)len+=(ccow[i].w-ccow[i-].w);
else {MAX=max(MAX,len);len=;dgh=ccow[i].c;}
}
MAX=max(MAX,len);
for(int i=;i<=n;i++)
{
hg[i]=hg[i-];
if(ccow[i].c=='H')hg[i]++;
else hg[i]--;
if(sta[hg[i]+n]==-)sta[hg[i]+n]=i;
else big[hg[i]+n]=i;
}
for(int i=;i<=*n;i++)
{
if(big[i]!=-)MAX=max(MAX,ccow[big[i]].w-ccow[sta[i]+].w);
}
cout<<MAX;
}

[9018_1592]USACO 2014 Open Silver Fairphoto的更多相关文章

  1. USACO 2014 Open Silver Fairphoto

    这道题只是银牌组的第一题而我就写了 3K 的代码.唉. Description - 问题描述 FJ's N cows (2 <= N <= 100,000) are standing at ...

  2. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  3. USACO翻译:USACO 2014 FEB SILVER 三题

    USACO 2014 FEB SILVER 一.题目概览 中文题目名称 自动打字 路障 神秘代码 英文题目名称 auto rblock scode 可执行文件名 auto rblock scode 输 ...

  4. USACO翻译:USACO 2014 MARCH Silver三题

    USACO 2014 MARCH 一.题目概览 中文题目名称 农田灌溉 懒牛 牛叫 英文题目名称 irrigation lazy mooomoo 可执行文件名 irrigation lazy mooo ...

  5. USACO翻译:USACO 2014 US Open 三题

    USACO 2014 US Open 一.题目概览 中文题目名称 牧场装饰 里程表 牛像展览 英文题目名称 decorate odometer fairphoto 可执行文件名 decorate od ...

  6. USACO翻译:USACO 2014 JAN三题(2)

    USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...

  7. USACO翻译:USACO 2014 JAN三题(1)

    USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...

  8. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  9. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

随机推荐

  1. 数据结构学习-AVL平衡树

    环境:C++ 11 + win10 IDE:Clion 2018.3 AVL平衡树是在BST二叉查找树的基础上添加了平衡机制. 我们把平衡的BST认为是任一节点的左子树和右子树的高度差为-1,0,1中 ...

  2. 图像的模糊-opencv

    调用两个API,一个是均值模糊,一个是高斯模糊.如下所示: #include<opencv2/opencv.hpp> #include<iostream> using name ...

  3. Card Hand Sorting 18中南多校第一场C题

    一.题意 随机给你一堆牌(标准扑克牌),之后让你按照: 第一优先规则:所有相同花色的在一起 第二优先规则:所有相同花色的必须按照升序或者降序排列 问,你最少要拿出多少张牌插入到其他的地方以维持这个状况 ...

  4. JAVA-数组或集合

    哈哈,今天我们来讲解一下有关于一些数组 或者是集合的知识点 1.ArrayList,LinkedList,Vector的区别 ArrayList,LinkedList,Vector都是实现List接口 ...

  5. 如何使用Python脚本

    来自官方文档 一.写 python 脚本: import sys import datetime for line in sys.stdin: line = line.strip() userid, ...

  6. 8 实现10mins用户登录与注册

    1.重新认识登录 2.实现登录功能 (1)Django 自带的authenticate, login模块 from django.contrib.auth import authenticate, l ...

  7. 让NVelocity做更多的事,VS Extension+NVelocity系列

    我不知道园子里到底有多少人喜欢使用NVelocity这个模板引擎,其实说实话,如果现在让我选,我对Razor的喜好要比NVelocity或者T4等等的模板引擎更多一些,当然了,个人看法而已.只是我在公 ...

  8. Android保持屏幕常亮唤醒状态

    第一步:  首先添加权限: <uses-permission android:name="android.permission.WAKE_LOCK"></uses ...

  9. Mecanim动画

    1.基础 现在Animation编辑器给个模型设计一个动画,都会自动为此模型加上Animator组件,并产生一个controller后缀的控制器和一个相关的anim后缀的动画剪辑, unity根据An ...

  10. PHP页面跳转总结

    一.使用php内置函数:header()函数 <?php$url='./test.php'; header("Location:$url"); ?> 注意Locatio ...