CF1348D Phoenix and Science [ 贪心+二分 ]
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Example
input
1 | 3 |
output
1 | 3 |
考虑增量,设最小需要 k 天, 那么第 i 天共收益 (k - i + 1) * c[i] , c[i] 为第 i 天有多少细菌。
可以观察到:
1、已经存在的细菌,产生的收益已经固定。即如果一个细菌已经存在,那么一次性计算出所有收益。
2、第 i 天,新增加的细菌数量为 ( 0 ~ c[i-1] ) , 设新增 x 个,如果 x * (k - i + 1) <= n (n 为还需要多少),那么就新增。这样不会影响最后结果,因为可以选择后面不在新增(分裂)。
贪心:k 次循环,每次二分计算出当前最多可以分裂多少。
1 | const int MAX=10000+10; |