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
100
3
3 4 3
4
1 1 1 1
0

Sample Output

0
17
8
 #include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#define MAX 55555
using namespace std;
int a[MAX],n,num,result;
void combine(int k)
{
int i,j;
int temp=a[k]+a[k-];
result+=temp;
for(i=k;i<num-;i++)
a[i]=a[i+];
num--;
for(j=k-;j>&&a[j-]<temp;j--)
a[j]=a[j-];
a[j]=temp;
while(j>=&&a[j]>=a[j-])
{
int d=num-j;
combine(j-);
j=num-d;
}
}
int main()
{
int i;
while(scanf("%d",&n)&&n){
if(n==) return ;
for(i=;i<n;i++)
scanf("%d",&a[i]);
num=;
result=;
for(i=;i<n;i++){
a[num++]=a[i];
while(num>=&&a[num-]<=a[num-])
combine(num-);
}
while(num>) combine(num-);
printf("%d\n",result);
}
return ;
}

POJ 1738:An old Stone Game 石子归并 (GarsiaWachs算法)的更多相关文章

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

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

  2. 题解报告:poj 1738 An old Stone Game(区间dp)

    Description There is an old stone game.At the beginning of the game the player picks n(1<=n<=5 ...

  3. BZOJ-3229 石子合并 GarsiaWachs算法

    经典DP?稳T 3229: [Sdoi2008]石子合并 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 426 Solved: 202 [Submit] ...

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

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

  5. 洛谷 P5569 [SDOI2008]石子合并 GarsiaWachs算法

    石子合并终极通用版 #include<bits/stdc++.h> using namespace std ; ]; int n,t,ans; void combine(int k) { ...

  6. POJ 1738 石子合并2 GarsiaWachs算法

    石子合并(GarsiaWachs算法) 只能用该算法过!!! 详解看代码 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~ ...

  7. POJ 1740 A New Stone Game(博弈)题解

    题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...

  8. 石子归并的三种打开方式——难度递增———51Node

    1021 石子归并    N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的代价.计算将N堆石子合并成一堆的最小代价.   ...

  9. 洛谷P1880 [NOI1995]石子合并 纪中21日c组T4 2119. 【2016-12-30普及组模拟】环状石子归并

    洛谷P1880 石子合并 纪中2119. 环状石子归并 洛谷传送门 题目描述1 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石 ...

随机推荐

  1. asp.net网站作为websocket服务端的应用该如何写

    最近被websocket的一个问题困扰了很久,有一个需求是在web网站中搭建websocket服务.客户端通过网页与服务器建立连接,然后服务器根据ip给客户端网页发送信息. 其实,这个需求并不难,只是 ...

  2. spring cloud微服务快速教程之(二)服务注册与发现 eureka

    0.为什么需要eureka 当我们从当体系统拆分为多个独立服务项目之后,如果aaa.com/uer.aaa.com/order;:相互之间调用,如果只是一个服务一个实例,那还可以直接通过固定地址(如h ...

  3. Android中使用SeekBar拖动条实现改变图片透明度

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为Lin ...

  4. Linux 文件、目录操作

    Linux中的路径只能使用/,不能使用\ 或\\. cd   切换目录 cd  /    切换到系统根目录,cd即change dir cd  /bin  切换到根目录下的bin目录 cd  ..  ...

  5. 【HDU - 1087 】Super Jumping! Jumping! Jumping! (简单dp)

    Super Jumping! Jumping! Jumping! 搬中文ing Descriptions: wsw成功的在zzq的帮助下获得了与小姐姐约会的机会,同时也不用担心wls会发现了,可是如何 ...

  6. C#中StreamWriter类使用总结

    C#中StreamWriter类使用总结 1.使用的命名空间是:System.IO; 2.用来将字符串写入文件. 常用属性:   AutoFlush:获取或设置一个值,该值指示是否 System.IO ...

  7. Ubuntu-Server18.04开启无线网卡并配置静态ip

    手里有一个笔记本快10年了,还是奔腾处理器,最近把它做成了Ubuntu-Server的系统,花了点时间折腾无线网卡,稍微记录一下,希望看见的人能少踩点坑. 1. 制作U盘启动工具,从Ubuntu官网下 ...

  8. css选择器四大类:基本、组合、属性、伪类

    什么是选择器?选择器的作用是通过它可以找到元素,把css样式传递给元素!css选择器主要分为:基本选择器.属性选择器.组合选择器与伪类选择器四个大类! css基本选择器 基本选择器又分为:*通配符.标 ...

  9. 出现ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly ....的解决方法

    在terminal上运行gluoncv时遇到了一个报错问题. ImportError: Python is not installed as a framework. The Mac OS X bac ...

  10. 【56】目标检测之NMS非极大值抑制

    非极大值抑制(Non-max suppression) 到目前为止你们学到的对象检测中的一个问题是,你的算法可能对同一个对象做出多次检测,所以算法不是对某个对象检测出一次,而是检测出多次.非极大值抑制 ...