8377: Fair Photography

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:19 通过:8

题目描述

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 Holsteins 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.


农民约翰的N头牛(1头<=10万头)站在一条长长的一维围栏旁的不同位置。第i头母牛站在x\u i位置(0…100000000范围内的整数),并且拥有b\u i品种(格恩西岛为“G”,荷斯坦为“H”)。没有两头母牛占据同一位置。FJ想为县集市拍摄连续间隔的奶牛照片,但我们希望他的所有品种都能在照片中得到公平的代表。因此,他希望确保,对于照片中出现的任何品种,每个品种的数量都是相等的(例如,一张所有霍尔斯泰犬的照片是可以的,一张有27只霍尔斯泰犬和27只根西岛犬的照片是可以的,但一张有10只霍尔斯泰犬和9只根西岛犬的照片是不可以的)。通过找到满足FJ限制条件的照片的最大尺寸,帮助FJ拍摄自己的照片。照片的大小是照片中奶牛的最大和最小位置之间的差异。有可能FJ最终只拍了一头牛的照片,在这种情况下,这张照片的尺寸为零。

输入格式

* Line 1: The integer N. * Lines 2..1+N: Line i+1 contains x_i and b_i.
There are six cows with breeds (from left to right) G, H, G, G, H, G.

输出格式

* Line 1: A single integer indicating the maximum size of a fair photo.
The largest fair photo Farmer John can take is of the middle 4 cows, containing 2 Holsteins and 2 Guernseys.

输入样例 复制

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

输出样例 复制

7