Fence Repair POJ - 3253

本文介绍了一种通过优先队列实现的最优木板切割策略,旨在解决将一块长板分割成多个指定长度的小板的问题,同时最小化切割成本。文章详细解释了算法思路,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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块给定长度的板子,每分割一次板子需要付板子较长的那一部分的钱数,钱数等于较长板子的长度。

思路:

每次被拿来分割的肯定是两个板子中较长的那一个,所以假设板子已经全部分割出来了,求分割板子的逆过程。先找出最小的两个板子,他们的长度之和就是原来被拿来分割的板子的长度,即切这块板子需要付的钱。

因为每次都要找出长度最小的两个板子,这里使用了优先队列,greater是从大到小排,每次弹出长度最小的两个(这样才能使付的钱尽可能少),求和之后加入原队列并重新排序。每次求的和(两个较短板子的长度之和)即是上一次切板子(选取上一次分割后得到的较长的板子来进行这一次的分割)需要付的钱。

C++代码:

#include <iostream>
#include<vector>
#include<queue>
using namespace std;
//提交时错在没有改成__int64
int main()
{
    int n,l,i;
    __int64 l1,l2,total=0;
    priority_queue<__int64,vector<__int64>,greater<__int64> >q; //greater是从大到小排列,栈顶是最小的
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>l;
        q.push(l);
    }
    while(q.size()>1)
    {
        l1=q.top(); //保存栈顶值,再弹栈
        q.pop();
        l2=q.top();
        q.pop();
        total+=l1+l2;
        q.push(l1+l2);
    }
    cout<<total<<endl;
    return 0;
}

 

优先队列使用方法参考出处:

http://www.cnblogs.com/void/archive/2012/02/01/2335224.html 优先队列priority_queue 用法详解

http://www.cnblogs.com/mr-wid/archive/2013/01/22/2871105.html 学习C++ -> 向量(vector)

https://www.cnblogs.com/xzxl/p/7266404.html C++STL——优先队列

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值