雷切尔(Rachel)正在提交每位学生的作业。她给了他们几分钟时间来安排他们的作业,然后再提交。就在那时,拉胡尔想起他的任务是与罗汉在一起。 Rohan必须将任务交给Rahul。所有的学生都坐在一条直线上。他无法在老师面前通过作业,因为那样她会假定他们复制了作业并会惩罚他们。我们必须帮助罗汉(Rohan)找到一种方法,将任务传给拉胡(Rahul),以免被抓。
可以在某些限制下通过分配,如下所示:
- 一个学生只能将作业传递给他的直接邻居,即i-1和i + 1
- 在任何给定的时间,学生可以将作业传给其他学生,接受其他学生的作业,也可以保留该作业给自己。
- 老师一直在监视学生至包括两者和
- 如果正在监视学生,则他无法通过便笺或接受便笺,否则将被抓住。
- 如果他保留任务并受到监视,则不会被抓到。
给定四个输入n,m,s,f,其中n是学生总数,m是老师对学生保持警惕的步骤数至 ,s是Rohan的位置,f是Rahul的位置。
每个m查询包含三个输入:她在看的时间,她在看的最左边的孩子和她在看的最右边的孩子。
我们必须输出三个单词的序列: 和表示当前学生的通过方向。
例子:
Input: n = 4, m = 4, s = 3, f = 4
1 2 4
2 1 2
3 3 4
4 2 3
Output: KeepRight
During time = 1, teacher is watching all
the student from 2 to 4. Student 3 who
has the assignment therefore cannot pass
it to his neighbor, thus he keeps the
assignment. During time = 2, teacher is
watching student 1 and 2, therefore
student 3 can pass the assignment to
his right to student 4. Since student
4 is Rahul therefore our answer is
"KeepRight"
Input: n = 10, m = 1, s = 1, f = 10
1 5 6
Output: RightRightRightRightRightRight
RightRightRightRight
During time = 1, teacher is watching
student 5 and 6 therefore student 1
can easily pass the assignment to his
right to student 2. After this teacher
stops watching any student therefore
they can keep on passing the assignment
to their right until the assignment
reaches Rahul. Therefore the answer is
"RightRightRightRightRightRight
RightRightRightRight"
方法:
在给定的时刻,如果老师正在观看当前正在举行作业的学生或将要传递作业的学生,则该学生会将其保留给自己,否则他会将作业传递给坐在Rahul旁的邻居。
下面是实现:
C++
// CPP program to find the way
#include
using namespace std;
void solve(int n, int m, int s, int f,
long t[], int l[], int r[])
{
int dir;
string val;
// If Rahul sits to right of Rohan
// then student will either pass
// it right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan
// then student will either pass
// it left or keep it to himself
else {
dir = -1;
val = "Left";
}
string ans = "";
// current is keeping track of
// the position of assignment
// at a given time
int i = 0, current = s;
// tim variable keeping track of
// current time
long tim = 1;
while (1) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the
// student having the assignment
// or the student to whom the
// assignment is to be passed
// then the student keeps it to
// itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
cout << ans << endl;
}
// Driver function for the program
int main()
{
int n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
long t[m + 2] = { 1, 2, 3, 4 };
// matrix saving leftmost child being
// watched for each query
int l[m + 2] = { 2, 1, 3, 2 };
// matrix saving rightmost child
// being watched for each query
int r[m + 2] = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
return 0;
}
Java
// Java progam to find the way
import java.io.*;
class Hiding {
static void solve(int n, int m, int s, int f,
long t[], int l[], int r[])
{
int dir;
String val;
// If Rahul sits to right of Rohan
// then student will either pass it
// right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan then
// student will either pass it left
// or keep it to himself
else {
dir = -1;
val = "Left";
}
String ans = "";
int i = 0, current = s;
// Variable keeping track of current time
long tim = 1;
while (1 > 0) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the student
// having the assignment or the
// student to whom the assignment is
// to be passed then the student
// keeps it to itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
System.out.println(ans);
}
// Driver Program
public static void main(String args[])
{
int n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
long t[] = { 1, 2, 3, 4 };
// matrix saving leftmost child
// being watched
int l[] = { 2, 1, 3, 2 };
// matrix saving rightmost child
// being watched
int r[] = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
}
}
Python3
# Python program to find the way
def solve(n, m, s, f, t, l, r):
val = "";
# If Rahul sits to right of Rohan
# then student will either pass it
# right or keep it to himself
if (s < f):
dir = 1;
val = "Right";
# If Rahul sits to left of Rohan then
# student will either pass it left
# or keep it to himself
else:
dir = -1;
val = "Left";
ans = "";
i = 0;
current = s;
# Variable keeping track of current time
tim = 1;
while (1 > 0):
# If time duration of query
# matches with the current time
if (i < m and tim == t[i]):
# If teacher is watching the student
# having the assignment or the
# student to whom the assignment is
# to be passed then the student
# keeps it to itself
if ((current >= l[i] and current <= r[i]) or \
(current + dir >= l[i] and current + dir <= r[i])):
ans += "Keep";
tim += 1;
i += 1;
continue;
i += 1;
# If the students are safe then student
# passes the assignment to his neighbor
current += dir;
ans += val;
tim += 1;
# If the assignment has reached Rahul
# then we break the loop
if (current == f):
break;
print(ans);
# Driver Program
if __name__ == '__main__':
n, m, s, f = 4, 4, 3, 4;
# matrix saving time for each query
t = [ 1, 2, 3, 4 ];
# matrix saving leftmost child
# being watched
l = [ 2, 1, 3, 2 ];
# matrix saving rightmost child
# being watched
r = [ 4, 2, 4, 3 ];
solve(n, m, s, f, t, l, r);
# This code is contributed by 29AjayKumar
C#
// C# progam to find the way
using System;
class Hiding
{
static void solve(int n, int m, int s, int f,
long []t, int []l, int []r)
{
int dir;
String val;
// If Rahul sits to right of Rohan
// then student will either pass it
// right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan then
// student will either pass it left
// or keep it to himself
else {
dir = -1;
val = "Left";
}
String ans = "";
int i = 0, current = s;
// Variable keeping track of current time
long tim = 1;
while (1 > 0) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the student
// having the assignment or the
// student to whom the assignment is
// to be passed then the student
// keeps it to itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current +
dir <= r[i]))
{
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
Console.Write(ans);
}
// Driver Program
public static void Main()
{
int n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
long []t = { 1, 2, 3, 4 };
// matrix saving leftmost
// child being watched
int []l = { 2, 1, 3, 2 };
// matrix saving rightmost
// child being watched
int []r = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
}
}
// This code is contributed by nitin mittal
输出:
KeepRight
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。