有人可以解释为什么这段代码会出现SIGSEGV错误。
它为CodeChef提供了问题http://www.codechef.com/TCFS15P/problems/SPIDY2
的错误我已经尝试检查任何超出范围的数组调用,但似乎无法弄明白。
using namespace std;
typedef long long int ll;
ll ab(ll x)
{
return (x>0)?x:-x;
}
vector<ll> height;
ll dps(ll n,ll score, ll index)
{
if (index == n)
{
return score;
}
ll mins = INT_MAX;
ll k=0;
ll temp = 0;
ll add = 0;
for(k=1;(index+k) <= n ; k = (2*k) )
{
if ((index+k)>n)
{
break;
}
add = ab(height[index+k]-height[index]);
temp = dps(n , score + add , index+k );
if (temp < mins)
{
mins = temp;
}
}
return mins;
}
int main()
{
ll n,i,temp;
cin>>n;
height.push_back(0);
for(i=1;i<=n;i++)
{
cin>>temp;
height.push_back(temp);
}
ll value = 0;
value = dps(n,0,1);
cout<<value<<endl;
height.clear();
return 0;