CHITHH - Editorial

Author Roman Furko
Tester Balajiganpathi
Editorialist Utkarsh Lath

Medium

binary search

Problem:

Given an array A of N strings, answer M queries of the following type

• query**(l, v, k)** means concatenate strings at indexes l, l + v, l + 2v … and report the kth character of the concatenated string.

Explanation:

The most obvious approach for solving the problem is:

• For each query, construct the concatenated string, and report the kth character.

Since each string can be of length upto 10, and each each query may require upto N such strings, such a solution would need upto O(10 N) instructions per query. However, even for subtask 1, the solution would need 10000 instructions per query, therefore, 109 instructions for all queries, which should time out. However, on thinking a bit, you would realize that in order to get the kth character, you dont actually need to concatenate the strings. kth character of the concatenated string is going to to be the pth character of some qth string. These p and q can be obtained by summing the lengths of strings that need to be concatenated instead of actually concatenating them, and stop as soom as this sum reaches becomes k or more. The code below uses this idea. This would be good enough to get us through the first subtask, because complexity of answering a query is O(N).
``` answer-query(l, v, k) p = k q = l while (q <= N and p > A[q].length) p -= A[q].length q += v return A[q][p] ```

Now, lets focus on how to solve subtask 2, and may be we will get some insight about how to solve
subtask 3. Subtask 2 has constraint on v, that it is going to be small. Lets first assume that v will be 1 in all queries, and then can we answer the queries in better than O(N) time. In the above code for subtask 1, we are calculating the sum of lengths of A* till it exceeds k, and p is the last such index. How can we speed this up ? binary search should easily be able to help us. For each i, we could store the sum of lengths of all strings upto index i, and then do binary-search on this array to find the last position q such the sum of lengths of all strings with indices between l and q is at most k-1.

So, v=1 case does not look very hard to deal with. How about handling small v ?
Can we use the same idea ?

Since there are small number of v’s, we can handle each v individually. Lets say we want to handle a fixed v = v0. Like the case with v=1 where we constructed an array to store sum of lengths of all strings upto index i, we can build another array, to store sum of lengths of string upto index i, skipping v0 at a time. That means, I build an array
Bv0 = A + A[i-v<sub>0</sub>] + A[i-v<sub>0</sub>] … A[i%v<sub>0</sub>]**.
My binary search for v=1 case can be modified accordingly as well, so that for a given pair (l, v0), it looks only at those indices i for which i % v0 = l % v0, and finds the last index p such that Bv0[p] - Bv0[l-v0] is at most k-1.

How much time and space would it take ? If v ≤ vmax, we would build one array of size N for each v in the range. Each query can be answered in O(log N) time. So our overall complexity is O(N * vmax + M log N). This is sufficient to solve subtask 2.

For handling subtask 3, you need to realize that for large v, the naive solution would not take too much time. In fact, you will iterate through at most ⌊ N/v ⌋ array entries in the solution given for subtask 1. Therefore, for small enough v, we could use this trivial method.

Lets say for v ≤ v0, I construct the array Bv0. For answer a query (l, v, k), if **v ≤ v0, we use the arrays Bv constructed above, and for v > v0, we answer the queries by trivial method. The complexity of such a solution will be O(N * v0 + M * N / v0). For v0 = √M, this quantity is minimum, and equal to O( N * √M). This complexity is good enough to pass all subtasks.

Solutions:

tags:
editorial, ltime04, chithh, medium, preprocessing, binary-search

4 Likes

I think there is a little problem with the testcases - http://www.codechef.com/viewsolution/2733311 here I have done a pretty fast solution that takes O(n) for a query and all the testcases pass.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9080144,"math_prob":0.96479416,"size":3990,"snap":"2019-26-2019-30","text_gpt3_token_len":1050,"char_repetition_ratio":0.11189162,"word_repetition_ratio":0.027522936,"special_character_ratio":0.26090226,"punctuation_ratio":0.120317824,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99606967,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-24T13:34:50Z\",\"WARC-Record-ID\":\"<urn:uuid:2e8de44c-30d3-4f2a-a34b-5e2e215781ca>\",\"Content-Length\":\"18916\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:380bea33-968c-4a69-90e5-d62d44cae5e7>\",\"WARC-Concurrent-To\":\"<urn:uuid:556d1f35-062f-4f01-bab8-f7d4710b2a3a>\",\"WARC-IP-Address\":\"35.174.253.215\",\"WARC-Target-URI\":\"https://discuss.codechef.com/t/chithh-editorial/3295\",\"WARC-Payload-Digest\":\"sha1:S2HC2FSKQ525Q5LJSBPQV5LWLBVZPQUV\",\"WARC-Block-Digest\":\"sha1:4HMRKVM5P4ZWCWB5ENQR2K6RWIQ7LONZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560627999539.60_warc_CC-MAIN-20190624130856-20190624152856-00538.warc.gz\"}"}