『poj 3253』 fence repair Solution

题目大意:

给一块长木板,现要将其锯成$n$段,共需锯$n-1$次,每次锯的代价为所锯木板的长度,求最小总代价。

Solution

弄个优先队列完事。。(STL大法好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int N;
int ()
{
long long ans = 0;
ios::sync_with_stdio(false);
cin >> N;
for (register int i = 1; i <= N; ++i)
{
int a;
cin >> a;
q.push(a);
}
while (q.size() >= 2)
{
int l1, l2;
l1 = q.top(), q.pop();
l2 = q.top(), q.pop();
ans += l1 + l2;
q.push(l1 + l2);
}
cout << ans << endl;
return 0;
}