POJ 3253 Fence Repair 用二叉堆解

来源:互联网 发布:手机解压软件苹果 编辑:程序博客网 时间:2024/06/07 03:34

题目链接:https://cn.vjudge.net/problem/15032/origin

Fence Repair
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 53620 Accepted: 17663

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

3858

Sample Output

34

这个题可以用优先队列解,这里试下二叉堆解法。


AC代码(可作为二叉堆模板)

#include <iostream>#include <cstdio>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int maxsize=20200;struct binary_heap{    int heap[maxsize],id[maxsize],pos[maxsize],n,cnt;    binary_heap():n(0),cnt(0){}    binary_heap(int *a,int len):n(0),cnt(0)    {        for(int i=0;i<len;i++)        {            heap[++n]=a[i];            id[n]=pos[n]=n;        }        for(int i=n/2;i>=1;i--)            down(i);    }    void push(int value)    {        heap[++n]=value;        id[n]=++cnt;        pos[cnt]=n;        up(n);    }    int top()    {        return heap[1];    }    void pop()    {        swap(heap[1],heap[n]);        swap(id[1],id[n--]);        pos[id[1]]=1;        down(1);    }    void change(int i,int value)    {        heap[pos[i]]=value;        down(pos[i]);        up(pos[i]);    }    void erase(int i)    {        heap[pos[i]]=-INF;        up(pos[i]);        pop();    }    void up(int i)    {        int x=heap[i],y=id[i];        for(int j=i/2;j>=1;j/=2)        {            if(heap[j]>x)            {                heap[i]=heap[j];                id[i]=id[j];                pos[id[i]]=i;                i=j;            }        }        heap[i]=x;        id[i]=y;        pos[y]=i;    }    void down(int i)    {        int x=heap[i],y=id[i];        for(int j=i*2;j<=n;j*=2)        {            j+= j<n&&heap[j+1]<heap[j];            if(heap[j]<x)            {                heap[i]=heap[j];                id[i]=id[j];                pos[id[i]]=i;                i=j;            }        }        heap[i]=x;        id[i]=y;        pos[y]=i;    }    int size()    {        return n;    }};int main(){    int N,a[maxsize];    ios::sync_with_stdio(false);    while(cin>>N)    {        long long ans=0;        for(int i=0;i<N;i++)            cin>>a[i];        binary_heap d(a,N);        while(d.size()>=2)        {            int x=d.top();d.pop();            int y=d.top();d.pop();            ans+=x+y;            d.push(x+y);        }        cout<<ans<<endl;    }    return 0;}