theoneandonlytron's blog

By theoneandonlytron, history, 2 days ago, In English

This blog documents my learning journey in competitive programming. I hope someone enjoys it and finds it helpful for their own learning.

Credit to Codeforces for the AMAZING problems, ChatGPT for helping me derive proofs, and CP-31 for more amazing problems!

Theory

Before reading this blog, I would recommend knowing basic DSA, USACO Silver topics, and some Gold topics from usaco.guide. There isn't really any specific knowledge you need; it's mostly about finding observations.

Problems

2258B2

The first observation is that if the number of operations we can apply is greater than log2(m), then we can turn everything into ones by repeatedly applying operations with 2^k, 2^(k-1), 2^(k-2), ..., 2^0.

So, we only need to calculate the maximum answer for operation counts below log2(m).

Let's say the maximum number of copies occurs at a number x, using k operations, where k < log2(m).

The optimal strategy to get the maximum number of copies of x is to apply operations with x * 2^k, x * 2^(k-1), .... This is because we shouldn't use 2^(k+1): we would still need to turn x * 2^(k+1) into copies of x, which would require more than k operations and shouldn't be optimal.

For a number y, we can turn it into y / x copies of x if y <= x * (2 * (k + 1) - 1). It could also be 2^(k+1) * x, because the last remaining number becomes x after all the operations have been applied.

So, we can iterate through all multiples of x: x, 2x, 3x, ..., for every number x. The complexity of this part should be O(n log n).

Therefore, the overall complexity of our algorithm would be O(n log² n).


2249B

This problem mainly relies on considering the maximum value of the permutation.

Let's say the maximum value occurs at position i. Then the first i-1 numbers should be the prefix maxima, and the last n-i numbers should be the suffix maxima.

Prefix maxima must be increasing, and suffix maxima must be decreasing. So, the maximum can be at position i if and only if the first i-1 numbers are increasing and the rest are decreasing.

Another observation is that all equal numbers must form a contiguous subarray. For example:

111444555777...

This happens because once a prefix maximum changes, it cannot change back, causing equal numbers to stay next to each other. We can add another check to verify this beforehand.

If equal numbers form contiguous groups, then a number x appearing in positions 0 through j cannot also appear in positions j+1 through n. So, the second condition is a[i] != a[i+1].

Now we have the conditions for possible positions i. The position of i is relative, meaning that the number of ways to redistribute the other n-1 elements does not depend on i. So, we can count the ways to distribute those n-1 elements by processing them from smallest to largest.


2242D

I didn't figure this problem out, but the idea is really good.

If we build prefix sums of the digits in each number, two numbers are the same if and only if their prefix sums are equal. When we merge two digits, we are just deleting the prefix sum at position i. We cannot delete the last position.

So, we want to retain as many prefix positions as possible. We just need to find the LCS between the two arrays.

We can use dp[i][j] to represent the maximum LCS length after considering the first i positions of the second number and the first j positions of the first number.

The main observation is to change our perspective from the moving parts to some non-moving parts that we can work with.

If that doesn't work, there might be a greedy way to approach the problem and turn it into DP.


2237D

This was a problem I couldn't figure out, but after taking a quick peek at a hint, I figured out some of it.

First, we can turn all characters into one group if there are two equal adjacent characters. We can prove this as follows.

Let a and b represent two different characters.

Consider a subsegment of the form aaa...b. We can turn pairs of a into b, working from right to left.

If the number of a characters is odd, we end up with something like abb. There must be at least two b characters because the original run must contain at least three a characters: its length is odd, and it contains an equal adjacent pair.

Then we can do:

abb -> aa -> b

Since there is already a segment of b characters next to it, we can merge the b segments together, giving us at least two characters. If there is no b segment, we can instead turn the b into an a using the a segment on the left.

But this is not enough. The second idea is to assign a weight to each character.

Assign weight 2 to 1 and weight 1 to 0. Then, after every operation, the total weight modulo 3 stays unchanged, which is amazing.

If the total weight is 0 modulo 3, reducing the string to one character is impossible, because we would eventually need to merge a 0 and a 1, which is not allowed.

So, we can prove that whenever the total weight is nonzero modulo 3, we can turn the string into one character.

Consider the contiguous group obtained after merging everything into one group. Let its character be a.

If its length is 1 modulo 3, we can turn it into a string of the form:

babababab...a

Each ba uses three original characters, so we eventually end up with aa and can repeatedly reduce everything to one character.

If its length is 2 modulo 3, we can turn it into a string of the form:

abababa...b

And we can do the same thing.


2245D1

This problem is fun, ngl. I didn't think my idea was enough lol :))

The key is to consider the node with the maximum absolute value. Let's call that node u.

  • If node u is positive, then ALL edges from node u to node v must be NONNEGATIVE.
  • If node u is negative, then ALL edges from node u to node v must be NEGATIVE.

If two nodes u and v have the same maximum absolute value but different signs, the positive one should be processed first because -a + a = 0.

This gives us an approach: repeatedly take a node whose remaining edges are all positive or all negative.

We can do this using Kahn's algorithm / leaf peeling, which is basically BFS, kind of.


2236F1

I couldn't figure this problem out, but it was great. Once you get the hint, you basically get the whole problem.

The key is to consider EACH PRIME SEPARATELY.

Why? Because if:

lcm(p1, p2, ..., pn) = p1 * p2 * ... * pn

Then a prime cannot appear in both pi and pj for i != j. Otherwise, the LCM would not retain all the copies counted in the product.

So, for each prime, we take the sum of its exponents across all ai. If one chosen element contains that prime, no other chosen element can contain it.

Then we add 1 for the case where we do not choose that prime at all.

The answer is the product of these values over all primes.


2223B

This problem is fun.

We can count the total number of inversions modulo MOD by going from 1 to n and counting how many products ai * j, for j from 1 to n, are greater than ai * y, for y from 1 to n.

We can use two types of sets: one to store all products ai * j, and another to store the products for each specific j, because j must not equal y.

Then we divide the answer by:

C(n, 2) * 2! = n * (n-1)

Because when we consider a pair ai, aj, we have n * (n-1) possible assignments.

But O(n² log n) with ordered sets is too slow because the overhead is HUGE.

We can optimize this using coordinate compression: sort the values and replace each value with its index. This is a cool trick, but it only works here because the values do not change.

Then we can replace the ordered sets with Fenwick trees.\

Thank you for reading! I hope you found something helpful in my learning journey :)

»
16 minutes ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by theoneandonlytron (previous revision, new revision, compare).