POJ 1738 An old Stone Game(石子合并 经典)
| Time Limit: 5000MS | Memory Limit: 30000K | |
| Total Submissions: 3672 | Accepted: 1035 |
Description
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
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
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(石子合并 经典)的更多相关文章
- [08山东省选]2298 石子合并 即POJ 1738 An old Stone Game
2298 石子合并 2008年省队选拔赛山东 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 在 ...
- 题解报告: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 ...
- CSU 1592 石子合并 (经典题)【区间DP】
<题目链接> 题目大意: 现在有n堆石子,第i堆有ai个石子.现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数.求合并所有石子的最小代价. Input ...
- nyoj 737 石子合并 经典区间 dp
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...
- POJ 1738 石子合并2 GarsiaWachs算法
石子合并(GarsiaWachs算法) 只能用该算法过!!! 详解看代码 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~ ...
- ny737 石子合并(一) 总结合并石子问题
描述: 在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分. 试设计一个算法,计算出将n堆石子合并 ...
- 区间DP初探 P1880 [NOI1995]石子合并
https://www.luogu.org/problemnew/show/P1880 区间dp,顾名思义,是以区间为阶段的一种线性dp的拓展 状态常定义为$f[i][j]$,表示区间[i,j]的某种 ...
- POJ 1740 A New Stone Game(博弈)题解
题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...
- 题解报告:NYOJ #737 石子合并(一)(区间dp)
描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值 ...
随机推荐
- [Recompose] Handle React Events as Streams with RxJS and Recompose
Events are the beginning of most every stream. Recompose provides a createEventHandler function to h ...
- Using a Plugin
创建hello.world <?xml version="1.0"?> <sdf version="1.4"> <world na ...
- Introspector
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; im ...
- Linux登陆类型-Linux中如何临时配置IP
Linux登录: 本地登录,直接在Linux主机上接上键盘显示器,然后输入用户名密码登录 远程登录,通过网络进行登录(需要IP 账户名 密码) windows中远程登录软件有 xshell.putty ...
- Word中查找替换软回车键和回车键
在Word中使用搜索功能搜索“^p”组合字符串可以查找文档中的所有换行符(回车键),使用“^l”(英文输入状态下shift+6与小写字符L的组合)可以搜索所有的软回车符.使用替换功能就可以搜索替换二者 ...
- web安全:防范点击劫持的两种方式
防范点击劫持的两种方式 什么点击劫持?最常见的是恶意网站使用 <iframe> 标签把我方的一些含有重要信息类如交易的网页嵌入进去,然后把 iframe 设置透明,用定位的手段的把一些引诱 ...
- jvm 性能分析
VM本身提供了一组管理的API,通过该API,我们可以获取得到JVM内部主要运行信息,包括内存各代的数据.JVM当前所有线程及其栈相关信息等等.各种JDK自带的剖析工具,包括jps.jstack.ji ...
- 阿里云server改动MySQL初始password---Linux学习笔记
主要方法就是改动 MySQL依照文件以下的my.cnf文件 首先是找到my.cnf文件. # find / -name "my.cnf" # cd /etc 接下来最好是先备份my ...
- HBase概念学习(八)开发一个类twitter系统之表设计
这边文章先将可能的需求分析一下,设计出HBase表,下一步再開始编写client代码. TwiBase系统 1.背景 为了加深HBase基本概念的学习,參考HBase实战这本书实际动手做了这个样例. ...
- 改动GDAL库支持RPC像方改正模型
近期在做基于RPC的像方改正模型.方便对数据进行測试,改动了GDAL库中的RPC纠正模型,使之能够支持RPC像方改正參数. 以下是RPC模型的公式,rn,cn为归一化之后的图像行列号坐标,PLH为归一 ...