• [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;
        
    }
  • 相关阅读:
    高阶篇:1.2)材料和工艺的选择
    高阶篇:4.1.1)QFDI(客户需求转换为设计要求)
    高阶篇:4)可靠性设计-总章
    高阶篇:1.5)如何选择更好的概念-Pugh矩阵法
    知识点篇:2)产品结构设计目标的分类
    高阶篇:1.1)竞品(标杆产品)的拆解和分析benchmarking
    支持向量机
    机器学习概述
    HDU_oj_2055 An easy problem
    HDU_oj_2054 A==B ?
  • 原文地址:https://www.cnblogs.com/evenbao/p/9451981.html
Copyright © 2020-2023  润新知