象限覆盖 CodeChef Lighthouses
来源:互联网 发布:阿里云高可用 编辑:程序博客网 时间:2024/05/22 10:35
http://www.codechef.com/problems/LIGHTHSE
PROBLEM:
Given are the coordinates of
大洋上共有 N 个岛屿,从 1 到 N 标号。与广袤的大海相比,我们可以认为每个岛都只是平面上的一个点。给定所有岛屿的笛卡尔坐标,坐标系 X 轴指向东,Y 轴指向北。你需要照亮所有的岛屿。为此,你可以在某些岛屿上修建灯塔。每个岛屿至多只能建一座灯塔。每个灯塔可以照亮以它为原点的坐标系的一个象限,即东北、西北、西南、东南的其中之一。注意如果一个岛屿落在光照的边缘,也被认为被照亮了,这意味着修建灯塔的岛屿总是被照亮的。请你找出一个照亮所有岛屿所需灯塔数(记为 L)最小的方案,并输出方案中每个灯塔的位置和方向。
题解:https://discuss.codechef.com/questions/74774/lighthse-editorial
PROBLEM LINK:
Practice
Contest
Author: Evgenij Artemov
Tester: Kevin Atienza
Editorialists: Pushkar Mishra and Suhash Venkatesh
DIFFICULTY:
Easy-Medium
PREREQUISITES:
Ad-hoc
PROBLEM:
Given are the coordinates of
EXPLANATION:
Subtask 1
In this subtask, there can be a maximum of 8 islands. This clearly hints towards an exponential algorithm. We can try all possible placements of lighthouses and check which one covers all the islands while using the minimum number of lighthouses.
How do we try all possibilities? We can use recursion. Let's say there are
During the recursion, we place a lighthouse on a particular island and then check how many unlit islands it lights. The placement of lighthouse can be done in 4 ways. To keep a track of the way in which the lighthouse was placed, we use base
There are
Subtask 2
Let's consider two particular sets of points:
- The first set consists of all those islands which have the least
x -coordinate value. - The second set consists of all those islands which have the highest
x -coordinate value.
Now, we can make the following observations:
- If in the first set, there is an island such that it has the minimum
y -coordinate amongst all givenN islands, then we can place a lighthouse on it facing in the NE direction. This will illuminate all the islands. - If in the first set, there is an island such that it has the maximum
y -coordinate amongst all givenN islands, then we can place a lighthouse on it facing in the SE direction. This will illuminate all the islands. - If in the second set, there is an island such that it has the minimum
y -coordinate amongst all givenN islands, then we can place a lighthouse on it facing in the NW direction. This will illuminate all the islands. - If in the second set, there is an island such that it has the maximum
y -coordinate amongst all givenN islands, then we can place a lighthouse on it facing in the SW direction. This will illuminate all the islands.
What if none of the aforementioned four conditions are true? Then, we can clearly say that all the islands can't be illuminated by one lighthouse; we need more.
In that case, let's pick up a point from the first set and another from the second set. Let the coordinates of these points be
y1≤y2 In this case, we can place a lighthouse on the island at(x1,y1) facing NE, and another one on the island at(x2,y2) facing SW. This way, we will be able to illuminate all the islands.y1>y2 In this case, we can place a lighthouse on the island at(x1,y1) facing SE, and another one on the island at(x2,y2) facing NW. This way, we will be able to illuminate all the islands.
Thus, we can accomplish the task either by placing 1 or 2 lighthouses, as the case may be. Finding the two sets of points can be done by simple iterations over the set of all the given points. Further steps can also be carried out in
COMPLEXITY:
SAMPLE SOLUTIONS:
Author
Tester
Editorialist
for cas in xrange(input()): n = input() pts = [] for i in xrange(n): x, y = map(int, raw_input().strip().split()) pts.append((x, y)) minx = min(x for x, y in pts) maxx = max(x for x, y in pts) miny = min(y for x, y in pts) maxy = max(y for x, y in pts) for i in xrange(n): if pts[i] == (minx, miny): print "1\n%d NE" % (i+1) break if pts[i] == (minx, maxy): print "1\n%d SE" % (i+1) break if pts[i] == (maxx, miny): print "1\n%d NW" % (i+1) break if pts[i] == (maxx, maxy): print "1\n%d SW" % (i+1) break else: mini = min(xrange(n), key=lambda i: pts[i]) maxi = max(xrange(n), key=lambda i: pts[i]) if pts[mini][1] < pts[maxi][1]: print "2\n%d NE\n%d SW" % (mini+1, maxi+1) else: print "2\n%d SE\n%d NW" % (mini+1, maxi+1)
- 象限覆盖 CodeChef Lighthouses
- 【codechef】Lighthouses
- CodeChef
- CodeChef
- CodeChef
- CodeChef
- (CodeChef
- CodeChef
- CodeChef
- CodeChef
- CodeChef
- CodeChef
- CodeChef
- 四象限分析
- 时间“四象限”法
- 时间“四象限”法
- Magic Quadrant 魔力象限
- 1044 第几象限
- 【IOS 开发学习总结-OC-13】★★objective-c面向对象之——KVC(键值编码)
- hdu 5360 Hiking
- 马哥Linux运维笔记----2
- hihocoder 1158 质数相关(二分图匹配 最大独立集)
- WebServie 基础
- 象限覆盖 CodeChef Lighthouses
- 多线程
- 《剑指Offer》面试题:栈的压入弹出序列是否匹配
- C#软件开发实例.私人订制自己的屏幕截图工具(二)创建项目、注册热键、显示截图主窗口
- Objective-C-----用NSArray显示一年中所有的月份
- Linux中tty框架与uart框架之间的调用关系剖析
- 【JDBC】day04_事务_批处理_自动主键_DAO
- Android Studio导出的密钥签名市场认证安装包
- 最大熵模型总结