考虑按顺时针方向(从 1 到 n 编号)按递增顺序排列在圆圈中的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 < i 并且 y 位于 i 和 j 之间:
情况 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
?>
Javascript
YES
NO
YES
NO
NO
YES
YES
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。