Fence Repair(sdut 2432)

Fence Repair

Time Limit: 2000MS Memory Limit: 65536KB

Problem 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


Example Input

3
8
5
8

Example Output

34

题目大意是要割木头,要割的长度最短,其实也就相当于是把几块木头拼起来,问按什么顺序拼拼接长度最短;

题目是在堆的专题里的,,但是,,,我不会建阿夫曼树(QAQ。。)于是学习了c++中的优先队列2333,久仰c++大名,今天才体会到它的强大,以后就是c++的人了2333,这道题,,就暂且算是会了吧.;

优先队列做的c++代码

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>//队列的头文件
#include <algorithm>
#include <bits/stdc++.h>//传说中的万能头文件
using namespace std;
int main()
{
    int n,i;
    long long a[500100];
    long long sum=0,a1,a2;
    scanf("%d",&n);
    for(i=0; i<n; i++)
        scanf("%lld",&a[i]);
    priority_queue<int,vector<int>,greater<int> >q;//优先队列,有了它以后出列就按从小到大出了,
                                                   //简直和作弊似的爽死了2333
    for(i=0; i<n; i++) 
    q.push(a[i]);//进入队列
    while(q.size()>1)//这里注意:因为最后只剩下一堆,所以长度要控制为大于1
    {
        a1=q.top();//将a1定义为首元素
        q.pop();//出队列
        a2=q.top();//同上
        q.pop();//同上
        sum+=a1+a2;
        q.push(a1+a2);//这里注意是将a1+a2进入队列,不能是sum;
    }
    printf("%lld\n",sum);
}
[/c++]
c++自带的优先队列中 
priority_queue<int>q;
代表从大到小出队;
而
priority_queue<int,vector<int>,greater<int> >q;
 代表从小到大出队 
其中 
第二个参数为容器类型。
第二个参数为比较函数。 
以下是借dalao的c用阿夫曼树写的代码,膜<span></span></div>


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MALLOC(x,y) ((x *)malloc(sizeof(x)*y))
void up(long long *heap, int index)
{
    long long temp = heap[index];
    while (index > 0 && temp < heap[(index - 1) / 2])
    {
        heap[index] = heap[(index - 1) / 2];
        index = (index - 1) / 2;
    }
    heap[index] = temp;
}
void down(long long *heap,int index,int len)
{
    int dad = index , son = dad * 2 + 1;
    while (son < len)
    {
        if (son + 1 < len && heap[son + 1] < heap[son])son += 1;
        if (heap[dad] < heap[son])break;
        else
        {
            long long temp;
            temp = heap[dad];
            heap[dad] = heap[son];
            heap[son] = temp;
            dad = son;
            son = son * 2 + 1;
        }
    }
}
void push(long long *heap, int len , long long data)
{
    heap[len] = data;len += 1;
    up(heap,len - 1);
}
long long pop(long long *heap,int len)
{
    long long data = heap[0];
    heap[0] = heap[len - 1];
    down(heap,0,len - 1);
    return data;
}
int main()
{
    long long *heap = MALLOC(long long,20009),heaplen = 0;
    int num;
    int i;
    long long ok = 0;
    scanf("%d",&num);
    for (i = 0 ; i < num ; i ++)
    {
        long long data;
        scanf("%lld",&data);
        push(heap,heaplen,data);
        heaplen += 1;
    }
    for (i = 0 ; i < num - 1 ; i ++)
    {
        long long lchild,rchild;
        lchild = pop(heap,heaplen);heaplen -= 1;
        rchild = pop(heap,heaplen);heaplen -= 1;
        ok += lchild + rchild;
        push(heap,heaplen,lchild + rchild);heaplen += 1;
    }
    printf("%lld",ok);
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注