迷之阶梯
.
.
.
.
.
.
程序:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n,a[500],m[32];
long long f[500];
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
m[0]=1;
for (int i=1;i<=31;i++)
m[i]=m[i-1]*2;
for (int i=1;i<=n;i++)
f[i]=2147483647;
f[1]=0;
for (int i=1;i<=n;i++)
{
if (a[i-1]+1==a[i]) f[i]=f[i-1]+1;
for (int j=1;j<=i-1;j++)
{
int w=a[i]-a[j],k;
for (k=0;k<=31;k++)
if (m[k]>=w) break;
if (j+k<=i-1) f[i]=min(f[i],f[j+k]+k+1);
}
}
if (f[n]<2147483647) printf("%d",f[n]); else printf("-1");
return 0;
}