POJ 1738

2023-05-16

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  

GarsiaWachs算法是从第一个石堆开始找符合stone[k-1]<stone[k+1]的石堆k,然后合并k-1与k堆,再向前找j堆石子,满足stone[j]>stone[k]+stone[k-1],插入到石堆j的后面。然后重新寻找。


代码:


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[50010];
int now,ans;
int n;
void sol(int x)
{
   int i,j,tmp;
   tmp = a[x-1] + a[x];
   ans += tmp;
   for(i=x; i<now-1; i++)
    a[i] = a[i+1];
   now--;
   for(j=x-1; j>0&&a[j-1]<tmp; j--)
    a[j] = a[j-1];
    a[j] = tmp;
   while(j>=2 && a[j] >= a[j-2])
   {
   	  int dis = now - j;
   	  sol(j-1);
   	  j = now - dis;
   }  
   return ;
}
int main()
{
	while(~scanf("%d", &n))
	{
	  if(n==0)break;
	  for(int i=0; i<n; i++)
	  {  
	  	scanf("%d", &a[i]);
	  }
	  now = 1;
	  ans = 0;
	  for(int i=1; i<n; i++)
	  {
	   	a[now++] = a[i];
	   	while(now >= 3 && a[now-3] <= a[now-1])
	  	{
	  	   sol(now-2);	
		}
	  }
      while(now > 1) sol(now-1); 
	  cout << ans << endl;	  
    }
  return 0;	
} 

