[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 ...
随机推荐
- 交叉编译qt5.6
按照网上的攻略编译QT5.6 https://www.lijingquan.net/2016/07/08/build-kernel-busybox-qt5-6-tslib-imx28/ 出现问题,找不 ...
- Azure Cloud Service - PaaS
使用Azure Cloud Service有一段时间了,前阵子在公司内部做一个Cloud Service培训的时候就在想,能不能用一幅图把Cloud Service所涉及的概念都罗列出来.于是就有了下 ...
- TouTiao开源项目 分析笔记3
1.搭建NewsTabLayout片段 1.1.加载布局 @Nullable @Override public View onCreateView(LayoutInflater inflater, @ ...
- Android开发——View滑动的三种实现方式
0. 前言 Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...
- android gradle.properties
gradle.properties 里面配置的东西,在gradle 文件里面可以直接引用. 例如: 在你工程根目录的gradle.properties 文件里面 可以这样配置: ## Project- ...
- Python语法之com[1][:-7]
strCom = com[0] + ": " + com[1][:-7] 如上应该是一个字符串合成,最后的[1][:-7],我理解是去除com[1]的最后7个字符. 比如com[0 ...
- [转]全图形PPT设计指南
三.什么时候使用 全图形PPT并不适用于所有时候,一般来说,我们在以下场合可以考虑使用:陈述一个故事.名人简介.产品介绍.读书笔记.心灵鸡汤.生活情趣等. 四.如何制作全图形PPT 全图形PPT的制作 ...
- Github前端项目排名
Github前端项目排名(2016-04-04) 一.前言 近几年前端技术日新月异,从 RequireJS 到 AngularJS 再到 React,似乎每天都有新的技术诞生.而大神们总能第一时间 ...
- 《数据结构》C++代码 前言
现在大二正在上<数据结构>课,课内的书上代码实现很喜欢无脑用类.变量名字很长,而且常常实现太繁琐,并且代码有些无法运行,这些对于老手无所谓,但初学者看起来却会很不舒服.因此写点自己实现这些 ...
- c语言字符串内存分配小记
一.疑问 有这样一道题: #include "stdio.h" int main() { ]; ]; scanf("%s", word1); scanf(&qu ...