Sumanto's blog

By Sumanto, 5 years ago, In English

I implemented my solution using Binary Search in this Problem A: Deadline. Issue is I'm Facing that,by changing the value of high from 1e18 to n-1, my solution which was giving wrong answer now being accepted. To be on the safe side, I kept high=1e18, but it's giving wrong answer. Any Help on this would be really appreaciated. Am I missing something conceptually on knowledge of Binary Search?

Wrong Answer due to high=1e18
Accepted after high=n-1
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

try high=1e18+2 and see if that still gives you WA

»
5 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

I believe both of your binary searches is wrong, consider the test case 1 5 9, the answer should be Yes.(two days of optimization). However, your program gives No. This is because when in your binary search low = 1, high = 2, then low + (high - low) / 2 = 1. You skip the possibility of using two days for optimization. For the high = n - 1 solution, it has the same problem but I believe it is coincidently correct under the settings of this particular problem. (or it is incorrect I am not sure)

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    I agree, the function x+(x+d)/(x+1) is not monotonic so binary search should not be expected to work.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    okay thanks, but any other way to solve the problem using binary search as the problem has tag of binary search.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      The function f(x) = x+(x+d)/(x+1) is concave upwards. It means that there are two intervals, for the first interval f(x) > f(x + 1), and for the second interval f(x) < f(x + 1). So you may binary search for the first x that satisfy f(x) < f(x + 1)