poj3253 Fence Repair

2024-04-07 04:08
文章标签 repair fence poj3253

本文主要是介绍poj3253 Fence Repair,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



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
3
8
5
8
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).
给你一个无限长的木板,你需要锯成所给的长度,费用是与长度相等,问所需要的最小长度,完全转化为哈夫曼树嘛!!

ac代码:

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct cmp1{ bool operator ()(long long &a,long long &b){
        return a>b;
    }
};
int main(){
   long long n,m;
   while(cin>>n)
   {
       priority_queue<long long,vector<long long>,cmp1> que;
       for(int i=1;i<=n;i++)
       {
           scanf("%lld",&m);
           que.push(m);
       }
        long long re=0;
        while(!que.empty())
        {
            long long sum=0;
            sum+=que.top();
            que.pop();
            if(!que.empty())
            {
                sum+=que.top();
                que.pop();
                if(!que.empty())
                que.push(sum);
            }
            re+=sum;
        }
        cout<<re<<endl;
   }
   return 0;
}


这篇关于poj3253 Fence Repair的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881601

相关文章

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

【codeforces】484E. Sign on Fence 可持久化线段树

传送门:【codeforces】484E. Sign on Fence 题目分析:看了题解才会呢,感觉太巧妙了~ 先对高度从高到低排序,然后构造可持久化线段树,每个节点保存一个高度下对应区间的左连续最大1个数,右连续最大1个数,区间最长连续1个数,然后查询一个区间内最长连续1长度就是线段树区间合并操作。每次询问【L,R,W】就是二分询问的高度(每个高度是一棵线段树),然后看询问区间的连续

Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,,,刷子可以在横着和竖着刷,不能跳着刷,,, 如果是竖着刷,应当是篱笆的条数,横着刷的话,就是刷完最短木板的长度,再接着考虑没有刷的木板,,, 递归调用,,, #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define

codeforces 234F - Fence

感觉是个背包,开始想用dp[i][j]表示红色容量为j的最小值,同时记录这时i的颜色,但这样状态无法转移,可以再加一维dp[i][j][0]表示i为红色,dp[i][j][1]表示i为绿色,枚举j时表示的是前i个恰好用完j容量,绿色容量为tot[i]-j,所以初始化时dp[0][0][0]=dp[0][0][1]=0,其他为无穷大。 #include<iostream>#include<s

POJ 3691 HDU 2457 DNA repair (AC自动机,DP)

http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5690 Accepted: 2669 Description Biologists

Repair LED lights

Repair LED lights  修理LED灯,现在基本用灯带,就是小型LED灯串联一起的 1)拆旧灯条,这个旧的是用螺丝拧的产品 电闸关掉。 2)五金店买一个,这种是磁铁吸附的产品 现在好多都是铝线啊。。。 小部件,我没用上

poj3253 FenceRepair

大致题意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用   提示: 以 3 5 8 5为例: 先从无限长的木板上锯下长度为 21 的木板,花费 21 再从长度为21的木板上锯下长度为5的木板,花费5 再从长度为16的木板上锯下长度为8的

poj1821 Fence 单调队列dp

题意:有n个人刷长度为m的墙。对于每个人有3种属性分别为l,s,w,分别表示这个人可以刷墙的长度,这个人如果刷墙则要刷包含s 位置的一段区间,每刷1单位长的墙获得的利润。问如何安排这n个人,使得获得总利润尽可能大。 思路:设dp[i][j]表示前i个人刷 j面墙可获得的最大利润,那么这个状态可由两个转移而来。 1.如果第i个人刷墙,那么dp[i][j]=max(dp[i][j],dp[i-1

POJ--3253 -- Fence Repair

Fence Repair   Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19763   Accepted: 6269 Description Farmer John wants to repair a small length of the fence around t

pacemaker之fence_xvm:libvirtd

FENCE设备可以分为两种:内部FENCE和外部FENCE,常用的内部FENCE有IBM RSAII卡,HP的iLO卡,还有IPMI的设备等,外部fence设备有UPS、SANSWITCH、NETWORKSWITCH等 本示例针对libvirtd的虚拟机,宿主机为linux。 一 os环境 虚拟机系统为openEuler 20.03 LTS SP1 aarch64 # cat /etc/o