POJ--3253 Fence Repair

2023-10-13 03:32
文章标签 poj 3253 repair fence

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

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 35738 Accepted: 11586

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

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<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int aa[20010];
int main()
{int n;while(cin>>n){long long ans=0;for(int i=0;i<n;i++)cin>>aa[i];while(n>1){int min1=0,min2=1;if(aa[min1]>aa[min2]) swap(min1,min2);for(int i=2;i<n;i++){if(aa[i]<aa[min1]){min2=min1;min1=i;}else if(aa[i]<aa[min2]){min2=i;}}int t=aa[min1]+aa[min2];ans+=t;if(min1==n-1) swap(min1,min2);aa[min1]=t;aa[min2]=aa[n-1];n--;}cout<<ans<<endl;}return 0;
}

第二种为优先队列:(这个更加高效)
以下是我的AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
int aa[20005];
int main()
{//freopen("input.in","r",stdin);int n;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%d",&aa[i]);LL ans=0;priority_queue<int,vector<int>,greater<int> > que;for(int i=0;i<n;i++){que.push(aa[i]);}while(que.size()>1){int min1,min2;min1=que.top();que.pop();min2=que.top();que.pop();ans+=min1+min2;que.push(min1+min2);}printf("%lld\n",ans);}return 0;
}


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



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

相关文章

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

poj 3294(Life Forms) 2分+ 后缀数组

我曾用字符串hash写,但是超时了。只能用后最数组了。大致思路:用不同的符号吧字符串连接起来,构建后缀数组,然后2分答案,依次扫描后缀数组,看是否瞒住条件。 VIEW CODE #include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>#include<cassert>#

poj 2391 Ombrophobic Bovines (网络流)

这是一道很经典的网络流的题目。首先我们考虑假如我们的时间为无穷大。我们吧每个点拆成2个点 i和i' .。虚拟源点s和汇点t。对于每个点建边(s,i, a[i])  (i‘,t,ib[i]) 。 其中a[i]为给点有多少牛,b[i]为容量。i和j连通 建边 (i,j',inf);如果最大流==所有牛的个数,就可能装下所有的牛。那么现在我们考虑时间。假设最大时间为T.那么如果i到j的的最短时间>T

poj 1330 LCA 最近公共祖先

水题目。直接上代码了。 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点。实际上集合内的每个点是相同的,这样的集合就是一个强连通分量。 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图。在dag的图上面进行dp。可以先用拓扑排序后dp。或者建反响边记忆化搜索 。 VIEW

Repair LED lights

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

poj 1564 Sum It Up -- DFS 递归

题意:给一个数 t ,以及 n 个数,求 n 个数中的几个数加起来的和为 t 的情况有多少种。 注意:题目要求相同的组合方式不能出现2次,即 “3 4 1 1 1 1 ” 的结果为:“1+1+1”。 思路:一个  for  循环遍历一遍,每个 i 表示以当前数为起点开始一次DFS递归,当所遍历的和为 t 时,输出该组合方式,如果大于 t 则返回,小于则往下递归。以二维数组保存已经输出过的数据,

道路 Road(百练,POJ)

题目链接: 1724 -- ROADS (poj.org) 题目描述: 思路: 这题目乍一看,是一个含有2个标尺的单源最短路问题,挺难处理的。 既然没法直接用最短路处理,那我们就“记录信息”,将花费的时间也记录进dp数组,然后跑“状态最短路”。 用f[i][j] 表示到达点i 且 总花费时间为j的最短距离,然后跑堆优化的dijkstra算法就好。由于不含有负边权,因此可以搞一个vis数

[POJ 1284] Primitive Roots (数论,原根)

POJ - 1284 题意是,求一个质数的原根 原根的定义是,对于正整数 aimodp(i=[1,p−1]) a^i mod p (i=[1,p-1])得到的集合为{1,2,…,p-1},那么则称 a是 p的一个原根 对于任意正整数 p,其原根个数为 ϕ(ϕ(p)) \phi( \phi(p) ), ϕ(n) \phi(n)为欧拉函数,表示小于n且与n互质的数的个数 而质数的欧拉函数值

[POJ 3764] The xor-longest Path (Tire树 + 贪心)

POJ - 3674 题意是给你一个树,每条边有一个权值,求得树上一条路径,使路径上每条边权值的异或和最大 首先用一个 DFS把根到任意点的路径的异或和求出来 xorv[i] 由异或的性质可得点 u和点 v的异或和即为 xorv[u]^xorv[v] ( 根到两点 LCA的异或和会消去) 然后问题就转化成在区间内找两个值,使得他们的异或和最大 与 LightOJ - 1269一样的做法,