1117 聪明的木匠

 

一位老木匠需要将一根长的木棒切成N段。每段的长度分别为L1,L2,......,LN(1 <= L1,L2,…,LN <= 1000,且均为整数)个长度单位。我们认为切割时仅在整数点处切且没有木材损失。
木匠发现,每一次切割花费的体力与该木棒的长度成正比,不妨设切割长度为1的木棒花费1单位体力。例如:若N=3,L1 = 3,L2 = 4,L3 = 5,则木棒原长为12,木匠可以有多种切法,如:先将12切成3+9.,花费12体力,再将9切成4+5,花费9体力,一共花费21体力;还可以先将12切成4+8,花费12体力,再将8切成3+5,花费8体力,一共花费20体力。显然,后者比前者更省体力。
那么,木匠至少要花费多少体力才能完成切割任务呢?
 
Input
第1行:1个整数N(2 <= N <= 50000)
第2 - N + 1行:每行1个整数Li(1 <= Li <= 1000)。
Output
输出最小的体力消耗。
Input示例
3
3
4
5
Output示例
19

水题,其实就是合并果子

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cctype>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 int n,ans;
 9 
10 priority_queue<int,vector<int>,greater<int> > q;
11 
12 inline void read(int&x) {
13     int f=1;register char c=getchar();
14     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
15     for(;isdigit(c);x=x*10+c-48,c=getchar());
16     x=x*f;
17 }
18 
19 int hh() {
20     read(n);
21     for(int v,i=1;i<=n;++i) read(v),q.push(v);
22     for(int x,y;!q.empty();) {
23         x=q.top();q.pop();
24         y=q.top();q.pop();
25         ans+=x+y;
26         if(q.empty()) break;
27         q.push(x+y);
28     }
29     printf("%d
",ans);
30     return 0;
31 }
32 
33 int sb=hh();
34 int main(int argc,char**argv) {;}
代码
 
原文地址:https://www.cnblogs.com/whistle13326/p/7562616.html