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 Nplanks. 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).
 
题解:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2e4+;
#define INF 0x3f3f3f3f
#define ll long long
int a[maxn];
int n; int main(){
cin>>n;
for( int i=; i<n;i++ ){
scanf("%d",a+i);
}
ll ans=;
while(n>){
int mini=,maxi=;
if(a[mini]>a[maxi]) swap(mini,maxi);
for( int i=; i<n; i++ ){
if(a[i]<a[mini]){
maxi=mini;
mini=i;
}
else if(a[i]<a[maxi]){
maxi=i;
}
}
int t=a[mini]+a[maxi];
ans+=t;
if(mini==n-) swap(mini,maxi);
a[mini]=t;
a[maxi]=a[n-];
n--;
}
cout<<ans<<endl;
return ;
}

Fence Repair POJ - 3253 (贪心)的更多相关文章

  1. 贪心算法——Fence Repair(POJ 3253)

    题目描述 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1,L2,L3--LN,未切割前木板的长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.请 ...

  2. R - Fence Repair POJ - 3253

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

  3. Fence Repair (POJ 3253)

    农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1.L2.L3...LN,未切割前的木板长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.例如长度为 ...

  4. Fence Repair POJ - 3253 哈夫曼思想 优先队列

    题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...

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

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

  6. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考

    Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...

  7. bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板(贪心+堆)

    一开始被题目读错题= =以为每次只能割一块,那么就是从大到小切 但是其实是可以分为几堆来切的 所以可以逆着来,变为合并n个木板代价最小 易证每次找最小的两堆合并代价最小 用优先队列维护堆..偷偷懒= ...

  8. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  9. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

随机推荐

  1. java学习笔记04-基本数据类型

    编写一款可用的软件,离不开对数据的操作(经常有人说:程序=数据+算法).数据可能有很多类型,比如对于年龄来说,数据就是整数. 对于金额来,数据是带小数的.在java中,可以分为内置数据类型和引用数据类 ...

  2. module.exports与exports区别

    CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. ...

  3. 【easy】437. Path Sum III 二叉树任意起始区间和

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. FTP、FTPS和SFTP

    FTP 一.两种传输方式 ASCII传输方式 假定用户正在拷贝的文件包含的简单ASCII码文本,如果在远程机器上运行的不是UNIX,当文件传输时ftp通常会自动地调整文件的内容以便于把文件解释成另外那 ...

  5. Ext z自写checkbox

    软件弹出框Exr.window里面有许多个CheckBox,用ext自带的CheckBoxGroup也可以实现,但是在后期处理的时候远没有自写的CheckBox灵活,还出了一些莫名其妙的bug 忍无可 ...

  6. ES2018新特性(译文)

    原文链接:css-tricks.com 第9版ECMAScript标准于2018年6月发布,正式名称为ECMAScript 2018(简称ES2018).从ES2016开始,ECMAScript规范的 ...

  7. python3 函数注意要点

    一.定义一个函数: def test(): #用def关键词开头 print('*****') def test2(a,b): #a,b为形参 print(a,b) return a,b # retu ...

  8. 2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用

    一.原理与实践说明 1.实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实践,如ms08-067; (1分) 一个针对浏览器的攻 ...

  9. 用 pdf.js兼容部分安卓显示PDF在线预览 时,a标签直接链接参数文件不能含中文的解决办法

    例子: 项目部署在 Tomcat 上的: <a href="../generic/web/viewer.html?file=doc/register/要显示的文件.pdf" ...

  10. RAID磁盘阵列及CentOS7系统启动流程

    磁盘阵列(Redundant Arrays of Independent Disks,RAID),有“独立磁盘构成的具有冗余能力的阵列”之意,,数据读取无影响.将数据切割成许多区段,分别存放在各个硬盘 ...