2015 多校联赛 ——HDU5350(huffman)

 

Problem Description
MZL is a mysterious mathematician, and he proposed a mysterious function at his young age.
Stilwell is very confused about this function, and he need your help.
First of all, given  n positive integers  Ai and  AiAi+1.
Then, generate  n positive integers  Bi
Bi=j=inAj

Define  f(i,j) for  i,jZ
f(i,j)=⎧⎩⎨⎪⎪⎪⎪⎪⎪0min(f(i1,j+1),f(i,j2)+Bi)1011037(i,j)=(1,1)i,j[1,n], (i,j)(1,1)otherwise

Find  f(n,1).
 

 

Input
The first line of the input contains a single number  T, the number of test cases.
For each test case, the first line contains a positive integer  n, and the next line contains  n positive integers  Ai.
T1001n105n1061Ai104.
 

 

Output
For each test case, output  f(n,1) in a line.
 

 

Sample Input
3 3 1 1 1 5 28 26 25 24 1 10 996 901 413 331 259 241 226 209 139 49
 

 

Sample Output
5 233 11037

 

 




//完全没看出来是个huffman - -!


#include <iostream>
#include <cstdio>
#include<algorithm>
#include<cstring>
#include<functional>
#include<queue>
typedef long long ll;
using namespace std;


int main()
{
    int T;
    int n,a;
    priority_queue<ll, vector<ll>, greater<ll> >q;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        while(!q.empty())
        {
            q.pop();
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a);
            q.push(a);
        }
        ll sum = 0;
        while(!q.empty())
        {
            ll x = q.top();
            q.pop();
            if(q.empty())
            {
                continue;
            }
            ll y = q.top();
            q.pop();
            ll tmp= x + y;
            sum+=tmp;
            q.push(tmp);
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

  

 

转载于:https://www.cnblogs.com/Przz/p/5409800.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值