What are practical uses for STL's 'partial_sum'?

前端 未结 11 1706
耶瑟儿~
耶瑟儿~ 2021-02-04 03:47

What/where are the practical uses of the partial_sum algorithm in STL?

What are some other interesting/non-trivial examples or use-cases?

11条回答
  •  我在风中等你
    2021-02-04 04:42

    You know, I actually did use partial_sum() once... It was this interesting little problem that I was asked on a job interview. I enjoyed it so much, I went home and coded it up.

    The problem was: Given a sequential sequence of integers, find the shortest sub-sequence with the highest value. E.g. Given:

    Value: -1  2  3 -1  4 -2 -4  5
    Index:  0  1  2  3  4  5  6  7
    

    We would find the subsequence [1,4]

    Now the obvious solution is to run with 3 for loops, iterating over all possible starts & ends, and adding up the value of each possible subsequence in turn. Inefficient, but quick to code up and hard to make mistakes. (Especially when the third for loop is just accumulate(start,end,0).)

    The correct solution involves a divide-and-conquer / bottom up approach. E.g. Divide the problem space in half, and for each half compute the largest subsequence contained within that section, the largest subsequence including the starting number, the largest subsequence including the ending number, and the entire section's subsequence. Armed with this data we can then combine the two halves together without any further evaluation of either one. Obviously the data for each half can be computed by further breaking each half into halves (quarters), each quarter into halves (eighths), and so on until we have trivial singleton cases. It's all quite efficient.

    But all that aside, there's a third (somewhat less efficient) option that I wanted to explore. It's similar to the 3-for-loop case, only we add the adjacent numbers to avoid so much work. The idea is that there's no need to add a+b, a+b+c, and a+b+c+d when we can add t1=a+b, t2=t1+c, and t3=t2+d. It's a space/computation tradeoff thing. It works by transforming the sequence:

    Index: 0 1 2  3  4
    FROM:  1 2 3  4  5
      TO:  1 3 6 10 15
    

    Thereby giving us all possible substrings starting at index=0 and ending at indexes=0,1,2,3,4.

    Then we iterate over this set subtracting the successive possible "start" points...

    FROM:  1 3 6 10 15
      TO:  - 2 5  9 14
      TO:  - - 3  7 12
      TO:  - - -  4  9
      TO:  - - -  -  5
    

    Thereby giving us the values (sums) of all possible subsequences.

    We can find the maximum value of each iteration via max_element().

    The first step is most easily accomplished via partial_sum().

    The remaining steps via a for loop and transform(data+i,data+size,data+i,bind2nd(minus(),data[i-1])).

    Clearly O(N^2). But still interesting and fun...

提交回复
热议问题