📌  相关文章
📜  查找是否存在范围的方向,使得没有两个范围相交

📅  最后修改于: 2021-04-22 08:46:59             🧑  作者: Mango

给定N范围[L,R]的速度vel [] 。任务是为每个范围分配一个方向,即“向左”或“向右” 。所有范围将从时间t = 0开始沿指定的方向移动。如果在无限时间内没有两个范围重叠,则查找是否存在可能的方向分配。
例子:

方法:

  • 在无限时间内,如果有两个速度不同的范围,则它们永远不会重叠,而与它们的方向无关,因为速度较大的范围将始终领先速度较慢的范围。
  • 现在,考虑具有相同速度的范围。如果存在一个点,该点至少以相同的速度位于至少三个范围内,则无法进行方向分配,因为即使这些范围中的两个被分配了不同的方向,但第三个范围也肯定会与其他任何一个范围相交,并且相同的方向。
  • 因此,对于所有速度,找出是否存在至少3个范围以使它们共享一个公共点,如果是,则不可能进行分配。否则有可能。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define MAX 100001
using namespace std;
 
// Structure to hold details of
// each interval
typedef struct
{
    int l, r, v;
} interval;
 
// Comparator to sort intervals
// based on velocity
bool cmp(interval a, interval b)
{
    return a.v < b.v;
}
 
// Function that returns true if the
// assignment of directions is possible
bool isPossible(int range[][3], int N)
{
    interval test[N];
    for (int i = 0; i < N; i++) {
        test[i].l = range[i][0];
        test[i].r = range[i][1];
        test[i].v = range[i][2];
    }
 
    // Sort the intervals based on velocity
    sort(test, test + N, cmp);
 
    for (int i = 0; i < N; i++) {
        int count[MAX] = { 0 };
        int current_velocity = test[i].v;
 
        int j = i;
 
        // Test the condition for all intervals
        // with same velocity
        while (j < N && test[j].v == current_velocity) {
            for (int k = test[j].l; k <= test[j].r; k++) {
                count[k]++;
 
                // If for any velocity, 3 or more intervals
                // share a common point return false
                if (count[k] >= 3)
                    return false;
            }
            j++;
        }
 
        i = j - 1;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int range[][3] = { { 1, 2, 3 },
                       { 2, 5, 1 },
                       { 3, 10, 1 },
                       { 4, 4, 1 },
                       { 5, 7, 10 } };
    int n = sizeof(range) / sizeof(range[0]);
 
    if (isPossible(range, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int MAX = 100001;
 
// Structure to hold details of
// each interval
static class interval
{
    int l, r, v;
}
 
static class Sort implements Comparator
{
    // Comparator to sort intervals
    // based on velocity
    public int compare(interval a, interval b)
    {
        return (a.v < b.v ? 1 : 0);
    }
}
 
// Function that returns true if the
// assignment of directions is possible
static boolean isPossible(int range[][], int N)
{
    interval test[] = new interval[N];
    for (int i = 0; i < N; i++)
    {
        test[i] = new interval();
        test[i].l = range[i][0];
        test[i].r = range[i][1];
        test[i].v = range[i][2];
    }
 
    // Sort the intervals based on velocity
    Arrays.sort(test, new Sort());
 
    for (int i = 0; i < N; i++)
    {
        int count[] = new int[MAX];
        int current_velocity = test[i].v;
 
        int j = i;
 
        // Test the condition for all intervals
        // with same velocity
        while (j < N && test[j].v == current_velocity)
        {
            for (int k = test[j].l; k <= test[j].r; k++)
            {
                count[k]++;
 
                // If for any velocity, 3 or more intervals
                // share a common point return false
                if (count[k] >= 3)
                    return false;
            }
            j++;
        }
        i = j - 1;
    }
    return true;
}
 
// Driver code
public static void main(String args[])
{
    int range[][] = {{ 1, 2, 3 },
                     { 2, 5, 1 },
                     { 3, 10, 1 },
                     { 4, 4, 1 },
                     { 5, 7, 10 }};
    int n = range.length;
 
    if (isPossible(range, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Arnab Kundu


Python
# Python implementation of the approach
MAX = 100001
 
# Function that returns true if the
# assignment of directions is possible
def isPossible(rangee, N):
     
    # Structure to hold details of
    # each interval
    test = [[0 for x in range(3)] for x in range(N)]
    for i in range(N):
        test[i][0] = rangee[i][0]
        test[i][1] = rangee[i][1]
        test[i][2] = rangee[i][2]
         
    # Sort the intervals based on velocity
    test.sort(key = lambda x: x[2])
    for i in range(N):
        count = [0] * MAX
        current_velocity = test[i][2]
        j = i
         
        # Test the condition for all intervals
        # with same velocity
        while (j < N and test[j][2] == current_velocity):
            for k in range(test[j][0], test[j][1] + 1):
                count[k] += 1
                 
                # If for any velocity, 3 or more intervals
                # share a common poreturn false
                if (count[k] >= 3):
                    return False
            j += 1
        i = j - 1
     
    return True
     
# Driver code
rangee = [[1, 2, 3] ,[2, 5, 1] ,[3, 10, 1],
        [4, 4, 1 ],[5, 7, 10 ]]
n = len(rangee)
if (isPossible(rangee, n)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by SHUBHAMSINGH10


C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
     
// Structure to hold details of
// each interval
public class interval
{
    public int l, r, v;
}
 
     
public class sortHelper : IComparer
{
   public int Compare(interval a, interval b)
   {
      return (a.v < b.v ? 1 : 0);
   }
}
     
static int MAX = 100001;
 
// Function that returns true if the
// assignment of directions is possible
static bool isPossible(int [,]range, int N)
{
    interval []test = new interval[N];
    for (int i = 0; i < N; i++)
    {
        test[i] = new interval();
        test[i].l = range[i, 0];
        test[i].r = range[i, 1];
        test[i].v = range[i, 2];
    }
 
    // Sort the intervals based on velocity
    Array.Sort(test, new sortHelper());
 
    for (int i = 0; i < N; i++)
    {
        int []count = new int[MAX];
        int current_velocity = test[i].v;
 
        int j = i;
 
        // Test the condition for all intervals
        // with same velocity
        while (j < N && test[j].v == current_velocity)
        {
            for (int k = test[j].l; k <= test[j].r; k++)
            {
                count[k]++;
 
                // If for any velocity, 3 or more intervals
                // share a common point return false
                if (count[k] >= 3)
                    return false;
            }
            j++;
        }
        i = j - 1;
    }
    return true;
}
 
// Driver code
public static void Main(string []args)
{
    int [,]range = {{ 1, 2, 3 },
                     { 2, 5, 1 },
                     { 3, 10, 1 },
                     { 4, 4, 1 },
                     { 5, 7, 10 }};
                      
    int n = range.GetLength(0);
 
    if (isPossible(range, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by rutvik_56


输出:
No