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
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;
}

  

最新文章

  1. 搜索框(Thinkphp5.0)
  2. Talk about VR
  3. zabbix错误记录
  4. Spring与其他Web框架集成
  5. linux常见驱动修改
  6. iOS开发UI篇-实现tableView的层级显示
  7. c++,static 静态成员变量 / 静态成员函数
  8. Java 时钟
  9. windows7下django项目搭建
  10. Mego开发文档 - 处理并发冲突
  11. java中的反射整理
  12. .net EF之CodeFirst代码先行(转)
  13. 如何让Enum枚举实现异或操作
  14. thinkphp 3.2 去除调试模式后报错,怎么解决
  15. 神州数码OSPF路由汇总配置
  16. 微信小程序开发(1) 天气预报
  17. WinForm下窗体权限设计
  18. redis滴
  19. 数据存储 共享参数 SharedPreferences
  20. Javascript经典算法学习1:产生随机数组的辅助类

热门文章

  1. 201621123050 《Java程序设计》第8周学习总结
  2. 代码中输入数字自动筛选出最大值,使用array,for loop and if (21.9.2017)
  3. 理解Python迭代对象、迭代器、生成器
  4. Struts2之配置
  5. 韩顺平dedecms讲解上课记录
  6. typedef 使用
  7. sublimeText3 中配置sass环境,并将编译后文件保存到指定文件夹
  8. WPF 自定义ItemsControl/ListBox/ListView控件样式
  9. OAuth2.0学习(1-11)新浪开放平台微博认证-使用OAuth2.0调用微博的开放API
  10. jvascript变量提升