给定n对(S [i],F [i]),其中对于每个i,S [i] 注意:所有为F [i]的端点都是整数,并且也是唯一的。这些对中的任何一个都不会同时开始和结束。同样,没有对的端点与另一个的起点相同。 例子: Input : Input : n = 5, v = {1, 3}, {2, 4}, {5, 9}, {6, 8}, {7, 10}} 方法: 通过使用排序可以解决上述问题。首先,我们必须将对中的每个第一元素和对中的第二个元素以及每个元素的位置插入一个向量中。然后相对于该对的第一个元素对所有元素进行排序。之后,对中的第二个元素使用set数据结构。然后,我们必须迭代存储有第一元素和第二元素及其各自位置的向量,如果找到了第一个元素,则从集合中计算与当前对相交的所有范围,并且如果第二个元素遇到一对,然后简单地从集合中删除第二对。否则,添加当前对中的第二个元素。 下面是上述方法的实现:
n = 6, v = {{9, 12}, {2, 11}, {1, 3}, {6, 10}, {5, 7}, {4, 8}}
Output :
{9, 12} is intersecting with: {6, 10} {2, 11}
{2, 11} is intersecting with: {1, 3} {9, 12}
{1, 3} is intersecting with: {2, 11}
{6, 10} is intersecting with: {5, 7} {4, 8} {9, 12}
{5, 7} is intersecting with: {6, 10}
{4, 8} is intersecting with: {6, 10}
Explanation:
The first pair(9, 12) is intersecting with second(2, 11) and fourth(6, 10) pair.
The second pair(2, 11) is intersecting with third(1, 3) and first(9, 12) pairs.
The third pair(1, 3) is intersecting with the second(2, 11) pair.
The forth pair(6, 10) is intersecting with fifth(5, 7), sixth(4, 8) and first(9, 12) pair.
The fifth pair(5, 7) is intersecting with the fourth(6, 10) pair.
The sixth pair(4, 8) is intersecting with the fourth(6, 10) pair.
Output :
{1, 3} is intersecting with: {2, 4}
{2, 4} is intersecting with: {1, 3}
{5, 9} is intersecting with: {7, 10}
{6, 8} is intersecting with: {7, 10}
{7, 10} is intersecting with: {6, 8} {5, 9}
Explanation:
The first pair(1, 3) is intersecting with the second(2, 4) pair.
The second pair(2, 4) is intersecting with the first(1, 3) pair.
The third pair(5, 9) is intersecting with the fifth(7, 10) pair.
The fourth pair(6, 8) is intersecting with the fifth(7, 10) pair.
The fifth pair(7, 10) is intersecting with the third(5, 9) and fourth(6, 8) pair.// CPP Program to Find all the
// intersecting pairs from a given array
#include
{9, 12} is intersecting with: {6, 10} {2, 11}
{2, 11} is intersecting with: {1, 3} {9, 12}
{1, 3} is intersecting with: {2, 11}
{6, 10} is intersecting with: {5, 7} {4, 8} {9, 12}
{5, 7} is intersecting with: {6, 10}
{4, 8} is intersecting with: {6, 10}