Editorial for DMOPC '18 Contest 2 P4 - Damage Output


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: r3mark

For the first subtask, brute-force all subarrays and their sums.

Time Complexity: \mathcal{O}(N^3)

For the second subtask, check all subarrays and use a prefix sum to calculate their sums.

Time Complexity: \mathcal{O}(N^2)

For the third subtask, we do a two-pointer walk. Keep a left and right pointer, initially both set to the starting index. If the subarray currently described by the two pointers has a sum less than M, move the right pointer. Otherwise, move the left pointer. Since all the elements are non-negative, this will find the smallest subarray with sum at least M.

Time Complexity: \mathcal{O}(N)


Comments


  • 0
    嗨我很迟钝  commented on Oct. 11, 2018, 10:46 a.m.

    Typo on first line(brute-forc)


    • 0
      Kirito  commented on Oct. 11, 2018, 11:21 a.m.

      Fixed. Thank you for pointing that out!