Advertisement
Guest User

10474 - Where is the Marble?

a guest
Oct 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. """
  2. https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1415
  3. 10474 - Where is the Marble?
  4. """
  5.  
  6. # test case counter
  7. count = 0
  8. while True:
  9.     N, Q = map(int, input().split())
  10.     if N + Q == 0:
  11.         break
  12.  
  13.     nums = []
  14.     queries = []
  15.  
  16.     for i in range(N):
  17.         nums.append(int(input()))
  18.  
  19.     for i in range(Q):
  20.         queries.append(int(input()))
  21.  
  22.     nums = sorted(nums)
  23.     # test case incrementation and test case number
  24.     count += 1
  25.     print("CASE#", str(count) + ":")
  26.  
  27.     for i in range(Q):
  28.         # setting the flag
  29.         found = False
  30.         # checking whether the queries are in the list
  31.         hi = N - 1
  32.         lo = 0
  33.         while hi >= lo:
  34.             mid = int((hi+lo)/2)
  35.             if nums[mid] == queries[i]:
  36.                 print(queries[i], "found at", mid+1)
  37.                 found = True
  38.                 break
  39.             elif nums[mid] > queries[i]:
  40.                 hi = mid -1
  41.             else:
  42.                 lo = mid + 1
  43.  
  44.         # not found response
  45.         if found is False:
  46.             print(queries[i], "not found")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement