POJ-3253 Fence Repair

来源:互联网 发布:淘宝网店运营书籍 编辑:程序博客网 时间:2024/06/09 20:27

Fence Repair
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 47701 Accepted: 15644

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

Source

USACO 2006 November Gold

题意:农夫约翰为了修理栅栏,需要把一块长木板切成N块(一维分割),准备切成的子木板长度和等于原来长木板的长度,每次切断木板时,开销为这块木板的长度,比如把长度为21的木板切成8,8,5,先把21切成13,8,开销为21,再把13切成5,8,开销为13,总开销为34.,现在给出切割出的子木板的块数和每块的长度,求最小开销。

思路:与哈夫曼编码思想类似。每次都寻找最短的两块木板拼接,记录开销,并将拼接出的新木板加入所有木板中,直到木板块数等于1,计算完成。切割方法可以按照哈夫曼二叉树的思想进行,最短的木板在树中层数越深。这里放另外一种办法。

我们总希望找到当前所有木板里最短的两块进行拼接,那有没有一种结构可以按照数据的升序或降序动态排序且支持加入取出元素呢......当然有了(☆゚∀゚)

priority_queue <int,vector<int>,greater<int> > board;
priority_queue是C++STL模板库里的一种数据结构,功能顾名思义,优先队列,第一个参数int表示队列元素类型,取double,string之类的也是可以的,vector<int>是存储元素的一个容器,greater<int>表示该队列为升序排列,less<int>则为降序排列。常用函数基本和queue类型相同,但是queue获取队头元素使用q.front(),而priority_queue获取队头元素使用的是q.top(),注意区分。
#include<iostream>#include<queue>using namespace std;#define ull unsigned long long#define ll long longpriority_queue <int,vector<int>,greater<int> > board;ll ans;int main(){    int n;    cin >> n;    ans=0;    for(int i=0;i<n;i++)    {        int temp;        cin >> temp;        board.push(temp);    }    while(!board.empty())    {        int a,b;        a=board.top();        board.pop();        b=board.top();        board.pop();        ans=ans+a+b;        board.push(a+b);        if(board.size()==1)            break;    }    cout << ans << endl;    return 0;}





原创粉丝点击