[UCSD白板题] Covering Segments by Points
Problem Introduction
You are given a set of segments on a line and your goal is to mark as few points on a line as possible so that each segment contains at least one marked point.
Problem Description
Task.Given a set of n segments \(\{ [a_0, b_0], [a_1, b_1] \cdots [a_{n-1}, b_{n-1}] \}\) with integer coordinates on a line, find minimum number \(m\) of points such that each segment contains at least one point. That is, find a set of integers \(X\) of the minimum size such that for any segment \([a_i, b_i]\) there is a point \(x \in X\) such that \(a_i \leq x \leq b_i\).
Input Format.
The first line of input contains the number \(n\) of segments. Each of the following line contains the two integers \(a_i\) and \(b_i\)(separated by a space) defining the endpoints of the i-th segment.
Constraints.
$1 \leq n \leq 100; 0 \leq a_i \leq b_i \leq10^9; $ for all \(0 \leq i < n\).
Output Format.
Output the minimum number \(m\) of points on the first line and the integer coordinates of \(m\) points(separated by a space) on the second line. You can output the points in any orders. If there are many such sets of points, you can output any set. (It is not difficult to see that there always exists a set of points of the minimum size such that all the coordinates of the points are integers.)
Sample 1.
Input:
3
1 3
2 5
3 6
Output:
1
3
Sample 2.
Input:
4
4 7
1 3
2 5
5 6
Output:
2
3 6
Solution
# Uses python3
import sys
from collections import namedtuple
from operator import itemgetter
Segment = namedtuple('Segment', 'start end')
def optimal_points(segments):
points = []
endpoint = -1
segments = sorted(segments, key=itemgetter(1))
for segment in segments:
if endpoint < segment.start:
endpoint = segment.end
points.append(endpoint)
return points
if __name__ == '__main__':
input = sys.stdin.read()
n, *data = map(int, input.split())
segments = list(map(lambda x: Segment(x[0], x[1]), zip(data[::2], data[1::2])))
points = optimal_points(segments)
print(len(points))
for p in points:
print(p, end=' ')
[UCSD白板题] Covering Segments by Points的更多相关文章
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [UCSD白板题] Number of Inversions
Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
随机推荐
- 转载:Solr的自动完成实现方式(第三部分:Suggester方式续)
转自:http://www.cnblogs.com/ibook360/archive/2011/11/30/2269126.html 在之前的两个部分(part1.part2)中,我们学会了如何配置和 ...
- X-Frame-Options 响应头
[X-Frame-Options 响应头] 参考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/X-Frame-Options
- C# WebBrowser 网页缩放的方法
1.引用COM:MicroSoft Internet Controls 2. 核心代码如下: private void button2_Click(object sender, EventArgs e ...
- Redis在windows下安装和PHP中使用
1.redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- WebLogic 中的基本概念
完全引用自: WebLogic 中的基本概念 WebLogic 中的基本概念 上周参加了单位组织的WebLogic培训,为了便于自己记忆,培训后,整理梳理了一些WebLogic的资料,会陆续的发出来, ...
- Hibernate报错:org.hibernate.ObjectNotFoundException: No row with the given identifier exists 解决办法
报错信息: org.hibernate.event.internal.DefaultLoadEventListener onLoad INFO: HHH000327: Error performing ...
- Pow 算法
#include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...
- libcurl 函数curl_easy_perform在release下崩溃的问题
今天遇到一个很奇怪的问题: 工程中用到了libcurl, debug可以正常运行,release每次都崩溃,断到curl_easy_perform这一行.堆栈中也得不到有用信息,于是GOOGLE一番, ...
- System.StackOverflowException的一个例子(转)
今天按着书上的例子写呀写,写了一下午终于做出了一个三层模式的通讯录(当然很简单),但是,弄了最后却碰到个运行时的 异常,弄得我这个asp.net菜鸟郁闷了再郁闷.异常如下:发生类型为 System.S ...
- 简易购物车 --day2
代码段 f =float(input('输入你的工资')) goods=['1.apple','2.mac','3.ph','4.python','5.php'] price=[35,26.5,14, ...