Hey , Can anyone please tell how to solve D of today's atcoder beginner contest
Link here
How to efficently brute it ?
Any help will be appreciated
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Name |
---|
Hey! I'm one of the writer in ABC 123, and I'll explain solution for D.
There are four solutions.
First, sort $$$E_{i\cdot Y + j} A_i + B_j$$$, which has $$$X \times Y$$$ elements, in decreasing order. Then resize this long array $$$E$$$ to first $$$K$$$ value. Then, get first $$$K$$$ elements of $$$E_i + C_j$$$ by sorting.
The time complexity is $$$O(XY \log XY + KZ \log KZ)$$$.
First, sort array $$$A, B, C$$$ into descending order.
Then, the only candidate of top-$$$K$$$ value is $$$A_i + B_j + C_k$$$ where $$$i \times j \times k \leq K$$$.
The number of candidates is 106,307. Hence, we can get top-$$$K$$$ value by sorting, which fits into the time limit.
In fact, the number of candidates is $$$O(K \log^2 K)$$$ for general $$$K$$$. Thus, the time complexity is $$$O(K \log^3 K)$$$.
First, sort $$$A, B, C$$$ into descending order.
The main observation is that, if $$$(A_i, B_j, C_k)$$$ is not selected, then any of $$$(A_{i+1}, B_j, C_k), (A_i, B_{j + 1}, C_k), (A_i, B_j, C_{k + 1})$$$ will not be selected.
We can start from $$$(A_1, B_1, C_1)$$$ and process with priority queue, pushing three triplets $$$(A_{i+1}, B_j, C_k), (A_i, B_{j + 1}, C_k), (A_i, B_j, C_{k + 1})$$$ when $$$(A_i, B_j, C_k)$$$ is selected. Repeat this process until $$$K$$$ elements will be selected.
The time complexity is $$$O(K \log K)$$$.
Think about binary-searching the answer. Do kind of pruning brute-force using sorted $$$A, B, C$$$. We can judge if the answer is less than $$$K$$$ or not, in time complexity $$$O(K + X + Y + Z)$$$.
The total time complexity is $$$O((K + X + Y + Z) \log max)$$$.
By the way, you can use this blog as discussion of ABC 123 (because we didn't write announcement because of clashing to codeforces round).
Hello, I think the editorial PDF for the fourth solution of D puts a wrong link of source code.
Thank you! Fixed now.
I wrote the 3rd solution in practice, but was not sure about its complexity. Can't the priority queue hold elements more than $$$K$$$ in intermediate stages, so that its complexity is larger than $$$O(Klog K)$$$?
Yes. More than $$$K$$$, but less than or equal to $$$3 \times K + 1$$$, so it's $$$O(K)$$$.
That's because, if it has $$$A_i + B_j + C_k$$$ in top-$$$K$$$, priority queue holds $$$3$$$ elements $$$A_{i+1} + B_j + C_k, A_i + B_{j+1} + C_k, A_i + B_j + C_{k+1}$$$. That doesn't appear in three will not be held in priority queue. This implies that the maximum pushed elements in priority queue is at most $$$3 \times K + 1$$$ (including $$$A_1 + B_1 + C_1$$$).
Can you Please explain why you add + 4 to the formula of problem C .
It's easy, that's because the traveling distance from start to goal, without the designated selected road, is $$$4$$$.