日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Heaps(Contest2080

發(fā)布時(shí)間:2023/12/31 综合教程 37 生活家
生活随笔 收集整理的這篇文章主要介紹了 Heaps(Contest2080 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Problem H: Heaps

Time Limit:2 SecMemory Limit:128 MB
Submit:48Solved: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

轉(zhuǎn)載請(qǐng)注明出處: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
",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 ****************************************************************/

總結(jié)

以上是生活随笔為你收集整理的Heaps(Contest2080的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。