由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 分享今天做的一道基础题
相关主题
interval tree vs. merge intervals问个google面试题
Merge Interval那道题Apple iCloud 电面
insert interval 没必要二分吧问个merge interval的变体题
问一个题目merge intervals问个C的基本问题
问一道题狗电面
问道排序题JAVA里sort的algorithm time complexity是多少
问个算法题, 关于区间 overlap的怎么理解递归解决的“swap every two elements in a linked list”?
新鲜G面筋(Fail)问题:Find the minimum number of "swaps" needed to sort an array
相关话题的讨论汇总
话题: int话题: intervals话题: beg话题: segment话题: end
进入JobHunting版参与讨论
1 (共1页)
w****x
发帖数: 2483
1
算法导论上的一道, 觉得蛮好的...
/*
Consider a sorting problem in which the numbers are not known exactly.
Instead, for each number, we know an interval on the real line to which it
belongs.
That is, we are given n closed intervals of the form [ai, bi], where ai ≤
bi.
The goal is to fuzzy-sort these intervals, i.e., produce a permutation 〈i1,
i2,..., in〉
of the intervals such that there exist , satisfying c1 ≤ c2 ≤ ··· ≤ cn.
Design an algorithm for fuzzy-sorting n intervals.
Your algorithm should have the general structure of an algorithm
that quicksorts the left endpoints (the ai 's), but it should take advantage
of overlapping intervals to improve the running time.
(As the intervals overlap more and more, the problem of
fuzzy-sorting the intervals gets easier and easier.
Your algorithm should take advantage of such overlapping, to the extent that
it exists.)
*/
struct SEGMENT
{
int a;
int b;
};
void partition(SEGMENT a[], int beg, int end, int& x, int& y)
{
if (beg >= end) return;
int i = beg;
for (int j = beg; j < end; j++)
{
if (a[j].a <= a[end].a)
swap(a[i++], a[j]);
}
swap(a[i], a[end]);
int k = beg;
for (int j = beg; j < i; j++)
{
if (a[j].b < a[i].b)
swap(a[k++], a[j]);
}
x = k-1;
y = i+1;
}
void seg_sort(SEGMENT a[], int b, int e)
{
if (NULL == a || b >= e)
return;
int x,y;
partition(a, b, e, x, y);
seg_sort(a, b, x);
seg_sort(a, y, e);
}
void segmentSort(SEGMENT a[], int n)
{
if (NULL == a || n <= 0)
return;
seg_sort(a, 0, n-1);
}
s***u
发帖数: 101
2
请教一下, 第一部分中
int i = beg;
for (int j = beg; j < end; j++)
{
if (a[j].a <= a[end].a)
swap(a[i++], a[j]);
}
swap(a[i], a[end]);
这样不能够把和 end overlap 的interval 全部放在end之前吧
是不是要写成:
if (a[j].a <= a[end].b) ?
l*********8
发帖数: 4642
3
怎样定义intervals 之间的大小关系?

i1,
cn.

【在 w****x 的大作中提到】
: 算法导论上的一道, 觉得蛮好的...
: /*
: Consider a sorting problem in which the numbers are not known exactly.
: Instead, for each number, we know an interval on the real line to which it
: belongs.
: That is, we are given n closed intervals of the form [ai, bi], where ai ≤
: bi.
: The goal is to fuzzy-sort these intervals, i.e., produce a permutation 〈i1,
: i2,..., in〉
: of the intervals such that there exist , satisfying c1 ≤ c2 ≤ ··· ≤ cn.

w****x
发帖数: 2483
4

大概就是先把起点小于pivot segment的intervals排出在左部, 然后再在那部分
intervals里面排出end point大于pivot interval起点的线段, 这部分线段有共同的
overlap就当已经排好序了

【在 s***u 的大作中提到】
: 请教一下, 第一部分中
: int i = beg;
: for (int j = beg; j < end; j++)
: {
: if (a[j].a <= a[end].a)
: swap(a[i++], a[j]);
: }
: swap(a[i], a[end]);
: 这样不能够把和 end overlap 的interval 全部放在end之前吧
: 是不是要写成:

1 (共1页)
进入JobHunting版参与讨论
相关主题
问题:Find the minimum number of "swaps" needed to sort an array问一道题
请教2个 huge file的面试题问道排序题
问个sorting的题目问个算法题, 关于区间 overlap的
bloomberg刚店面晚。 悔阿新鲜G面筋(Fail)
interval tree vs. merge intervals问个google面试题
Merge Interval那道题Apple iCloud 电面
insert interval 没必要二分吧问个merge interval的变体题
问一个题目merge intervals问个C的基本问题
相关话题的讨论汇总
话题: int话题: intervals话题: beg话题: segment话题: end