wish_me's blog

By wish_me, 9 years ago, In English

Given a sequence of n integers, you have to find out the non-decreasing subsequence of length k with minimum sum. If no sequence exists output -1.

Examples:

Input : [58 12 11 12 82 30 20 77 16 86], k = 3 Output : 39 {11 + 12 + 16}

Input : [58 12 11 12 82 30 20 77 16 86], k = 4 Output : 120 {11 + 12 + 20 + 77}

Input : [58 12 11 12 82 30 20 77 16 86], k = 5 Output : 206

  • Vote: I like it
  • -10
  • Vote: I do not like it

| Write comment?
»
9 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

If you are looking for a solution than here

»
9 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

what's your expected time complexity?

  • »
    »
    9 years ago, hide # ^ |
    ← Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    In my article which I wrote, the time complexity is O(n^2*k).

  • »
    »
    9 years ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    I am expecting knlogn? Is it possible?

    • »
      »
      »
      9 years ago, hide # ^ |
      ← Rev. 4  
      Vote: I like it 0 Vote: I do not like it

      Let's go with i through our array and store k Fenwick trees to find minimum of prefix. Get(bit[j], x) returns minimum sum of non-decreasing subsequence of length j which last element is not greater than x.

      Now we want to continue some sequences with our a[i]. For a certain sequence length j we can make sequence of length (j + 1) with minimum sum S = Get(bit[j], a[i]) + a[i], then minimise(bit[j + 1], a[i], S). Correct me if i'm wrong.