The parity (whether $$$n$$$ is odd or even) matters.
Swapping two adjacent cats keeps both of them close to their original location and changes both of their locations.
If $$$n$$$ is even, the optimal distance is $$$n$$$, and if $$$n$$$ is odd the optimal distance is $$$n+1$$$.
$$$i+j \leq 2 \cdot n$$$
The number of pairs $$$(a, b)$$$ such that $$$a \cdot b \leq x$$$ is $$$O(x log x)$$$.
If $$$n$$$ is even, the optimal distance is $$$n$$$, and if $$$n$$$ is odd the optimal distance is $$$n+1$$$.
What's the minimum value that an edge from $$$a$$$ to $$$b$$$ can be?
Use edges with negative value whenever you can.
The sum of the values of edges with positive weight must be $$$\geq$$$ the maximum value in the array.
Fix the initial node chosen and root the tree there, what is the contribution of each pair of nodes?
Nothing matters besides the path from node $$$a$$$ to node $$$b$$$, and the initially chosen node $$$r$$$.
You are given two stacks of size $$$a_1$$$ and $$$a_2$$$. In a single step, you randomly choose a stack to remove a single item from. What is the probability that $$$a_1$$$ becomes $$$0$$$ before $$$a_2$$$? Can you extend this argument for an arbitrary probability $$$p$$$ to remove from one of the two stacks, and a probability of $$$1-2p$$$ to do nothing?
1540C2 - Converging Array (Hard Version)
What are the invariants? The monovariants?
What is the prefix sum array of the converged array? What is the difference array of the converged array?
What if you conducted the process on a prefix of size $$$1$$$, then of size $$$2$$$, and on and on?
How many values $$$x$$$ are actually interesting?
The answer is always unique.
Try to figure out what the location of the $$$i$$$-th element would be if you only looked at the first $$$i$$$ elements, then the first $$$i+1$$$, etc. to find an $$$O(nq)$$$ solution.
Use sqrt decomposition to optimize it.
How much does each chef's initial dish contribute at time $$$k$$$?
What if you really, really wanted to use matrix exponentiation?
How can you multiply some vectors by a matrix in $$$O(N)$$$ time? (go back to linear algebra class)
Decompose into linear combinations of eigenvectors.