Advertisement
Guest User

Facebook Interview

a guest
Feb 29th, 2012
18,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. You have a room-full of balances and weights. Each balance weighs ten pounds and is considered perfectly balanced when the sum of weights on its left and right sides are exactly the same. You have placed some weights on some of the balances, and you have placed some of the balances on other balances. Given a description of how the balances are arranged and how much additional weight is on each balance, determine how to add weight to the balances so that they are all perfectly balanced.
  2.  
  3. There may be more than one way to balance everything, but always choose the way that places additional weight on the lowest balances.
  4.  
  5. The input file will begin with a single integer, N, specifying how many balances there are.
Balance 0 is specified by lines 1 and 2, balance 1 is specified by lines 3 and 4, etc...
Each pair of lines is formatted as follows:
  6.  
  7. WL <balances>

  8. WR <balances>
  9.  
  10. WL and WR indicate the weight added to the left and right sides, respectively. <balances> is a space-delimited list of the other balance that are on that side of this balance. <balances> may contain zero or more elements.
  11.  
  12. Consider the following input:
  13. 4
  14. 
0 1
  15. 
0 2
  16. 
0
  17. 
0 3
  18. 
3
  19. 
0
  20. 
0
  21. 
0
  22.  
  23. Balance 0 has balance 1 on its left side and balance 2 on its right side
  24. 
Balance 1 has balance 3 on its right side
  25. 
Balance 2 has three pounds on its left side

  26. Balance 3 has nothing on it
  27.  
  28. Since balance 3 has nothing on it it is already perfectly balanced, and weighs a total of 10 pounds.

  29. Balance 2 has no other balance on it, so all we need to do is balance it by putting three pounds on its right side. Now it weighs a total of 16 pounds.

  30. Balance 1 has balance three on its right side, which weighs 10 pounds, so we just put 10 pounds on its left side. Balance 1 weighs a total of 30 pounds.
  31. 
Balance 0 has balance 1 on its left side (30 pounds), and balance 2 on its right side (16 pounds), we can balance it by adding 14 pounds to the right side.
  32.  
  33. The output should be N lines long, with the nth line listing the weight added to the nth balance, formatted as follows:
  34.  
  35. <index>: <weight added to left side> <weight added to right side>
  36.  
  37. So the output for this problem would be:
  38.  
  39. 0: 0 14
  40. 
1: 10 0
  41. 
2: 0 3

  42. 3: 0 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement