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

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

Sample Output

0
17
8
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll a[],ans,n,pos;
void check(int x)
{
int i,j;
int mx=a[x]+a[x-];
ans+=mx;
for(i=x;i<pos-;i++)
a[i]=a[i+];
pos--;
for(j=x-;j> && a[j-]<mx;j--)
a[j]=a[j-];
a[j]=mx;
while(j>= && a[j-]<=a[j])
{
int y=pos-j;//j后面有多少个数,往前移;
check(j-);
j=pos-y;
}
}
int main()
{
while(scanf("%lld",&n)&& n)
{
for(int i=;i<n;i++)
{
scanf("%lld",&a[i]);
}
pos=;ans=;
for(int i=;i<n;i++)
{
a[pos++]=a[i];
while(pos>= && a[pos-]<=a[pos-])
{
check(pos-);
}
}
while(pos>) check(pos-);
printf("%lld\n",ans);
}
return ;
}

POJ 1738 An old Stone Game(石子合并 经典)的更多相关文章

  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. CSU 1592 石子合并 (经典题)【区间DP】

    <题目链接> 题目大意: 现在有n堆石子,第i堆有ai个石子.现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数.求合并所有石子的最小代价. Input ...

  4. nyoj 737 石子合并 经典区间 dp

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...

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

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

  6. ny737 石子合并(一) 总结合并石子问题

    描述: 在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分. 试设计一个算法,计算出将n堆石子合并 ...

  7. 区间DP初探 P1880 [NOI1995]石子合并

    https://www.luogu.org/problemnew/show/P1880 区间dp,顾名思义,是以区间为阶段的一种线性dp的拓展 状态常定义为$f[i][j]$,表示区间[i,j]的某种 ...

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

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

  9. 题解报告:NYOJ #737 石子合并(一)(区间dp)

    描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值 ...

随机推荐

  1. Java定时器TimeTask

    package com.alan.timer; import java.util.Calendar;import java.util.Date;import java.util.Timer;impor ...

  2. Android 使用Retrofit获取JSON数据

    在大家使用网络请求的时候,往往会出现一种情况:需要拿到服务器返回来的JSON字符串,而Retrofit会默认将Json解析,而又没有直接暴露出拿到Json字符串的方法: 今天测接口的时候,发现当数据正 ...

  3. 分享一下vue ui的分页的用法

    var newToAddLeave = function(){ var goIndex = { template:` <i-table :columns="overTimeList.c ...

  4. Eclipse如何新建一个tomcat_server发布web项目

    方法/步骤     通过Eclipse打开一个正在开发阶段的java的web项目,没有项目的,只是为了学习的话,可以新建一个测试的java的web项目即可.在功能窗口找到如图所示的Servers窗口, ...

  5. pigofzhou的巧克力棒

    Description 众所周知,pigofzhou有许多妹子.有一天,pigofzhou得到了一根巧克力棒,他想把这根巧克力棒分给他的妹子们.具体地,这根巧克力棒长为 n,他想将这根巧克力棒折成 n ...

  6. vue.js原生组件化开发(二)——父子组件

    前言 在了解父子组件之前应先掌握组件开发基础.在实际开发过程中,组件之间可以嵌套,也因此生成父子组件. 父子组件创建流程 1.构建父子组件 1.1 全局注册 (1)构建注册子组件 //构建子组件chi ...

  7. noip 2018 day1 T1 铺设道路 贪心

    Code: #include<cstdio> using namespace std; int main() { int last=0,ans=0; int n;scanf("% ...

  8. C/C++(C++重载,默认参数,引用)

    C++重载详解 重载就是同名而非同义,具体意义根据上下文的语境而言. 重载规则: 1,函数名相同. 2,参数个数不同,参数的类型不同,参数顺序不同,均可构成重载. 3,返回值类型不同则不可以构成重载. ...

  9. request获取各种路径总结、页面跳转总结。

    页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...

  10. selenium 窗口句柄之间的切换

    以前使用selenium时都是在单窗口的模式下,本次新增多窗口下的窗口之间切换 from selenium import webdriver from selenium.webdriver.commo ...