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 题目1105 S-Trees(二叉树模拟)

    S Trees Time Limit 1000MS Memory Limit 10000KTotal Submissions 1499 Accepted 807 Description A Strange Tree S tree over
  • POJ-2453

    As we known data stored in the computers is in binary form The problem we discuss now is about the positive integers and
  • 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 3764--The xor-longest Path---DFS + Trie(最大异或值)

    POJ 3764 The xor longest Path Time Limit 2000MS Memory Limit 65536K Description In an edge weighted tree the xor length
  • POJ - 2823 滑动窗口

    题目 xff1a 给一个长度为 NN 的数组 xff0c 一个长为 KK 的滑动窗体从最左端移至最右端 xff0c 你只能看到窗口中的 KK 个数 xff0c 每次窗体向右移动一位 找出窗体在各个位置时的最大值和最小值 思路 xff1a 网
  • POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)

    Increasing Sequences Time Limit 1000MS Memory Limit 10000KTotal Submissions 3025 Accepted 1147 Description Given a strin
  • poj 1131进制转换

    POJ 1131 Octal Fractions 任意进制之间小数的转换 给定一个八进制的小数题目要求你把它转换为十进制小数 xff0c 转换后小数的位数是转换前八进制小数位数的3倍且不输出末尾无意义的零 即后置零 我采用的方法是乘10然后
  • poj 1068 parencondings

    题目描述 xff1a 定义 S 为一个合法的括号字符串 S 可以用以下两种方式编码 xff1a 1 用一个整数数组 P 来表示 xff0c 其中元素 p i 是 S 中每个 39 39 前的 39 39 的个数 xff1b 2 用一个整数数
  • 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
  • poj 2096 Collecting Bugs

    Problem poj org problem id 2096 vjudge net contest 151678 problem Q Reference blog csdn net xingyeyongheng article detai
  • POJ--1328:Radar Installation (贪心)

    1 题目源地址 http poj org problem id 1328 2 解题思路 该题题意是为了求出能够覆盖所有岛屿的最小雷达数目 每个小岛对应x轴上的一个区间 在这个区间内的任何一个点放置雷达 则可以覆盖该小岛 区间范围的计算用 x
  • 【题解】poj2689(LibreOJ10197) 线性筛

    题目链接 筛出2到sqrt u 的所有质数 再标记 l u 中是质数p倍数的数 最后枚举相邻质数 部分代码实现参考了大佬题解 题目描述 给定两个整数 L R L R L R 求闭区间 L
  • poj 1330 Nearest Common Ancestors

    Problem poj org problem id 1330 vjudge net contest 80844 problem C Meaning 求最近公共祖先 Note 真 LCA 模版题 那就备份一发 LCA 模版 链式前向星存图
  • POJ 2659 Raid|分治法|平面最近点对

    题目描述 总时间限制 1000ms 内存限制 65536kB 描述 After successive failures in the battles against the Union the Empire retreated to its
  • POJ 275 Drainage Ditches|网络流|dinic模版

    问题描述 总时间限制 1000ms内存限制 65536kB 描述 Every time it rains on Farmer John s fields a pond forms over Bessie s favorite clover
  • 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
  • 【扩展KMP】POJ_3450| HDU_2328 Corporate Identity

    原题直通车 POJ 3450 Corporate Identity HDU 2328 Corporate Identity 题意概述 找出N个串中最长公共子串 分析 一 可以直接枚举其中一个串的所有字串 跟所有串进行匹配找到结果 二 用其中

随机推荐

  • 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