hihocoder-1498-Diligent Robots算法
There are N jobs to be finished. It takes a robot 1 hour to finish one job.ide
At the beginning you have only one robot. Luckily a robot may build more robots identical to itself. It takes a robot Q hours to build another robot. ui
So what is the minimum number of hours to finish N jobs?spa
Note two or more robots working on the same job or building the same robot won't accelerate the progress.code
The first line contains 2 integers, N and Q. blog
For 70% of the data, 1 <= N <= 1000000 內存
For 100% of the data, 1 <= N <= 1000000000000, 1 <= Q <= 1000it
The minimum number of hours.io
10 1
5
題解:class
使用貪心算法的思想。
樣本複製以後須要去生產纔有效,因此機器人一定是先複製完。
還有一個想法是是否須要一邊複製一邊生產,這個想法是否認的,假如n生產m複製,獲得的是 m + (q+1)*n. 若是是一塊兒複製,則獲得的是 (m + n)*q , 大於前者。
#include <cstdio> #include <cstdlib> int main() { int q; long long cnt, n, k; while(scanf("%lld %d", &n, &q) != EOF) { cnt = 0; k = 1; while(2*q*k < n) { k *= 2; cnt += q; } cnt += n / k; if(n%k != 0) { cnt += 1; } printf("%lld\n", cnt ); } return 0; }