JZOJ · stairs [DP]

Description–

Of N stairs, the stair height of the i-th stage is the H [i]. You have to climb the stairs through a series of operations, each step, you can only do one of the following three options:

1, if the height of the stair is just higher than the height of your current location staircase 1, then you can climb the stairs after a staircase from the current.

2, as long as you are not in the first stage stairs, if you wish, you can go back to the previous level from the current stair staircase.

3, if you are in step backward K recently (Let's assume now retreated to the i-th stage stairs), then you can go from the current level i j-th stage stairs step to reach the stairs, where j> i, and meet H [ j] - H [i] <= 2 ^ K. 2 ^ K wherein K represents a power of 2.

You start at level 1 stairs, how many steps you are going to get at least the N-stairs? If N does not reach the first stairs, the output of -1.


Input–

The first line, an integer N. 2 <= N <= 50.

Second row, N integers, the first integer i denotes the i stairs height H [i], in ascending order of height.

0 <= H [i] <= 1000000000. H [1] must be equal to 0.

Output–

Line, an integer.


Sample Input–

【1】
5
0 1 2 3 6
【2】
5
0 1 2 4 8
【3】
8
0 2 3 4 5 6 7 8
【4】
7
0 1 2 3 5 10 100
【5】
15
0 1 2 3 4 7 10 15 50 100 200 300 400 500 1000

Sample Output–

【1】7
【2】9
【3】-1
【4】-1
【5】36


Notes -

For 30% of the data, N <= 10.


Problem-solving ideas -

Done - fans of the ladder


Code -

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n,f[205],a[205];
int main()
{
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
	
	scanf("%d",&n);
	for (int i=1;i<=n;++i)
	  scanf("%d",&a[i]);
	memset(f,0x7f,sizeof(f));//初始化一个较大的数
	f[1]=0;
	for (int i=2;i<=n;++i)
	{
		if (a[i-1]+1==a[i])
		  f[i]=min(f[i],f[i-1]+1);//向上跳一阶
		for (int j=1;j<i;++j)//枚举从哪一阶跳上来
		  for (int k=1;k<i-j;++k)//向后退了K阶
		    if (a[j]+pow(2,k)>=a[i])
		      f[i]=min(f[i],f[j+k]+k+1);
	}
	if (f[n]!=f[0]) printf("%d",f[n]);
	else printf("-1");//到不了
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43654542/article/details/94760949