Problem Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN-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 theN 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

Line 1: One integer N, the number of planks

Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to makeN-1 cuts

Sample Input

3
8
5
8

Sample Output

34

题意:要把一个木板截成给定长度的几块小木板,每一次费用是当前木板的长度,给出要截成的小木板的个数 n 与长度,求最小花费。

思路:根据贪心思想,要使费用最小,就要让每次锯成的两块木板的长度和最小。使用优先队列,每次选择最小的两块木头相加,相加后再次进入队列,再选择最小的两块木头相加,依次类推,直至队列为空取得最小值。

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 10001
#define MOD 123
#define E 1e-6
using namespace std;
priority_queue < int,vector<int>,greater<int> > q;//最小值优先的优先队列
int main()
{int n;scanf("%d",&n);for(int i=0;i<n;i++){int temp;scanf("%d",&temp);q.push(temp);}long long sum=0;while(n!=1)//共截取n-1次{int a,b;a=q.top();//取队首元素q.pop();//队首元素出队b=q.top();//取队首元素q.pop();//队首元素出队sum+=a+b;//记录花费q.push(a+b);//队列中最小值与次小值的和入队n--;}printf("%lld\n",sum);return 0;
}

Fence Repair(POJ-3253)相关推荐

  1. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  2. 贪心算法-----poj 3253 Fence Repair(切木板)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  3. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  4. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  5. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  6. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  7. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  8. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  9. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  10. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

最新文章

  1. build/envsetup.sh脚本分析
  2. JavaScript实现longestCommonSubsequence最长公共子序列算法(附完整源码)
  3. web项目开发人员配比_我如何找到Web开发人员的第一份工作
  4. 套娃的开始——Network in Network网络学习笔记
  5. 2018前端学习总结
  6. python操作excel模板_Python Excel模板读写,维护公式和格式
  7. 1.7 单层卷积网络
  8. CI restful小试(二)
  9. 【CCCC】L3-006 迎风一刀斩 (30分),几何关系,找规律 (拼合多边形==斜边等价)
  10. Windows Server 2003 安全指南
  11. linux基础第四天
  12. 简述线性反馈移位寄存器
  13. Openssl学习——x509证书函数
  14. 这几个Python数据可视化探索实例,拿走不谢
  15. 一个简单的RingBuffer
  16. Android 9.0中sdcard 的权限和挂载问题
  17. APP开发定制开发语言
  18. 常用的一些LDO芯片及使用
  19. 如何修改Maven仓库地址为阿里云仓库
  20. WinCE快捷方式浅析

热门文章

  1. 武书连2019中国大学排行榜公布:浙大排名超越北大
  2. LwIP移植到FreeRTOS(STM32F107+DP83848)
  3. Ubuntu搭建nfs服务器
  4. 给 JDK 报了一个 P4 的 Bug,结果居然……
  5. 温昱:架构实践全景图
  6. JEECG 智能开发平台二次开发帮助文档
  7. 什么情况导致全表扫描,而不是用索引 收藏
  8. 编程体系结构(08):Spring.Mvc.Boot框架
  9. Linux系统:centos7下搭建Nginx和FastDFS文件管理中间件
  10. Servlet - 基础