Блог пользователя Amit9

Автор Amit9, история, 4 часа назад, По-английски

This is the question from a recent online assessment by a company. Need your help in solving it, thank you for your time!

Problem Statement

Alice and Bob are playing a game in a town with houses arranged in a straight line. Houses are numbered from 1 to N.

Alice is standing at house x, and Bob is standing at house y . Each minute, Alice and Bob can decide individually to either move to the next house on the left (if they are not already at the first house), move to the next house on the right (if they are not already at the last house), or stay at their current house.

The goal of the game is for Alice and Bob to ensure that every house in the town is visited by at least one of them. Your task is to determine the minimum number of minutes required for Alice and Bob to achieve this goal in several games.

Input format

  • The first line of input contains one integer t, the number of games.

  • The next t lines describe each game and contain three integers n, x and y, the number of houses in the town, Alice's starting position, and Bob's starting position, respectively.

  • The combined length of all arrays (10^6 (∑n ≤ 10^6)) does not exceed 10^6.

Constraints

  • 1 <= t <= 2.10^4

  • 2 ≤ n ≤ 10^6; 1 ≤ x, y ≤n; x≠y

Output format:

  • For each game, print one integer the minimum number of minutes required for Alice and Bob to visit every house in the town.

Example 1

1
993

Output
4

Explanation
Total number of houses are 9
Alice is at 9th house and Bob is at 3rd house
1st min -> Alice moves to left 8th position, Bob moves to right 4th position
2nd min -> Alice moves to left 7th position, Bob moves to 3rd position
3rd min -> Alice moves to 6th position, Bob to 2nd
4th min -> Alice moves to 5th position, Bob to 1st

My code(Not sure if it is correct, but it passed most of test case i took)

Edit: The above solution is wrong.

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

»
4 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

how did you arrive at x-y-2?

  • »
    »
    3 часа назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    After taking so many test cases, i found that pattern.
    I would be glad if anyone provide the proof of his solution too.

    • »
      »
      »
      3 часа назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      take x=9 y=5 n=9. Here your approach doesn't seem to work. The ans required is 4 whereas your solution returns 2.

      • »
        »
        »
        »
        3 часа назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Yeah, you are correct, thanks!
        Maybe Greedy will not work for this problem, i tried hours implementing different greedy approaches but none of them worked, and now this too.
        If you get a correct solution/approach fell free to comment. Thanks!

»
86 минут назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Using binary search can be a possible soln.

My Code
»
16 минут назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

let a be the number of houses left to the left person,

b be the number of houses right to the right person,

and c be the number of houses in between those 2

if(a+c+c>b) return b

if(b+c+c>a) return a

return ceil((a+b+c+c)/2)