A: Shuffle Hashing
At the beginning, I didn't read the constraint clearly and thought that it is annoying.
There is a solution, where is the total string length. You can use sliding window and a frequency array to check whether the frequency of all characters are the same as the password.
For the constraints of this problem, it can be solved using brute force. Simply compare each substring of with the same length as using
multiset<char>. Then we have a solution of complexity , where is the length of one string.
B: A and B
This is the third problem I attempted. When I first read the problem, I didn't know how to approach it. I tried writing a greedy solution, but that didn't pass even the first test case. The greedy solution was something like this:
ll a, b, i; for (i = 1; a != b; i++) if (a < b) a += i; else b += i;
I noticed that you can select a subset of such that the sum is in . This means that we simply need to ensure that . We can binary search on to find the minimum .
Also, we need to increment until the parity of and are the same.
C: Berry Jam
I attempted this problem after finishing A, because I had no idea how to do B yet.
This problem is an interesting one. We can exhaust each left position, and find the right position closest to the middle such that removing the left part and the right part results in equal jars of strawberry and blueberry jam.
We can consider the parts to the left and to the right of the ladder separately. Lets call the jar of jam to the left of the ladder , and the jar of jam to the right of the ladder . Then we can define two arrays and , where
Then we know for a valid arrangement , where we eat jars of jam from the left and jars of jam from the right, we must have . We can simply simply store the smallest for each with a map, then simply query the index where .
D: Segment Tree
I could not solve this problem during the contest. I misread the question and didn't notice that one interval must not be completely in another, and wrote a greedy solution that, even if fixed, cannot run within the time limit.
E: Tests for problem D
I solved this problem within contest time. I drew the tree on a paper and wrote down the suggested solution, and tried to write a DFS applying the same strategy. It worked.
Edit: the solution was a TLE. Don't use
I tried to derive the formula. The formula is
I could not simplify the formula so it can be computed within the time limit.