题目
A. Inna and Alarm Clock
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let’s suppose that Inna’s room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.
The morning has come. All n alarm clocks in Inna’s room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:
- First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
- Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.
Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100).
Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.
Output
In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.
Examples
input
4
0 0
0 1
0 2
1 0
output
2
input
4
0 0
0 1
1 0
1 1
output
2
input
4
1 1
1 2
2 3
3 3
output
3
Note
In the first sample, Inna first chooses type “vertical segments”, and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.
In the third sample it is important to note that Inna doesn’t have the right to change the type of the segments during the game. That’s why she will need 3 horizontal or 3 vertical segments to end the game.
题目理解
这道题给了一个100x100的房间,设定以左下角为原点(0,0),给出一系列坐标为闹钟所在位置。首先Inna选择一个方向,横向或者纵向,然后Inna选择一个坐标,在选定的方向走多步,遇到闹钟则会关掉。求Inna最少需要选择多少个坐标。
解题思路
这算是一道贪婪题目,横纵方向只能选一次,则要么是横向,要么纵向,Inna一次可以走多步,所以在同一横纵方向上的所有坐标都视为一次选择。通过每个坐标的横纵坐标,获取每种横纵坐标的个数,其最小值则为Inna的最小选择值。
代码如下(python)
a = int(input().strip())
v = set()
h = set()
for i in range(a):
cnt = [int(x) for x in input().strip().split(' ')]
v.add(cnt[0])
h.add(cnt[1])
print(min(len(v), len(h)))
代码解析
v = set()
选择set数据结构,因为set会自动除重,所以相同的坐标只会被计算一次。