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

来源:互联网 发布:淘宝二手书出售流程 编辑:程序博客网 时间:2024/04/28 03:44
传送门: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 }
View Code

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

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

0 0