📜  查找给定数组中所有相交的对

📅  最后修改于: 2021-04-21 22:30:09             🧑  作者: Mango

给定n对(S [i],F [i]),其中对于每个i,S [i]

注意:所有为F [i]的端点都是整数,并且也是唯一的。这些对中的任何一个都不会同时开始和结束。同样,没有对的端点与另一个的起点相同。

例子:

方法:

通过使用排序可以解决上述问题。首先,我们必须将对中的每个第一元素和对中的第二个元素以及每个元素的位置插入一个向量中。然后相对于该对的第一个元素对所有元素进行排序。之后,对中的第二个元素使用set数据结构。然后,我们必须迭代存储有第一元素和第二元素及其各自位置的向量,如果找到了第一个元素,则从集合中计算与当前对相交的所有范围,并且如果第二个元素遇到一对,然后简单地从集合中删除第二对。否则,添加当前对中的第二个元素。

下面是上述方法的实现:

// CPP Program to Find all the
// intersecting pairs from a given array
  
#include 
using namespace std;
  
// Function that print intersecting pairs
// for each pair in the vector
void findIntersection(vector > v, int n)
{
    vector > data;
  
    vector > answer(n);
  
    // Store each pair with their positions
    for (int i = 0; i < n; i++) {
        data.push_back(make_pair(v[i].first, i));
  
        data.push_back(make_pair(v[i].second, i));
    }
  
    // Sort the vector with respect to
    // first element in the pair
    sort(data.begin(), data.end());
  
    int curr = 0;
  
    // set data structure for keeping
    // the second element of each pair
    set > s;
  
    // Iterating data vector
    for (auto it : data) {
  
        // check if all pairs are taken
        if (curr >= n)
            break;
  
        // check if current value is a second element
        // then remove it from the set
        if (s.count(it))
  
            s.erase(it);
  
        else {
  
            // index of the current pair
            int i = it.second;
  
            // Computing the second element of current pair
            int j = v[i].second;
  
            // Iterating in the set
            for (auto k : s) {
  
                // Check if the set element
                // has higher value than the current
                // element's second element
                if (k.first > j)
                    break;
  
                int index = k.second;
  
                answer[i].push_back(index);
  
                answer[index].push_back(i);
  
                curr++;
  
                // Check if curr is equal to
                // all available pairs or not
                if (curr >= n)
                    break;
            }
  
            // Insert second element
            // of current pair in the set
            s.insert(make_pair(j, i));
        }
    }
  
    // Printing the result
    for (int i = 0; i < n; i++) {
  
        cout << "{" << v[i].first << ", " << v[i].second << "}"
             << " is intersecting with: ";
  
        for (int j = 0; j < answer[i].size(); j++)
  
            cout << "{" << v[answer[i][j]].first << ", "
                 << v[answer[i][j]].second << "}"
                 << " ";
  
        cout << "\n";
    }
}
  
// Driver Code
int main()
{
  
    // initialise the size of vector
    int n = 6;
  
    // initialise the vector
    vector > v = { { 9, 12 },
                                  { 2, 11 },
                                  { 1, 3 },
                                  { 6, 10 },
                                  { 5, 7 },
                                  { 4, 8 } };
  
    findIntersection(v, n);
  
    return 0;
}
输出:
{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}