POJ 3253 Fence Repair (贪心)
题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度。求出按照要求将木板切割完毕后的最小开销。
思路:比较奇特的贪心
每次切割都会将当前木板一分为二,可以按切割要求画出二叉树。
总开销 = 各叶子节点的值 x 该叶子节点的深度
树的深度一定,为了使总开销尽可能的小,那么比较深的叶子节点的值应尽可能的小,所以二叉树应为:

模拟出所建立的二叉树,求出根节点的值即可
为了节省时间,可以利用优先队列每次取出最小的两个值合并,并将两数之和重新加入数组,直到数组内的元素个数为一时结束,时间复杂度为O(nlogn)。
#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define LL long long
#define MOD 100000007
#define MAXSIZE 20005 using namespace std; int n,a[MAXSIZE]; LL Solve()
{
LL ans = ;
priority_queue<int,vector<int>,greater<int> >Q;
for(int i=;i<=n;i++)
Q.push(a[i]);
while(n > )
{
int minn1 = Q.top();
Q.pop();
int minn2 = Q.top();
Q.pop();
ans += (minn1 + minn2);
Q.push(minn1 + minn2);
n--;
}
return ans;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]); LL ans = Solve();
printf("%lld\n",ans);
}
return ;
}
POJ 3253 Fence Repair (贪心)的更多相关文章
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- POJ 3253 Fence Repair 贪心+优先队列
题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分. 思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
随机推荐
- java 网络编程 TCP协议 java 服务器和客户端 java socket编程
一个 HelloWord 级别的 Java Socket 通信的例子.通讯过程: 先启动 Server 端,进入一个死循环以便一直监听某端口是否有连接请求.然后运行 Client 端,客 ...
- CentOS7 yum 安装 PHP 5.6.24
配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...
- (链表 递归) leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- (set) 人见人爱A-B hdu2034
人见人爱A-B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- go操作redis和mysql示例
一:redis示例 使用redis的包是: github.com/garyburd/redigo/redis 1:编写第一个示例: 链接,设置,获取 redis_basic.go package ma ...
- linux基本
一.初识 Linux与windows相比的优点是:长期稳定的运行,避免了因为系统的问题导致的项目运行中断:占用资源少:开源软件多. Centos(Community Enterprise Operat ...
- 递归思维判断数组a[N]是否为一个递增数组
递归的方法:记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束: bool fun( int a[], int n ) { ) { return true; } ) { ] ...
- 剑指Offer_编程题_7
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 class Solution { public: int Fibonacci(int n) ...
- Kubernetes 集群ca验证
创建集群时跳过ca验证 # vim /etc/kubernetes/apiserver 去除KUBE_ADMISSION_CONTROL中的 SecurityContextDeny,ServiceAc ...
- jQuery使用(八):运动方法
show().hide().toggle() 参数:null或(duration,easing,callblack) fadeIn().fadeout().fadeToggle().fadeTo() ...