码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ 3253] Fence Repair

时间:2018-08-09 23:05:46      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:man   http   lib   stack   ack   tar   fstream   repair   enc   

[题目链接]

           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

标签:man   http   lib   stack   ack   tar   fstream   repair   enc   

原文地址:https://www.cnblogs.com/evenbao/p/9451981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!