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,vector<int>,greater<int> > Q;
 7 int n,x,y;
 8 long long ans=0LL;
 9 int main(){
10     scanf("%d", &n);
11     for (int i=1; i<=n; i++) scanf("%d", &x),Q.push(x);
12     for (int i=1; i<n; i++){
13         x=Q.top(); Q.pop(); y=Q.top(); Q.pop();
14         ans+=(x+y);
15         Q.push(x+y);
16     }
17     printf("%lld
", ans);
18     return 0;
19 }
原文地址:https://www.cnblogs.com/mzl0707/p/5894144.html