考虑n个框,它们沿顺时针方向(从1到n)以递增顺序排列成一个圆圈。您将获得q个查询,每个查询包含两个整数i和j。任务是检查是否可以通过杆将框i连接到框j ,而没有相交的杆来连接先前查询中的其他框。同样,每个盒子最多只能连接到另一个盒子,任何盒子都不能与其自身相连。
例子:
Input: n = 10, q = 7
q1 = (1, 5)
q2 = (2, 7)
q3 = (2, 3)
q4 = (2, 4)
q5 = (9, 9)
q6 = (10, 9)
q7 = (8, 6)
Output:
YES
NO
YES
NO
NO
YES
YES
Box 1 and v 5 can be connected by a rod.
Box 2 and Box 7 cannot be connected by a rod because this rod intersects with the rod connecting Box 1 and Box 5.
Box 2 and Box 3 can be connected without criss-cross.
Box 2 and Box 4 cannot be connected as Box 2 is already connected to Box 3.
Box 9 and Box 9 cannot be connected as no box can be connected to itself.
Box 10 and Box 9 can be connected.
Box 8 and Box 6 can be connected.
方法:
假设框x已连接到框y。我们需要将方框i连接到方框j。
现在,观察到在两种情况下,连接箱i和箱j的两个杆与连接箱x和箱y的杆相交。
情况1: x
情况2: x位于i和j之间,并且y> j:
我们将明确检查是否打算将一根杆连接到其自身,或者该杆是否打算连接两个盒,从而至少已经连接了其中一个盒的情况。
因此,我们将检查以上两个条件。如果两个都满足,则无法连接,否则我们可以连接盒子。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
#define MAX 50
// Print the answer to each query
void solveQuery(int n, int q, int qi[], int qj[])
{
int arr[MAX];
for (int i = 0; i <= n; i++)
arr[i] = 0;
for (int k = 0; k < q; k++) {
// setting the flag for exception
int flag = 0;
// replacing the greater element in i and j
if (qj[k] < qi[k]) {
int temp = qi[k];
qi[k] = qj[k];
qj[k] = temp;
}
// checking if that box is not
// used in previous query.
if (arr[qi[k]] != 0 || arr[qj[k]] != 0)
flag = 1;
// checking if connecting to the same box
else if (qi[k] == qj[k])
flag = 1;
else {
// case 1: x < i and y lies between i and j
for (int i = 1; i < qi[k]; i++) {
if (arr[i] != 0 && arr[i] < qj[k] && qi[k] < arr[i]) {
flag = 1;
break;
}
}
// case 2: x lies between i and j and y >j
if (flag == 0) {
for (int i = qi[k] + 1; i < qj[k]; i++) {
if (arr[i] != 0 && arr[i] > qj[k]) {
flag = 1;
break;
}
}
}
}
// if flag is not reset inbetween.
if (flag == 0) {
cout << "YES\n";
arr[qi[k]] = qj[k];
arr[qj[k]] = qi[k];
}
else
cout << "NO\n";
}
}
// Driver code
int main()
{
int n = 10;
int q = 7;
int qi[] = { 1, 2, 2, 2, 9, 10, 8 };
int qj[] = { 5, 7, 3, 4, 9, 9, 6 };
solveQuery(n, q, qi, qj);
return 0;
}
Java
// Java implementation of
// above approach
class GFG
{
static int MAX = 50;
// Print the answer to each query
static void solveQuery(int n, int q,
int qi[], int qj[])
{
int[] arr = new int[MAX];
for (int i = 0; i <= n; i++)
arr[i] = 0;
for (int k = 0; k < q; k++)
{
// setting the flag for exception
int flag = 0;
// replacing the greater
// element in i and j
if (qj[k] < qi[k])
{
int temp = qi[k];
qi[k] = qj[k];
qj[k] = temp;
}
// checking if that box is not
// used in previous query.
if (arr[qi[k]] != 0 ||
arr[qj[k]] != 0)
flag = 1;
// checking if connecting
// to the same box
else if (qi[k] == qj[k])
flag = 1;
else
{
// case 1: x < i and y lies
// between i and j
for (int i = 1; i < qi[k]; i++)
{
if (arr[i] != 0 && arr[i] < qj[k] &&
qi[k] < arr[i])
{
flag = 1;
break;
}
}
// case 2: x lies between
// i and j and y >j
if (flag == 0)
{
for (int i = qi[k] + 1;
i < qj[k]; i++)
{
if (arr[i] != 0 && arr[i] > qj[k])
{
flag = 1;
break;
}
}
}
}
// if flag is not reset inbetween.
if (flag == 0)
{
System.out.println("YES");
arr[qi[k]] = qj[k];
arr[qj[k]] = qi[k];
}
else
System.out.println("NO");
}
}
// Driver code
public static void main(String[] args)
{
int n = 10;
int q = 7;
int qi[] = { 1, 2, 2, 2, 9, 10, 8 };
int qj[] = { 5, 7, 3, 4, 9, 9, 6 };
solveQuery(n, q, qi, qj);
}
}
// This code is contributed
// by ChitraNayal
Python 3
# Python 3 implementation of
# above approach
MAX = 50
# Print the answer to each query
def solveQuery(n, q, qi, qj):
arr = [None] * MAX
for i in range(n + 1):
arr[i] = 0
for k in range(q):
# setting the flag
# for exception
flag = 0
# replacing the greater
# element in i and j
if (qj[k] < qi[k]):
qj[k], qi[k] = qi[k], qj[k]
# checking if that box is not
# used in previous query.
if (arr[qi[k]] != 0 or
arr[qj[k]] != 0):
flag = 1
# checking if connecting
# to the same box
elif (qi[k] == qj[k]):
flag = 1
else :
# case 1: x < i and y
# lies between i and j
for i in range(1, qi[k]) :
if (arr[i] != 0 and
arr[i] < qj[k] and
qi[k] < arr[i]):
flag = 1
break
# case 2: x lies between
# i and j and y >j
if (flag == 0):
for i in range(qi[k] + 1, qj[k]) :
if (arr[i] != 0 and
arr[i] > qj[k]):
flag = 1
break
# if flag is not reset inbetween.
if (flag == 0):
print("YES")
arr[qi[k]] = qj[k]
arr[qj[k]] = qi[k]
else:
print("NO")
# Driver code
if __name__ == "__main__":
n = 10
q = 7
qi = [ 1, 2, 2, 2, 9, 10, 8 ]
qj = [ 5, 7, 3, 4, 9, 9, 6 ]
solveQuery(n, q, qi, qj)
# This code is contributed
# by ChitraNayal
C#
// C# implementation of
// above approach
using System;
class GFG
{
static int MAX = 50;
// Print the answer to each query
static void solveQuery(int n, int q,
int[] qi, int[] qj)
{
int[] arr = new int[MAX];
for (int i = 0; i <= n; i++)
arr[i] = 0;
for (int k = 0; k < q; k++)
{
// setting the flag for exception
int flag = 0;
// replacing the greater
// element in i and j
if (qj[k] < qi[k])
{
int temp = qi[k];
qi[k] = qj[k];
qj[k] = temp;
}
// checking if that box is not
// used in previous query.
if (arr[qi[k]] != 0 || arr[qj[k]] != 0)
flag = 1;
// checking if connecting
// to the same box
else if (qi[k] == qj[k])
flag = 1;
else
{
// case 1: x < i and y lies
// between i and j
for (int i = 1; i < qi[k]; i++)
{
if (arr[i] != 0 && arr[i] < qj[k] &&
qi[k] < arr[i])
{
flag = 1;
break;
}
}
// case 2: x lies between
// i and j and y >j
if (flag == 0)
{
for (int i = qi[k] + 1;
i < qj[k]; i++)
{
if (arr[i] != 0 &&
arr[i] > qj[k])
{
flag = 1;
break;
}
}
}
}
// if flag is not reset inbetween.
if (flag == 0)
{
Console.Write("YES\n");
arr[qi[k]] = qj[k];
arr[qj[k]] = qi[k];
}
else
Console.Write("NO\n");
}
}
// Driver code
public static void Main()
{
int n = 10;
int q = 7;
int[] qi = { 1, 2, 2, 2, 9, 10, 8 };
int[] qj = { 5, 7, 3, 4, 9, 9, 6 };
solveQuery(n, q, qi, qj);
}
}
// This code is contributed
// by ChitraNayal
PHP
j
if ($flag == 0)
{
for ($i = $qi[$k] + 1;
$i < $qj[$k]; $i++)
{
if ($arr[$i] != 0 &&
$arr[$i] > $qj[$k])
{
$flag = 1;
break;
}
}
}
}
// if flag is not reset inbetween.
if ($flag == 0)
{
echo "YES\n";
$arr[$qi[$k]] = $qj[$k];
$arr[$qj[$k]] = $qi[$k];
}
else
echo "NO\n";
}
}
// Driver code
$n = 10;
$q = 7;
$qi = array( 1, 2, 2, 2, 9, 10, 8 );
$qj = array( 5, 7, 3, 4, 9, 9, 6 );
solveQuery($n, $q, $qi, $qj);
// This code is contributed
// by ChitraNayal
?>
YES
NO
YES
NO
NO
YES
YES