You are given an array A of size N. For each prefix of the A, you have to calculate the value described in the below function.
int func(){
int ans = 0;
for(int i = 0; i <= prefix_index; i++){
int c = 0;
for(int j = 0; j <= prefix_index; j++){
if(A[j] >= A[i]) c++;
}
ans = max(c * A[i], ans);
}
return ans;
}
i.e., in a prefix, the value for the index i is, number of elements greater than or equal to A[i] * A[i]. The answer for the prefix is maximum of these values. The output is a list of N numbers (one for each prefix).
constraints:
1 <= N <= 150000
1 <= A[i] <= 1e9
sample test case:
A = [10, 11, 12, 10, 11, 50, 70]
ans: [10, 20, 30, 40, 50, 60, 100]
test case explanationfor prefix 1 to 1number of values greater than or equal to 10 is 1. so value here is 10 * 1 = 10
The max value is 10.
for prefix 1 to 2number of values greater than or equal to 10 is 2. value = 10 * 2 = 20
number of values greater than of equal to 11 is 1. value = 11 * 1 = 11
The max value is 20.
for prefix 1 to 3number of values greater than or equal to 10 is 3. value = 10 * 3 = 30
number of values greater than of equal to 11 is 2. value = 11 * 2 = 22
number of values greater than of equal to 12 is 1. value = 12 * 1 = 12
The max value is 30.
for prefix 1 to 4number of values greater than or equal to 10 is 4. value = 10 * 4 = 40
number of values greater than of equal to 11 is 2. value = 11 * 2 = 22
number of values greater than of equal to 12 is 1. value = 12 * 1 = 12
number of values greater than or equal to 10 is 4. value = 10 * 4 = 40
The max value is 40.
for prefix 1 to 5number of values greater than or equal to 10 is 5. value = 10 * 5 = 50
number of values greater than of equal to 11 is 3. value = 11 * 3 = 33
number of values greater than of equal to 12 is 1. value = 12 * 1 = 12
number of values greater than or equal to 10 is 5. value = 10 * 5 = 50
number of values greater than or equal to 11 is 3. value = 11 * 3 = 33
The max value is 50.
for prefix 1 to 6number of values greater than or equal to 10 is 6. value = 10 * 6 = 60
number of values greater than of equal to 11 is 4. value = 11 * 4 = 44
number of values greater than of equal to 12 is 2. value = 12 * 2 = 24
number of values greater than or equal to 10 is 6. value = 10 * 6 = 60
number of values greater than or equal to 11 is 4. value = 11 * 4 = 44
number of values greater than or equal to 50 is 1. value = 50 * 1 = 50
The max value is 60.
for prefix 1 to 7number of values greater than or equal to 10 is 7. value = 10 * 7 = 70
number of values greater than of equal to 11 is 5. value = 11 * 5 = 55
number of values greater than of equal to 12 is 3. value = 12 * 3 = 36
number of values greater than or equal to 10 is 7. value = 10 * 7 = 70
number of values greater than or equal to 11 is 5. value = 11 * 5 = 55
number of values greater than or equal to 50 is 2. value = 50 * 2 = 100
number of values greater than or equal to 70 is 1. value = 70 * 1 = 70
The max value is 100.
I believe this can be solved using segment tree. Solutions without using segment tree will be also interesting. But want to know how to solve using segment tree.
Is your approach, calculate value at an index i and compare the value with the previous ans?
But the element at this index can affect ans calculated before.
That's interesting then. Share your solution if you'll solve it.
Sounds like Li-Chao tree under Problem 2 section.
I think the following idea works: maintain a fenwick tree on values initialized with 0. now iterate from left to right at each index the value for this index is prefix_sum of (A[i]-1)*(A[i]) this prefix sum can be calculated in logn time with fenwick tree After processing this index add 1 to A[i] in fenwick tree Now problem boils down to basic prefix minimum queries which can be solved easily.
Is your approach, fenwick tree to calculate the number of elements greater than or equal to A[i] (lets call this count as c) and multiply it with A[i]?
lets assume we updated the fenwick tree and calculated the answer for prefix ending at i. for the prefix i+1, if we add the A[i+1] in the tree, it will change the "c" of elements less than or equal to A[i+1] and that will change the value c*A[j] (j from 0 to i, where A[j] <= A[i+1]). In that case, we should calculate again for prefix ending at i.
wouldn't it lead to n^2 solution?
I am sorry I misinterpreted the question
I apologize
What are the constraints on N and the values in A?
constraints:
1 <= N <= 150000
1 <= A[i] <= 1e9