标签:oar span mos and scanf contains size sam who
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn‘t own a saw with which to cut the wood, so he mosies over to Farmer Don‘s Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn‘t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Output
Sample Input
3 8 5 8
Sample Output
34
Hint
1 #include <cstdio> 2 #include <queue> 3 // bits/stdc++.h编译错误 4 using namespace std; 5 priority_queue<long long, vector<long long>, greater<long long> > value; 6 //优先队列设定数值小的先出队 7 int main() 8 { 9 int n; 10 long long ans = 0; //ans记录耗费的钱财 11 while(scanf("%d", &n) != EOF){ //输入木板数量 12 ans = 0; //没有切割是耗费钱财为0 13 while(!value.empty()){ //清空队列 14 value.pop(); 15 } 16 for(int i = 0; i < n; i++){ //输入并入队所有长度 17 int temp; 18 scanf("%d", &temp); 19 value.push(temp); 20 } 21 while(value.size() > 1){ //如果队中所剩木板数量大于则1进行拼接 22 int len1 = value.top(); 23 value.pop(); 24 int len2 = value.top(); 25 value.pop(); 26 //出队两个最小长度进行拼接操作 27 ans += len1 + len2; //记录消耗的钱财 28 value.push(len2 + len1); //新长度入队 29 } 30 printf("%lld\n", ans); //输出答案 31 } 32 return 0; 33 }
标签:oar span mos and scanf contains size sam who
原文地址:https://www.cnblogs.com/suvvm/p/9902174.html