Блог пользователя uet23_danh

Автор uet23_danh, история, 11 месяцев назад, По-английски

Given an array $$$a$$$, you have to divide this array into maximum numbers of continous subarrays that the sum of a subarray is greater or equal to every subarrays that come after it. $$$1 \le n \le 5 \times 10^5, 1 \le a_i \le 10^9$$$.

Example:

Input

4
6 5 2 3

Output

3

A possible solutions:

6 | 5 | 2 3

Can anyone help me with this problems? I only came up with $$$O(n^2)$$$ solution. Thanks in advance!

  • Проголосовать: нравится
  • +17
  • Проголосовать: не нравится

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can you share problem link

  • »
    »
    11 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Sorry, I don't have the submission link

  • »
    »
    11 месяцев назад, # ^ |
      Проголосовать: нравится +15 Проголосовать: не нравится

    Link of this problem :IZHO 2019 day2 problem E.I solved this problem with binarysearch + dp.

    • »
      »
      »
      11 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      could you elaborate on that?

      • »
        »
        »
        »
        11 месяцев назад, # ^ |
        Rev. 2   Проголосовать: нравится +6 Проголосовать: не нравится

        Let's assume (1<=i<=n) dp[i] — — maximum number of segment to position i. Let's assume (1 <= i <= n) val[i] — sum of segment that end in position i.Let's assume (1 <= i <= n) p[i] — is sum of elements to position i.Now let's calculate dp[i] we try to find maximum j such that (j + 1) to i is the last segment and (j < i) and (p[i]-p[j] >= val[j]) === (p[i] >= val[j] + p[j]). I tried my best I hope this is useful

»
11 месяцев назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

nvm

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I have a weird idea. Unsure if it works. Let's add all elements to a set each element is currently a subarray. If there's an element $$$x$$$ with an element $$$y$$$ after it such that $$$x < y$$$ then we remove $$$y$$$ from the set and add it to $$$x$$$. Can someone provide a case where this fails?

  • »
    »
    11 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    If I understood your idea correctly, $$$[3, 3, 4]$$$ would fail. Your idea would merge the last two elements making the array $$$[3, 7]$$$ which would need to be merged again. The optimal solution is to merge the first two elements making the array $$$[6, 4]$$$.