CSU 1616: Heaps(区间DP)

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

1616: 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

题目大意:就是原始的石子合并的问题,相同的部分就不多介绍了,不同的便是在合并石子时,所消耗的费用不是两堆石子的总数,而是把总数代入公式:P(x)= (a0+a1*x+a2*x2+…+am*xm),同时题目也给出了a0--am的数值;

解题思路:解法就是普通的的区间DP算法,但是在做的时候老是超时,最后还是在学长的指导下,明白在第三重循环是可以不全循环,而是从上一次的两个dp中的断点之间找,并且预处理出从0到石子最大值数的带入公式的结果。唉,自己果然还是太水,改了这么久。。。。。,,看看我的备注就懂了。。心酸啊T^T
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5
 6 using namespace std;
 7
 8 #define ll long long
 9
10 //const long long MAX=0xfffffffffffffff;
11
12 ll num[1010],nmul[40050];   //nmul:储存预处理的结果
13 ll dp[1010][1010],mm[10];
14 ll snum[1010];      //记录各数字的和
15 int kk[1010][1010]; //记录上一层次的断点
16
17 ll Pow(ll a,int k)
18 {
19     ll s=1;
20     for(int i=1; i<=k; i++)
21         s*=a;
22     return s;
23 }
24
25 void Mul(int n,int m)
26 {
27     for(int i=0;i<=snum[n-1];i++)
28     {
29         nmul[i]=0;
30         for(int j=0;j<=m;j++)
31             nmul[i]+=mm[j]*Pow(i,j);
32     }
33 }
34
35 int main()
36 {
37     int t,n,m;
38     /*for(int i=0; i<=40005; i++)
39     {
40         for(int j=0; j<=4; j++)
41             dd[i][j]=Pow(i,j);
42     }*/
43     scanf("%d",&t);
44     while(t--)
45     {
46         //snum[0]=0;
47         //memset(dp,0,sizeof(dp));
48         scanf("%d",&n);
49         for(int i=0; i<n; i++)
50         {
51             scanf("%lld",&num[i]);
52             if(i==0)
53                 snum[i]=num[i];
54             else
55                 snum[i]=snum[i-1]+num[i];
56         }
57         scanf("%d",&m);
58         for(int i=0; i<=m; i++)
59             scanf("%lld",&mm[i]);
60         Mul(n,m);
61         for(int i=0; i<n; i++)
62             dp[i][i]=0,kk[i][i]=i;
63         for(int l=2; l<=n; l++)
64         {
65             for(int s=0; s<n-l+1; s++)
66             {
67                 int e=s+l-1;
68                 ll ss=1e63;             //一定要定义成最大值
69                 for(int k=kk[s][e-1]; k<=kk[s+1][e]; k++)   //从两个断点之间找
70                 {
71                     if(ss>dp[s][k]+dp[k+1][e])
72                     {
73                         ss=dp[s][k]+dp[k+1][e];
74                         kk[s][e]=k;
75                     }
76                     //ss=dp[s][k]+dp[k+1][e]>ss?ss:dp[s][k]+dp[k+1][e];
77                 }
78                 /*ll sum=0,sss=0;
79                 for(int k=s; k<=e; k++)
80                     sum+=num[k];
81                 for(int k=0; k<=m; k++)
82                     sss+=mm[k]*Pow(sum,k);*/
83                 dp[s][e]=ss+nmul[snum[e]-snum[s-1]];;
84                 //printf("s=%d,e=%d,dp=%lld\n",s,e,dp[s][e]);
85             }
86         }
87         printf("%lld\n",dp[0][n-1]);
88     }
89     return 0;
90 }
时间: 2024-10-25 21:38:34

CSU 1616: Heaps(区间DP)的相关文章

CSU 1616: Heaps(区间DP)优化版

1616: Heaps Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 31  Solved: 5 [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 bore

CSU 1620: A Cure for the Common Code (区间DP KMP预处理)

链接 : http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1620 题意 : 给一个字符串 问怎么压缩字符串使得最终个数最小 具体怎么压缩请参照图示 很好明白. 题目就是需要找到 对于每个后缀 看成一个新字符串 找出它的前缀的最小循环节. 过程和大白书 P213页 是一样的,只需要对每个后缀跑一遍KMP求出周期. 剩下的过程就是区间DP了. dp[i][j] = min ( dp[i][k], dp[k+1][j] ); 如果i - j这一段存在

CSU 1620: A Cure for the Common Code(KMP+区间DP)

Description Input Output Sample Input abcbcbcbca abbbcdcdcdabbbcdcdcd 0 Sample Output Case 1: 7 Case 2: 11 HINT 题意:给一个小写子母的串,相邻相同的子串可以合在一起,问这个串合并后最短可得多长. 解题:KMP & 区间DP #include<stdio.h> #include<string.h> const int N = 505; int next1[N]; c

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就

合并石子 区间dp水题

合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值. tags:最基本的区间dp,这题范围小,如果n大一些,还是要加个平行四边行优化. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g

Luogu P2734 游戏 A Game 区间DP

P2734 游戏 A Game 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜. 题目描述 编一个执行最优策略的程序,最优策略就是使玩家在与最好的对手对弈时,能得到的在当前情况下最大的可能的总分的策略.你的程序要始终为第二位玩家执行最优策略. 输入输出格式 输入格式: 第一行: 正整数N, 表示序列中正整数的个数

HDU-4283 You Are the One (区间DP)

Problem Description The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there ar