石子合并(GarsiaWachs算法)

只能用该算法过!!!

详解看代码

 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<algorithm>
#include<vector>
// #include<malloc.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long
const int Inf = 0x3f3f3f3f;
const double eps = 1e-;
const double pi = acos(-);
// inline int r(){
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
// return x*f;
// } int stone[];
int n,t,ans; void combine(int k){
int tem=stone[k]+stone[k-];//合并k和k-1堆
ans+=tem;
for(int i=k;i<t-;i++) stone[i]=stone[i+];//k以后的往前移位
t--;
int j;
for(j=k-;j>&&stone[j-]<tem;j--) stone[j]=stone[j-];//k-1以后的往后移位
stone[j]=tem;//在j处插入tem
while(j>&&stone[j-]<=stone[j]){//在新得到的序列里递归处理
int d=t-j;
combine(j-);
j=t-d;//回溯
}
}
int main(){
while(~scanf("%d",&n)){
if(n==) break;
for(int i=;i<n;i++) scanf("%d",&stone[i]);
t=;
ans=;
for(int i=;i<n;i++){
stone[t++]=stone[i];
while(t>&&stone[t-]<stone[t-]) combine(t-);//从1开始遍历到结尾
}
while(t>) combine(t-);//合并完后如果不为一堆再合并一次
printf("%d\n",ans);
}
return ;
}

POJ 1738 石子合并2 GarsiaWachs算法的更多相关文章

  1. 【BZOJ 3229】 3229: [Sdoi2008]石子合并 (GarsiaWachs算法)

    3229: [Sdoi2008]石子合并 Description 在一个操场上摆放着一排N堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合 ...

  2. 51 nod 1023 石子归并 V3(GarsiaWachs算法)

    1023 石子归并 V3基准时间限制:2 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一 ...

  3. zjnu1181 石子合并【基础算法・动态规划】——高级

    Description 在操场上沿一直线排列着  n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的两堆石子合并成新的一堆,  并将新的一堆石子数记为该次合并的得分.允许在第一次合并前对调一 ...

  4. BZOJ-3229 石子合并 GarsiaWachs算法

    经典DP?稳T 3229: [Sdoi2008]石子合并 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 426 Solved: 202 [Submit] ...

  5. [08山东省选]2298 石子合并 即POJ 1738 An old Stone Game

    2298 石子合并 2008年省队选拔赛山东  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在 ...

  6. 石子合并(直线版+环形版)&(朴素写法+四边形优化+GarsiaWachs算法)

    石子合并-直线版 (点击此处查看题目) 朴素写法 最简单常见的写法就是通过枚举分割点,求出每个区间合并的最小花费,从而得到整个区间的最小花费,时间复杂度为O(n^3),核心代码如下: ; i < ...

  7. 洛谷 P5569 [SDOI2008]石子合并 GarsiaWachs算法

    石子合并终极通用版 #include<bits/stdc++.h> using namespace std ; ]; int n,t,ans; void combine(int k) { ...

  8. 51 nod 石子归并 + v2 + v3(区间dp,区间dp+平行四边形优化,GarsiaWachs算法)

    题意:就是求石子归并. 题解:当范围在100左右是可以之间简单的区间dp,如果范围在1000左右就要考虑用平行四边形优化. 就是多加一个p[i][j]表示在i到j内的取最优解的位置k,注意能使用平行四 ...

  9. NYOJ 737---石子归并(GarsiaWachs算法)

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

随机推荐

  1. 在Oracle SQL Developer中创建新连接

    步骤: 1.如下图 2.点击“测试”,如果左下角的状态出现“成功”,说明OK

  2. [转载]中国天气网API

    最近在做个网站要用到天气网的api,在网上找了些参考资料,这篇文章对天气网api的介绍比较详细,所以转载之,谢谢原作者的辛勤劳动和奉献精神. 原文地址:http://g.kehou.com/t1033 ...

  3. Windows下的Memcache安装与测试教程

    Windows下的Memcache安装 1.下载memcache for windows. 下载地址:http://splinedancer.com/memcached-win32/,推荐下载bina ...

  4. Eclipse 调整代码颜色的地方

    Editors - Text Editors General-Apperance-Colors and Fonts 各工作区里面的Editor和Syntax Coloring

  5. pthread_create用法

    linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...

  6. 【leetcode】3Sum Closest(middle)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. HDU 1429 胜利大逃亡(续)(三维BFS)

    题目链接 题意 : 中文题不详述. 思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接 #include <stdio.h> #include <stri ...

  8. [itint5]判断是否为二叉搜索树

    http://www.itint5.com/oj/#25 这题在leetcode上是用中序遍历来做的,但是这里由于有相等的情况,即左子树小于等于根,这样中序遍历无法完全判定.可以用递归来做,用递归给出 ...

  9. Java Web开发 之小张老师总结GET和POST区别

    get和post区别1.传输方式不同,get在request-line中传输(即在URL中传输).post在request-line及 request-body中传输(可认为隐藏传输)2.get传输长 ...

  10. mybatis的知识点总结

    1.接口绑定:两种方法,基于注解或者基于xml文档mapper,但要注意mapper的namespace要与接口路径完全一致. 2.orm格式转换:通过设置resultMap和ResultType,将 ...