Hdu 5350 MZL's munhaff function 2015ACM多校对抗赛第五场

来源:互联网 发布:销售单软件 编辑:程序博客网 时间:2024/05/16 17:32

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=5350

MZL's munhaff function

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 156    Accepted Submission(s): 101


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
331 1 1528 26 25 24 110996 901 413 331 259 241 226 209 139 49
 

Sample Output
523311037
Hint
case 1 :f(1,1)=0f(1,2)=f(1,1)+3=3f(1,3)=f(1,2)+3=6f(2,1)=min(f(2,1)+2,f(1,2))=3f(2,2)=min(f(2,1)+2,f(1,3))=5f(2,3)=f(2,2)+2=7f(3,1)=min(f(3,1)+1,f(2,2))=5
 

Source
2015 Multi-University Training Contest 5
 
神奇的Huffman树。
Ai是Huffman的叶子节点,将A降序排列
dp[i][j]表示放置Ai,还剩j个叶子节点,Huffman的取值。
转移有两种情况:
放在叶子节点处,dp[i+1][j-1],叶子节点还剩j-1个,Ai已经放置好了,考虑Ai+1。
放在非叶子节点处,即下一层,dp[i][j*2],还要考虑次Ai,但是叶子节点扩展两倍了。
和题目给的递推正好互逆。
考虑Bi的值,发现,最后答案f[n][1]就是Huffman树的权值和 减去 叶子节点权值和。

#include<iostream>#include<cstdio>#include<queue>#include<algorithm>using namespace std;#define LL long longstruct node{    LL val;    node(LL v=0):val(v){}    bool operator < (const node &a)const{        return val>a.val;    }};priority_queue <node> que;int main(){    LL T,n;scanf("%lld",&T);    while(T--){        scanf("%lld",&n);        while(!que.empty())que.pop();        for(int i=0;i<n;i++){            LL val;scanf("%lld",&val);            que.push(node(val));        }        if(n==1){puts("0");continue;}        LL ans=0;        while(que.size()>=2){            LL val=que.top().val;que.pop();              val+=que.top().val;que.pop();            ans+=val;            que.push(node(val));        }        printf("%lld\n",ans);    }    return 0;}




0 0