Description

There is an old stone game.At the beginning of the game the player picks n(1<=n<=50000) piles of stones in a line. The goal is to merge the stones in one pile observing the following rules: 
At each step of the game,the player can merge two adjoining piles to a new pile.The score is the number of stones in the new pile. 
You are to write a program to determine the minimum of the total score. 

Input

The input contains several test cases. The first line of each test case contains an integer n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game. 
The last test case is followed by one zero. 

Output

For each test case output the answer on a single line.You may assume the answer will not exceed 1000000000.

Sample Input

  1. 1
  2. 100
  3. 3
  4. 3 4 3
  5. 4
  6. 1 1 1 1
  7. 0

Sample Output

  1. 0
  2. 17
  3. 8
    解题思路:GarsiaWachs算法,时间复杂度为O(n^2)。它的算法步骤如下:设序列是stone[1n],从左往右找一个满足stone[k-1]<=stone[k+1]的k,然后合并stone[k-1]和stone[k]为tmp,再从位置k-1向左找一个最大的j,使其满足stone[j]>tmp,并将tmp插到j的后面。一直重复,直到将所有石子合并。在这个过程中,可以假设stone[0]和stone[n+1]是+∞的。
    举个例子:186 64 35 32 103
  4. 35<103,∴第一次满足条件的k下标(下标从0开始计算)为3,我们先把3532删除,得到它们的和67,并向前寻找一个第一个大于67的数,把67插入到它后面,得到:186 67 64 103,现在由5个数变为4个数了,继续同样的操作:186 131 103,则k=2(别忘了,设stone[0]和stone[n+1]等于+∞)此时的序列为234 186,最后一次合并便得到420。最终的答案呢?就是各次合并的代价之和,即420+234+131+67=852
    基本思想是通过树的最优性得到一个节点间深度的约束,之后证明操作一次之后的解可以和原来的解一一对应,并保证节点移动之后它所在的深度不会改变。具体实现这个算法需要一点技巧,精髓在于不停快速寻找最小的k,即维护一个“2-递减序列”朴素的实现的时间复杂度是O(n*n),但可以用一个平衡树来优化,使得最终复杂度为O(nlogn)。
    (转)补证:问题分析:(1)、假设我们只对3堆石子a,b,c进行比较, 先合并哪2堆, 使得代价总和最小。
    score1=(a+b)+((a+b)+c),score2=(b+c)+((b+c)+a),当score1<=score2时,化简得a<=c,因此可得出只要ac的关系确定,合并的顺序也就确定了。
    (2)、GarsiaWachs算法, 就是基于(1)的结论实现的。找出序列中满足stone[k-1]<=stone[k+1]最小的k, 合并stone[k-1]+stone[k]为tmp, 接着往前面找满足条件stone[j]>tmp, tmp值插入stone[j]的后面(数组的右边). 循环这个过程一直到只剩下一堆石子结束。
    (3)、为什么要将tmp插入stone[j]后面, 可以理解为(1)的情况,从stone[j+1]到stone[k-2]看成一个整体stone[mid],那么对于stone[j],stone[mid], tmp,必有tmp<stone[j],∴不管怎样都是stone[mid]和tmp先合并, 即将tmp值插入stone[j]的后面是不影响结果的。
    AC代码(141ms):
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. using namespace std;
  5. const int maxn=;
  6. const int inf=0x7fffffff;//2147483647
  7. int n,m,t,ans,stone[maxn];
  8. void dfs(int k){
  9. int tmp=stone[k-]+stone[k];
  10. ans+=tmp;t--;
  11. for(int i=k;i<t;++i)stone[i]=stone[i+];//元素左移,表示删掉了一个元素
  12. int j=;k--;
  13. for(j=k;stone[j-]<tmp;--j)stone[j]=stone[j-];//元素右移,找到第一个满足条件的j
  14. stone[j]=tmp;//将tmp插到j后面
  15. while(j>=3&&stone[j-]<=stone[j]){//继续向前查找是否还有满足条件的情况
  16. int d=t-j;//保存当前t离操作点的距离d
  17. dfs(j-);//合并第j-1堆和第j-2堆石子
  18. j=t-d;//设置新的操作点j
  19. }
  20. }
  21. int main(){
  22. while(~scanf("%d",&n)&&n){
  23. for(int i=;i<=n;++i)scanf("%d",&stone[i]);
  24. t=,ans=;stone[]=stone[n+]=inf;
    for(int i=;i<=n;++i){
  25. stone[t++]=stone[i];
  26. while(t>&&stone[t-]<=stone[t-])dfs(t-);//表示当前至少有3堆石子,并且满足stone[k-1]<=stone[k+1],k=t-2,就合并第t-3和第t-2堆石子
  27. }
  28. while(t>)dfs(t-);//如果剩下的堆数至少为3-1=2堆,则继续合并,直至剩下一堆石子
  29. printf("%d\n",ans);
  30. }
  31. return ;
  32. }
  1.  

