C ++排序间隔结构以及删除重叠间隔

时间:2019-05-22 16:05:44

标签: c++ sorting set

这里的间隔与Line相同。尽管我为两个目的找到了正确的解决方案:

1)排序间隔;

2)在添加与现有间隔重叠的间隔时拒绝。

struct Line {
    int down;
    int up;

    Line(int down, int up) {
        this->down = down;
        this->up = up;
    }
};

int main() {
    auto cmp = [] (const Line& a, const Line& b) {//NOTE: this can even detect the intersection, as well as sort the interval!!
        if (a.up <= b.down) return true;
        else return false;
    };

    set<Line, decltype(cmp)> s(cmp);
    Line l1{2, 3};
    Line l2{3, 5};
    Line l3{1, 2}; //success for insertion
    Line l4{4, 6}; //fail for insertion b/c of insersection


    auto ret = s.insert(l2);
    cout << "insert 3->5 ";
    if (ret.second) cout << "success " << endl;
    else cout << "fail " << endl;

    ret = s.insert(l3);
    cout << "insert 1->2 ";
    if (ret.second) cout << "success " << endl;
    else cout << "fail " << endl;

    ret = s.insert(l1);
    cout << "insert 2->3 ";
    if (ret.second) cout << "success " << endl;
    else cout << "fail " << endl;

    ret = s.insert(l4);
    cout << "insert 4->6 ";
    if (ret.second) cout << "success " << endl;
    else cout << "fail " << endl;

    return 0;
}

Output:
insert 3->5 success
insert 1->2 success
insert 2->3 success
insert 4->6 fail
set finally contains: 1->2, 2->3, 3->5,

还有一些令人困惑的事情:

1)为什么简单地比较a.up <= b.down就足以检测重叠间隔了?

我们不应该做类似检测间隔重叠的标准方法吗?

max(a.down, b.down) < min(a.up, b.up)

2)第三步对我来说还不清楚

insert 3->5 success
insert 1->2 success //OK, b/c 2 <= 3, a.up <= b.down return true
insert 2->3 success //Why success? 3 > 1, i.e., a.up > b.down, return false; does it only compare with interval 3->5?

3)为什么下面的代码对于删除重叠间隔无效?

    auto cmp_set = [](const Line& a, const Line& b) {
        if (max(a.down, b.down) < min(a.up, b.up)) return 0; //standard way to detect overlapping of intervals
        else { //sort the two intervals 
            if(a.up <= b.down) return -1;
            else return 1;
        }
    };

1 个答案:

答案 0 :(得分:2)

  

1)为什么仅比较a.up <= b.down足以检测重叠间隔?

[a.down, a.up[

[b.down, b.up[max(a.down, b.down) < min(a.up, b.up)重叠 等效于a.down < b.up && b.down < a.up(请记住,区间定义中也有a.down <= a.up && b.down <= b.up)。

我们还可以列出可能性:

  • a.down < a.up < b.down < b.up (a < b)
  • a.down < b.down < a.up < b.up交叉点
  • a.down < b.down < b.up < a.up交叉路口(全部包含)
  • b.down < a.down < a.up < b.up交叉路口(全部包含)
  • b.down < a.down < b.up < a.up交叉点
  • b.down < b.up < a.down < a.up (b < a)
  

我不清楚第三步

     

insert 3->5 success insert 1->2 success //OK, b/c 2 <= 3, a.up <= b.down return true insert 2->3 success //Why success? 3 > 1, i.e., a.up > b.down, return false; does it only compare with interval 3->5?

Line l1{2, 3};
Line l2{3, 5};
Line l3{1, 2};
Line l4{4, 6};

要在l1中插入{l3, l2}

  • l1 < l2,所以l1l2之前
  • !(l1 < l3),所以l1不在l3之前
  • l3 < l1,所以l1l3之后 -> {l3, l1, l2}

对于l4中的{l3, l1, l2},我们有:

  • !(l4 < l2)(类似于l3l1),因此l4不在l2之前
  • !(l2 < l4),因此l4不在l2之后

我们都有!(l2 < l4) && !(l4 < l2),所以有等价,我们不插入l4

  

3)为什么下面的代码对于删除重叠间隔无效?

我不知道您在哪里使用它。