[AtCoder] E - Bowls and Beans
Problem Link: https://atcoder.jp/contests/abc404/tasks/abc404_e
Greedy
- For position i, if there exists a postion j in [i - C[i], i - 1], where A[j] > 0, then just use 1 operation to move all of beans in i to j.
- The cost is 1. The remaining cost is piggybacked on the beans in position j. Essentially, after we move all beans from i to j, all these beans will get moved to postion 0 without incurring any extra cost.
- This is true because we need to move all beans in postion j anyway.
- if there is no such position j, then we should spend a cost of 1 to move from i to a position j such that: from j it will take the smallest cost to reach to position 0. i.e, j - C[j] needs to be the smallest for j in [i - C[i], i - 1].
- A nice trick is to set A[0] > 0.