#P1704. Maximizing Increasing Problem Solving Days
Maximizing Increasing Problem Solving Days
Maximizing Increasing Problem Solving Days
In this problem, you are given the predicted number of problems that will be solved on each of the next \(N\) days, stored in an array \(A\) (1-indexed). In addition, there are \(K\) special days (with indices given) on which you must solve problems, and on those days you must solve exactly \(A_i\) problems. You are allowed to choose other days to solve problems as well. However, when you list the days on which you solved problems in chronological order and record the corresponding \(A_i\) values, the resulting sequence (the "problem-solving curve") must be strictly increasing.
More formally, let \(A_1, A_2, \ldots, A_N\) be the predicted numbers. You are given a set \(F\) of forced day indices. You need to select a subsequence of days \(S\) (with the days in increasing order) such that:
- If a day \(i\) is in \(F\), then it must be in \(S\).
- For any two consecutive days \(i\) and \(j\) in \(S\) (with \(i<j\)), \(A_i < A_j\) holds.
Your task is to choose \(S\) with the maximum possible number of days. Note that if \(K>0\), the forced days must already be in strictly increasing order in terms of their \(A_i\) values; otherwise, it is impossible to obtain a strictly increasing sequence and the answer is \(-1\).
Input Format:
The first line contains two integers \(N\) and \(K\) (\(0 \le K \le N\)).
The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\), where \(A_i\) is the predicted number of problems on day \(i\).
If \(K > 0\), the third line contains \(K\) distinct integers representing the forced day indices. The indices may be given in any order.
Output Format:
Print a single integer which is the maximum number of days you can solve problems such that the curve is strictly increasing (including all forced days). If it is impossible to have a strictly increasing sequence due to forced days, print \(-1\).
Explanation:
You can think of the solution as follows: The forced days partition the \(N\) days into segments. In each segment, you are allowed to choose days as long as their predicted numbers lie between the lower and upper bounds imposed by the neighboring forced days (or \(-\infty\) / \(+\infty\) for the first/last segment). On each segment, the problem reduces to finding the longest increasing subsequence under these bounds. Finally, add the number of forced days. If \(K = 0\), simply output the length of the longest increasing subsequence in the entire array.
inputFormat
The input begins with a line containing two integers \(N\) and \(K\). The next line contains \(N\) space-separated integers \(A_1, A_2, \ldots, A_N\). If \(K > 0\), the third line contains \(K\) space-separated integers representing the forced day indices.
Sample Input 1:
6 2 3 1 4 2 5 6 3 5
Sample Input 2:
5 0 5 3 4 6 2
Sample Input 3: (Forced days do not form a strictly increasing sequence)
4 2 2 2 3 4 1 2
outputFormat
Output a single integer representing the maximum number of days you can solve problems while maintaining a strictly increasing sequence. If it is impossible because the forced days violate the strictly increasing condition, output -1
.
Sample Output 1: 4
Sample Output 2: 3
Sample Output 3: -1
sample
6 2
3 1 4 2 5 6
3 5
4