题解报告:poj 1738 An old Stone Game(区间dp)的更多相关文章

  1. POJ 1651:Multiplication Puzzle(区间DP)

    http://poj.org/problem?id=1651 题意:给出n个数字,每取中间一个数,就会使得权值加上中间这个数和两边的乘积,求取剩两个数最少的权值是多少. 思路:区间dp. 一开始想了挺 ...

  2. [08山东省选]2298 石子合并 即POJ 1738 An old Stone Game

    2298 石子合并 2008年省队选拔赛山东  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在 ...

  3. POJ 1738 An old Stone Game(石子合并 经典)

    An old Stone Game Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 3672   Accepted: 1035 ...

  4. 【题解】POJ 3417 Network(倍增求LCA+DP+树上差分)

    POJ3417:http://poj.org/problem?id=3417 思路 我们注意到由“主要边”构成一颗树 “附加边”则是非树边 把一条附加边(x,y)加入树中 会与树上x,y之间构成一个环 ...

  5. poj 1694 An Old Stone Game 树形dp

    //poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...

  6. POJ 2671 Jimmy's Bad Day题解(很详细很友好,类似区间dp)

    有问题的话欢迎在评论区提出 题意: 题目链接 你是一个送快递的,现在给你一个环,环的边有权值,代表走这条边所花的时间,每个点代表一个地点,点有点权,代表这个点上有多少货物需要你送.初始时间\(t=0\ ...

  7. 题解报告:hdu 1520 Anniversary party(树形dp入门)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

  8. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  9. POJ 题目3280 Cheapest Palindrome(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7148   Accepted: 34 ...

随机推荐

  1. GDI泄露+改EXE名

    CDC 应该是成对使用 GetDC and ReleaseDC(不用new and delete) 泄露 1.改变生产exe名称:工程->设置->连接->输出文件名:Release/ ...

  2. Mac OS用minikube安装单节点kubernetes

    参考 https://kubernetes.io/docs/tasks/tools/install-minikube/ https://github.com/linianhui/code/blob/m ...

  3. split+ Pattern切割字符串

    今天在对一个String对象进行拆分的时候,总是无法到达预计的结果.呈现数据的时候出现异常,后来debug之后才发现,错误出在String spilt上,于是开始好好研究下这东西,开始对api里的sp ...

  4. 备忘录模式-Memento

    备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 备忘录模式结构图: 何时使用备忘录模式: Memento模式比适合 ...

  5. Java程序员从笨鸟到菜鸟之(十五)Html基础积累总结(下)

     本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 一:表格 1.表格的基本语法 <table>...</table> ...

  6. Mac开发必备工具(三)—— Fish shell

    Fish shell 简介 fish 可以根据输入自动匹配历史命令.它的一大特点是开箱即用,没有zsh那些繁琐的配置.官网:http://www.fishshell.com/. 安装与配置 在终端里使 ...

  7. YTU 2419: C语言习题 等长字符串排序

    2419: C语言习题 等长字符串排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 650  解决: 249 题目描述 在主函数中输入n(n<=10)个等长的字符串.用另一函数对 ...

  8. CGAffineTransform属性

    transform我们一般称为形变属性,其本质是通过矩阵变化改变控件的大小.位置.角度等,这里我们通过一个例子来看一下具体的操作,在下面的例子中我们也会看到UIImageView控件的常用操作. - ...

  9. [Selenium] 如何使用Chrome Options 定制测试Chrome 浏览器的特定属性 (类似FirefoxProfiles)

    Chrome Options 类似于FirefoxProfiles,用于定制待测试的Chrome 浏览器的特定属性 1)如果希望测试某个浏览器插件,可通过addExtensions方式提前加载以.cr ...

  10. [Selenium] 使用自定义的FirefoxProfile

    FirefoxProfile 用于定制待测试的Firefox 浏览器的特定属性,其中包括所存储的密码.书签.历史信息.Cookies等.某些测试用例需要用到特定的用户信息,因此可通过定制当前Firef ...