POJ 3253 Fence Repair
来源:互联网 发布:网络协议概念三要素 编辑:程序博客网 时间:2024/05/29 04:47
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
Output
Sample Input
3885
Sample Output
34
分析:此题可以转化为哈夫曼树的构造问题,用固定长度的Li(1<= i <= N):(1)从N个木块中每次找出最短的两块,合并,放入队列中(2)重复1过程,知道队列中剩余一个元素。如此,便可保证总消耗最小。
<pre name="code" class="cpp">#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<queue>using namespace std;//重载操作,将队列中的元素从小到大排列struct cmp{ bool operator()(int &a,int &b) { return a > b; }};int main(){ int t; while(~scanf("%d",&t)) { priority_queue<int,vector<int>,cmp>que;//定义优先队列 int x; for(int i = 0;i < t;i++) { scanf("%d",&x); que.push(x); } long long sum = 0; while(que.size() > 1) { int a = que.top(); que.pop(); int b = que.top(); que.pop(); sum += a + b; que.push(a + b); } printf("%lld\n",sum); } return 0;}
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- POJ 3253Fence Repair
- POJ--3253 -- Fence Repair
- poj-3253-Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- poj 3253 Fence Repair
- POJ - 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- poj 3253---Fence Repair
- iOS 最新版 CocoaPods 的安装流程(非原创)
- nodejs + redis/mysql 连接池问题
- 【C#设计模式-观察者模式】
- 抽象工厂模式
- 指针和引用的区别
- POJ 3253 Fence Repair
- iOS开发多线程篇—线程间的通信
- (不易)POJ-2229 DP,数的分解
- 在Androd studio 中,TextView设置Text居然都是大写
- NLTK笔记1
- 流媒体开发(三)视频播放
- Android 下拉刷新框架实现
- Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换
- 通俗易懂机器学习——朴素贝叶斯算法