假设总体interval的范围是[A,B] 1.所有interval按照ai排序 (nlogn) 2.count the number of intervals that intersect (A+B)/2 O(n) 3.recursively do 1 & 2 for F(A,(A+B)/2), F((A+B)/2, B), update the maximun intersect count 4.base case是一个区间里没有完整的一个interval 跟merge sort差不多思路 time complexity f(n)=2*f(n/2)+O(n) 根据master theorem 总体时间nlogn 故总体时间还是nlogn
【在 w****x 的大作中提到】 : giving lots of intervals [ai, bi], find a point intersect with the most : number of intervals : 要nlogn的解法哦, 要写程序的, segment tree就算了
c*n
18 楼
Sort all N intervals by begin and end time respectively, saying B[] and E[] , for each Interval I(begin, end), find number of intervals with begin time later than I.end, A, and number of intervals with end time early than I. begin, B. Then the the number of intervals intersecting with I is N - A - B - 1. As B[] and E[] are sorted array, A and B can be calculated in lgN, so total time is nlogn.
假设总体interval的范围是[A,B] 1.所有interval按照ai排序 (nlogn) 2.count the number of intervals that intersect (A+B)/2 O(n) 3.recursively do 1 & 2 for F(A,(A+B)/2), F((A+B)/2, B), update the maximun intersect count 4.base case是一个区间里没有完整的一个interval 跟merge sort差不多思路 time complexity f(n)=2*f(n/2)+O(n) 根据master theorem 总体时间nlogn 故总体时间还是nlogn
【在 w****x 的大作中提到】 : giving lots of intervals [ai, bi], find a point intersect with the most : number of intervals : 要nlogn的解法哦, 要写程序的, segment tree就算了
c*n
38 楼
Sort all N intervals by begin and end time respectively, saying B[] and E[] , for each Interval I(begin, end), find number of intervals with begin time later than I.end, A, and number of intervals with end time early than I. begin, B. Then the the number of intervals intersecting with I is N - A - B - 1. As B[] and E[] are sorted array, A and B can be calculated in lgN, so total time is nlogn.
这是Introduction to Algorithms书上的课后题。 是Point of Most intersection( POM)问题。 用sweep line algorithm,要点是将每个区间的左右端点分开并排序, 得到2n个end point数组,简单的做法可以将2N个端点augment +1 和-1计数, 也可以在扫描过程中维持一个当前仍然被交叉的区间的集合(用二叉树),插入和删除 都是log n 这样的算法不仅可以计数还可以输出哪些区间有交叉。 另外还可以变形为求和其他区间交叉最多的segment, 这时需要再保持额外的数据结构,比如当前端点处已经扫描过的左端点数量。
【在 w****x 的大作中提到】 : giving lots of intervals [ai, bi], find a point intersect with the most : number of intervals : 要nlogn的解法哦, 要写程序的, segment tree就算了
【在 w****x 的大作中提到】 : giving lots of intervals [ai, bi], find a point intersect with the most : number of intervals : 要nlogn的解法哦, 要写程序的, segment tree就算了
l*8
44 楼
Input intevals: [10 25] [20 45] [30 48] [40 44] [50 55] Step 1: sort all the numbers and mark '+1' if the number is the start of an interval , mark '-1' if the number is the end of an interval. 10 20 25 30 40 44 45 48 50 55 +1 +1 -1 +1 +1 -1 -1 -1 +1 -1 Step 2: accumulate the markers: 1 2 1 2 3 2 1 0 1 0 The largest one during the accumulation is 3, which corresponds to [40 44]. So every point inside [40 44] intersect with 3 intervals.
【在 h*****g 的大作中提到】 : 还是没懂,谁能给详细解释下? : 最好给个例子
h*g
45 楼
十分感谢!
interval .
【在 l*********8 的大作中提到】 : Input intevals: [10 25] [20 45] [30 48] [40 44] [50 55] : Step 1: : sort all the numbers and mark '+1' if the number is the start of an interval : , mark '-1' if the number is the end of an interval. : 10 20 25 30 40 44 45 48 50 55 : +1 +1 -1 +1 +1 -1 -1 -1 +1 -1 : Step 2: : accumulate the markers: : 1 2 1 2 3 2 1 0 1 0 : The largest one during the accumulation is 3, which corresponds to [40 44].
h*a
46 楼
What if the endpoint of one interval is also the starting point of another interval, how do we put the marks? Thanks.
interval .
【在 l*********8 的大作中提到】 : Input intevals: [10 25] [20 45] [30 48] [40 44] [50 55] : Step 1: : sort all the numbers and mark '+1' if the number is the start of an interval : , mark '-1' if the number is the end of an interval. : 10 20 25 30 40 44 45 48 50 55 : +1 +1 -1 +1 +1 -1 -1 -1 +1 -1 : Step 2: : accumulate the markers: : 1 2 1 2 3 2 1 0 1 0 : The largest one during the accumulation is 3, which corresponds to [40 44].
【在 h****a 的大作中提到】 : What if the endpoint of one interval is also the starting point of another : interval, how do we put the marks? : Thanks. : : interval : .
这是Introduction to Algorithms书上的课后题。 是Point of Most intersection( POM)问题。 用sweep line algorithm,要点是将每个区间的左右端点分开并排序, 得到2n个end point数组,简单的做法可以将2N个端点augment +1 和-1计数, 也可以在扫描过程中维持一个当前仍然被交叉的区间的集合(用二叉树),插入和删除 都是log n 这样的算法不仅可以计数还可以输出哪些区间有交叉。 另外还可以变形为求和其他区间交叉最多的segment, 这时需要再保持额外的数据结构,比如当前端点处已经扫描过的左端点数量。
【在 w****x 的大作中提到】 : giving lots of intervals [ai, bi], find a point intersect with the most : number of intervals : 要nlogn的解法哦, 要写程序的, segment tree就算了
【在 w****x 的大作中提到】 : giving lots of intervals [ai, bi], find a point intersect with the most : number of intervals : 要nlogn的解法哦, 要写程序的, segment tree就算了
l*8
51 楼
Input intevals: [10 25] [20 45] [30 48] [40 44] [50 55] Step 1: sort all the numbers and mark '+1' if the number is the start of an interval , mark '-1' if the number is the end of an interval. 10 20 25 30 40 44 45 48 50 55 +1 +1 -1 +1 +1 -1 -1 -1 +1 -1 Step 2: accumulate the markers: 1 2 1 2 3 2 1 0 1 0 The largest one during the accumulation is 3, which corresponds to [40 44]. So every point inside [40 44] intersect with 3 intervals.
【在 h*****g 的大作中提到】 : 还是没懂,谁能给详细解释下? : 最好给个例子
h*g
52 楼
十分感谢!
interval .
【在 l*********8 的大作中提到】 : Input intevals: [10 25] [20 45] [30 48] [40 44] [50 55] : Step 1: : sort all the numbers and mark '+1' if the number is the start of an interval : , mark '-1' if the number is the end of an interval. : 10 20 25 30 40 44 45 48 50 55 : +1 +1 -1 +1 +1 -1 -1 -1 +1 -1 : Step 2: : accumulate the markers: : 1 2 1 2 3 2 1 0 1 0 : The largest one during the accumulation is 3, which corresponds to [40 44].
h*a
53 楼
What if the endpoint of one interval is also the starting point of another interval, how do we put the marks? Thanks.
interval .
【在 l*********8 的大作中提到】 : Input intevals: [10 25] [20 45] [30 48] [40 44] [50 55] : Step 1: : sort all the numbers and mark '+1' if the number is the start of an interval : , mark '-1' if the number is the end of an interval. : 10 20 25 30 40 44 45 48 50 55 : +1 +1 -1 +1 +1 -1 -1 -1 +1 -1 : Step 2: : accumulate the markers: : 1 2 1 2 3 2 1 0 1 0 : The largest one during the accumulation is 3, which corresponds to [40 44].
【在 h****a 的大作中提到】 : What if the endpoint of one interval is also the starting point of another : interval, how do we put the marks? : Thanks. : : interval : .