hdu数塔

数塔问题--经典的动态规划问题 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? ![这里写图片描述](http://img.blog.youkuaiyun.com/20170227224434064?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3Jpb25fcGlzdGFjaG 阅读详情

数塔

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40929    Accepted Submission(s): 24309


Problem Description
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

已经告诉你了,这是个DP的题目,你能AC吗?
 

Input
输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。
 

Output
对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。
 

Sample Input
1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
 

Sample Output
30
#include <bits/stdc++.h>	//万能的头文件
using namespace std;

//状态转移方程:f[i,j]=max{f(i+1,j),f(i+1,j+1}+a[i][j]
//i代表第i层,j代表第j列(i,j>=1) 
int T,n;
int a[1005][1005];
int res[1005][1005];

int slv()	//添二维表 
{
	for(int j=1;j<=n;j++)	//初始化最后一层 
		res[n][j]=a[n][j];
	for(int i=n-1;i>=1;i--)	
	{
		for(int j=1;j<=i;j++)
		{
			res[i][j]=max(res[i+1][j],res[i+1][j+1])+a[i][j];
		}
	}
	return res[1][1];
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=i;j++)
			{
				cin>>a[i][j];
			}
		}
		cout<<slv()<<endl;
	}
	return 0;
} 


动态规划数塔问题 动态规划数塔问题代码,能运行,运用三个二维数组, 立即下载

相关推荐

【Scrapy】Scrapy教程4——命令行工具

Scrapy支持使用模块设置自己的项目命令。如何实现可参考的实践方法。然后在项目的settings文件中配置来添加自定义命令。以上就是对Scrapy命令行工具的说明,有不明确的地方,可参考官方文档欢迎大家在各大平台关注我,我会不定期发布技术相关文章和各种小工具,并且欢迎共同技术探讨!

jayhgq的博客 1250

DP经典问题,数塔

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? DP问题一般都是从后向前,因为要找到最大值的连线,我们可以从倒数第二行开始,在2时可以选择19和7,这个时候我们选19,并把它加在2上,这代表着若走到这,后面最多能再提供21这样大小的值,如图 下一步同理,在10,6,8各个节...

班吉拉的博客 3059

VS2022 LNK1168无法打开进行写入 解决办法

C语言编程---VS---错误和警告---链接器工具错误---LNK1168--无法打开进行写入

weixin_67166415的博客 7161

数塔(经典DP)

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:  有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?   已经告诉你了,这是个DP的题目,你能AC吗? Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 &lt;= N &lt;= 100),表示数塔的高度,接下来用N行数字表示数...

CHEN_ZAN_YU的博客 1304

Mid-360与FAST-LIO2实战:从点云采集到高精度建图全流程解析

本文详细解析了使用Livox Mid-360激光雷达与FAST-LIO2算法实现高精度实时建图的全流程。内容涵盖硬件准备、软件环境搭建、算法部署配置、数据采集实战,并重点探讨了多楼层场景下的参数调优与误差抑制策略,为机器人及无人机三维感知应用提供了实用指南。

prometheus9mon的博客 414

PCL 动态半径滤波【2025最新版】

使用PCL实现的2022年最新SCI论文——【Design of Dust-Filtering Algorithms for LiDAR Sensors Using Intensity and Range Information in Off-Road Vehicles】 一文中提出用于自动驾驶点云剔除粉尘、雨雪等影响因素导致的噪声点滤波算法。

点云侠的博客 1473

HDU 2084 数塔 (dp)

HDU 2084 数塔 (dp) 题目描述: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 题目限制:数塔高度&lt;=100 分析 :典型dp,N&lt;=100,直接开二维数组储存每个节点的数据即可。因为最后一层每个节点的值确定,从倒数第二层开始遍历,求经过倒数第二层每个节点的最大值,并依次向上遍历,最终遍历根节点,...

ronnie14165的博客 560

数字游民产业小报告 · 2025

2025 年全球数字游民约(不同统计口径差异很大,低估法约 4,000 万,高估法 8,000 万),美国来源占比最高。:2025 Q1 约岗位允许一定程度远程;2024 年美国员工至少部分远程(全球 2023 年约 28%)。:到 2025 年,提供数字游民/远程工作签证(媒体口径 50–73 个不等;福布斯 66、Citizen Remote 50+、有机构清单称 73)。:以英国为例,期望未来一年全勤到岗、企业因强制返岗出现离职,说明“灵活/远程”仍是劳动力市场的筹码之一。

Anthony1453的博客 1467

数塔问题

