[9018_1592]USACO 2014 Open Silver Fairphoto
题目描述
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的更多相关文章
- USACO 2014 Open Silver Fairphoto
这道题只是银牌组的第一题而我就写了 3K 的代码.唉. Description - 问题描述 FJ's N cows (2 <= N <= 100,000) are standing at ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2014 FEB SILVER 三题
USACO 2014 FEB SILVER 一.题目概览 中文题目名称 自动打字 路障 神秘代码 英文题目名称 auto rblock scode 可执行文件名 auto rblock scode 输 ...
- USACO翻译:USACO 2014 MARCH Silver三题
USACO 2014 MARCH 一.题目概览 中文题目名称 农田灌溉 懒牛 牛叫 英文题目名称 irrigation lazy mooomoo 可执行文件名 irrigation lazy mooo ...
- USACO翻译:USACO 2014 US Open 三题
USACO 2014 US Open 一.题目概览 中文题目名称 牧场装饰 里程表 牛像展览 英文题目名称 decorate odometer fairphoto 可执行文件名 decorate od ...
- USACO翻译:USACO 2014 JAN三题(2)
USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...
- USACO翻译:USACO 2014 JAN三题(1)
USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
随机推荐
- POJ:2100-Graveyard Design(尺取)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 8504 Accepted: 2126 Cas ...
- 读懂CCS链接命令文件(.cmd)
链接器的核心工作就是符号表解析和重定位,链接命令文件则使得编程者可以给链接器提供必要的指导和辅助信息.多数时候,由于集成开发环境的存在,开发者无需了解链接命令文件的编写,使用默认配置即可.但若需要对计 ...
- Oozie 实战之 Hive
1.编辑job.propertiers nameNode=hdfs://cen-ubuntu.cenzhongman.com:8020 jobTracker=localhost:8032 queueN ...
- TouTiao开源项目 分析笔记2
1.Constant常量定义类 1.1.源代码 public class Constant { public static final String USER_AGENT_MOBILE = " ...
- P2580 于是他错误的点名开始了(trie)
P2580 于是他错误的点名开始了 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉 ...
- P3819 松江1843路(洛谷月赛)
P3819 松江1843路 题目描述 涞坊路是一条长L米的道路,道路上的坐标范围从0到L,路上有N座房子,第i座房子建在坐标为x[i]的地方,其中住了r[i]人. 松江1843路公交车要在这条路上建一 ...
- 容器技术Docker
什么是decker容器 简单理解就是将代码和部署环境一起打包的一个容器
- easyui js拼接html,class属性失效的问题
问题:要在前一个按钮之后添加相同的样式的按钮,通过$("#cj").html(str); 这样的形式添加,却不能添加上样式 <div id="btn" c ...
- USACO刷题之路,开始了
几天前,重新开始刷题了. 重新刷题有几个原因: 1.曾经的OI经历,如今除了悟性高些.知识多些,大多已经遗忘.不希望真的让之前的OI水平就这么丢了. 2.越来越觉得,刷题真的是一件很开心的事情.大学中 ...
- 那些牛掰的 HTML5的API(二)
1:视频播放器 2:地理定位 我们的支持html5 的浏览器给我们提供一个接口(api),可以用来获取你当前的位置. 主要是通过geolocation(地理位置),对象 ,去访问硬件,来获取到经纬度. ...