UVa - 1017. Staircases 动态规划法 题解

One curious child has a set of Nlittle bricks (5 ≤ N≤ 500). From these bricks he builds different staircases. Staircase consists of steps of different sizes in a strictly descending order. It is not allowed for staircase to have steps equal sizes. Every staircase consists of at least two steps and each step contains at least one brick. Picture gives examples of staircase for N=11 and N=5:
Problem illustration
Your task is to write a program that reads the number Nand writes the only number Q— amount of different staircases that can be built from exactly Nbricks.

Input

Number N

Output

Number Q

Sample

input output
212
995645335


的确是很难理解的题目,理解了思想就可以写出很简单的代码。

最后我是这样理解的:

1 也不管题目原有的限制,什么需要两行以上,使用动态规划法需要先打破这个限制

2 先计算当前高度为h的时候,那么如果使用不同的砖块去堆,能堆出多少个梯子?由于动态规划法从最小情况计算起,所以这里就先假设了前面数列已经记录好了h-1个高度能使用n个砖块堆出多少个梯子了。那么要堆出h高度,情况1:只需要在h高度的最高点,即h-1这一行梯子增加一个砖块,那么就能达到了h高度了:;情况2:知道了使用n-h个砖块能堆出多少个梯子的情况了,比如是table[n-h]记录了,那么h个砖块另外堆一行h高度的行放在h-1高度行后面,就能堆出满足条件的梯子了。

最终就能得到推导公式计算了。

程序很简单,如下:

void Staircases()
{
	int N = 0;
	cin>>N;
	vector<long long> tbl(N+1);
	tbl[0] = 1;
	for (int i = 1; i <= N; i++)//i代表当前高度
	{
		for (int j = N; j >= i ; j--)//j代表当前使用多少个bricks
		{
			tbl[j] += tbl[j-i];
		}
	}
	cout<<tbl[N]-1<<endl;//-1代表去掉所有砖块单独成一行的情况。
}

要想通它的确不容易,高度的动态规划思维和抽象思维。

下面博客没什么注解,但是偷看了下人家的程序,标明一下,看过这么多网上的程序,有不同的做法,而的确是他写的最简洁了。

http://blog.csdn.net/jyysc2010/article/details/9917439



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值