affix's blog

By affix, 10 years ago, In English

we define function f(i,j) on array A as below:

f(i,j) = (i-j)^2 + (A[i+1] + A[i+2] + A[i+3] + ... + A[j])^2

find the minimum value of function f.

2 <= A.size <= 10^5

-10^4 <= A[i] <= 10^4

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it +12 Vote: I do not like it

Firstly , we can make a cumulative sum array Prefix[i] . The function f(i,j) can be written as = ( i — j ) ^ 2 + ( Prefix[i] — Prefix[j] ) ^ 2 . Now this is analogous with closest pair point problem where each coordinate is ( i , Prefix[i] ) . The Complexity would be O(N log N ) .