dragonslayerintraining's blog

By dragonslayerintraining, 5 weeks ago, In English,

1209A — Paint the Numbers

Author: MikeMirzayanov

Consider the smallest element $$$x$$$ in the array. We need to paint it in some color, right?

Observe, that we can paint all elements divisible by $$$x$$$ in that color as well.

So we can perform the following while the array is not empty:

  • find the minimum element $$$x$$$,
  • assign new color and remove all elements divisible by $$$x$$$

Complexity: $$$\mathcal{O}(n^2)$$$.

1209B — Koala and Lights

Author: FieryPhoenix

Because each individual light flashes periodically, all the lights together are periodic as well.

Therefore, we can simulate the lights up to the period to get the answer.

The actual period can be calculated as follows:

Spoiler

However, computing the actual period is not necessary and a very large number will work (like $$$1000$$$).

Challenge: Can we bound the time even more?

1209C — Paint the Digits

Author: MikeMirzayanov

The sequence must split into two non-decreasing where the end of the first $$$\le$$$ start of the second.

Let's bruteforce the value $$$x$$$, so that all elements $$$< x$$$ go to the color $$$1$$$, all elements $$$> x$$$ go to the color $$$2$$$, and for $$$=x$$$ we are not sure.

Actually, we can say that all elements equal to $$$x$$$, which go before the first element $$$> x$$$ сan safely go to the color $$$2$$$, while the rest can only go to the color $$$1$$$.

So we colored our sequence and we now only need to check whether this coloring is fine.

Complexity is $$$10 \cdot n$$$.

1209D — Cow and Snacks

Author: FieryPhoenix

Since every animal has exactly two favorite snacks, this hints that we should model the problem as a graph. The nodes are the snacks, and the edges are animals with preferences connecting snack nodes.

Let's consider a connected component of the graph with size greater than $$$1$$$. The first animal (edge) in that component to eat must take two snacks (nodes), all other snack nodes will be eaten by exactly one animal edge. It is always possible to find an order so that no other animals takes two snacks (for example, BFS order). Thus, a connected component with $$$c$$$ vertices can satisfy at most $$$c-1$$$ animals.

Let $$$N$$$ be the number of snacks, $$$M$$$ be the number of animals, and $$$C$$$ be the number of connected components (including those of size 1). The number of satisfied animals is $$$N-C$$$, so the number of of unhappy animals is $$$M-(N-C)$$$.

Complexity: $$$\mathcal{O}(n+m)$$$

1209E1 — Rotate Columns (easy version)

Authors: MikeMirzayanov and _kun_

There many approaches possible, let's describe one of them.

Let's change the problem to the following:

  • Rotate columns any way you want.
  • Select in each row one value, maximizing the sum.

This can be done with a dp, states are (prefix of columns, mask of taken columns). Basically at each step we are rotating the current column and fixing values for some of the rows.

The most simple way to write this makes $$$3^n \cdot m \cdot n^2$$$ (for every submask->mask transition iterate over all possible shifts and elements to consider in cost function).

But if we precalculate the cost function in advance, we will have $$$\mathcal{O}((3^n + 2^n n^2) \cdot m)$$$.

1209E2 — Rotate Columns (hard version)

The previous solution is slightly too slow to pass the large constraints.

Let's sort columns by the maximum element in them. Observe, that it is surely unoptimal to use columns which go after first $$$n$$$ columns in sorted order (we could've replaced them with some unused column).

So we can solve the hard version with previous solution in which we consider only best $$$n$$$ columns.

Complexity $$$\mathcal{O}((3^n + 2^n \cdot n^2) \cdot n + T(sort))$$$

1209F — Koala and Notebook

Author: dragonslayerintraining

First split all edges into directed edges with single digit labels, creating $$$\mathcal{O}(m\log{m})$$$ dummy vertices if necessary.

Since the first edge will not be zero (no leading zeros), longer paths are always greater. With a BFS, this reduces the problem to finding lexicographical minimal paths in a DAG.

To avoid needing to compare long sequences, we will instead visit all the vertices in order by their lexicographical minimal path. This can be done efficiently by something like BFS/DFS.

The main idea is to visit sets of vertices at a time. If we have a set of vertices whose minimal paths are $$$P$$$, we can find the set of vertices whose minimal paths are $$$P0$$$ by following all outgoing $$$0$$$ edges. Then, we find the set of vertices whose minimal paths are $$$P1$$$ by following all outgoing $$$1$$$ edges, and so on for all digits. Since we ignore vertices once they are visited, this is $$$\mathcal{O}(m\log{m})$$$

1209G1 — Into Blocks (easy version)

Author: Errichto

Let's solve easier version first (we will reuse core ideas in hard version as well).

Clearly, if some two integers are ``hooked'' like in $$$1 \ldots 2 \ldots 1 \ldots 2$$$, then they will end up being turned into the same integer.

