CrazyEngineers
  • Puzzle - There are n animals in the queue to Dr. Dolittle. [Queue Problem]

    PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92
    Updated: Oct 23, 2024
    Views: 935
    There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home.

    Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.

    Input

    The first line of input data contains two space-separated integers n and k (1≤n≤105, 0≤k≤1014). In the second line are given space-separated integers a1,a2,...,an (1≤ai≤109).

    Output

    If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue.

    How to solve this?!? Brute force will not work, as the inputs are very big... 😔
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • vik001ind

    MemberMay 14, 2011

    For given inputs, a program can be written. It's not as hard as it looks.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMay 14, 2011

    Can ya share the logic... I can't even understand the description here... 😔
    Are you sure? This action cannot be undone.
    Cancel
  • vik001ind

    MemberMay 14, 2011

    We will first store the values of a in an queue (linked list). We will iterate this list & decrement the element after each examination. As a becomes zero, we will delete that element from list. We can have a counter to record the no. of animals checked after certain time.
    To counter with examination & tests we can use a sleep function, after certain amount of time, doc can check the how many animals he has checked so far(<k or >k).
    The question doesn't clearly says, what is asked ..
    Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMay 14, 2011

    Thanks... Lemme try and say whats happening... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMay 15, 2011

    This is what exactly brute force solution to this problem... But the value of k ranges between 1 and 10 ^ 14 and the constraint is that the program should generate output within 2 seconds...... 😔 So this approach ll fail...
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register