水波.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ 1738 的相关文章

  • POJ 滑动窗口(优先队列的应用)

    数据结构与算法A 第三章 栈与队列 练习题 滑动窗口 思路 对于最大最小值分别维护一个优先队列 xff08 保存元素下标 xff09 以最小值为例 每次遇到一个新元素 xff0c 从队尾插入 插入时删去队列中比该值大的元素 xff08 因为
  • POJ 1738

    There is an old stone game At the beginning of the game the player picks n 1 lt 61 n lt 61 50000 piles of stones in a li
  • POJ滑动窗口

    题目描述 现在有一堆数字共N个数字 xff08 N lt 61 10 6 xff09 xff0c 以及一个大小为k的窗口 现在这个从左边开始向右滑动 xff0c 每次滑动一个单位 xff0c 求出每次滑动后窗口中的最大值和最小值 例如 xf
  • POJ 2449 Remmarguts‘ Date---SPFA求评估函数 + A*最小堆BFS

    POJ 2449 Remmarguts Date Time Limit 4000MS Memory Limit 65536K Description Good man never makes girls wait or breaks an
  • POJ 3259 Wormholes(负权环路)

    题意 xff1a 农夫约翰农场里发现了很多虫洞 xff0c 他是个超级冒险迷 xff0c 想利用虫洞回到过去 xff0c 看再回来的时候能不能看到没有离开之前的自己 xff0c 农场里有N块地 xff0c M条路连接着两块地 xff0c W
  • POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)

    Increasing Sequences Time Limit 1000MS Memory Limit 10000KTotal Submissions 3025 Accepted 1147 Description Given a strin
  • poj 1068 parencondings

    题目描述 xff1a 定义 S 为一个合法的括号字符串 S 可以用以下两种方式编码 xff1a 1 用一个整数数组 P 来表示 xff0c 其中元素 p i 是 S 中每个 39 39 前的 39 39 的个数 xff1b 2 用一个整数数
  • poj 2513 colored sticks

    代码 include lt iostream gt include lt stdio h gt using namespace std define MAX 27 define S 500003 struct Node int id Nod
  • POJ 1635 Subway tree systems

    题目 xff1a Some major cities have subway systems in the form of a tree i e between any pair of stations there is one and o
  • 汉诺塔问题(Hanoi)-python递归实现

    描述 描述 一 汉诺塔问题 有三根杆子A B C A杆上有N个 N gt 1 穿孔圆盘 盘的尺寸由下到上依次变小 要求按下列规则将所有圆盘移至C杆 每次只能移动一个圆盘 大盘不能叠在小盘上面 提示 可将圆盘临时置于B杆 也可将从A杆移出的圆
  • POJ--2709:Painter (贪心)

    1 题目源地址 http poj org problem id 2709 2 解题思路 每个颜料盒可能有3 12种颜色 其中每种颜色50ml 任意三种颜色 假设每种颜色Xml 可以混合出Xml的灰色 现在给出所需颜色的种数N 给出N个值分别
  • POJ--1328:Radar Installation (贪心)

    1 题目源地址 http poj org problem id 1328 2 解题思路 该题题意是为了求出能够覆盖所有岛屿的最小雷达数目 每个小岛对应x轴上的一个区间 在这个区间内的任何一个点放置雷达 则可以覆盖该小岛 区间范围的计算用 x
  • POJ 2479 Dual Core CPU|网络流|dinic模版

    问题描述 总时间限制 15000ms 单个测试点时间限制 5000ms 内存限制 65536kB 描述 As more and more computers are equipped with dual core CPU SetagLilb
  • 【题解】poj2689(LibreOJ10197) 线性筛

    题目链接 筛出2到sqrt u 的所有质数 再标记 l u 中是质数p倍数的数 最后枚举相邻质数 部分代码实现参考了大佬题解 题目描述 给定两个整数 L R L R L R 求闭区间 L
  • POJ--1458:Common Subsequence (DP求最长公共子序列)

    1 题目源地址 http poj org problem id 1458 2 基本题意 给出两个序列 求出最长子序列的长度并输出 经典的动态规划求解 求最长公共子序列的经典DP解法代价为O mn 其中m和n分别为两个字符串的长度 具体实现如
  • POJ 2659 Raid|分治法|平面最近点对

    题目描述 总时间限制 1000ms 内存限制 65536kB 描述 After successive failures in the battles against the Union the Empire retreated to its
  • poj 2155 Matrix

    Problem poj org problem id 2155 vjudge net contest 146952 problem A Meaning 一个 N N 的矩阵 A 初始时全部值为 0 有两种操作 1 C x1 y1 x2 y2
  • poj 1195 Mobile phones

    Problem poj org problem id 1195 vjudge net contest 146952 problem C Meaning 有一个 S S 的正方形区域 两维的下标范围都是是 0 S 1 有 4 种操作 1 0
  • poj1240

    本题为已知M 叉树的前序遍历与后序遍历 要求给出对应树有多少种可能 与poj2255类似 只要划分出子树 就把问题规模缩小了 然后就可以递归 目前只会写递归 M叉树的前序遍历为 根 子树1 子树2 子树K K lt M M叉树的后序遍历为
  • Knight Moves_dfs_2018_3_10

    A friend of you is doing research on the Traveling Knight Problem TKP where you are to find the shortest closed tour of

