马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
int solve(int left, int right)
{
if(left == right)
return num[left];
mid = (left + right) / 2;
lans = solve(left, mid);
rans = solve(mid + 1, right);
sum = 0, lmax = num[mid], rmax = num[mid + 1];
for(i = mid; i >= left; i--) {
sum += num[i];
if(sum > lmax) lmax = sum;
}
sum = 0;
for(i = mid + 1; i <= right; i++) {
sum += num[i];
if(sum > rmax) rmax = sum;
}
ans = lmax + rmax;
if(lans > ans) ans = lans;
if(rans > ans) ans = rans;
return ans;
}
int main(void)
{
scanf("%d", &n);
for(i = 1; i <= n; i++)
scanf("%d", &num[i]);
printf("%d\n", solve(1, n));
return 0;
}
我算出了是这是O(n^2)的时间复杂度,但是感觉不对,应该怎么算呢? |