8295: Cow Lineup

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

题目描述

农夫约翰请了一位专业摄影师给他的一些奶牛拍照。

约翰拥有多种不同品种的奶牛,他希望照片中包含每个品种的奶牛至少一头。

约翰的 N 头奶牛沿一条直线站立在不同的位置。

每头奶牛用其所在位置坐标以及其品种编号来描述。

约翰计划为一段连续区间内的奶牛进行拍照,照片的成本等于它的尺寸大小,即照片中位于两端的奶牛的位置坐标之差。

请帮助约翰计算包含每个品种的奶牛至少一头的照片的最低成本。

输入格式

第一行包含整数 N

接下来 N 行,每行包含两个整数 x,ID,表示一头奶牛的坐标位置以及品种编号。

输出格式

输出满足条件的照片的最低成本。

数据范围

1≤N≤50000,
1≤x,ID≤1e9

输入样例:

6
25 7
26 1
15 1
22 3
20 1
30 1

输出样例:

4

样例解释

位置范围 [22,26] 可包含全部 3 个不同品种的奶牛。

Problem 2: Cow Lineup [Brian Dean]


Farmer John has hired a professional photographer to take a picture of some
of his cows.  Since FJ's cows represent a variety of different breeds, he
would like the photo to contain at least one cow from each distinct breed
present in his herd.


FJ's N cows are all standing at various positions along a line, each
described by an integer position (i.e., its x coordinate) as well as an
integer breed ID.  FJ plans to take a photograph of a contiguous range of
cows along the line.  The cost of this photograph is equal its size -- that
is, the difference between the maximum and minimum x coordinates of the
cows in the range of the photograph.  


Please help FJ by computing the minimum cost of a photograph in which there
is at least one cow of each distinct breed appearing in FJ's herd.


PROBLEM NAME: lineup


INPUT FORMAT:


* Line 1: The number of cows, N (1 <= N <= 50,000).


* Lines 2..1+N: Each line contains two space-separated positive
       integers specifying the x coordinate and breed ID of a single
       cow.  Both numbers are at most 1 billion.


SAMPLE INPUT (file lineup.in):


6
25 7
26 1
15 1
22 3
20 1
30 1


INPUT DETAILS:


There are 6 cows, at positions 25,26,15,22,20,30, with respective breed IDs
7,1,1,3,1,1.


OUTPUT FORMAT:


* Line 1: The smallest cost of a photograph containing each distinct
       breed ID.


SAMPLE OUTPUT (file lineup.out):


4


OUTPUT DETAILS:


The range from x=22 up through x=26 (of total size 4) contains each of the
distinct breed IDs 1, 3, and 7 represented in FJ's herd.

输入格式

INPUT FORMAT:


* Line 1: The number of cows, N (1 <= N <= 50,000).


* Lines 2..1+N: Each line contains two space-separated positive
       integers specifying the x coordinate and breed ID of a single
       cow.  Both numbers are at most 1 billion.


输出格式

OUTPUT FORMAT:


* Line 1: The smallest cost of a photograph containing each distinct
       breed ID.

输入样例 复制

6
25 7
26 1
15 1
22 3
20 1
30 1

输出样例 复制

4