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. MySQL - SQL语句优化方法

    1.使用 show status 了解各种 SQL 的执行频率 mysql> show status like 'Com%'; 该命令可以查询 sql 命令的执行次数. 2.定位执行效率较低的 ...

  2. [MySQL]mysql binlog回滚数据

    1.先开启binlog log-bin = /var/log/mysql/mysql_bin #binlog日志文件,以mysql_bin开头,六个数字结尾的文件:mysql_bin.000001,并 ...

  3. 虚拟机VMware官网最新版附密钥,kali,ubuntu,centos,deepin迅雷下载地址。

    以下全部都是官网的迅雷复制链接 版本都是当前时间可下载的最新版本 VMware官网迅雷下载链接: https://download3.vmware.com/software/wkst/file/VMw ...

  4. 谈谈:这次疫情对一个普通iOS开发者的影响!

    “2019年已经很难了,2020年开局0-5那就更难了啊!”大家应该都很清楚,这次疫情对于国家的整体经济体系影响非常大,但是要说有多大,我也不了解,毕竟我只是个程序员!但是对iOS开发者影响有多大,我 ...

  5. Python基础之程序暂停

    当我们执行某些程序时,由于机器速度很快导致肉眼无法直接看到执行结果时程序便停止运行.这时候我们迫切需要在程序中暂停,专业术语叫做阻塞.下面列举几种常用的程序暂停方法: input()用法:直接在欲等待 ...

  6. SQL Server无备份误删数据的恢复

    在正式生产数据库中,因为客户现场管理不规范产生了一条错误数据,由于自身睡眠不佳加上客户方言表达,将编号记错,在没有备份的情况下,直接连远程数据库执行了delete操作. 由于备份设置的是每日0点,当天 ...

  7. 00 - PXE | 环境准备

    00 - PXE | 环境准备 TFTP PXE 1. TFTP服务搭建 安装环境Centos7.3 1.1 安装 # yum install xinetd # yum install tftp # ...

  8. nginx 反向代理及 https 证书配置

    nginx 反向代理及 https 证书配置 author: yunqimg(ccxtcxx0) 1. 编译安装nginx 从官网下载 nginx源码, 并编译安装. ./configure --pr ...

  9. 纪中20日c组模拟赛

    赛后感想 多写点东西总是好的,但是在最后,算法就不要改动了(就这样我少了10分) 题解 T1 2121. 简单游戏 T2 2122. 幸运票

  10. web攻击与防御

    攻击方式 利用输出值转义漏洞 跨站脚本攻击(XSS) SQL注入攻击 OS命令注入攻击 HTTP首部注入攻击 邮件首部注入攻击 文件目录遍历攻击 利用设置或设计缺陷 强制游览 开放重定向 不正确的错误 ...