[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 ...
随机推荐
- python——字符串的操作判断
s为字符串 s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False. s.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False. s ...
- Javascript Step by Step - 03
前言 ajax 即“Asynchronous JavaScript and XML”(异步的JavaScript和XML).现在这个词的覆盖面有所扩展,把允许浏览器与服务器通信而无需刷新当前页面的技术 ...
- P2985 [USACO10FEB]吃巧克力Chocolate Eating
P2985 [USACO10FEB]吃巧克力Chocolate Eating 题目描述 Bessie has received N (1 <= N <= 50,000) chocolate ...
- 剑指Offer - 九度1510 - 替换空格
剑指Offer - 九度1510 - 替换空格2013-11-29 20:53 题目描述: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之 ...
- 剑指Offer - 九度1351 - 数组中只出现一次的数字
剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目5
2014-03-20 03:23 题目:给定一个字符串,输出其全排列. 解法:可以调用STL提供的next_permutation(),也可以自己写一个.对于这种看起来简单的题目,应该在能优化的地方, ...
- markdown备忘
文章主要(99%)参考自:markdown基本语法 这是二级标题 这是加粗的文字 这是倾斜的文字` 这是引用的内容 这是引用的内容 以下是分割线: 超链接 列表内容1 列表内容2 列表内容3 1.列表 ...
- [C++] 拓展属性
inline函数 函数重载 占位参数和默认参数 /*__________________________________________________________________ 背景: C++ ...
- java单例模式(类只能创建唯一对象)
//饿汉式 class Single { private static final Single s= new Single(); private Single(){} public static S ...
- 上手Caffe(二)
@author:oneBite本文简述如何在windows环境下,运行caffe的“hello world”例程体会适用caffe的流程:转换输入数据格式>在solver.prototxt中配置 ...