Coding Challenge Beta (by AlgoRave DTU) Editorial

Правка en5, от tanmaytaneja2, 2023-11-11 14:33:55

We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!

D – Mic-Mac Mayhem

Author: tt12

Tutorial

Now with the condition on consecutive moves, the optimal way of moving would be to move equal number of 'L' and 'U' moves in an alternating manner. We would then be left with only one type of move to perform, say 'L' (therefore, k1 > k2 and hence cost till now = k2*l + k2*u). We would have now reached the 'y' coordinate of the destination.

Finally, to move to the 'x' coordinate of the destination, we have to perform the remaining k1 — k2 'L' moves. Since these moves are not allowed to be consecutive, we must perform either a 'U' move or a 'D' move after every 'L' move such that the 'y' coordinate remains unchanged at the end. Hence during these set of moves, |U| = |D|. Hence, (k1 — k2) / 2 moves both of the type 'U' and 'D' must be performed along with k1 — k2 'L' moves.

Final cost = k2*l + k2*u + (k1 — k2)*l + (k1 — k2)/2 * (u + d).

To reduce the complexity of solving multiple types of cases, one can rotate the graph by swapping values of {x,y}, {l,r}, {u,d} accordingly so as to always solve for a specific type of solution.

Implementation

E – The Mysterious Tree

Author: vrintle

Tutorial

Let's consider current bit count to be $$$x$$$, so let's mark nodes with bit count $$$x$$$ in red color, and the other nodes in black color. So, this problem boils down to finding maximum distance between any two red colored nodes (a similar codeforces problem). The answer to this sub problem is the diameter of this red-black tree. Note that, in this version, the diameter ends will always be red nodes.

Time complexity: $$$\mathcal{O}(n \cdot \log{n})$$$

PS: I have thought about this partial diameter problem before the actual div3 contest linked above.

Implementation

include <bits/stdc++.h>

using namespace std;

define int int64_t

define endl '\n'

define sz(v) ((int) v.size())

define all(v) v.begin(), v.end()

define vi vector

define pii pair<int, int>

define inf 2e18

int max(int x, int y) { return x > y ? x : y; } int min(int x, int y) { return x < y ? x : y; }

const int N = 2e5 + 7; vi g[N];

void solve() { int n; cin >> n; vi a(n); for(int i = 0; i < n; i++) { cin >> a[i]; a[i] = __builtin_popcount(a[i]); g[i].clear(); } for(int _ = 0; _ < n — 1; _++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } int ans = 0; for(int b = 1; b <= 20; b++) { vi dist(n, inf); function<void(int, int)> dfs = [&](int u, int p) { for(auto& v: g[u]) { if(v == p) continue; dist[v] = dist[u] + 1; dfs(v, u); } }; for(int i = 0; i < n; i++) { if(a[i] == b) { dist[i] = 0; dfs(i, -1); break; } } int mx = 0; for(int i = 0; i < n; i++) { if(a[i] == b) mx = max(mx, dist[i]); } for(int i = 0; i < n; i++) { if(a[i] == b && dist[i] == mx) { dist.assign(n, inf); dist[i] = 0; dfs(i, -1); break; } } for(int i = 0; i < n; i++) { if(a[i] == b) ans = max(ans, dist[i]); } } cout << ans; }

int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef VRINTLE freopen("./tests/tree_27.txt", "r", stdin); freopen("./out.txt", "w", stdout); freopen("./err.txt", "w", stderr); #endif int t = 1; cin >> t; while(t--) { solve(); cout << endl; } return 0; } ```

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en31 Английский harshsingla06 2023-11-14 12:28:30 0 (published)
en30 Английский harshsingla06 2023-11-14 12:27:00 665 Tiny change: ' + number of wides) ≥ y$ then V' -> ' + number Of Wides) ≤ y$ then V'
en29 Английский Gaurav3478 2023-11-11 19:54:54 14 Tiny change: '3$ is $3 \times 3 \times 7$. So, t' -> '3$ is $3 \cdot 3 \cdot 7$. So, t'
en28 Английский Gaurav3478 2023-11-11 19:54:18 223
en27 Английский Gaurav3478 2023-11-11 19:51:16 18 Tiny change: '$63$ is $3x3x7$. So, th' -> '$63$ is $3 \times 3 \times 7$. So, th'
en26 Английский Gaurav3478 2023-11-11 19:50:20 84
en25 Английский Gaurav3478 2023-11-11 19:47:32 19 Tiny change: ' found in O(sqrt(n)). We will ' -> ' found in $\mathcal{O}(\sqrt{n})$. We will '
en24 Английский 0x4F5F6F 2023-11-11 18:39:22 911
en23 Английский vrintle 2023-11-11 17:22:14 2 Tiny change: 'construct 4 * 3 * 2 = 4! permutati' -> 'construct $4 * 3 * 2 = 4!$ permutati' (saved to drafts)
en22 Английский Gaurav3478 2023-11-11 16:56:13 2079 (published)
en21 Английский 0x4F5F6F 2023-11-11 16:40:17 241
en20 Английский 0x4F5F6F 2023-11-11 16:37:21 1910
en19 Английский 0x4F5F6F 2023-11-11 16:27:35 1645
en18 Английский tanmaytaneja2 2023-11-11 15:22:03 4
en17 Английский tanmaytaneja2 2023-11-11 15:21:45 2 Tiny change: 'rmutations$)$ \n\nEx' -> 'rmutations $)$ \n\nEx'
en16 Английский tanmaytaneja2 2023-11-11 15:21:32 4
en15 Английский tanmaytaneja2 2023-11-11 15:20:57 2 Tiny change: 'rmutations$)$. \n\nExte' -> 'rmutations). \n\nExte'
en14 Английский tanmaytaneja2 2023-11-11 15:19:45 1426
en13 Английский tanmaytaneja2 2023-11-11 14:53:32 12 Tiny change: 'moves, $|U moves| = |D moves|$. Hence,' -> 'moves, $|U| = |D|$. Hence,'
en12 Английский tanmaytaneja2 2023-11-11 14:53:16 48
en11 Английский tanmaytaneja2 2023-11-11 14:49:54 20
en10 Английский tanmaytaneja2 2023-11-11 14:48:06 62
en9 Английский tanmaytaneja2 2023-11-11 14:45:26 4
en8 Английский tanmaytaneja2 2023-11-11 14:44:48 120
en7 Английский tanmaytaneja2 2023-11-11 14:39:37 92
en6 Английский vrintle 2023-11-11 14:37:22 24
en5 Английский tanmaytaneja2 2023-11-11 14:33:55 2462
en4 Английский vrintle 2023-11-10 21:05:02 19 Tiny change: 'upto $\log{(n)} = 18$. Le' -> 'upto $\log(n) = 18$. Le'
en3 Английский vrintle 2023-11-10 21:00:42 16
en2 Английский vrintle 2023-11-10 20:57:32 2810
en1 Английский 0x4F5F6F 2023-11-08 17:39:43 164 Initial revision (saved to drafts)