Longest Increasing Sequence
InterviewTime limit5sMemory limit512 MB
Split a sequence into the maximum number of contiguous blocks whose sums form a strictly increasing sequence, and output the divider positions.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Prefix sum, Greedy, Binary search
- Solved
- No attempts yet
Problem
Time passed, and Taro became a high school student. Influenced by his older brother, who was a university student, he began to take an interest in computer science. As Taro read through a computer science textbook, he learned that there is a famous problem called the "longest increasing subsequence problem." Taro understood this problem, but he wondered whether he could make a similar problem himself. After some trial and error, he came up with the following problem.
- There is a sequence A consisting of
nintegers. - Insert
m-1dividers to split sequence A intomsequences. Each sequence after the split must contain at least one number. - The
mnumbers obtained by summing all integers in each of themsequences, arranged in the order of the original sequence, must form a strictly increasing sequence. That is, the resulting sequence must satisfyBi < Bi+1. - The goal is to maximize the length
mof the sequenceBthat can ultimately be produced.
For example, consider the case where sequence A is {5,-4,10,-3,8}. Prepare a sequence C representing the divider positions and set C={2,4}. Then sequence A is split into (5,-4),(10,-3),(8); summing each part gives 1,7,8, so the resulting sequence B is {1,7,8}.

Taro named this problem the "longest increasing sequence problem" and also devised an algorithm to solve it. Then he contacted you, now a working adult, to ask you to check it. Your job is to write a program that solves the problem made by Taro, who has grown up. Since checking is the job, you also output the positions of the m-1 dividers. For an optimal m, there may be multiple such ways to divide; as long as this m is output correctly, outputting any one of the possible ways is fine.
Input
n+1 integers are given, separated by newlines.
n
A1
A2
...
An
nrepresents the length of the given sequenceA.Airepresents the i-th element of sequenceA.
Output
m
C1 C2 .. Cm-1
- The first line contains a single integer
m.mrepresents the length of the sequenceBthat can ultimately be produced. The second line containsm-1integersCiseparated by spaces.Cirepresents the i-th divider position when sequenceAis properly split intomparts. See the figure for the definition of a divider position. It satisfies1≤Ci<n. The sequence C ofm-1integers on the second line must be sorted in ascending order. Therefore, ifi < j, thenCi < Cjholds.
- If
m=1, the second line is an empty line. - When sequence
Bis generated from sequenceAaccording to sequenceC, if sequenceBis not an increasing sequence (that is, if there exists ani(1≤i<m)withBi≥Bi+1), the result is WrongAnswer.
Constraints
1≤n≤4000|Ai|≤ 108- For an integer
k,|k|represents the absolute value ofk