[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 ...
随机推荐
- ROW_NUMBER() OVER的用法
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) //PARTITION 分割 一.ROW_NUMBER() OVER ...
- MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”
MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...
- Java线程基础实例
概述 Java线程是一个在实战开发中经常使用的基础功能,而在Java中线程相关的类在java.lang和java.util.concurrent里 Thread package thread.base ...
- 【译】RabbitMQ:工作队列(Work Queue)
在第一篇我们写了两个程序通过一个命名的队列分别发送和接收消息.在这一篇,我们将创建一个工作队列在多个工作线程间分发耗时的工作任务. 工作队列的核心思想是避免立刻处理资源密集型任务导致必须等待其执行完成 ...
- linux学习之——vim简明教程
摘自 http://blog.csdn.net/niushuai666/article/details/7275406 ——————————正文开始—————————— 你想以最快的速度学习人类史上 ...
- Javascript.ReactNative-2-javascript-syntax-in-react-native
JavaScript Syntax in React Native Contents: Arrow Function Let+Const Default + Rest + Spread Destruc ...
- oracle 密码过期处理
1.查看用户的proifle是哪个,一般是default sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的密码有 ...
- io流(详询请加qq:2085920154)
import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ioTest ...
- 20151207Study
Liberal lawmakers proposed a bill to reduce the cost of medicine for older Americans.自由主义立法者提出一条减少老年 ...
- 图解VMware内存机制
在写<VMware内存机制初探>之后,原本是计划写一篇<VMware内存机制再探>的,讲一讲VMware内存机制中的另外几个重要内容,比如透明内存共享(TPS, Transpa ...