题解 CF1428E Carrots for Rabbits
这题的其它题解都没有去证明保证贪心正确性的关键性质
首先最小的
然后对式子进行移项,
有了这个结论意味着不可能出现一行前面很小突然就变得很大的情况(因为只会更小),于是贪心就对了。
std::priority_queue<std::array<long long, 3>> q;
void main() {
std::cin >> n >> k;
for (int i = 1; i <= n; i++) std::cin >> a[i];
auto sqr = [](long long x) { return x * x; };
auto f = [&](long long l, long long p) {
l = a[l];
return sqr(l/p)*(p-l%p) + (l%p)*sqr(l/p+1);
};
for (int i = 1; i <= n; i++)
q.push({f(i, 1)-f(i, 2), i, 2}), ans += f(i, 1);
for (int i = 1; i <= k - n; i++) {
auto c = q.top(); q.pop();
ans -= c[0];
if (c[2] < a[c[1]])
q.push({f(c[1], c[2])-f(c[1], c[2]+1), c[1], c[2]+1});
}
std::cout << ans;
}