/* We're going to loop through all values of k from 0 to max-pile-size, and print out whether k works. This will help us develop the binary search solution. */ #include #include using namespace std; class Solution { public: int minEatingSpeed(vector& piles, int h); }; int Solution::minEatingSpeed(vector& piles, int h) { int k; size_t i; int max_pile; int timesteps; bool success; max_pile = piles[0]; for (i = 1; i < piles.size(); i++) if (piles[i] > max_pile) max_pile = piles[i]; for (k = 1; k <= max_pile; k++) { timesteps = 0; for (i = 0; i < piles.size(); i++) { timesteps += (piles[i] / k); if (piles[i] % k != 0) timesteps++; } success = (timesteps <= h); printf("k: %2d Timesteps: %d. Success: %s\n", k, timesteps, (success) ? "Y" : "N"); } return 0; } int main() { vector p; int h; Solution s; while (cin >> h) p.push_back(h); /* Read the piles and h and put them into p */ p.pop_back(); /* Remove h from p. */ cout << s.minEatingSpeed(p, h) << endl; return 0; }