Codeforces Round #823 — editorial
Codeforces Round #823 — editorial
1730A - Planets
To solve the problem, it was enough to count the number of planets with the same orbits and sum up the answers for the orbits separately. For one orbit, it is advantageous either to use the second machine once and get the cost , or to use only the first one and get the cost equal to .
Jury solution: 173498496
1730B - Meeting on the Line
There are many solutions to this problem, here are 2 of them.
1) Let people be able to meet in time , then they could meet in time , where . So we can find by binary search. It remains to learn how to check whether people can meet for a specific time . To do this, for the -th person, find a segment of positions that he can get to in time : if then this segment is empty, otherwise it is . Then people will be able to meet only if these segments intersect, that is, the minimum of the right borders is greater than or equal to the maximum of the left borders. In order to find the answer by the minimum , you need to intersect these segments in the same way (should get a point, but due to accuracy, most likely, a segment of a very small length will turn out) and take any point of this intersection. Asymptotics is .
2) If all were equal to , then this would be a classical problem, the solution of which would be to find the average of the minimum and maximum coordinates of people. We can reduce our problem to this one if we replace the person (, ) with two people: (, ) and (, ). Proof. Let the meeting be at the point . Let . Then this person will need of time to get to her, and the two we want to replace him with — and . it is easy to see that the first value is equal to the initial value, and the second is not greater than the initial value, then the maximum of these two values is equal to the initial value. The proof is similar for the case . Then for any meeting point, the time in the original problem and after the replacement does not differ, which means that such a replacement will not change the answer and it can be done. Asymptotics is .
binary search: 173497901
classic: 173497940
1730C - Minimum Notation
We leave all suffix minimums by the digits (digits less than or equal to the minimum among the digits to the right of them), remove the rest and replace them with (using the described operations) and add to lexicographically minimum order on the right (due to the appropriate order of operations, this is possible). The suffix minimums should be left, because no matter what digit we leave after , it will be no less than , and therefore will not improve the answer. The rest must be removed at the end with operations, since there is a number to the right less than this one, i.e. if you remove everything before it (put at the current position), the answer will become less than if you leave another digit at this position.
Jury solution: 173498569
1730D - Prefixes and Suffixes
If you reflect the second string and see what happens, it is easy to see that the elements at the same positions in both strings after any action remain at the same positions relative to each other. Let's combine them into unsorted pairs and treat these pairs as single objects. Now we need to compose a palindrome from these objects. This is always possible with the help of these actions, if there is a palindrome consisting of these objects (pay attention to odd palindromes, there must be a pair of the form (a, a) in the center).
Proof of possibility:
Let's make an array of pairs, in one action we expand some prefix of this array and the elements in the pairs of this prefix are swapped. Let's prove that we can change the order of the pairs in the array as we like. We will build from the end. Let all the pairs after position already stand as we want, and now the pair that we want to place in position at position . Let's do the following:
— will move the pair from position to the beginning.
— swap elements within a pair if needed (so pairs are considered unsorted).
— move the pair from the beginning to position .
(* the action is optional if you don't want to change the order of the elements in the pair)
With this construction, we can get any permutation of these pairs and a palindrome, if it is possible. If you divide the final palindrome into two strings and expand the second one back, you get the first string.
Example:
From the test suite from the condition:
, , expanded .
Couples:
, , , , .
Pairs unordered:
, , .
Pairs in a palindrome:
, , , , .
Real couples:
, , , , .
Strings: expanded , .
!!! The pair !!!
Jury solution: 173498668
1730E - Maximums and Minimums
Let's introduce some new variables: - the position of the nearest left greater or equal than ( if there is none). - position of the nearest right greater than ( if there is none). - position of the nearest left lower than ( if there is none). - position of the nearest right lower than ( if there is none). All this can be calculated, for example, using a stack in or using binary search and sparse table in
Let's iterate over the position of the leftmost maximum of the good segment. Then the -th element will be the maximum on the segment [l, r] if and . For the segment to pass the test, the minimum must be a divisor of the maximum. Let's iterate over this divisor and find the number of segments where the maximum is and the minimum is . Consider positions of occurrence of and — the nearest left and right to (they can be found using two pointers). Let's find the number of segments satisfying the condition , in which the element is a minimum. To do this, similar conditions must be added to : and . Intersecting these conditions, we obtain independent segments of admissible values of the left and right boundaries of the desired segment. Multiplying their lengths, we get the number of required segments. Similarly, the number of segments satisfying , in which is the minimum, is found, but in order not to count 2 times one segment, one more condition must be added: . The sum of these quantities over all and divisors of will give the answer to the problem.
To enumerate divisors, it is better to precompute the divisors of all numbers in , where is the constraint on . So the whole solution runs in , where is the maximum number of divisors.
1730F - Almost Sorted
Let's build a permutation from left to right. If the current prefix contains the number , let's call the element used, otherwise — unused. Consider the smallest unused element . All elements greater than must also be unused, and all elements less than must be used. Then the current state can be described by the number and the mask, -th bit of which indicates whether the element with the value is used.
Let's solve the problem by dynamic programming: is the minimum number of inversions. We can continue building a permutation by adding the number such that and hasn't been used yet. New inversions can be divided into two types: those formed with indices of elements less than (they can be counted using Fenwick tree) and those formed with indices of elements not less than (but their number is not more than ).
The time complexity is .
Comments
Post a Comment