BZOJ1724: [Usaco2006 Nov]Fence Repair 切割木板

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1724

题目大意:

Farmer John想修理牧场栅栏的某些小段。为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50,000)。然后,FJ去买了一块很长的木板,它的长度正好等于所有需要的木板的长度和。接下来的工作,当然是把它锯成需要的长度。FJ忽略所有切割时的损失——你也应当忽略它。 FJ郁闷地发现,他并没有锯子来把这块长木板锯开。于是他把这块长木板带到了Farmer Don的农场,想向FD借用锯子。 作为一个有商业头脑的资本家,Farmer Don没有把锯子借给FJ,而是决定帮FJ锯好所有木板,当然FJ得为此付出一笔钱。锯开一块木板的费用,正比于木板的长度。如果这块木板的长度是21,那么锯开它的花费便是21美分。 谈妥条件后,FD让FJ决定切割木板的顺序,以及每次切割的位置。请你帮FJ写一个程序,计算为了锯出他想要的木板,他最少要花多少钱。很显然,按不同的切割顺序来切开木板,FJ的总花费可能不同,因为不同的切割顺序,会产生不同的中间结果。

题解:一个堆就ok了,只不过是学写系统堆,所以就刷刷水

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<queue>
 7 #define N 20005
 8 #define ll long long
 9 using namespace std;
10 int n,a[N];
11 ll ans;
12 priority_queue<int,vector<int>,greater<int> > h;
13 int read()
14 {
15     int x=0; char ch; bool bo=0;
16     while (ch=getchar(),ch<‘0‘||ch>‘9‘) if (ch==‘-‘) bo=1;
17     while (x=x*10+ch-‘0‘,ch=getchar(),ch>=‘0‘&&ch<=‘9‘);
18     if (bo) return -x; return x;
19 }
20 int main()
21 {
22     n=read();
23     for (int i=1; i<=n; i++) a[i]=read(),h.push(a[i]);
24     int x,y;
25     for (int i=1; i<n; i++)
26     {
27         x=h.top(),h.pop();
28         y=h.top(),h.pop();
29         ans+=x+y; h.push(x+y);
30     }
31     printf("%lld",ans);
32 }

注释:priority_queue<int,vector<int>,greater<int> > h;

  优先队列要定义三个量:储存地址,储存类型,比较方式如上所示,千万注意最后不能打成>> 而要是> >不然无法编译!

时间: 2024-10-06 19:51:53

BZOJ1724: [Usaco2006 Nov]Fence Repair 切割木板的相关文章

bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板(贪心+堆)

一开始被题目读错题= =以为每次只能割一块,那么就是从大到小切 但是其实是可以分为几堆来切的 所以可以逆着来,变为合并n个木板代价最小 易证每次找最小的两堆合并代价最小 用优先队列维护堆..偷偷懒= = 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 priority_queue<int,v

BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板

题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50,000).然后,FJ去买了一块很长的木板,它的长度正好等于所有需要的木板的长度和.接下来的工作,当然是把它锯成需要的长度.FJ忽略所有切割时的损失—

1724: [Usaco2006 Nov]Fence Repair 切割木板

1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 854  Solved: 426[Submit][Status] Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50,000).然后,FJ去买了一块很长的木板,它的长度正好等于所有需要的木板的

1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #define rep( i , n ) fo

POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 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)

poj3253 Fence Repair STL优先队列

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=3253 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

编程算法 - 篱笆修理(Fence Repair) 代码(C)

篱笆修理(Fence Repair) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把一块木板切成N块, 每次切两块, 切割的开销是木板长度, 求将木板切割完的最小开销. 即霍夫曼编码(Huffman). 贪心算法, 类似二叉树型结构, 最短板和次短板是兄弟结点, 选取两个最小木板, 最后进行切割, 合并两个最小木板, 依次递推. 代码: /* * main.cpp * * Created on: 2014.7.17 * Author:

POJ 3253 Fence Repair (贪心 + Huffman树)

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28155   Accepted: 9146 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)

Fence Repair STL——优先队列

题目描述: 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