[POJ 3253] Fence Repair
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 3253] Fence Repair相关的知识,希望对你有一定的参考价值。
[题目链接]
http://poj.org/problem?id=3253
[算法]
首先, 进行了(n - 1)次切割后,原木板一定被切成了a1,a2,a3...an共n块
我们不妨考虑从终止状态到开始状态的最小代价,这与原问题是完全等价的,不难看出最后的答案就是哈夫曼最优编码
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; typedef long long ll; int i,n,x,y; ll ans; priority_queue< ll,vector<ll>,greater<ll> > q; namespace IO { template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == ‘-‘) f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar(‘-‘); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + ‘0‘); } template <typename T> inline void writeln(T x) { write(x); puts(""); } } ; int main() { IO :: read(n); for (i = 1; i <= n; i++) { IO :: read(x); q.push(x); } while (q.size() > 1) { x = q.top(); q.pop(); y = q.top(); q.pop(); ans += x + y; q.push(x + y); } IO :: writeln(ans); return 0; }
以上是关于[POJ 3253] Fence Repair的主要内容,如果未能解决你的问题,请参考以下文章