A building has stairs on the left and right, with rooms in between. A person begins at the left staircase on the first floor and must switch off every room light. Find the minimum distance travelled, where $N<15$ floors and $M<100$ rooms per floor.
Dynamic programming is a little awkward because the final position is not necessarily on the top floor. The editorial simply brute-forces choices in $O(2^n * n)$.
voidsolve() { sa(n), sa(m); repp(i, 1, n) { scanf("%s", val[i] + 1); } //memset(dp,-1,sizeof(dp)); int up = 0; repp(i, 1, n) { int flag = 0; for (int k = 1; k <= m + 2; k++) { if (val[i][k] == '1') { flag = 1; } } if (flag) { up = i; break; } } if (up == 0) { puts("0"); return; } for (int i = n; i >= up; i--) { if (i == up) { int left = -1, right = -1; int cnt = 0; for (int k = 1; k <= m + 2; k++) { if (val[i][k] == '1') { right = k; } } for (int k = m + 2; k >= 1; k--) { if (val[i][k] == '1') { left = k; } } left = m + 2 - left; int res = dp[i + 1][0] + (right-1) + (up!=n); if(up!=n)res = min(res, dp[i + 1][1] + (left) + 1); printf("%d\n", res); return; } elseif (i == n) { int p = 0; for (int k = 1; k <= m + 2; k++) { if (val[i][k] == '1') { p = k; } } if (p != 0) { dp[i][0] = (p - 1) * 2; } dp[i][1] = m + 1; } else {
int left = -1, right = -1; int cnt = 0; for (int k = 1; k <= m + 2; k++) { if (val[i][k] == '1') { right = k; } } if (right == -1) { dp[i][0] = dp[i + 1][0] + 1; dp[i][0] = min(dp[i][0], dp[i + 1][1] + m + 2); dp[i][1] = dp[i + 1][1] + 1; dp[i][1] = min(dp[i][1], dp[i + 1][0] + m + 2); continue; } for (int k = m + 2; k >= 1; k--) { if (val[i][k] == '1') { left = k; } } left = m + 2 - left; dp[i][0] = dp[i + 1][0] + (right-1) * 2 + 1; dp[i][0] = min(dp[i][0], dp[i + 1][1] + m + 1 + 1);
ll le = 0, ri = n; while (le<ri) { ll mid = (le + ri + 1) / 2; if (check(mid)) { le = mid; } else { ri = mid - 1; } } printf("%lld ", le); if (le == 0) { printf("0"); return; } repp(i, 1, n) { v[i] = 1LL * val[i] + 1LL * i*le; } sort(v + 1, v + n + 1);
int cnt = 0; ll sum = S; repp(i, 1, le) { sum -= v[i]; } printf("%lld", S - sum); }
D Sagheer and Kindergarten
A kindergarten has $n$ children and $m$ toys. Children request toys over time. A child who receives every requested toy plays and then returns them; otherwise the child waits and makes no other requests. If some children can never receive their requested toys, they cry.
The first $k$ requests are given and guaranteed not to cause crying. For each of $q$ queries, add the final request and calculate how many children cry.
Treat every child as a vertex. Add an edge when one child waits for another child’s toy. Crying occurs exactly when a cycle appears.
For each query, check whether the two vertices are in the same tree. Use DFS order to determine the ancestor–descendant relationship.
A tree has $p_i$ apples at every vertex. A player may either eat any number of apples at a leaf, or move any number from a parent to one child. The second player may swap the apple counts of two vertices once. Count the swaps that let the second player win.
Every leaf depth has the same parity. This is a staircase game on a tree; I do not know how to handle it when the parities differ.
XOR the values of vertices whose depth parity matches the leaves. If the result is $0$, the second player wins. To count swaps, enumerate one set and add the count of matching values in the other.