2014-05-06 10:18
原题:
Given a set of intervals, find the interval which has the maximum number of intersections (not the length of a particular intersection). So if input (,) (,) (,), (,) should be returned. Some suggest to use Interval Tree to get this done in O(logn), but I did not understand how to construct and use the Interval Tree after reading its wiki page. Is there any other way to do it? If Interval tree is the only option, please educate me how to construct/use one. Thanks.
题目:有一堆一维的区间,请判断其中和其他区间相交次数最多的区间是哪一个。比如例子(1, 6)、(2, 3)、(4, 11),(1, 6)和其他两个相交了,所以是相交最多的。另外,这位叫“Guy”的老兄又在秀自己的无知了。说自己的第一想法是用线段树来解题,然后又说自己看了Wiki以后不知道怎么写线段树(既然压根儿不会你提它干嘛)。
解法:我的第一想法是可以用线段树,不过我不会写线段树,所以我试着用树状数组来解决问题。没想到,还真琢磨出一个来。一种暴力的解法自然是两层循环遍历,统计谁的相交次数最多。如果想把复杂度降低到O(n * log(n)),就得使元素有序。首先要明白一点:当A区间和B区间相交时,A和B的相交次数都要加1。那么,当A和BCD都相交时,A的相交次数直接加3,B、C、D的相交次数都加1。如果直接就这么加,复杂度肯定是平方级别的。但你既然看到“都加1”这种字眼,应该会联想到树状数组。树状数组的一种适用模型,就是给区间加上同一个值,然后查询单个元素,符合这道题的需求。我的代码里实现了一个简单的树状数组类,可以批量修改元素,和查询单个元素。单个操作的时间都是O(log(n))。这样n个区间统计完了以后,可以做到O(n * log(n))。在做相交统计之前,需要保证元素有序,比如按"先X后Y"或者“先Y后X”的顺序给区间排序,这个过程也是O(n * log(n))的。总体时间复杂度为O(n * log(n))。从这题可以看出:出题人不靠谱,下面回帖的答题者也大多不靠谱,有光说思路不写代码的,有分析完全错误的,还有断言时间复杂度不可能低于O(n^2)的。总之,这一题让我对Careercup上的题目质量大失所望。如果像“Guy”这样水平的用户活跃在Careercup上,这网站就完蛋了。如果需要了解树状数组,可以自行百度“树状数组”或者Google“Binary Indexed Tree”。
代码:
// http://www.careercup.com/question?id=6332750214725632
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; class BinaryIndexedTree {
public:
BinaryIndexedTree(int _n = ): n(_n) {
v.resize(n + );
}; // add val to all elements from v[1] to v[x]
void addAll(int x, int val) {
while (x >= ) {
v[x] += val;
x -= lowBit(x);
}
}; // add val to all elements from v[x] to v[y]
void addInterval(int x, int y, int val) {
if (x > y) {
addInterval(y, x, val);
return;
}
addAll(x - , -val);
addAll(y, val);
}; // return v[x]
int sum(int x) {
int res = ;
while (x <= n) {
res += v[x];
x += lowBit(x);
} return res;
}; ~BinaryIndexedTree() {
v.clear();
};
private:
int n;
vector<int> v; int lowBit(int x) {
return x & -x;
};
}; struct Interval {
int start;
int end;
Interval(int _start = , int _end = ): start(_start), end(_end) {}; bool operator < (const Interval &other) {
if (start != other.start) {
return start < other.start;
} else {
return end < other.end;
}
}; friend ostream& operator << (ostream &cout, const Interval &i) {
cout << '(' << i.start << ',' << i.end << ')';
return cout;
};
}; Interval solve(vector<Interval> &v)
{
int n = (int)v.size(); if (n == ) {
return Interval(, );
} else if (n == ) {
return v[];
} sort(v.begin(), v.end());
BinaryIndexedTree bit(n); int i, j;
int ll, rr, mm;
for (i = ; i < n - ; ++i) {
if (v[i + ].start >= v[i].end) {
// no overlapping
continue;
} if (v[n - ].start < v[i].end) {
// all overlapped
j = n - ;
} else {
ll = i + ;
rr = n - ;
while (rr - ll > ) {
mm = (ll + rr) / ;
if (v[mm].start < v[i].end) {
ll = mm;
} else {
rr = mm;
}
}
j = ll;
}
// from [i + 1, j], they all overlap with v[i].
bit.addInterval(i + , j + , );
bit.addInterval(i + , i + , j - i);
} int ri;
int res, mres; ri = ;
mres = bit.sum();
for (i = ; i < n; ++i) {
res = bit.sum(i + );
ri = res > mres ? i : ri;
} return v[ri];
} int main()
{
int i;
int n;
vector<Interval> v;
Interval res; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i].start >> v[i].end;
}
res = solve(v);
cout << res << endl;
v.clear();
} return ;
} /*
// A simple test for the BIT above.
int main()
{
string cmd;
int n;
BinaryIndexedTree *bit = nullptr;
int x, y, val;
int i; while (cin >> n && n > 0) {
bit = new BinaryIndexedTree(n);
while (true) {
for (i = 1; i <= n; ++i) {
cout << bit->sum(i) << ' ';
}
cout << endl;
cin >> cmd;
if (cmd == "e") {
break;
} else if (cmd == "a") {
cin >> x >> val;
bit->addAll(x, val);
} else if (cmd == "ai") {
cin >> x >> y >> val;
bit->addInterval(x, y, val);
}
}
delete bit;
bit = nullptr;
} return 0;
}
*/