problem link: http://www.spoj.com/problems/C1LJUTNJ/
thanks!!!
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
problem link: http://www.spoj.com/problems/C1LJUTNJ/
thanks!!!
Name |
---|
You're trying to divide candies on piles and each pile should contain some candies. For each missing candy you obtain some anger and you want to minimaze total amount of anger. First logical question is: "Is better to keep one pile full and other almost empty, or trying to balanced it, so each pile missing some number of candies?"
The answer is simple, when you write it down. Imagine two piles. First pile is missing x candies, second is missing x+k candies for some positive k. Amount of anger is x^2 + (x+k)^2 = 2(x^2) + 2xk + k^2.
Now take one candy from bigger pile (with only x candies missing) and take it on second pile. You missing x+1 candies on first and x+k-1 candies on second pile. The amount of anger is (x+1)^2 + (x+k-1)^2 and when we multiply brackets it's equal to 2(x^2) + 2xk + k^2 -2k + 2. So total amount of anger decreased, when we balanced number of missing candies on piles.
So solution is clear now. We want to divide candies in such manner, that on each pile number of missing candies is equal (or may differ by 1).
The easiest and the fastest way to find number of missing candies on each pile is use binary search. If we want r candies missing everywhere, we should find out, if sum of all piles minus r for each of them (maybe even less than r, if pile is not big enough) is less or equal than M — total number of candies.
When we find optimal (as small as possible) value of r just fairly distribute all candies between piles. And you've got it :)
I think, that hardest part is to realize, that you want to balance number of missing candies on each pile. After this, it's pretty easy.
i dont think ur approach works correctly if there exists a pile with less than
r
candies initially.i think this was the case on which i got WA.
thnx @monsterspy it was a great explanation, got the logic!!