Advertisement
RaFiN_

Happy Line cf -549G

Jul 4th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. //Happy Line cf -549G
  2. We have an array of persons (we will call it persons, persons[n - 1] — the head, persons[0] — the tail). For each person we will calculate this number val = money of a person — (number of steps to reach the head of the queue). Next we sort persons by val.
  3.  
  4. Suppose there is an answer ans. ( ans[0] — the tail, ans[n - 1] — the head). ans[i].money — how much money is left with i th person in ans.
  5.  
  6. persons[n - 1].val ≥ ans[n - 1].money, so we can transform the answer so that persons[n - 1] is in the head.
  7.  
  8. persons[n - 2].val + 1 ≥ ans[n - 2].money, so again we can transform the answer. Why val + 1? Because we need to get to n - 2, no to n - 1.
  9.  
  10. Then persons[i].val + (n - i - 1) ≥ ans[i].money. So you can put persons[i] in the ans on ith position with money persons[i].val + (n - i - 1).
  11.  
  12. Excuse me for my mistakes.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement