BZOJ 1724 [Usaco2006 Nov]Fence Repair 切割木板:贪心 优先队列【合并果子】

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1724

题意:

  你要将一块长木板切成n段,长度分别为a[i](长木板的长度 = ∑ a[i])。

  每一次切割的花费为被切割木板的长度。

  问你切完的最小花费。

 

题解:

  合并果子。

  反过来想:切割 = 合并

  贪心策略:每次选目前所有堆中最小的两个合并。(尽可能少移动大的果子堆)

  实现:优先队列

 

AC Code:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 int n;
 9 long long ans=0;
10 priority_queue<int,vector<int>,greater<int> > q;
11 
12 int get_top()
13 {
14     int now=q.top();
15     q.pop();
16     return now;
17 }
18 
19 int main()
20 {
21     cin>>n;
22     int a;
23     for(int i=0;i<n;i++)
24     {
25         cin>>a;
26         q.push(a);
27     }
28     while(q.size()>1)
29     {
30         int v1=get_top();
31         int v2=get_top();
32         ans+=v1+v2;
33         q.push(v1+v2);
34     }
35     cout<<ans<<endl;
36 }

 

posted @ 2017-09-24 23:14  Leohh  阅读(220)  评论(0编辑  收藏  举报