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
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
Название |
---|
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$$$.