http://poj.org/problem?id=3253

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).
这个题说的是一个人要围篱笆,有一块木板,把它切成n份,每份i米长,而没切割一次就收切割成两份的总长的价格,比如8米的木板切成3,5,就要付8美元,然后问你最少付多少钱
然后我想的就是把序列从小到大排列,因为要求最小的钱,所以最大的木板切一次就够了,小的切几次也没关系,所以先把所有加一遍,然后n-1个加起来,再把n-2ge加起来,……最后把所有的都加起来,
但是书上说把它看成树,有多少层就切割多少次,所以比较小的要在最小层,先排序,之后把最小的两个相加,把它当作一个板子,这样就相当于有n-1个板子,依次……

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
    int n,a[50000];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    ll sum=0;
    while(n>1)//直到计算出短板为1
    {
       int l1=0,l2=1;
       if(a[l1]>a[l2])
       {
           swap(l1,l2);
       }
       for(int i=2;i<n;i++)
       {
           if(a[i]<a[l1])
           {
               l2=l1;
               l1=i;
           }
           else if(a[i]<a[l2])
           {
               l2=i;
           }
       }
       int s=a[l1]+a[l2];
       sum+=s;
       if(l1==n-1)
          swap(l1,l2);
       a[l1]=s;
       a[l2]=a[n-1];
       n--;

}
    printf("%lld\n",sum);
    return 0;
}

poj3253 Fence Repair的更多相关文章

  1. 优先队列 poj3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 51411   Accepted: 16879 De ...

  2. Poj3253 Fence Repair (优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 67319   Accepted: 22142 De ...

  3. poj-3253 fence repair(贪心题)

    题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...

  4. poj3253 Fence Repair【哈夫曼树+优先队列】

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

  5. poj3253 Fence Repair(贪心+哈夫曼 经典)

    https://vjudge.net/problem/POJ-3253 很经典的题,运用哈夫曼思想,想想很有道理!! 具体实现还是有点绕人,最后被long long卡了一下,看数据大小的时候单纯相乘了 ...

  6. POJ3253 Fence Repair(贪心)

    分割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,增加队列,原来两个板出队,直到队列中为空或者仅仅剩下一个板时结束.这里使用优先队列较为方便. #include<iostream&g ...

  7. POJ3253 Fence Repair【贪心】

    我们的小伙伴Bingo真的很调皮,他在上课的路上看到树上有个鸟窝,他就想去把他捅下来,但是鸟窝很高他够不到,于是他就到处找木棍,想把这些木棍接在一起,然后去捅鸟窝.他一共找了N跟木棍 (1 ≤ N ≤ ...

  8. [POJ3253]Fence Repair(单调队列)

    题目链接 http://poj.org/problem?id=3253 题目描述 大意:切长度为a的木条的花费是a,给定最终切好的n段各自的长度,问由原来的一根木条(长度为n段长度和)以最终总花费最小 ...

  9. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

随机推荐

  1. C# 方法的回调(上)

    在C#编程中方法的回调有以下几种方式 通过接口.通过委托.定时回调.多线程回调,异步回调 下面就以代码的形式来讲解这种方式 通过接口回调 代码示例如下 定义接口,定义了一个Run 方法: interf ...

  2. Secure Digital

    https://en.wikipedia.org/wiki/Secure_Digital#Technical_details Secure Digital (SD) is a non-volatile ...

  3. SQL IN 操作符、SQL BETWEEN 操作符、SQL Alias(别名)

    IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...

  4. selenium python 安装

    环境为Win64位系统,默认已经安装python2.7到D:\Python27,此次使用的浏览器为chrome 下面是selenium的安装和chromedriver.exe的下载 1.安装selen ...

  5. 获取EMF文件内全部文字, 并按照左上到右下的顺序排序

    因为工作要求, 需要对EMF文件文字内容做分析.....SO, 如下代码出现了 懒得加注释了, 反正对外接口属性就那么几个, 根据英文猜吧, 很容易的 说明一下: 这个东西结果会对所有文字内容按照左上 ...

  6. JQuery中的DOM操作

    JQuery中有很多DOM操作,但是因为之前没有总结过,所以用来用去都是那几个,一写html中的表单交互,尤其是那些复杂的表单交互,就是一大坨的js,我自己看着都费劲. 所以我感觉有必要总结一下 &l ...

  7. SqlServer删除表中重复记录

    重复记录:有两个意义上的重复记录 一是完全重复的记录,也即所有字段均重复的记录: 二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略. 1.对于第一种重复,比较容易 ...

  8. JAVASE02-Unit07: 基本IO操作 、 文本数据IO操作

    基本IO操作 . 文本数据IO操作 java标准IO(input/output)操作 package day07; import java.io.FileOutputStream; import ja ...

  9. Python开发【前端】:Ajax

    Ajax Ajax即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术,AJAX = ...

  10. 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta

    我直接在父类的构造方法中调用了sessionj结果就报这个错误 搜了好久 让改web.config 可是不起作用 代码如下: public class BasePage:System.Web.UI.P ...