2015 多校联赛 ——HDU5350(huffman)
2015-08-04 22:33
211 查看
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 Ai≥Ai+1.
Then, generate n positive
integers Bi
Bi=∑j=inAj
Define f(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
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.
T≤100, 1≤n≤105, ∑n≤106, 1≤Ai≤104.
Output
For each test case, output f(n,1) in
a line.
Sample Input
Sample Output
//完全没看出来是个huffman - -!
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 Ai≥Ai+1.
Then, generate n positive
integers Bi
Bi=∑j=inAj
Define f(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
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.
T≤100, 1≤n≤105, ∑n≤106, 1≤Ai≤104.
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; }
相关文章推荐
- 贝塞尔曲线的应用
- 超图的地图慧
- 分布式与集群的区别通俗解释
- 什么是逻辑分区管理 LVM ,如何在Ubuntu中使用?
- 什么是逻辑分区管理 LVM ,如何在Ubuntu中使用?
- swagger整合spring mvc教程
- 输入一个字符串,如果第一个字符是大写并且其他字符不是大写,那么输出true,否则输出false。
- 服务器数据库挂掉 Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ";
- vsftpd的工作原理和配置
- 【更博冒泡一下.】
- hdu 5344 MZL's xor (2015多校第五场第2题) 简单化简
- hdoj 1016 素数圆环问题(深度优先搜索z和道题 )
- php判断是否为一维数组--count的妙用
- 模积和
- poj 1830 开关问题高斯消元
- UVA 11478 Halum
- First Missing Positive
- Docker容器的跨主机访问
- Git 版本控制使用
- POJ 1160:Post Office 邮局经典DP