zoukankan      html  css  js  c++  java
  • [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;
        
    }
  • 相关阅读:
    Min25 筛与 Powerful Numbers
    「CF576D」 Flights for Regular Customers
    「CF568C」 New Language
    「CF559E」 Gerald and Path
    「CF555E」 Case of Computer Network
    20210604
    20210603模拟赛总结
    20210602模拟赛总结
    CF603E 整体二分
    20210601模拟赛总结
  • 原文地址:https://www.cnblogs.com/evenbao/p/9451981.html
Copyright © 2011-2022 走看看