【算法分析】
● 本题陈述有歧义。经过查阅相关文献,可知题目实际要求的是合并后整体减半,而非合并前各自减半。并且,要注意题意是“结果向下取整”,即中间结果为浮点数。仅最终结果向下取整。
● 贪心策略为“每次合并当前最短的两段绳子”。依此贪心策略,可求得本题所需最大长度。若先合并其他更长的两段绳子,后续操作会被多次对折,导致总损耗更大。
`例如,若输入为[3,4,10],则:
★ 贪心策略 –> (3+4)/2=3.5,(3.5+10)/2=6.75,取整得6。
★ 非贪心策略 –> (10+4)/2=7,(7+3)/2=5,取整得5。`
● round() 函数的功能是“四舍五入”。
【算法代码】
#include <bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int>> q;
int u,v;
int n,x,t;
int main() {
cin>>n;
while(n--) {
cin>>x;
q.push(x);
}
while(q.size()>1) {
u=q.top(), q.pop();
v=q.top(), q.pop();
t=round((u+v)/2);
q.push(t);
}
cout<<q.top();
return 0;
}
/*
in:
8
10 15 12 3 4 13 1 15
out:
14
*/