HDU 5350(MZL's munhaff function-哈夫曼树)
来源:互联网 发布:查看软件版本 编辑:程序博客网 时间:2024/06/06 01:26
MZL's munhaff function
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 230 Accepted Submission(s): 133
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, givenn positive integers Ai and Ai≥Ai+1 .
Then, generaten positive integers Bi
Bi=∑j=inAj
Definef(i,j) for i,j∈Z
f(i,j)=⎧⎩⎨⎪⎪⎪⎪⎪⎪0min(f(i−1,j+1),f(i,⌈j2⌉)+Bi)1011037(i,j)=(1,1)i,j∈[1,n], (i,j)≠(1,1)otherwise
Findf(n,1) .
Stilwell is very confused about this function, and he need your help.
First of all, given
Then, generate
Define
Find
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 integern , and the next line contains n positive integers Ai .
T≤100 ,1≤n≤105 ,∑n≤106 ,1≤Ai≤104 .
For each test case, the first line contains a positive integer
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
523311037Hintcase 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
Author
SXYZ
Source
2015 Multi-University Training Contest 5
Recommend
wange2014 | We have carefully selected several similar problems for you: 5421 5420 5419 5418 5417
哈夫曼树
解决如下问题:有一堆数,每次拿2个数,换成它们的和,代价是它们的和,求把数变为1个的最小花费
显然每次取当前最小(构建哈夫曼树)
考虑本题,f[i][j] 表示拿了 前Ai个数,当前最后那层有j个空的叶节点
我们可以拿一个数去填空点,不需要代价f[i+1][j-1]
也可以把叶子节点上有数往下画一层 f[i][2j] 代价就是上面所有取的数 Bi
#include<bits/stdc++.h> using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (1000000+10)#define MAXAi (10000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int n;ll a[MAXN];priority_queue<ll,vector<ll> , greater<ll> > q;int main(){//freopen("H.in","r",stdin);int T;cin>>T;while (T--) {cin>>n;For(i,n) {scanf("%lld",&a[i]);q.push(a[i]);}////if (n==1) {//puts("0");continue;//}ll ans=0;while (q.size()>=2){ll val=q.top(); q.pop();val+=q.top(); q.pop();ans+=val;q.push(val);}cout<<ans<<endl;while (!q.empty()) q.pop();}return 0;}
1 0
- HDU 5350(MZL's munhaff function-哈夫曼树)
- HDU 5350 MZL's munhaff function 题解
- Hdu 5350 MZL's munhaff function 2015ACM多校对抗赛第五场
- HDU 5344 MZL's xor
- HDU 5347 MZL's chemistry
- HDU 5344 MZL's xorti
- HDU 5344 MZL's xor
- HDU 5347 MZL's chemistry
- HDU 5351 MZL's Border
- hdu 5344 MZL's xor
- hdu 5347 MZL's chemistry
- HDU 5351 MZL's Border
- hdu 5344 MZL's xor
- HDU 5352 MZL's City
- HDU 5347 MZL's chemistry
- HDU 5351 MZL's Border
- hdu 5334 MZL's xor
- HDU 5344 MZL's xor
- [笔记][Java7并发编程实战手册]3.7 并发阶段任务中的阶段切换phaser
- Linux内核移植和根文件系统制作(一)
- HDU 4407 Sum(容斥原理)
- 记录我的安卓开发之旅——安卓开发笔记一——xml实现圆角布局
- 九度oj 1135
- HDU 5350(MZL's munhaff function-哈夫曼树)
- Deep Learning及NLP(自然语言处理)杂谈--第一部分
- 华为数字芯片工程师实习生面试全过程
- 【SOJ1136】【cogs775】山海经
- linux内核移植和根文件系统制作(二)
- Mysql与Oracle区别
- 【分享】もっと 姉、ちゃんとしようよっ!+アフターストーリー 【日文硬盘版】(带全CG存档&日文攻略
- Linux下文本常用处理技巧
- linux内核移植和根文件系统制作(三)