zoukankan      html  css  js  c++  java
  • poj 3253 Fence Repair

    题目连接

    http://poj.org/problem?id=3253  

    Fence Repair

    Description

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the Nplanks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

    FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

    Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

    Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

    Input

    Line 1: One integer N, the number of planks 
    Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

    Output

    Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

    Sample Input

    3
    8
    5
    8

    Sample Output

    34

    哈弗曼编码,注意结果爆int要用long long。。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<queue>
    #include<set>
    using std::set;
    using std::sort;
    using std::pair;
    using std::swap;
    using std::string;
    using std::priority_queue;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) decltype((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int N = 20010;
    const int INF = ~0u>>1;
    typedef long long ll;
    struct Node {
    	ll dat, w;
    	Node *ch[2];
    	Node() {}
    	Node(ll _dat_, ll _w_, Node *l = NULL, Node *r = NULL) {
    		dat = _dat_, w = _w_;
    		ch[0] = l, ch[1] = r;
    	}
    	Node(const Node &x) {
    		dat = x.dat, w = x.w;
    		ch[0] = x.ch[0], ch[1] = x.ch[1];
    	}
    	inline bool operator<(const Node &x) const {
    		return w > x.w;
    	}
    }A[N];
    struct Hoffmancode {
    	ll sum;
    	Node *root;
    	priority_queue<Node> q;
    	inline void solve(int n) {
    		ll v;
    		sum = 0;
    		rep(i, n) {
    			scanf("%lld", &v);
    			A[i] = Node(v, v);
    			q.push(A[i]);
    		}
    		CreateHoffmanTree();
    		CreateHoffmanCode(root, "");
    		printf("%lld
    ", sum);
    	}
    	inline void CreateHoffmanTree() {
    		Node *l = NULL, *r = NULL;
    		while (!q.empty() && q.size() != 1) {
    			l = new Node(q.top()); q.pop();
    			r = new Node(q.top()); q.pop();
    			q.push(Node(INF, l->w + r->w, l, r));
    		}
    		if (1 == q.size()) {
    			root = new Node(q.top()); q.pop();
    		}
    	}
    	inline void CreateHoffmanCode(Node *x, string s) {
    		if (!x) return;
    		if (x->dat != INF) {
    			sum += x->dat * s.length();
    		}
    		CreateHoffmanCode(x->ch[0], s + '0');
    		CreateHoffmanCode(x->ch[1], s + '1');
    	}
    }work;
    int main() {
    #ifdef LOCAL
    	freopen("in.txt", "r", stdin);
    	freopen("out.txt", "w+", stdout);
    #endif
    	int n;
    	while (~scanf("%d", &n)) {
    		work.solve(n);
    	}
    	return 0;
    }
  • 相关阅读:
    高效管理,经理人须熟练运用的几个工具
    投资感情 收获人心
    忽如一夜入冬来
    贺嫦娥奔月
    正确处理人际关系,给自己做无形的投资
    观南溪豆干有感
    身在职场,请善待你的每一张白纸
    游一品天下有感
    增强影响力,如何提升你的“领袖气质”?
    oracle 创建表空间
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4876096.html
Copyright © 2011-2022 走看看