显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int a[210],f[40010][210];
bool ava[41010][210];
int main(void)
{
freopen("ladder.in","r",stdin);
freopen("ladder.out","w",stdout);
int i,j,k,n,c=0,ct=0;
cin>>n;
if (n==1)
{
cout<<"0\n";
return(0);
}
for (i=1;i<=n;i++)
cin>>a[i];
f[0][1]=1;
ava[0][1]=true;
for (i=1;;i++)
{
ct=c;
for (j=1;j<=n;j++)
{
for (k=1;k<j;k++)
{
if (ava[i-1][k])
{
if (a[j]-a[k]<=(1<<(f[i-1][k]-1)))
{
f[i][j]=1;
if (!ava[i][j])
{
ava[i][j]=true;
c++;
}
}
}
}
if (ava[i-1][j+1])
{
f[i][j]=max(f[i][j],f[i-1][j+1]+1);
if (!ava[i][j])
{
ava[i][j]=true;
c++;
}
}
}
if (ava[i][n])
{
cout<<i<<endl;
break;
}
if (c==ct||i>=40000)
{
cout<<"-1\n";
break;
}
}
return(0);
}