细分树在解决子阵列时

时间:2015-08-08 05:38:28

标签: java algorithm data-structures tree

我正在努力解决此问题Problem

声明:
对于给定的序列A(A1,A2 ...... AN),您的任务是找到A的所有可能连续子序列的最小元素的和S

示例:

1 2 3
Possible contiguous sub sequences {(1),(2),(3),(1,2),(1,2,3),(2,3)}
Sum of minimum elements = 1 + 2 + 3 + 1 + 1 + 2 = 10

我正在使用细分树来解决此问题。我正在查询范围中的最小元素,然后在剩余索引上递归求解。

代码:

public static long get_me_baby(int a , int b  , int n , long[] A){

      if(a>b) return 0;

    int index = query(1,0,n, a, b, A);
    long temp = (index-a+1)*(b-index+1);
    return  A[index]*temp +get_me_baby(a,index-1,  n, A)+get_me_baby(index+1, b,n, A);

}

Full Working Code

我很沮丧为什么我得到了错误的答案。我已经尝试了数百次,但仍然没有得到正确答案,我不知道我在哪里犯了错误 请帮帮我

0 个答案:

没有答案