随机推荐

  • Android 导入导出excel xls、xlsx

    1 导入依赖 implementation group 39 org apache poi 39 name 39 poi ooxml 39 version 39 3 17 39 implementation group 39 org apa
  • Android 导出PDF PdfDocument

    导出PDF 64 param view 要导出的view xff0c 如果view高度过高 xff08 超过一屏的高度 xff09 xff0c 在改view外部套一层Scrollview即可 如果要导出列表类型View 比如Listview
  • Android 获取所有短信-彩信

    1 权限 lt 读取短信 gt lt uses permission android name 61 34 android permission READ SMS 34 gt lt uses permission android name
  • Photoshop CC 2018 安装包安装教程

    Photoshop CC 2018功能特点 1 更紧密连接的 Photoshop 全新的智慧型锐利化 2 智慧型增加取样 内含 Extended 功能 Camera RAW 8 和图层支援 3 可编辑的圆角矩形 多重形状和路径选择 相机防手
  • 【原创】什么是原码、反码、补码?

    原码 反码 补码是计算机中对数字的二进制表示方法 原码 xff1a 将最高位作为符号位 xff08 0表示正 xff0c 1表示负 xff09 xff0c 其它数字位代表数值本身的绝对值的数字表示方式 反码 xff1a 如果是正数 xff0
  • snprintf中的错误,不要出现目标地址也是源地址的情况

    在使用snprintf时 xff0c 千万要注意一点 xff0c 不要出现目标地址也是源地址的情况 看如下例子 xff1a span class token macro property span class token directive
  • Linux下的shell进度条

    一 关于Shell Shell的作用是解释执行用户的命令 xff0c 它有两种执行命令的方式 xff1a 交互式和批处理 Shell脚本和编程语言很相似 xff0c 也有变量和流控制语句 xff0c 但Shell脚本是解释执行 xff0c
  • you-get相关使用命令

    you get i url 获取视频格式 清晰度等信息 you get o E folder url 保存路径 在当前目录的路径栏 输入cmd即可打开命令行 或者 shift 右键 用powershell打开 you get p PotPl
  • Spring Cloud从入门到放弃(四):Eureka的常用配置

    前言 Spring Cloud系列 点击查看Spring Cloud系列文章 eurka的常用配置 eureka server前缀的配置项 是否允许开启自我保护模式 xff0c 缺省 xff1a span class token boole
  • STM32之点亮LED

    学习一个新的处理器 xff0c 第一个程序肯定就是点亮LED xff0c 它可以让我们较快的 较清晰的了解到一个处理器的程序结构 xff0c 学习32也不例外 xff0c 首先第一个程序我们就来点亮LED xff0c 点亮LED程序有很多种
  • 判断两条线段是否相交

    判断两条直线是否相交有两步 xff1a 1 xff1a 快速排斥 xff08 可以筛除大部分 xff09 2 xff1a 跨立试验 xff08 下面会有所说明 xff09 现在开始解释 xff1a 第一步快速排斥 xff0c 实际上就是先判
  • 2015-2016 ACM-ICPC Pacific Northwest Regional Contest Div.2( Problem V Gears)

    题目地址 xff1a 点击打开链接 题意 xff1a 给你很多齿轮 xff0c 让你判断第一个齿轮和第n个齿轮的关系 有三种关系题目中已经给出 解题思路 xff1a 算是比较直观的一个dfs题目了 xff0c 重点是怎么样处理这个dfs 结
  • 免费馅饼(简单动态规划)

    都说天上不会掉馅饼 xff0c 但有一天gameboy正走在回家的小径上 xff0c 忽然天上掉下大把大把的馅饼 说来gameboy的人品实在是太好了 xff0c 这馅饼别处都不掉 xff0c 就掉落在他身旁的10米范围内 馅饼如果掉在了地
  • CF816B-Karen and Coffee

    B Karen and Coffee time limit per test2 5 seconds memory limit per test512 megabytes inputstandard input outputstandard
  • B. Mister B and Angle in Polygon 421.div2

    B Mister B and Angle in Polygon time limit per test 2 seconds memory limit per test 256 megabytes input standard input o
  • openwrt下安装lighttpd/webdav模块及改变安装目录

    Openwrt下安装lighttpd及Webdav模块 安装lightttpd 1 opkg update 2 opkg install lighttpd 依赖libxml库 3 修改 etc lighttpd lighttpd conf
  • Game of the Rows CodeForces - 839B

    Daenerys Targaryen has an army consisting of k groups of soldiers the i th group contains ai soldiers She wants to bring
  • ccf 交通规划

    201609 4试题名称 xff1a 交通规划时间限制 xff1a 1 0s内存限制 xff1a 256 0MB问题描述 xff1a 问题描述 G国国王来中国参观后 xff0c 被中国的高速铁路深深的震撼 xff0c 决定为自己的国家也建设
  • ccf 游戏

    试题编号 xff1a 201604 4试题名称 xff1a 游戏时间限制 xff1a 1 0s内存限制 xff1a 256 0MB问题描述 xff1a 问题描述 小明在玩一个电脑游戏 xff0c 游戏在一个 n m的方格图上进行 xff0c
  • POJ 1738

    There is an old stone game At the beginning of the game the player picks n 1 lt 61 n lt 61 50000 piles of stones in a li