Description

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

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 make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. (着重理解红体字对于题意理解很重要)
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

请读者自己先读一遍英文原题

(题目理解:先计算一共要多长木板?8+5+8=21第一次切割肯定从从长度21的原木板上切一刀,也就是21元的的第一刀的钱花定了,之后就看你会不会精打细算了,注意:每刀的钱数是这刀下去的切开的两段木板长度和!!)

编程实现:

法一(简单的哈弗曼编码,运用到直接插入排序)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7. long long L[20010];
  8. int main() {
  9. int n;
  10. while(~scanf("%d", &n)) {
  11. int i, j;
  12. for(i = 0 ; i < n; i++) {
  13. scanf("%I64d", L + i);
  14. }
  15. sort(L, L + n);
  16. long long minn = 0, sum;
  17. for(i = 0; i < n - 1; i++) { //n - 1 次后就剩下一根木棍了,此时停止
  18. sum = L[i] + L[i + 1];
  19. minn += sum;
  20. for(j = i + 2; j < n; j++) {
  21. if(sum > L[j]) {
  22. L[j - 1] = L[j];  //向后移动
  23. if(j == n - 1) {  //到了最后一根时放最后
  24. L[j] = sum;
  25. }
  26. }
  27. else {
  28. L[j - 1] = sum; //找到位置
  29. break;
  30. }
  31. }
  32. }
  33. printf("%I64d\n", minn);
  34. }
  35. return 0;
  36. }

法二(STL优先队列)

//木板数目很多时,能提高程序速度#include<stdio.h>  #include<vector>  #include<queue>
#include <functional> // std::greater
using namespace std;
//定义cmp类  类似于sort函数中定义的cmp函数  此题并没有用到  纯属优先权队列尝试  直接使用greater即可
//class cmp
//{
//public:
//  bool operator()(const __int64 a, const __int64 b)const
//  {
//      return a>b;
//  }
//};
int main()
{int n;while (scanf("%d", &n) != EOF)//能用scanf就不要用cin  将cin更改为scanf后  时间由47ms降低至17ms   {priority_queue<__int64, vector<__int64>,greater<__int64> > q;//其中的greater若不写的话 则优先权最大的是最大元素   写了greater后  优先权最大的是最小元素  //优先权队列有机会的话会再详细探索  __int64 len;for (int i = 0; i<n; i++){scanf("%I64d", &len);   //由于结果范围可能很大  采用__int64  q.push(len); //输入要求的木板长度(费用)并入队  }__int64 mincost = 0; //最小费用  while (q.size()>1)//当队列中小于等于一个元素时跳出  {__int64 a = q.top();  //得到队首元素的值,并使其出队  即最小元素  q.pop();__int64 b = q.top(); //两次取队首,即得到最小的两个值  即次小元素  q.pop();mincost += a + b;q.push(a + b); //入队   将最小和次小元素的和入队列  }printf("%I64d\n", mincost);while (!q.empty())  //清空队列  q.pop();}
}

参考:https://blog.csdn.net/Strokess/article/details/50995416

转载必须注明出处:https://blog.csdn.net/qq_34793133/article/details/80663210

贪心算法-----poj 3253 Fence Repair(切木板)相关推荐

  1. POJ 3253 Fence Repair(修篱笆)

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

  2. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  3. POJ 3253 Fence Repair C++ STL multiset 可解

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53106 Accepted: 17508 Descri ...

  4. POJ 3253 Fence Repair 贪心

    每次合并选最短的两块木板,用优先队列优化. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include&l ...

  5. POJ No. 3253 Fence Repair

    POJ No. 3253 Fence Repair 官方地址 题目 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板的长度为L1.L2.-.LN,未切割前木板的长度恰好为切割后木板长 ...

  6. 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

    倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...

  7. [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1272  Solved: ...

  8. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板

    题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer ...

  9. bzoj1724[Usaco2006 Nov]Fence Repair 切割木板*

    bzoj1724[Usaco2006 Nov]Fence Repair 切割木板 题意: FJ需要n块木板,第i块木板长度为ai.但他只有一块长度为sigma(i,1,n)ai的木板.每切一次的代价为 ...

最新文章

  1. linux shell awk BEGIN END 处理文本之前之后执行操作 简介
  2. (转)rtmp协议简单解析以及用其发送h264的flv文件
  3. SQLErrorCodeSQLExceptionTranslator 使用以下的匹配规则
  4. Apache htaccess的简单总结,以及参数的使用
  5. [转载] JAVA数组实现学生成绩统计
  6. 火星文字充斥网络 网友怒骂其侮辱汉字
  7. 3 微信开发本地代理环境的搭建--实现将内网ip映射到外网
  8. ARC070D No Need
  9. PRML Chapter 02 Probability Distributions
  10. 计算机统计字符数,如何在电脑上统计文字字数及标点个数
  11. ALIGN:自动化模拟布局的系统
  12. 小程序api(常用)
  13. 很火的Fastapi框架,用async函数真的比普通函数快吗?
  14. 用Scrapy和Selenium爬取动态数据
  15. 通过抓包判断是否支持 802.11k and 11r
  16. 桌面管理之道让桌面看起来都舒服
  17. 二叉树的前序,中序,后序遍历Java实现
  18. APP用户协议和隐私政策怎么写?
  19. Cause: java.sql.SQLSyntaxErrorException: Unknown table ‘argue_backend‘ in field list 使用Mybatis报错
  20. ubuntu 上tp-link无线网卡驱动安装

热门文章

  1. 文献阅读——唐宋古逸佛教懺儀研究
  2. UWB精准定位方案,厘米级高精度技术应用,智能配对感应技术
  3. [量化-005]券商股是一切行情的风向标
  4. 医院医疗抛弃品也需要施行RFID资产管理系统,以免管理不当带来危险-新导智能
  5. 关于物联网卡机卡绑定
  6. Linux的linux aarch64和linux x86_64
  7. 怎么用C51语言实现50ms延时,单片机入门-C51语言实现简单的红绿LED交通灯控制
  8. 电子计算机在可视范围内由,电话的种类有(移动电话、可视电话)。——青夏教育精英家教网——...
  9. 辞退了一位简历造假的程序员
  10. 基于NCC的模板匹配算法的一些补充