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次合并后成为一堆.求出总的代价最小值 ...
随机推荐
- Android设置背景图片平铺
以LinearLayout为例,它提供的background属性将会将背景图片拉伸,相当难看.其实我们仅仅需做少量的改动就能够实现web编程中css背景图片的效果.来试试吧. 创建反复的背景图片 在d ...
- Mac上配置 Ruby on Rails和Git
Ruby on Rails on Mac =============================================================================== ...
- 查看SQLSERVER当前正在运行的sql信息
能够使用SQL Profiler捕捉在SQL Server实例上运行的活动.这种活动被称为Profiler跟踪.这个就不多说了,大家都知道,以下是使用代码为实现同样的效果. SET TRANSACTI ...
- EBS 第一个项目 学习总结 ---- 发运模块
EBS 组织架构: (一)业务组(BG) (二)法律实体(LE) (三)业务实体(OU) (四)库存组织(INV) (五)公司成本中心(Cost Center) (六)HR组织 (七)多组织接入控制 ...
- C++学习笔记11-面向对象2
1. 仅仅能初始化直接基类 一个类仅仅能初始化自己的直接基类.直接就是在派生列表中指定的类.假设类C 从类B 派生,类B 从类A 派生,则B 是C 的直接基类.尽管每一个C 类对象包括一个A 类部 ...
- To new is C++; To malloc is C; To mix them is sin (混淆C++中的new和C中的malloc是一种犯罪)
Introduction One of the most common questions that get asked during interviews for C++ programmers i ...
- vue2.0-elementUI
main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui' import ...
- hdoj--1342--Lotto(dfs)
Lotto Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- JAVA中jar包和war包的区别是
其实jar包和war包都可以看成压缩文件,用解压软件都可以打开,jar包和war包所存在的原因是,为了项目的部署和发布,通常把项目打包,通常在打包部署的时候,会在里面加上部署的相关信息. 这个打包实际 ...
- sql 通过某段时间求得改段时间内的工作时长,排除工作日
CREATE FUNCTION Fun_GetTotalHourBySomeTime(@TaskId NVARCHAR(30),@Bu_trupstartDate NVARCHAR(50),@Bu_t ...