[BZOJ 1012] maxnumber, bzojmaxnumber
Description
Http://www.lydsy.com/JudgeOnline/problem.php? Id = 1012
Analysis
- Maintain the maximum suffix
Similar to brute-force solutions, A Array records the value, and maxv records the maximum value from the current position to the back. each time you maintain the maxv array in the forward and backward mode, if you encounter a value that does not need to be changed, that is, the newly added element has no effect on the value of maxv at this position, you can jump out of the loop, because the newly added element has no influence on the value before this position, this is the most important Optimization in this algorithm. however, I still think that this algorithm is only inefficient at data processing. In essence, it is still the O (n2) algorithm.
Monotonous Stack
There are two methods for monotonous Stack: one is to use lower_bound binary accelerated search in the STL Algorithm Library template while simplifying the code, and the other is to use and query sets.
First, building a monotonic decreasing stack does not mean that the elements in the stack are decreasing. we store the subscript of array elements in the stack, the element values represented by the subscript must be strictly monotonically reduced. every time you encounter operation A, you will try to insert the position subscript of the new element into the stack. Be sure to insert the new element regardless of the number of elements left at the end of the stack. in the Q operation, the first element greater than or equal to the query position is searched in the second part of the stack. because the array element corresponding to the element in the stack is decreasing, the array element corresponding to the element is the largest after it and that is what we are looking.
The second method is to establish a monotonic decreasing stack, which has the same meaning as above. use and query sets. p [x] = y indicates that array elements represented by x are larger than array elements represented by y, the final p [x] is the array subscript of the largest element behind x, which can be easily identified by querying the set.
- Line Segment tree
It is a common single-point modification, with the maximum range query value.
Summary
This is a problem with multiple solutions. Different solutions have different complexity and programming difficulty. here we mainly talk about two novel solutions, which are very difficult to compile. Just pay attention to the details. pay more attention to the AC solution using a simple data structure, and try to think differently.
Code
Maximum maintenance Suffix:
Https://code.csdn.net/snippets/607398
Monotonous stack-lower_bound:
Https://code.csdn.net/snippets/607393
Monotonous stack-and query set:
Https://code.csdn.net/snippets/607395