求证明贪心思路正确性(已AC)
查看原帖
求证明贪心思路正确性(已AC)
687685
not_so_littlekayen楼主2025/7/28 09:15

先选取前 k+1k+1 个数,此时这些冰块里必定有一个冰块质量减 11resres 记录全部融化成 11 所需减少的总质量,考虑这段时间内会被影响到的冰块,numnum 为目前为止选择的冰块个数,这些冰块需要融化的时间即为 resnumk\lfloor \frac{res}{num-k} \rfloor,如果 anum+1a_{num+1} 在这些冰块全部质量变为 11 之前会融化,就把 anum+1a_{num+1} 加入这些冰块并重新更新 resnumk\lfloor \frac{res}{num-k} \rfloor 知道无法满足条件(即其他冰块无论如何不会在这些冰块融化之前融化)。如何证明其充分性和必要性?

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a;i <= b;i++)
#define int long long
const int Max = 1e6+5;
int n, k, a[Max], res = 0, num;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	rep(i, 1, n)cin >> a[i];
	sort(a+1, a+n+1);
	num = k+1;
	rep(i, 1, k+1)res += a[i]-1;
	rep(i, k+2, n)
	{
		if(a[i] <= res/(num-k))num++, res += a[i]-1;
		else break;
	}
	cout << res/(num-k);
	return 0;
}
2025/7/28 09:15
加载中...