Dhaka ICPC Preliminary 2021 Editorial
Dhaka ICPC Preliminary 2021 Editorial
Comment: (Continuing from Problem A) However, for this problem the issue was a bit different.
It had 200,000 lines of input, and many contestants submitted TLE solutions. Some resubmitted
their TLE solutions while they did not receive the verdict from the initial submission. Also we
suspect using endl causes some issue in Codemarshal. All these yielded a huge submission
queue. Initially we were thinking that it’s similar to the previous long queue issues, but the TLE
scenario made it more complicated and frustrating. After discussing a while and considering
many different options/scenarios we decided to reduce the data set and reduce the time limit.
This caused some side effects like rejudging a huge amount of solutions making the CM slow
for a moment but the queue cleared quite fast and we had a better contest experience.
1
Problem C: Bitonic Beaver
Author: Pritom Kundu
Contributor: Rafid Bin Mostofa, Md. Nafis Sadique
Analysis: A sequence is bitonic if and only if its smallest element is at the start or the end and
the sequence stays bitonic if the smallest element is removed. Or in other way, you can pick the
elements in zig-zag fashion, either from the front or back, how many ways you can do that.
It means all the sequences must have a common element at one of the ends of the array. So
let’s consider three cases.
Case 1: Let’s create a set of “the end of the array numbers” for all the given arrays. If they do
not have a common element then the answer is 0.
Case 2: Similarly if those sets have exactly one element in common then we can remove them
and call the counting subroutine for the remaining of the arrays.
Case 3: Since the number of elements of those sets is at most two, the remaining case is when
there are exactly two common elements across all the sets. This is a difficult case. First let’s
handle the easy subcase, when the remaining arrays are all equal. In such a case the answer is
2^(length of the arrays). Since each time we can take the smallest element from one end or the
other. If all the arrays are not equal, let’s first reverse some of them if necessary to make sure
the first element of all the arrays are the same. Now check the length of the common prefix and
length of the common suffix. Say they are P, S respectively. We can say that the resulting count
is equal to ncr(P + S, P) times the answer for the remaining portion of the arrays. Why?
Suppose you start taking elements from only prefix. After taking P elements, you are stuck.
Unless you take S elements from the suffix you can not take any element from the prefix end.
Similarly unless you take P elements from the prefix you can not take any element from the
suffix end. That means, you first need to take P and S elements from the ends, only then you
can approach the remaining arrays. Counting how many ways you can take P and S elements
from the end is an easy counting problem and this number is: ncr(P + S, P).
2
P = Number of nodes in which Squirrel will be happy
Q = Number of Node in that tree
G = GCD( P, Q)
So, Final result = ( P / G ) “/” ( Q / G )
Problem E: Pairdrome
Author: Aminul Haq
Contributor: Arghya Pal, Sabit Anwar Zahin, Md. Mahamudur Rahaman Sajib, Rafid Bin
Mostofa
Analysis: Let sub_a and sub_b be the substrings of the given input X and Y respectively. There
can be three cases, |sub_a| < |sub_b| or |sub_a| = |sub_b| or |sub_a| > |sub_b|. (Here |S|
denotes the length of any string S.)
Let's discuss for the case where |sub_a| < |sub_b|. If their concatenation is a palindrome, sub_a
definitely is a reverse of a suffix of sub_b, and the rest of sub_b is a palindrome. For example,
let sub_a = “xy”, sub_b = “zyzyx”, so here the reverse of sub_a (yx) is a suffix of sub_b and the
rest of sub_b “zyz” is a palindrome.
So we need to pre-calculate how many palindromic substrings have ended at each position of
string Y. This can be done by some palindrome algorithm (Manacher/palindromic tree etc).
After doing this, we have to count the matchings from sub_a with a suffix of sub_b and update
our answer, this can be solved with suffix array/automata along with another data structure
(depending on the implementation).
3
Problem G: Alice, Bob and Tree
Author: Mohammad Ashraful Islam
Contributor: Md. Muhiminul Islam Osim, S. M. Shaheen Sha
𝑁
𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒(𝑟𝑜𝑜𝑡, 𝑖)
Analysis: Given a tree and a formula ∑ ( (𝑊𝑟𝑜𝑜𝑡 − 𝑊𝑖) × (− 1) ).
𝑖=1
Observation 1: For a particular node 𝑖 we do NOT need to know the 𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒(𝑟𝑜𝑜𝑡, 𝑖) , we only
need to know whether the distance is odd or even.
Observation 2: Let’s bicolor the tree. That is, let’s color them with white and black color so that
adjacent pairs of nodes do not have the same color. After bicoloring, all the nodes with the same
color have even distance between them.
So, for a particular node X colored as WHITE, the formula can be rewritten as
𝑊ℎ𝑖𝑡𝑒 𝐶𝑜𝑢𝑛𝑡 𝐵𝑙𝑎𝑐𝑘 𝐶𝑜𝑢𝑛𝑡
((𝑊ℎ𝑖𝑡𝑒 𝐶𝑜𝑢𝑛𝑡 × 𝑊𝑋) − ∑ 𝑊𝑖 ) + ( ∑ 𝑊𝑖 − (𝐵𝑙𝑎𝑐𝑘 𝐶𝑜𝑢𝑛𝑡 × 𝑊𝑋) ).
𝑖=1 𝑖=1
Vice-versa for BLACK colored node.
For example L = 6
empty prefix chosen from S and T[0 : 5] is chosen as suffix from T
S = [] a b b a c a
T = [a a c b a a]
-------------------------
aacbaa
4
S = [a b] b a c a
T = a a [c b a a]
-------------------------
ab cbaa
Let’s generalize this solution, if prefix S[0 : i] is taken and T[j + 1 : Length(T) - 1] is taken as
suffix then if S[i] = T[j] then we won’t count the string as it is previously counted.
So the number of strings which are previously counted is equal to the number of (i, j) pairs such
that S[i] = T[j]. We can count that easily by counting the frequency of characters.
freqS[x] = frequency of character x in string S.
freqT[x] = frequency of character x in string T.
'𝑧'
number of strings which previously occurred = ∑ 𝑓𝑟𝑒𝑞𝑆[𝑐] * 𝑓𝑟𝑒𝑞𝑇[𝑐]
𝑐 = '𝑎'
'𝑧'
number of distinct strings = (𝐿𝑒𝑛𝑔𝑡ℎ(𝑆) + 1)(𝐿𝑒𝑛𝑔𝑡ℎ(𝑇) + 1) − ∑ 𝑓𝑟𝑒𝑞𝑆[𝑐] * 𝑓𝑟𝑒𝑞𝑇[𝑐]
𝑐 = '𝑎'
5
Now as we have seen, the solution only depends on the frequency of characters. So the update
and query part is easy if we can maintain this frequency with segment trees. We will maintain a
segment tree for each character from ‘a’ to ‘z’ for both string P and Q. Each segment tree will be
built on a binary array. For character C we can build an array such that if C is occuring in an
index in P then element on that index will be 1 otherwise 0 (same for Q). Then we will build the
segment tree on that binary array and with that segment tree we will try to maintain range sum.
For operation 1:
1 L1 R1 C1
We will update the segment tree of character C1 of string P and assign 1 to all indexes from L1
to R1. For all other segment trees of other characters of string P, we will assign 0. Time
complexity of operation 1 is O(26 * log(n)).
For operation 2:
2 L2 R2 C2
We will update the segment tree of character C2 of string Q and assign 1 to all indexes from L2
to R2. For all other segment trees of other characters of string Q, we will assign 0. Time
complexity of operation 2 is O(26 * log(n)).
For operation 3:
3 L1 R1 L2 R2
For each character from ‘a’ to ‘z’ we will do a range sum query in the respective segment tree
and count frequencies. Time complexity of operation 3 is O(26 * log(n)). So time complexity is
O(M * 26 * log(N)).
Problem I: Hovercraft
Author: Shahriar Manzoor
Contributor: Derek Kisman, Rafid Bin Mostofa, Md. Muhiminul Islam Osim
Analysis:
It is obvious that for minimum path length the journey through water must begin after crossing
more than half of the bridge. Suppose this journey begins after crossing L/2+x distance. Then
6
the total path length becomes 𝐿 + 𝑊 − ( 2𝐿𝑥 − 2𝑥). So now with the help of some basic
derivation the value of x can be determined for the minimum value of total distance covered.