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无法启动、闪退

    一.mysql下载完成后,运行bin目录下的mysql.exe,提示文件缺失   二.此报错为VC运行库不全或没有安装导致,百度搜索“微软常用运行库合集”进行下载安装即可解决   三.运行时闪退,无法 ...

  2. 实用,Windows后台守护进程iNeuDaemon发布。Linux操作系统下使用使用supervisor

    目       录 1.      概述... 1 2.      iNeuDaemon部署... 2 3.      iNeuDaemon配置监控服务项... 3 4.      应用效果... 3 ...

  3. JDK SPI 机制

    一.概述 最早看到 SPI 这个机制是在 dubbo 实现 中,最近发现原来也不是什么新东西,竟然就是 JDK 中内置的玩意,今天就来一探究竟,看看它到底是什么玩意! SPI的全称是 Service ...

  4. redis两种持久化方式RDB和AOF

    目录 前言 1. Redis 数据库结构 2. RDB 持久化 2.1. RDB 的创建和载入 2.1.1. 手动触发保存 SAVE 命令 BGSAVE 命令 SAVE 和 BGSAVE 的比较 2. ...

  5. Ubuntu切换为阿里镜像源

    前言 在VM虚拟机搭建Ubuntu系统学习或者测试时,常常要使用apt安装测试,但是由于系统自带的下载源在国外服务器上,下载速度慢的无法忍受.所以我们需要切换为国内镜像源,能显著加快安装包下载速度. ...

  6. 【React Native】在网页中打开Android应用程序

    React Native官方提供Linking库用于调起其他app或者本机应用.Linking的主要属性和方法有: 属性与方法 canOpenURL(url); 判断设备上是否有已经安装相应应用或可以 ...

  7. 【ffmpeg 视频下载】使用cmd视频下载

    概述 ffmpeg是什么? FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.并且,很多视频播放器都是采用他的内核. 安装与使用 安装ffmpeg ffmpeg下载 ...

  8. 《手把手教你构建自己的 Linux 系统》学习笔记(1)

    第二章.基础准备--Linux 系统定制原理 Linux 系统的组成及架构 共享库和静态库 在 Linux 系统中,函数库包含两种形式:共享库和静态库. 共享库 共享库在程序运行时,为其提供所需要的函 ...

  9. Qt代码区字符编码转换

    在做通讯练习的时候,发现发送给小助手字符乱码,图片如下 本人Qt Creator是UTF-8,需要改成gbk,代码如下 #include<QTextCodec> // 提供字符编码转换 Q ...

  10. VM虚拟机扩展Ubuntu磁盘空间

    VM虚拟机扩展Ubuntu磁盘空间 1 环境 VMware版本号:15.0.2 build-10952284 系统:Ubuntu18.04 Ubuntu只挂载一个硬盘,无分区 /dev/sda1 2 ...