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的更多相关文章

  1. [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 ...

  2. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  3. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  4. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  5. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  6. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  7. [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 ...

  8. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

  9. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

随机推荐

  1. .net core 安装失败 的问题彻底解决

    解决方法: 已经整理好包:   https://pan.baidu.com/s/1dFuU80p 下载解压运行: DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.e ...

  2. Camstar Portal modeling user guid --WorkCenter workCell workStation的关系

    业务需求:如何让用户登陆时选择  生产线(如:SMT生产线) Operation(如:维修员)  工作单元(如:印刷段) 工作站(如:上板) 关系图: 从上图可以看出: workCell是Resour ...

  3. Camstar Portal modeling user guid --自定义用户菜单

    通过studio 创建 menu definition 创建菜单 创建成功后到employee界面设置对应菜单就可以了

  4. MySQL查询今天/本周/上周/本月/上个月份的数据

    MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看. 查询当前今天的数据 SELECT name,sub ...

  5. 带锁的3D切割轮播图

    3D切割轮播图. 加入锁,限制点击太快次数 <!DOCTYPE html><html><head lang="en"> <meta cha ...

  6. Linux上的常用软件

    zsh (fish): a new shell oh my zsh: zsh's conf file apvlv: pdf viewer(vim style) autojump: quick 'cd'

  7. C++ 中数串互转、进制转换的类

    /******************************************************************** created: 2014/03/16 22:56 file ...

  8. iScroll-5 API 中文版

    http://wiki.jikexueyuan.com/project/iscroll-5/ http://www.mamicode.com/info-detail-331827.html IScro ...

  9. springmvc 学习笔记_1

    一.回顾JavaWeb中的MVC设计模式 1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域 2)今天说的MVC特指一种表现层设计模式,不限于Java语言 二.回顾struts2+ ...

  10. java多线程详解(4)-多线程同步技术与lock

    前言:本篇文章是对Synchronized和java.util.concurrent.locks.Lock的区别进行了详细的分析介绍 上一篇文章末最后介绍了synchronized的一些缺陷,本文主要 ...