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

📅  最后修改于: 2021-09-03 14:57:01             🧑  作者: Mango

给定 n 对 (S[i], F[i]),其中对于每个 i,S[i]< F[i]。当且仅当两个范围中的一个不完全位于另一个范围内,即一对中只有一个点位于另一对的起点和终点之间时,才称两个范围相交。我们必须打印每对的所有相交范围。

注意: F[i] 的所有端点都是整数并且也是唯一的。没有一对同时开始和结束。此外,没有一对的端点与另一个的起点相同。

例子:

方法:

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

下面是上述方法的实现:

// 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}

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live