Heaps(Contest2080 - 湖南多校对抗赛(2015.05.10)(国防科大学校赛决赛-Semilive)+scu1616)
Problem H: Heaps
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 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
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <set> 8 #include <vector> 9 #include <math.h> 10 #include <bitset> 11 #include <algorithm> 12 using namespace std; 13 #define ls 2*i 14 #define rs 2*i+1 15 #define up(i,x,y) for(i=x;i<=y;i++) 16 #define down(i,x,y) for(i=x;i>=y;i--) 17 #define mem(a,x) memset(a,x,sizeof(a)) 18 #define w(a) while(a) 19 #define LL long long 20 const double pi = acos(-1.0); 21 #define N 1005 22 #define mod 19999997 23 #define INF 0x3f3f3f3f 24 #define exp 1e-8 25 26 LL dp[N][N],vec[50000],sum[N]; 27 int s[N],a[N],t,n,m,tot,vis[N][N]; 28 29 LL col(LL x) 30 { 31 LL ans = a[0]; 32 int i,j; 33 up(i,1,m) 34 { 35 LL tem = 1; 36 up(j,1,i) tem*=x; 37 ans+=tem*a[i]; 38 } 39 return ans; 40 } 41 42 int main() 43 { 44 int i,j,k; 45 scanf("%d",&t); 46 w(t--) 47 { 48 scanf("%d",&n); 49 mem(sum,0); 50 mem(dp,0); 51 tot=0; 52 up(i,1,n) 53 { 54 scanf("%d",&s[i]); 55 sum[i] = sum[i-1]+s[i]; 56 tot+=s[i]; 57 } 58 scanf("%d",&m); 59 up(i,0,m) 60 { 61 scanf("%d",&a[i]); 62 } 63 up(i,1,tot) 64 { 65 vec[i]=col((LL)i); 66 } 67 up(i,1,n) vis[i][i] = i; 68 vis[0][1] = 1; 69 int len; 70 up(len,2,n) 71 { 72 up(i,1,n-len+1) 73 { 74 j = i+len-1; 75 dp[i][j] = 1LL<<60; 76 up(k,vis[i][j-1],vis[i+1][j]) 77 { 78 LL tem = dp[i][k]+dp[k+1][j]+vec[sum[j]-sum[i-1]]; 79 if(tem<dp[i][j]) 80 { 81 dp[i][j] = tem; 82 vis[i][j] = k; 83 } 84 } 85 } 86 } 87 printf("%lld\n",dp[1][n]); 88 } 89 90 return 0; 91 } 92 93 /************************************************************** 94 Problem: 1616 95 User: aking2015 96 Language: C++ 97 Result: Accepted 98 Time:620 ms 99 Memory:13728 kb 100 ****************************************************************/