So when we see integer $$$x$$$ with first occurrence at $$$a$$$ and last at $$$b$$$, let's mark segment $$$[a; b]$$$ as blocked. E.g. for array $$$[3, 3, 1, 2, 1, 2]$$$ we have not blocked only the bar between $$$3$$$ and $$$1$$$, that is we have $$$|3, 3 | 1, 2, 1, 2|$$$.

Now for every such segment we have to change all elements into a common element. So the answer for a segment is segment length minus the number of occurrences of the most frequent element.

One easy implementation is as follows: blocking is "+= 1 on segment" (can be done easily in $$$\mathcal{O}{(n + operations)}$$$ in offline, then for an element $$$x$$$, put the number of it's occurrences in the position of first occurrences.

Now we only need to compute max on disjoint segments, so it can be done in a naive way.

Complexity: $$$\mathcal{O}(n)$$$.

1209G2 — Into Blocks (hard version)

To adjust the solution for many queries we need to create some sophisticated data structure.

E.g. we all know that mentioned above "+= 1 on a segment" is easily done with a segtree. If we maintain for every value $$$a_i$$$ the corresponding set of occurrences, it's easy to update mentioned above ``number of occurrences in the first position''.

So what we need to do now? We need to dynamically recalculate the sum of minimums (and the set segments to calculate minimum can change quite much due to updates).

You probably also now that we can design a segtree which supports range increments and query (minimum, number of minimums) on the segment. In a similar way we can build a structure which returns (minimum, number of minimums, the sum of largest stored counts between minimums). Just maintain a few values in each node and do lazy propagation.

Complexity $$$\mathcal{O}(q \log n)$$$.

1209H — Moving Walkways

Author: Errichto

Some minor tips:

  • Everything is a walkway. When there is no walkway, it is a walkway of speed $$$0$$$.
  • You can increase all speeds by $$$1$$$ and assume that you own speed is in $$$[-1; +1]$$$
  • Energy is an entity which is $$$\delta$$$ speed $$$\times$$$ time, which is distance.

Also if you spend $$$x$$$ energy per segment of len $$$l$$$ and speed $$$v$$$, it is not important how exactly you will distribute it over the walking process. In any way, you will walk by feet $$$l - x$$$ meters in time $$$(l - x) / v$$$.

So it turns out it's better to distribute more energy to low-speeded walkways (because the denominator is smaller).

Assume that you (by default) save up all energy on any non-feet path (for feet path it's always optimal to walk with speed $$$\ge 1$$$ ($$$\ge 0$$$ after speeds hack), so now save up's).

Build an energy graphic where the Ox axis will correspond to the point you are in (not time). It will be a piecewise linear function, so it is enough to store it's value only in points corresponding to points between walkways. Iterate over walkways in the order of speed and try to steal as much energy as possible to the current walkway.

What are the limits of stealing energy?

  • there is a restriction based on $$$l$$$ and $$$v$$$ (if you take too much energy, you wouldn't be able to fully walk it up)
  • the graphic must still be able above $$$0$$$ at all points.

The latter condition is just a suffix minima on a segment tree.

Complexity: $$$\mathcal{O}(n \log n)$$$.

Read more »

 
 
 
 
  • Vote: I like it
  • +124
  • Vote: I do not like it

By dragonslayerintraining, history, 4 months ago, In English,

Question

Is there a data structure that supports $$$n$$$ of the following operations in $$$O(n\log{n})$$$?

  1. Create a set containing a single integer in the range $$$[1,n]$$$.
  2. Create the union of two sets, destroying the original sets in the process. The original sets may have overlapping ranges.
  3. For any integer $$$x$$$ and a set, split the set into two sets: one with all elements less than $$$x$$$ and one with all elements greater than or equal to $$$x$$$.
  4. For any integer $$$x$$$ and a set, add $$$x$$$ to all elements in the set. This operation is only allowed if all elements will remain in the range $$$[1,n]$$$.
  5. Count the number of elements in a set.

Partial Solutions

If operation 2 required the ranges to be disjoint, any balanced BST that supports split and join in $$$O(\log{n})$$$ will work.

Without operation 3, join-based tree algorithms will work since they support union in $$$O(m\log{\frac{n}{m}+1})$$$.

(Note that this is better than small to large, which takes $$$O(n\log^2{n})$$$ time total.)

Without operation 4, a version of segment trees will work.

Full Solution

I was able to prove that binary search trees will solve the problem in $$$O(n\log^2{n})$$$. (Was this "well known" before?)

If we merge using split and join, this bound is tight. A set can be constructed by merging the set of even-index element and odd-index elements, which will take $$$O(n\log{n})$$$. Those two sequences could have been constructed the same way recursively. This will take $$$O(n\log^2{n})$$$ time for $$$O(n)$$$ operations.

However, I conjecture that if we use join-based tree algorithms, it will actually be $$$O(n\log{n})$$$ in the worst case. (For example, in the case above, merges take $$$O(n)$$$.)

Can anyone prove or disprove this conjecture for any join-based tree algorithm?

If anyone can prove it for splay trees instead, or have another solution, I would also be interested.

Thanks.

Read more »

 
 
 
 
  • Vote: I like it
  • +88
  • Vote: I do not like it