Codeforces VP / Replenishment small note (continuous fill hole)

Codeforces VP / Replenishment small note

1149 C. Tree Generator

Give you a tree parentheses sequence, each exchange two brackets, after maintaining the diameter of each exchange.

Consider the sequence brackets and maintain the tree path information, is considered as the left parenthesis \ (- 1 \) , the right bracket seen \ (1 \) , then the upward vertical path section can be represented as a sequence segment and bracket , a section of vertical downward path sequence can be seen as a bracket section and the opposite number. We want to maintain the diameter of the tree, which is followed by a period of continuous and subtract followed by a period of continuous bad. Specifically, that
\ [\ max _ {\ forall
[l, r]} \ {\ sum_ {i = l} ^ kw [i] - \ sum_ {i = k + 1} ^ rw [i] \} \] simply can not prove if the selected interval represents the path of the tree, take to the equation and certainly not the optimal solution, this formula to maintain the tree line. Maintenance is a little trouble, like maintenance intervals and the largest sub-segment, when the need to update several multi-record information. Complexity \ (\ mathcal O (n-\ log n-) \) .

1149 D. Abandoning Roads

\ (n-\) points \ (m \) edges and only two right side \ (a, b (a < b) \) is a simple graph \ (G \) , \ (\ FORALL I \ ) obtains \ (G \) all the minimum spanning tree, \ (. 1-I \) minimum path length. \ (. 1 \ n-Leq \ Leq 70,. 1-n-\ Leq m \ Leq 200 is \) .

The question I began to think a wrong approach, first find the minimum spanning tree \ (b \) the number of edges denoted \ (k \) , and then were determined \ (1 \) to various points through not than \ (K \) Article \ (B \) shortest side. Wrong with this approach is that, although all can be shown on the starting point on the path \ (A \) side of the ring does not cut the path \ (B \) side, but so long as the endpoint will be able to start on the path the circumcision.

I also consider another during a practice, and later learned that an idea can be extended to a positive solution. Consider all \ (A \) side Unicom shrunk several blocks, not difficult to prove, if a spanning tree path after leaving a block Unicom Unicom into this block, not necessarily minimum spanning tree spanning tree, since ring which is selected will be cut \ (B \) side. At this time, like the strong direct compression \ (DP \) complexity is \ (O (2 ^ nm) \) a.

Further down analysis, if a Unicom block size \ (\ Leq 3 \) , even if there is to leave the block and then came to China Unicom China Unicom block this path, this path is also not necessarily the shortest, because it blocks the internal Unicom the longest side is \ (. 2A \) , do the above-described operation requires \ (2B \) . It can be ignored fall directly size \ (\ leq 3 \) restriction Unicom block, the complexity of the optimization \ (\ mathcal O (2 ^ {\ {n-FRAC. 4} {m}}) \) .

1168 C. And Reachability

Has a length \ (n-\) sequence \ (\ A) , \ (I, J \) there between when there is a directed edge and only if \ (i <j, A [ i] \ text {and} A [J]> 0 \) , each given \ (X, Y (X <Y) \) , from the inquiry \ (X \) whether the departure can reach \ (Y \) .

Bitwise consideration, the current number \ (A [X] \) , which can directly reach all the numbers which have a common position and back. Obviously, for each bit, the number of front to reach more than the number of the latter, it is only necessary to maintain every number \ (A [x] \) which has to reach a certain \ (1 \) foremost Number location. Finally, whether \ (A [y] \) of a bit \ (1 \) if the can \ (A [x] \) reaches its previous. Complexity \ (\ mathcal O (n-\ log ^ n-2) \) .

1168 D. Anagram Paths

There are a binary tree, each side has a lowercase letter or \ ( '?' \) , Each character on the edge of a modification, required to maintain all the leaves on the path to the root \ ( '?' \) To fill any lowercase if you can rearrange the letters to reach agreement, if possible, the need to output each character of the maximum possible number of occurrences.

First, if all the leaves are not equal depth, no matter how to change the side characters are no solution.

Then allowed \ (f [x] [c ] \) represents all \ (X \) leaves in the subtree to \ (X \) characters on a path \ (C \) the maximum number, \ (len [ x] \) represents \ (x \) from the leaves to.

We can generalize proved capable of rearranging to achieve consistent iff


\ [\ Forall x \ sum_ {
c} f [x] [c] \ leq len [x] \] consider when \ (X \) is a leaf when clearly necessary and sufficient. For any non-leaf nodes \ (X \) , assuming its subtree all points satisfy the condition, this time the need clearly. First consider the adequacy of the original line up all the paths, add to the mix for the new side, if it is one, it will definitely legitimate. If two edges, must be changed to satisfy the expression \ (f [x] [c ] \) Number \ (<2 \) , then there will be more out of the other side of the question mark character fill.

Direct violence maintenance \ (f [x] [c ] \) complexity is \ (O (nq) \) , but previously shown inspired us, with only one child node, the node can be incorporated directly with children It does not affect the answer. There is a small routine then binary tree, and if so combined, the binary tree no deeper than \ (\ mathcal O (\ sqrt n-) \) .

Proved that if only one son node binary tree does not exist, then the first \ (I \) depth layer must be larger than \ (i-1 \) depth layer, in which case up to a depth of \ (\ mathcal O (\ n-sqrt) \) , so that direct violence modify \ (DP \) values to, the complexity of the \ (\ mathcal O (Q \ n-sqrt) \) . Of course, direct dynamic \ (dp \) is also possible.

1188 D. Make Equal

A sequence \ (A \) , each may take \ (1 \) cost in which a number plus \ (2 \) required for the power of all the number becomes equal to the required minimum number of consideration .

First \ (a \) from small to large, if we will \ (a_n \) finally added \ (S \) , then \ (a_i \) finally added \ (S + a_n-a_i \ ) . Then the problem is transformed into a selected number \ (S \) , to minimize \ (\ sum_} ^ {n-I 'bit. 1 = (S + A_N-a_i) \) .

Consider Bitwise \ (dp \) decision \ (S \) in every election \ (0 \) or to vote \ (1 \) , because the final contribution to be processed is equal to \ (a_n-a_i, S \ ) the median sum and carry on a decision, we also need to record what the number is rounded up in its current state. Can be found, assuming that take into account before \ (k \) position, the current number of bits carry must be in accordance with the \ (\ mod 2 ^ k \ ) suffix under the sorting results, \ (dp \) recorded at the time of the suffix to the length, complexity \ (\ mathcal O (n-\ log n-) \) .

Guess you like

Origin www.cnblogs.com/mangoyang/p/11482903.html