We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Search
- Making Candies
- Discussions
Making Candies
Making Candies
Sort by
recency
|
140 Discussions
|
Please Login in order to post a comment
long minimumPasses(long m, long w, long p, long n) { using i128 = __int128_t; long long passes = 0; long long best = (long long)ceil((long double)n / (long double)(m * 1.0L * w)); long long candies = 0; while (true) { long long prod = (long long)min((i128)m * (i128)w, (i128)LLONG_MAX / 2); if (candies >= n) { best = min(best, passes); break; } best = min(best, passes + (long long)((n - candies + prod - 1) / prod)); if (candies < p) { long long need = p - candies; long long wait = (need + prod - 1) / prod; if (wait <= 0) wait = 1; passes += wait; i128 gained = (i128)prod * (i128)wait; if (gained > (i128)LLONG_MAX - candies) gained = (i128)LLONG_MAX - candies; candies += (long long)gained; continue; } long long canBuy = candies / p; candies -= canBuy * p; if (m < w) { long long diff = w - m; long long add = min(diff, canBuy); m += add; canBuy -= add; } else if (w < m) { long long diff = m - w; long long add = min(diff, canBuy); w += add; canBuy -= add; } long long half = canBuy / 2; m += half; w += (canBuy - half); passes += 1; i128 gained = (i128)m * (i128)w; if (gained > (i128)LLONG_MAX - candies) gained = (i128)LLONG_MAX - candies; candies += (long long)gained; } return best; }
Solution in Python 3
Is the assumption that you should buy production resources every time correct? Assume m=1, w=1, p=100, n=101 After 100 rounds we have 100 candies. If we run just one more round we can get to 101 If we "invest" these 100, then at the 101st round we only have 2 candies. What am I missing?
Many accepted submissions (that use greedy algo) are actually wrong.
Need to add these tests: ` Input: 5 5 25 50 Output: 2
And
Input: 5 5 1 100 Output: 2Many accepted submissions (that use greedy algo) are actually wrong. Need to add these tests: ` Input: 5 5 25 50 Output: 2
And
Input: 5 5 1 100 Output: 2