Assume you have k<=10^5 intervals [a_i, b_i] \in [1,10^18] (some of them may overlap), and you need to choose a set of intervals mutually disjoint such that their union is maximal. Not maximum number of disjoint intervals, but the union must cover the most.

Can't try all possible subsets 2^k infeasible. Greedy approaches ordering by a_i ( interval covering algorithm) and ordering by b_i ( maximum number of disjoint intervals algorithm ) didn't work Can't figure out if there is a dynamic program solution. Given the size of the input, I think the solution should be O(k log k) or O(k)

Examples 1. [1,4], [3,5], [5,9], [7, 18] Sol [3,5]u[7,18]

  1. [1,2], [2,6], [3,4], [5,7] Sol [1,2]u[3,4]u[5,7]

  2. [2,30], [25,39], [30,40] Sol [2,30]

分析:https://stackoverflow.com/questions/24026073/algorithm-to-find-maximum-coverage-of-non-overlapping-sequences-i-e-the-weig

Here is an O(nlog n)-time, O(n)-space algorithm. First, sort the array of tuples by their starting position if they are not already in this order. I'll assume zero-based array indices.

Let's call the beginning position of tuple i b(i) and the ending position e(i), so that its total length is e(i) - b(i) + 1. Also let's define a function next(i) that returns the position within the tuple list of the first tuple that can appear to the right-hand side of tuple i. Notice that next(i) can be calculated in O(log n) time with a binary search: just keep all the tuple beginning positions b(i) in an array b[], and search for the first j in the subarray b[i+1 .. n-1] having b[j] > e(i).

Let's define f(i) to be the maximum coverage of any nonoverlapping set of tuples that begins at or after tuple i. Since tuple i itself is either in this optimal set or not, we have:

f(i) = max(e(i) - b(i) + 1 + f(next(i)), f(i+1)) for 0 <= i < n

We also have the boundary condition f(n) = 0.

Clearly the largest possible coverage is given by f(0). This is easily calculated. In pseudo-C++:

 int b[] = /* Tuple beginning positions, in nondecreasing order */;
int e[] = /* Tuple end positions */;
int n = /* Number of tuples */; // Find the array position of the leftmost tuple that begins to the right of
// where tuple i ends.
int next(int i) {
return upper_bound(b + i + , b + n, e[i]);
} int maxCov[n + ]; // In practice you should dynamically allocate this // After running this, maxCov[i] will contain the maximum coverage of any
// nonoverlapping subset of the set of n - i tuples whose beginning positions
// are given by b[i .. n-1] and whose ending points are given by e[i .. n-1].
// In particular, maxCov[0] will be the maximum coverage of the entire set.
void calc() {
maxCov[n] = ;
for (int i = n - ; i >= ; --i) {
maxCov[i] = max(e[i] - b[i] + + maxCov[next(i)], maxCov[i + ]);
}
}

The loop in calc() runs n times, and each iteration makes one O(log n) call to the binary search function upper_bound().

We can reconstruct an actual set of this size by calculating both the inputs to max() for f(0), seeing which one actually produced the maximum, recording whether it implies the presence or absence of tuple 0, and then recursing to handle the remainder (corresponding to either f(next(0)) or f(1)). (If both inputs are equal then there are multiple optimal solutions and we can follow either one.)

Max coverage disjoint intervals的更多相关文章

  1. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  3. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  4. 352[LeetCode] Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  5. 352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  6. [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  7. LeetCode-Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  9. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

随机推荐

  1. 搭建自己的博客(二十二):通过ajax提交评论信息,并增加公式编辑功能

    编辑功能使用到了ckeditor的MathJax组件.ajax提交评论可以不用刷新浏览器. 1.变化的部分

  2. TensorFlow(三):非线性回归

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 非线性回归 # 使用numpy生成200个随机 ...

  3. 【Python代码】随机抽取文件名列表NameList中的Name作为训练集

    #!/usr/bin/env python #coding=utf-8 #随机抽取一部分图片作为测试集 import random NameList=[]#存储所有图片名字 ''' NameListP ...

  4. vue-cli构建路径加载资源出错问题

    这份文档是对应 @vue/cli 3.x 版本的,官方文档https://cli.vuejs.org/zh/guide/ 项目打包执行npm run build的时候,打开dist目录的index.h ...

  5. 2018-2019-2 《网络对抗技术》 Exp7 网络欺诈防范 20165222

    1.实践目标 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 2.实践内容 (1)简单应用SET工具建立冒名网站 (1分) apachectl start开启服务 ser ...

  6. SQL按照顺序时间段统计

    借助master..spt_values表 按照时间(半小时)划分统计时间段: select ,dateInfo.dday) as time) StartTime, ,),dateInfo.dday) ...

  7. ubuntu虚拟机安装及vim配置问题(转载)

    virtual box虚拟机Ubuntu系统安装及共享文件夹建立 https://blog.csdn.net/qq_27905767/article/details/82106753 Linux超全v ...

  8. 淘宝npm镜像安装失败的问题

    一:背景 心血来潮要简单搞一搞前端运行.打包的东西.结果第一步通过npm安装淘宝npm的时候就出问题了,如图: 二:解决方法 图片显示有点垃圾,但是看出来“Missing write access t ...

  9. Facebook libra白皮书

    https://libra.org/en-US/white-paper/ An Introduction to Libra Libra的使命是建立一个简单的全球货币和金融基础设施,为数十亿人服务.该文 ...

  10. PAT 甲级 1054 The Dominant Color (20 分)(简单题)

    1054 The Dominant Color (20 分)   Behind the scenes in the computer's memory, color is always talked ...