Problem H: Heaps

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 48  Solved: 9
[Submit][Status][Web Board]

Description

Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.

Zuosige knows there is a typical problem called Merging Stones. In the problem, you have N heaps of stones and you are going to merging them into one heap. The only restriction is that you can only merging adjacent heaps and the cost of a merging operation is the total number of stones in the two heaps merged. Finally, you are asked to answer the minimum cost to accomplish the merging.

However, Zuosige think this problem is too simple, so he changes it. In his problem, the cost of a merging is a polynomial function of the total number of stones in those two heaps and you are asked to answer the minimum cost.

Input

The first line contains one integer T, indicating the number of test cases.
In one test case, there are several lines.
In the first line, there are an integer N (1<=N<=1000).
In the second line, there are N integers. The i-th integer si (1<=si<=40) indicating the number of stones in the i-th heap.
In the third line, there are an integer m (1<=m<=4).
In the forth line, there are m+1 integers a0, … , am. The polynomial function is P(x)= (a0+a1*x+a2*x2+…+am*xm). (1<=ai<=5)

Output

For each test case, output an integer indicating the answer.

Sample Input

1
5
3 1 8 9 9
2
2 1 2

Sample Output

2840

HINT

转载请注明出处:http://www.cnblogs.com/yuyixingkong/

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define N 1005
#define mod 19999997
#define INF 0x3f3f3f3f
#define exp 1e-8 LL dp[N][N],vec[],sum[N];
int s[N],a[N],t,n,m,tot,vis[N][N]; LL col(LL x)
{
LL ans = a[];
int i,j;
up(i,,m)
{
LL tem = ;
up(j,,i) tem*=x;
ans+=tem*a[i];
}
return ans;
} int main()
{
int i,j,k;
scanf("%d",&t);
w(t--)
{
scanf("%d",&n);
mem(sum,);
mem(dp,);
tot=;
up(i,,n)
{
scanf("%d",&s[i]);
sum[i] = sum[i-]+s[i];
tot+=s[i];
}
scanf("%d",&m);
up(i,,m)
{
scanf("%d",&a[i]);
}
up(i,,tot)
{
vec[i]=col((LL)i);
}
up(i,,n) vis[i][i] = i;
vis[][] = ;
int len;
up(len,,n)
{
up(i,,n-len+)
{
j = i+len-;
dp[i][j] = 1LL<<;
up(k,vis[i][j-],vis[i+][j])
{
LL tem = dp[i][k]+dp[k+][j]+vec[sum[j]-sum[i-]];
if(tem<dp[i][j])
{
dp[i][j] = tem;
vis[i][j] = k;
}
}
}
}
printf("%lld\n",dp[][n]);
} return ;
} /**************************************************************
Problem: 1616
User: aking2015
Language: C++
Result: Accepted
Time:620 ms
Memory:13728 kb
****************************************************************/

Heaps(Contest2080 - 湖南多校对抗赛(2015.05.10)(国防科大学校赛决赛-Semilive)+scu1616)的更多相关文章

  1. Contest2073 - 湖南多校对抗赛(2015.04.06)

    Contest2073 - 湖南多校对抗赛(2015.04.06) Problem A: (More) Multiplication Time Limit: 1 Sec  Memory Limit:  ...

  2. Contest2071 - 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.csu.edu.cn/OnlineJudge/contest.php?c ...

  3. 湖南多校对抗赛(2015.05.03)Problem A: Twenty-four point

    给四个数 问能不能算出24点...我的方法比较烂...920ms 差点TLE.应该有更好的方法. #include<stdio.h> #include<string.h> #i ...

  4. 湖南多校对抗赛(2015.05.03)Problem B: War

    并查集.从后往前加边. #include<stdio.h> #include<string.h> #include<math.h> #include<algo ...

  5. Contest2089 - 湖南多校对抗赛(2015.05.31) Swipe(csu1648)

    Problem E: Swipe Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 100  Solved: 15[Submit][Status][Web ...

  6. Aquarium Tank(csu1634+几何+二分)Contest2087 - 湖南多校对抗赛(2015.05.24)-G

    Aquarium Tank Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 15  Solved: 4[Submit][Status][Web Board ...

  7. Clock Pictures(kmp + Contest2075 - 湖南多校对抗赛(2015.04.26))

    Problem H: Clock Pictures Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 73  Solved: 18[Submit][Stat ...

  8. CSU 2136 ——湖南多校对抗赛 I

    2136: 统帅三军! Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 55     ...

  9. 2015.05.15,外语,学习笔记-《Word Power Made Easy》 01 “如何讨论人格特点”

    2015.03.17,外语,读书笔记-<Word Power Made Easy> 01 “如何讨论人格特点”学习笔记 SESSIONS 1 本来这些章节都是在一两年前学习的,现在趁给友人 ...

随机推荐

  1. 微信机器人 index

    <?phprequire_once ('src/wechat.php');$wechat = new wechat();$act = isset($_GET['act'])?$_GET['act ...

  2. 《响应式Web设计实践》学习笔记

    原书: 响应式Web设计实践 目录: 第2章 流动布局 1. 布局选项 2. 字体大小 3. 网格布局 4. 混合固定宽度和流动宽度 第3章 媒介查询 1. 视口 2. 媒介查询结构 3. 内嵌样式与 ...

  3. 知乎十万级容器规模的Java分布式镜像仓库实践

    作者:知乎令孤无忌 前言 知乎在 2016 年已经完成了全量业务的容器化,并在自研容器平台上以原生镜像的方式部署和运行,并在后续陆续实施了 CI.Cron.Kafka.HAProxy.HBase.Tw ...

  4. python异常报错详解

    异常BaseException所有内置异常的基类.它不是直接由用户定义的类继承(为此使用Exception).如果 str()或者unicode()是在这个类的一个实例上被调用,返回实例的参数表示形式 ...

  5. GIT学习笔记——常用命令

    最近使用使用GIT较多,但命令很容易就忘记了,于是整理下,大多整理与一些文档和他人博客 在当前目录新建建一个纯git代码库 $ git --bare init 在当前目录新建一个Git代码库 $ gi ...

  6. MySQL-5.7.19 在阿里云 CentOS-7.0 上的安装

    工具准备 查看系统是否安装了yum工具: [root@wangbo srv]# rpm -qa | grep yum yum-metadata-parser-1.1.4-10.el7.x86_64 y ...

  7. Eclipse 修改自动补全触发器只能输入四个字符为多个字符

    如果eclipse中的[auto activation triggers for java]自动补全触发器这个位置的文本框有时候只能输入4个字符, 可以通过修改配置文件的方法实现,具体操作步骤如下: ...

  8. Log4J从基础到应用

    1.API中核心的三个接口(org.apache.log4j) Class Logger This is the central class in the log4j package. Most lo ...

  9. 神经网络(BP)算法Python实现及简单应用

    首先用Python实现简单地神经网络算法: import numpy as np # 定义tanh函数 def tanh(x): return np.tanh(x) # tanh函数的导数 def t ...

  10. netty入门(一)

    1. netty入门(一) 1.1. 传统socket编程 在任何时候都可能有大量的线程处于休眠状态,只是等待输入或者输出数据就绪,这可能算是一种资源浪费. 需要为每个线程的调用栈都分配内存,其默认值 ...