贪心算法,每次从所有绳子中选两根最短的做成一根绳子,注意将做好的绳子放回容器中。
注意优先队列重写排序规则的写法。
#include<cstdio>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
struct cmp {
bool operator () (double a, double b) {
return a > b;
}
};
priority_queue<double, vector<double>, cmp> heap;
int main() {
int n;
scanf("%d", &n);
for (int i = 0;i < n;i++) {
int temp;
scanf("%d", &temp);
heap.push(temp);
}
while (heap.size() > 1) {
double r1 = heap.top();
heap.pop();
double r2 = heap.top();
heap.pop();
heap.push((r1 + r2) / 2);
}
printf("%d", (int)heap.top());
}