数塔问题 对于诸如下图的数塔,若从顶层走到底层,每一步只能走到相邻的结点,求经过的结点的数字之和最大的路径。试设计求解该问题的动态规划算法。 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;algorithm&gt; using namespace std; int a[150][150]; int dp[150][150...

qq_41249282的博客 861

Qt之自定义带游标的QSlider

最近翻看6年前写了一篇关于QSlider的博客:Qt自定义带游标的slider,在滑块正上方显示当前值 发现存在很多问题,比如游标的坐标不准确;如果游标宽度稍大,滑到Slider最左或最右的时候会显示不全;游标不够美观。 PS:这里有个漂亮的Slider样式表https://blog.youkuaiyun.com/caoshangpa/article/details/53389517于是今天再实现了一遍,依然是子类化QSlider,效果如下,支持设置游标的尺寸:这一次的实现比之前简洁很多,只需要重新paintEvent

草上爬的博客 6279

【OAG vs RAG】RAG的“语义崩溃“:为什么你的知识库越大,AI越笨?_语义检索oag-优快云博客

19.9 元(AI 编程 / Agent 实战 | 本文同主题系统课程)·49.9 元(AI 时代成长方法论)。单篇不过瘾?订阅解锁全量源码、实战与答疑;文末附资料包领取方式 ↓图1:RAG语义崩溃概念图。

求知若渴、虚心若愚 73

HDU 2084 数塔

链接 数塔 - http://acm.hdu.edu.cn/showproblem.php?pid=2084 分析 自顶而下 —— 总共有2n2^n2n条路径。因为每一条路径都会一分为二,且初始时刻有1条路径。 #mermaid-svg-HC9zsVO4ZBzA6cI6 .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermai

jpphy0的博客 291

华为OD机考双机位C卷 - 相对开音节 (Java & Python & JS & GO & C++ & C)

华为OD机考双机位C卷 - 相对开音节 , 提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。

算法大师! 743

hdu 2084 数塔

hdu  2084  数塔                  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 DP 水 题目分析:经典数字三角形,前面做过一道poj1163,完全一样。 code: #include main() { int i,j,t,n,a[105][105]; scanf("%d",&t); while(t

slicer的技术交流栈 650

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

HDU | 2084 数塔【动态规划】

动态规划 专题 HDU 2084 数塔 题目描述 输入输出样例 时空限制 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 思路 简单的动态规划问题。找到递推式即可。 它是从倒数第二行往上开始递推的,每个数都等于它自己加上 正下方 和 斜右方 两者取最大值。 dp[j-1]...

Wonz 683

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

HDU-2084 数塔

HDU-2084 数塔 题目链接:添加链接描述 #include<bits/stdc++.h> using namespace std; int main() { int C,N,i,j; int a[101][101],dp[101][101]; cin>>C; while(C--){ cin>>N; memset(dp,0,sizeof(dp)); fo

qq_55788088的博客 142

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

HDU2084 数塔(DP)

HDU2084 数塔(DP) HDU2084 数塔DP 题意 思路 AC代码 原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2084 题意 中文自己看 思路 一道经典DP题型,基本思路是从下自上的进行选取最大的数。 例如从n-1行开始,判断每一个数的左右孩子哪一个大就和哪一个相加。 AC代码 imp...

HuiSeKongHuan的博客 330

hdu 2085 数塔 -- dp模板题

数塔 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目,你能AC吗?   Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整

u012964281的专栏 1402

HDU 2084数塔(超级简单动态规划)

HDU 数塔 Click here 超级简单的动态规划 有图有代码,保证你看的懂!!! #include<iostream> using namespace std; int max(int a,int b) { return a>b?a:b; } int main() { int c; cin>>c; int n; int dp[100][100]={0...

重燃2020!!! 168

HDU 1176 免费馅饼 数塔 .

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1176 很明显的数塔问题 #include #include #include #include using namespace std; const int maxn=100000+5; int pos[maxn][10+5]; //d[i] 表示当前在i位置,且拿到了d[i]个饼 int ma

BRCOCOLI的博客 305

HDU2084 数塔问题 简单线性规划

HDU2084 数塔问题 简单线性规划 题目来源:http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1003&ojid=0&cid=13029&hide=0 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点...

qq_fairy橙 的博客 311

A - 数塔 HDU - 2084

A - 数塔 HDU - 2084 题解:来复习dp了,去年暑假培训的忘的差不多了,dp是一种思想,没有具体模版,我这种脑袋不灵活的人,就只能做题理解,一边做一边理解。数塔这个题,是从后面往前面推,把fo里面的 I,J 循环带进去,模拟,就能明白了。#include&lt;iostream&gt; #include&lt;algorithm&gt; using namespace std; int...

memory_qianxiao的博客 450

hdu 2084 java 数塔 动态规划

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?已经告诉你了,这是个DP的题目,你能AC吗? Input输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 &lt;= N &lt;= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有...

lyd99的博客 521

HDU 2084 数塔 --- 入门DP

HDU 2084 数塔 从下往上递推,状态转移方程 dp[i][j] = max( dp[i+1][j], dp[i+1][j+1]) + a[i][j]; /* HDU 2084 数塔 --- 入门DP */ #include <cstdio> const int N = 105; int dp[N][N]; int MAX(int a, int b){ ...

weixin_30609331的博客 116
上一篇: 蓝桥杯基础练习 芯片测试
下一篇: 对memset函数的一点理解
OFShare
博客等级 码龄11年 8粉丝 140原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值