poj 3253 Fence Repair

来源:互联网 发布:先入为主知乎 编辑:程序博客网 时间:2024/04/30 02:57

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

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).

这道题大致思想和霍夫曼树差不多~我开始也是通过建霍夫曼树来求~但弄了半天都是WA~实在没办法~

发现其实每次只用输出最小的两个数~根本不用霍夫曼树~用优先队列就行~

不过~到现在我还是不知道我建的霍夫曼树到底为什么WA~囧啊~

#include"iostream"#include"queue"#include"cstdio"#include"vector"using namespace std;struct cmp{bool operator()(long long a,long long b){return a>b;\\让优先队列从小到大排列}};priority_queue<long long ,vector<long long>,cmp>Que; \\定义优先队列~int main(){long long n;long long i;long long temp;long long totall=0;long long s1;long long s2;scanf("%lld",&n);for(i=0;i<n;i++){scanf("%lld",&temp);Que.push(temp);}while(Que.size()!=1){s1=Que.top();\\每次将最小的出队~Que.pop();s2=Que.top();\\同上~Que.pop();temp=s1+s2;totall+=temp;\\最小和次小之和入队~Que.push(temp);}printf("%lld\n",totall);return 0;}
话说这是我建霍夫曼树的编码~运行正常~可为什么老是WA~纠结啊~抓狂

#include"iostream"#include"cstdio"#include"stdlib.h"using namespace std;typedef struct {long long weight;long long parent,lchild,rchild;}HTNode,*HuffmanTree;HuffmanTree  T;long long w[25000];long long totall=0;void Select(HuffmanTree &HT,long long n,long long & s1,long long & s2){long long i;long long s=100000,t=100000;for(i=1;i<=n;i++){if(HT[i].parent==0&&(HT[i].weight<s||HT[i].weight<t)){if(s<t){t=HT[i].weight;s2=i;}else{s=HT[i].weight;s1=i;}}}if(s1>s2){i=s1;s1=s2;s2=i;}}long CreateHuffmanTree(HuffmanTree &HT, long long w[25000],long long n){long long m;long long s1,s2;long long i;HuffmanTree p;if(n<=1)return totall=w[1];m=2*n-1;HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));for(p=HT+1,i=1;i<=n;++i,++p){p->weight=w[i];p->lchild=0;p->rchild=0;p->parent=0;}for(;i<=m;++i,++p){p->weight=0;p->lchild=0;p->rchild=0;p->parent=0;}for(i=n+1;i<=m;i++){Select(HT,i-1,s1,s2);HT[s1].parent=i;HT[s2].parent=i;HT[i].lchild=s1;HT[i].rchild=s2;HT[i].weight=HT[s1].weight+HT[s2].weight;totall+=HT[i].weight;}}int main(){long long n;long long i;scanf("%lld",&n);for(i=1;i<=n;i++)scanf("%lld",&w[i]);CreateHuffmanTree(T,w,n);printf("%lld\n",totall);}


原创粉丝点击