ICPC 2019 - Online Preliminary Problem Set Analysis
ICPC 2019 - Online Preliminary Problem Set Analysis
Analysis:
We need to check for each substring of length |P| in S whether it matches with P or not
efficiently. K is very small here, so we can get the lcp of P and the substring in S using suffix
array or hashing. When the next mismatch occurs increase a count and repeat. Since K is
small, we can break this loop whenever this count exceeds K and the overall complexity
becomes: O(|S|-|P| * K * log(|P|))
There is another divide and conquer solution which can pass with some optimizations.
Another solution is possible with FFT and that would work for any K, but in practice FFT is
quite slow. So there is a high chance such solutions will time out.
C. ICGeSi Standings
Problem Setter: Mohammad Ashraful Islam
Tester: Tanzir Islam
Alternate writer: Sourav Sen Tonmoy
Category: Greedy, Implementation, brute force
Expected no. of AC: 150+
Analysis:
Given three information:
1) Current ranklist
2) Submission details of each team.
3) Final ranklist
D. ICPC Standings
Problem Setter: Mohammad Ashraful Islam
Tester: Tanzir Islam
Alternate writer: Sourav Sen Tonmoy
Category: parsing + dp + sliding window
Expected no. of AC: 2-4
Analysis:
You can use stringstream for parsing which would make life a lot easier. After doing the
tiresome parsing, now you have a problem that can be solved with dp along with sliding
window technique. The pseudo code for getWay() function is given below:
Analysis:
N
Any string S will be a palindrome only if S i = S N −i+1 for (1 ≤ i ≤ 2). So every position is part of
a pair where the partner of any position i is (N-i+1). Let’s name this pair as palinpair where
palinpair(i, j) is basically the string that contains the characters at the positions i and j.
The order of the characters doesn’t matter. So we sorted them for simplicity in our solution.
So how many different values the palinpairs can produce? There can be a maximum of 5
distinct characters in our string. So there can be total 10 palinpair values where the
characters are not equal, which are ab, ac, ad, ae, bc, bd, be, cd, ce and de.
So before going to the original solution, let’s count the frequency of each palinpairs. Let’s
consider a map freq where freq[pr] = the frequency of palnpair value pr.
If we are able to apply our above observations correctly, then we are now only left with few
painpairs having frequency exactly one. And in every operation, we have a choice to make a
swap operation between the characters of any two of these palinpairs. Our target is to make
the characters equal for each of them.
This can be solved using a simple bitmask dp approach. Our DP state is just a mask where
the i’th bit is marked one only if the characters of the i’th palinpair value is not the same.
And DP[mask] defines the minimum number of swaps required to make mask = 0. Which
basically means, making the characters same in all possible palinpair values.
In every state, we can select any two palinpairs and make a swap between any two
characters of those palinpair values. And we always keep storing the optimal result from all
those states.
Note: For odd palindromes you also have to handle the case where the character at the
middle position can also be part of a swap operation.
F. Elevators
Problem Setter: Abdullah Al Munim
Tester: Tarango Khan
Alternate writer: Imran Bin Azad
Category: Ad-hoc
Expected no. of AC: 20+
Analysis:
Notice that we can solve the problem separately for each jump. Alice should make the jump
the first time she can.
Also observe that Alice can make the jump within max(2*h[i], 2*h[i+1]) time, as the two
elevators will be at the same level at least once in this time. We can get the time by some
case analysis.
G. Pairs Forming GCD
Problem Setter: Sourav Sen Tonmoy
Tester: Abdullah Al Munim
Alternate writer: Md. Shafiul Islam
Category: Number Theory
Expected no. of AC: 80+
Analysis:
Firstly, given N and G, let's talk about how can we compute number of pairs (X, Y) (1 ≤ X ≤ Y
≤ N) where GCD(X, Y) is equal to G. Essentially, we are looking for all (AG, BG) pairs where
(1 ≤ AG ≤ BG ≤ N), and (A, B) are co-primes.
For every B in range [1, N/G] number of A values is φ(B). We can precompute Euler's
totient values. And then number of pairs of integers (X, Y) (1 ≤ X ≤ Y ≤ N), for given N and G,
where GCD(X, Y) is equal to G, will be cumulative sum of totients till N
/G.
For a given N, it can be proved that, the number of such pairs will be non-increasing for
increasing values of G. Hence, we can apply binary search to find the maximum G, where
number of pairs is greater or equal to P.
Analysis:
This problem had two solutions, an O(1) inequality solving approach and O(log(N)) binary
search approach.
For the O(1) approach, if X bats first, assume that X scores runx in first innings and it is
optimal to assume y scores 0 runs and loses 10 wickets in the 2nd innings. Then you just
need to formulate the inequality where x has a higher NRR and solve it to get the value of
runx.
If Y bats first, assume that Y scores runy in the first innings and it is optimal to assume x
scores (runy + 7) runs to win in the 2nd innings in 0 balls. [ Just assume that team Y keeps
on bowling no-balls and when X and Y have equal score, X scores 7 runs in the next no
ball]. Then formulate the inequality and solve for runy.
For the 2nd approach, you can binary search on the score of the first innings and see if X
can still qualify from there. It is easy to do the binary search when x is batting first. When Y is
batting first, depending on the input, the binary property can be of two shape. But for one of
those shapes, the answer is always either 0 or -1. So handle the 0 case, and if you can't find
a solution for the 0 case, then do a binary search for the other shape. Next thing to note is,
you will face precision error if you use double or even long double since the answer can be
more than 1013. So you need to do integer calculation, but intermediate calculation is going
to cause overflow for long long! So use 128 bit integers to escape these errors!
It can be proved for the given limits, 128 bit integers would always suffice.
To understand why there can be two shapes of the binary property, you need to analyze the
original inequality. And if you can analyze the original inequality, you'd probably prefer to
solve it using the O(1) approach.
N.B. For O(1) approach calculation with long long is enough. 128 bit is only needed for the
binary search approach.
Analysis:
This was considered the hardest problem of the contest as any kinds of spelling mistakes,
forgetting newline or wrong case of characters lead to WA.
Analysis: Second hardest problem in the set. As we are given all the proper divisors of N, we
need to find the minimum and the maximum of those. Then answer will be min*max.