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. 一种高灵敏度自带DSP降噪算法的音频采集解决方案

    背景调研   随着AI渗透到各行各业,人们对语音的需求也越来越大,最近一两年,各种AI音频设备如雨后春笋般冒出.各种智能AI设备的推出,意味者市场对低成本的音频采集设备越来越多.针对这种情况,我们开发 ...

  2. MySQL基础(4) | 视图

    MySQL基础(4) | 视图 基本语法 1.创建 CREATE VIEW <视图名> AS <SELECT语句> 语法说明如下. <视图名>:指定视图的名称.该名 ...

  3. Vue整合d3.v5.js制作--柱状图(rect)

    先上效果图: 图中柱状图变成纯蓝色是鼠标滑动过的颜色(颜色可改,本人配色能力十分的强,建议直接用默认设置即可 ( ᖛ ̫ ᖛ )ʃ)) 1.环境说明 Vue版本:"vue": &q ...

  4. shell 一键配置单实例oracle基础环境变量(linux7)

    #!/bin/bash echo "修改主机名" hostnamectl set-hostname wangxfa hostname sleep 1 echo "查看并关 ...

  5. 00-django | 02-处理HTTP请求

    00-django | 02-处理HTTP请求 python Django Django 处理 HTTP 请求 Hello 视图函数 我们先以一个最简单的 Hello World 为例来看看 djan ...

  6. 洛谷P3719 [AHOI2017初中组]rexp

    洛谷P3719 [AHOI2017初中组]rexp 题目背景 以下为不影响题意的简化版题目. 题目描述 给出一个由(,),|,a组成的序列,求化简后有多少个a. 化简规则: 1.形如aa...a|aa ...

  7. 【python基础语法】多重循环嵌套,函数(第6天课堂笔记)

    1.什么是函数?函数有什么作用? 函数都是有特定功能的,都是由一些有特定功能的进行封装完成的 2.已经学过了哪些内置函数?分别有什么作用? print:输出 input:输入 type:查询数据类型 ...

  8. BloodHound可视化之域分析

    一.简介 BloodHound是一款将域内信息可视化的单页的web应用程序,是一款在域内进行信息收集的免费工具: bloodhound通过图与线的形式,将域内用户.计算机.组.会话.ACL以及域内所有 ...

  9. 【React Native】集成声网Agora语音通讯

    前言: 公司的产品是一款基于社交的内容聊天软件,需要集成语音通讯功能,在写iOS原生项目时,用到的就是Agora SDK,现在写React Native也直接采用了Agora的库. 集成iOS.And ...

  10. python神器pycharm的安装

    1.下载PyCharm 官网下载:http://www.jetbrains.com/pycharm/download/#section=windows百度网盘下载:https://pan.baidu. ...