贪心问题的结论往往很简单,但如何证明结论的正确性.....
结论:从最短的开始连最终长度最长。
证明:绳子的长度近似是一个逐渐增长的过程,在总长较短时连短的绳子段牺牲较少,而为了连接较长的绳子段所付出牺牲是值得且不可避免的。
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
double ans = 0;
int n;
cin >> n;
vector<ll>a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end()); //贪心:先连长度短的绳子,损失最小
ans = a[0]; //模拟题意连绳子
for (int i = 1; i < n; i++) {
ans = (ans / 2 + a[i] * 1.0 / 2);
}
cout << (ll)ans << endl;
return 0;
}