Pasar la tarea

Rachel está enviando la tarea de cada estudiante. Ella les ha dado unos minutos para organizar sus tareas antes de enviarlas. En ese momento, Rahul recuerda que su asignación es con Rohan. Rohan tiene que pasarle la tarea a Rahul. Todos los alumnos están sentados en línea recta. No puede pasar la tarea frente a la maestra porque entonces ella asumirá que han copiado la tarea y los castigará a ambos. Tenemos que ayudar a Rohan a encontrar la manera de pasarle la tarea a Rahul de tal manera que no lo atrapen.

La asignación se puede aprobar bajo algunas restricciones como se indica a continuación:

  • Un estudiante  i   puede pasar la tarea solo a su vecino inmediato, es decir, i-1 e i+1
  • En cualquier momento, un estudiante puede pasar la tarea a otro estudiante, aceptar la tarea de otro estudiante o quedarse con la tarea para sí mismo.
  • El maestro está vigilando a los estudiantes de  yo_yo   Rhode Island   incluyendo ambos  yo_yo   Rhode Island
  • Si un estudiante está siendo observado, no puede pasar la nota o aceptar la nota, de lo contrario, lo atrapan.
  • Si cumple con la tarea y está siendo vigilado, no lo atrapan.

Dadas cuatro entradas n, m, s, f donde n es el número total de estudiantes, m es el número de pasos durante los cuales el maestro vigila a los estudiantes desde  yo_yo   hasta  Rhode Island   , s es la posición de Rohan y f es la posición de Rahul.
Cada consulta m contiene tres entradas: la hora a la que está mirando, el niño que está más a la izquierda que está mirando y el niño que está más a la derecha que está mirando.

Tenemos que generar una secuencia de tres palabras:  "Izquierda"   [Tex] «Derecha» [/Tex] e  "Mantener"   indicar la dirección de paso del estudiante actual.

Ejemplos:

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"

Enfoque:
en un momento dado, si el maestro está observando al estudiante que actualmente tiene la tarea o al estudiante a quien se le va a pasar la tarea, entonces el estudiante se la guardará para sí mismo, de lo contrario le pasará la tarea a su vecino sentado hacia Rahul. .

A continuación se muestra la implementación:

C++

// CPP program to find the way
#include <bits/stdc++.h>
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 program 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# program 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

Javascript

<script>
 
// JavaScript program to find the way
 
function solve(n,m,s,f,t,l,r)
{
    let dir;
        let 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";
        }
  
        let ans = "";
        let i = 0, current = s;
  
        // Variable keeping track of current time
        let 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;
        }
        document.write(ans+"<br>");
}
 
// Driver Program
let n = 4, m = 4, s = 3, f = 4;
  
// matrix saving time for each query
let t = [ 1, 2, 3, 4 ];
 
// matrix saving leftmost child
// being watched
let l = [ 2, 1, 3, 2 ];
 
// matrix saving rightmost child
// being watched
let r = [ 4, 2, 4, 3 ];
 
solve(n, m, s, f, t, l, r);
 
 
// This code is contributed by rag2127
 
</script>

Producción:

KeepRight

Publicación traducida automáticamente

Artículo escrito por HGaur y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *