Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again next year!
- <s>The editorial for problem F will be added soon.</s> It is now added.
- ---
- <h2>[problem:1726A]</h2>
- Idea: [user:anubhavdhar,2022-09-06] <br>
- Editorial: [user:anubhavdhar,2022-09-06]
- <spoiler summary="Hint 1">
- Which subsegments are relevant?
- </spoiler>
- <spoiler summary="Hint 2">
- $a_n$ and $a_1$ can be taken up only by _some_ combination of elements, not all.
- </spoiler>
- <spoiler summary="Solution">
- [tutorial:1726A]
- </spoiler>
- <spoiler summary="Implementation">
- <spoiler summary="C++">
- </spoiler>
- </spoiler>
- <h2>[problem:1726B]</h2>
- Idea: [user:anubhavdhar,2022-09-06] <br>
- Editorial: [user:anubhavdhar,2022-09-06]
- <spoiler summary="Hint 1">
- Which cases of $n$ and $m$ are easy to come up with a solution? Which cases is it easy to show that no solution exist?
- </spoiler>
- <spoiler summary="Hint 2">
- What happens when $n$ is even but $m$ is odd?
- </spoiler>
- <spoiler summary="Solution">
- [tutorial:1726B]
- </spoiler>
- <spoiler summary="Implementation">
- <spoiler summary="C++">
- </spoiler>
- <spoiler summary="Python">
- </spoiler>
- </spoiler>
- <h2>[problem:1726C]</h2>
- Idea: [user:Newtech66,2022-09-06] <br>
- Editorial: [user:anubhavdhar,2022-09-06]
- <spoiler summary="Hint 1">
- When can a connected component begin?
- </spoiler>
- <spoiler summary="Hint 2">
- What when two ```'('``` appear consecutively?
- </spoiler>
- <spoiler summary="Solution">
- There are several ways to solve this problem (even bash-able with data structures like DSU and Segment Trees), but here is the one that is easiest to code.
- [tutorial:1726C]
- </spoiler>
- <spoiler summary="Implementation">
- <spoiler summary="C++">
- </spoiler>
- </spoiler>
- <h2>[problem:1726D]</h2>
- Idea: [user:Newtech66,2022-09-06] <br>
- Editorial: [user:Newtech66,2022-09-06]
- <spoiler summary="Hint 1">
- what does $m \le n + 2$ mean?
- </spoiler>
- <spoiler summary="Hint 2">
- What if we look at a spanning tree?
- </spoiler>
- <spoiler summary="Hint 3">
- Does any arbitrary spanning tree work?
- </spoiler>
- <spoiler summary="Solution">
- [tutorial:1726D]
- </spoiler>
- <spoiler summary="Implementation">
- </spoiler>
- <h2>[problem:1726E]</h2>
- Idea: [user:Newtech66,2022-09-06] <br>
- Editorial: [user:Newtech66,2022-09-06], [user:anubhavdhar,2022-09-06]
- <spoiler summary="Hint 1">
- How will the permutation cycles look like ?
- </spoiler>
- <spoiler summary="Hint 2">
- Can we fix the number of the largest sized permutation cycles and calculate the number of perfect permutations?
- </spoiler>
- <spoiler summary="Hint 3">
- How do we handle the smaller permutation cycles?
- </spoiler>
- <spoiler summary="Key fact">
- **Key fact:** There are only $3$ kinds of cycles in almost perfect permutations: $(i)$, $(i, j)$ and $(i, j, i + 1, j + 1)$.
- **Proof:**
- - Cycle size cannot be $3$: Take $(a, b, c)$ as a cycle with $a < b < c$, then $p_b = a$, $p^{-1}_b = c$, but $a < b < c$ implies $\lvert a - c \rvert > 1$.
- - Cycle size cannot be more than $4$: Let $(a_1, a_2, a_3, a_4, a_5, \ldots, a_k)$ be consecutive elements of a cycle with size $k$ ($k \geq 5$). This means $a_1 \ne a_5$, but $\lvert a_1 - a_3 \rvert = 1$ and $\lvert a_3 - a_5 \rvert = 1$. For now assume $a_1 - a_3 = 1$ (proof of $a_1 - a_3 = -1$ is very similar). This means $a_3 - a_5 = 1$ (as $a_1 \ne a_5$) and similarly $a_7 - a_5 = 1$ and so on (if $k \leq 6$, $a_7$ is basically $a_{7 \bmod k}$; i.e. it wraps around). Now, we observe that if $a_1 = x$, then $a_3 = x - 1$, $a_5 = x - 2$, $a_7 = x - 3$. Therefore $a_{k + 1} = x - (k + 1)$. But since that cycle is of size $k$, $a_{k + 1}$ = $a_j$, for some $j \leq k$. Therefore $(x - (k + 1)) = (x - j)$, for some $j \leq k$; which means $(k + 1) \leq k$ — A contradiction.
- - works for any $2$ sized cycles $(i, j)$: Proof is trivial.
- - works for any $4$ sized cycles $(i, j, i + 1, j + 1)$: Proof is trivial.
- - works **only for** $4$ sized cycles $(i, j, i + 1, j + 1)$: Consider cycle $(a, b, c, d)$. $\lvert a - c \rvert = 1$, $\lvert b - d \rvert = 1$. WLOG assume $a < c$ (therefore $c = a + 1$).
- 1. _Case I_: $b < d$. therefore cycle is $(a, b, a + 1, b + 1)$ — of the given form.
- 2. _Case II_: $b > d$. therefore cycle is $(a, d + 1, a + 1, d)$, which is same as $(d, a, d + 1, a + 1)$ — also of the given form.
- </spoiler>
- <spoiler summary="Solution 1: the easy way">
- Special thanks to [user:Um_nik,2022-09-06] for sharing this solution!
- Let's call $(i)$ as type $1$ cycles, $(i,j)$ as type $2$ cycles and $(i,j,i+1,j+1)$ as type $3$ cycles.
- What we will do, is we will fix the number of type $3$ and type $2$ cycles and come up with a formula.
- Let's say there are $s$ type $3$ cycles. We need to pick $2s$ numbers from $[1,n-1]$ such that no two are adjacent. The number of ways to do this is $\binom{n-2s}{2s}$. Then we can permute these numbers in $(2s)!$ ways and group the numbers in pairs of two, but these $s$ pairs can be permuted to get something equivalent, so we need to divide by $s!$. Hence we get:
- $$\begin{aligned}\binom{n - 2s}{2s} \frac{(2s)!}{s!} \end{aligned}$$
- Now, for the remaining $(n - 4s)$ elements, we know that only cycles will be of length $2$ or $1$. Let $I_k$ denote the number of permutations with cycles only of length $2$ and $1$. Then the final answer would be
- $$\begin{aligned}
- \sum_{s=0}^{\lfloor \frac{n}{4} \rfloor}{\binom{n - 2s}{2s} \frac{(2s)!}{s!} I_{n - 4s}}
- \end{aligned}$$
- Now, we would be done if we found out $I_k$ for all $k \in \{1, 2, ... n\}$. Observe that $I_1 = 1$ and $I_2 = 2$. Further for $k > 2$, the $k$-th element can appear in a cycle of length $1$, where there are $I_{k - 1}$ ways to do it; and the $k$-th element can appear in a cycle of length two, which can be done in $(k - 1) \cdot I_{k - 2}$ ways. Therefore we finally have:
- $$\begin{aligned} I_k = I_{k - 1} + (k - 1) \cdot I_{k - 2}\end{aligned}$$
- This completes the solution. Time complexity: $\mathcal{O}(n)$.
- </spoiler>
- <spoiler summary="Solution 2: the hard way">
- Using the observations in **Solution 1** this can be reduced to a counting problem using generating functions. Details follow.
- Now, from the remaining $n-4s$ numbers, let's pick $2k$ numbers to form $k$ type $2$ cycles. We can do this in $\binom{n-4s}{2k}$ ways. Like before, we can permute them in $(2k)!$ ways, but we can shuffle these $k$ pairs around so we have to divide by $k!$. Also, $(i,j)$ and $(j,i)$ are the same cycle, so we also need to divide by $2^k$ because we can flip the pairs and get the same thing.
- Finally, we have the following formula:
- $$\begin{aligned}
- \sum_{s=0}^{\lfloor \frac{n}{4} \rfloor}{\sum_{k=0}^{\lfloor \frac{n-4s}{2} \rfloor}{\frac{(2s)!}{s!}\binom{n-2s}{2s}\binom{n-4s}{2k}\frac{(2k)!}{2^k k!}}}
- \end{aligned}$$
- We can simplify this to get,
- $$\begin{aligned}
- \sum_{s=0}^{\lfloor \frac{n}{4} \rfloor}{\frac{(n-2s)!}{s!}\sum_{k=0}^{\lfloor \frac{n-4s}{2} \rfloor}{\frac{1}{2^k (n-4s-2k)! k!}}}
- \end{aligned}$$
- Now, observe that the answer is in the form of two nested summations. Let us focus on precomputing the inner summation using generating functions. We define the following generating function:
- $$\begin{aligned}
- P(x) &= \sum_{r \ge 0}{ \Bigg( \sum_{k=0}^{\lfloor \frac{r}{2} \rfloor}{\frac{1}{2^k (r-2k)! k!}} \Bigg) } x^r \\
- &= \sum_{r \ge 0}{ \Bigg( \sum_{k=0}^{\lfloor \frac{r}{2} \rfloor}{\frac{x^r}{2^k (r-2k)! k!}} \Bigg) } \\
- &=\sum_{r \ge 0}{ \Bigg( \sum_{k=0}^{\lfloor \frac{r}{2} \rfloor}{\frac{x^{(r - 2k)}}{(r-2k)!} \cdot \frac{x^{2k}}{2^k k!}} \Bigg) } \\
- &= \sum_{r \ge 0}{ \Bigg( \sum_{k=0}^{\lfloor \frac{r}{2} \rfloor}{\frac{x^{(r - 2k)}}{(r-2k)!} \cdot \frac{\big(\frac {x^2}{2} \big)^k}{k!}} \Bigg) } \\
- &= \Bigg(\sum_{i \ge 0}{\frac{x^{i}}{i!} \Bigg)} \cdot \Bigg( \sum_{j \ge 0}{ \frac{\big(\frac {x^2}{2} \big)^j}{j!}} \Bigg) \\
- P(x) &= \big(e^x \big) \cdot \big( e^{\frac {x^2}{2}} \big) = e^{x + \frac{x^2}{2}}
- \end{aligned} $$
- We can compute the first few terms of $e^x = \sum_{i \ge 0}{\frac{x^{i}}{i!}}$ and $e^{\frac {x^2}{2}} = \sum_{j \ge 0}{ \frac{x^{2j}}{2^j j!}}$; and using FFT, we can multiply these (in $\mathcal O(n \log n)$ time) to get the first few terms of $P(x) = e^{x + \frac{x^2}{2}}$.
- Finally, we can get the answer (in $\mathcal O(n)$ time) as,
- $$\begin{aligned}
- \sum_{s=0}^{\lfloor \frac{n}{4} \rfloor}{\frac{(n-2s)!}{s!} \Big([x^{n-4s}] P(x) \Big)}
- \end{aligned}$$
- where $[x^m]F(x)$ is the coefficient of $x^m$ in the Taylor expansion of $F(x)$.
- Time complexity: $\mathcal{O}(\max{(t_i)}\log{\max{(t_i)}} + \sum{t_i})$, where $t_i$ is the value of $n$ in the $i$-th test case.
- </spoiler>
- <spoiler summary="Implementation">
- <spoiler summary="Solution 1">
- </spoiler>
- <spoiler summary="Solution 2">
- </spoiler>
- </spoiler>
- <h2>[problem:1726F]</h2>
- Please note that it is no longer possible to submit solutions to this problem on Codeforces. You can read the details [here](https://codeforces.com/blog/entry/106700).
- Idea: [user:little_angel,2022-09-06] <br>
- Editorial: [user:Newtech66,2022-09-08]
- <spoiler summary="Hints">
- <spoiler summary="Hint 1">
- The $d_i$ are irrelevant. Take partial sums and add them to each $c_i$ modulo $T$.
- </spoiler>
- <spoiler summary="Hint 2">
- If your start time is fixed, it is never beneficial to wait at a green light. Drive whenever you can.
- </spoiler>
- <spoiler summary="Hint 3">
- Visualise the problem as $n$ parallel lines, each coloured with a red and green interval. Now imagine the car moving up (and looping back at $T$) whenever it has to wait at a red light, and shooting to the right immediately when the lights are green.
- </spoiler>
- <spoiler summary="Hint 4">
- You can model this as a shortest paths problem.
- </spoiler>
- </spoiler>
- <spoiler summary="Solution">
- There are solutions to this problem using lazy segment trees, or say, sets and maps. I'll describe my own solution here, which converts the problem to a shortest paths problem.
- First of all, the $d_i$ are irrelevant, since we can take partial sums and add them to $c_i$. In the final answer, just add the sum of all $d_i$.
- Now, we can offset the green (or red) intervals by the modified $c_i$ to get new red and green intervals modulo $T$.
- Imagine $n$ parallel lines of length $T$, with the corresponding intervals coloured red and green. Now, consider the intervals to be static. In this picture, the car can be visualised to be moving upwards along the line (looping back at $T$) when on red intervals and shooting to the right when it reaches a green interval. Notice that for a fixed starting time, it is always optimal to drive whenever you can. So essentially, fixing your start time also fixes your answer.
- Now let's convert this to a graph. Notice that only the endpoints of green intervals matter, so overall, we'll have $2n$ nodes in our graph, plus a dummy node representing the start of the journey that connects to all endpoints that can be reached without being blocked by a red interval, and a dummy node representing the end of the journey that connects to all endpoints from which you can reach the end without being blocked by a red interval. So in total, $2n+2$ nodes.
- We'll add an edge between two nodes with a weight equal to the amount of time we'll have to spend waiting for the red light to turn green. We can achieve this with a multiset storing all currently visible endpoints, and then doing a little casework to add edges from nodes blocked by the current red interval and then deleting them, and adding new ones. We'll do this only $\mathcal{O}(n)$ times. In the end, whatever is left in the multiset are all the visible endpoints and we can connect them to the dummy node for the end. We can repeat this in the backward direction to get the endpoints for the dummy node for the start, though in my code, I elected to do this with an interval union data structure.
- Finally, just run Dijkstra's algorithm on this graph. The answer is the sum of all $d_i$ plus the shortest path between the dummy node for the start and the dummy node for the end.
- Time complexity: $\mathcal{O}(n \log n)$
- </spoiler>
- <spoiler summary="Implementation">
- </spoiler>
- <h2>[problem:1726G]</h2>
- Idea: [user:Newtech66,2022-09-06] <br>
- Editorial: [user:Newtech66,2022-09-06], [user:anubhavdhar,2022-09-06]
- <spoiler summary="Hints">
- <spoiler summary="Hint 1">
- The happiness of a person can never decrease. It is obvious.
- </spoiler>
- <spoiler summary="Hint 2">
- Let $T$ be the final value all elements are equal to. For all $x<T$, there can be at most one occurrence of $<x,1>$ in the array, where $<a,b>$ means a person with happiness $a$ and personality $b$.
- </spoiler>
- <spoiler summary="Hint 3">
- There is only one possible $T$, given by $m+n-1$.
- </spoiler>
- <spoiler summary="Hint 4">
- If it is possible to place multiple numbers in the order at a certain point (i.e. if there are multiple possible $<u,v>$ which when placed will all yield $<T,v>$), it is always better to prefer the larger number, and to place a number with personality $1$ over one with personality $0$ in case of a tie.
- <spoiler summary="Subhint">
- If $T=M$, the $<M,1>$ group is special, since it doesn't matter when you place it since the final happiness value will still be $M$, and it won't affect any other group's viability either.
- </spoiler>
- </spoiler>
- <spoiler summary="Hint 5">
- For a valid ordering (if exists), the $0$ behaving people would be sorted in non-decreasing order of initial happiness.
- </spoiler>
- <spoiler summary="Hint 6">
- For a valid ordering (if exists) and at any intermediate step, for all $1$ behaving people $<u, 1>$ yet to speak, the number of people with a larger happiness than $u$ cannot exceed $(T - u)$. In other words, if we placed $<u, 1>$ at any intermediate step, its final happiness would not exceed $T$.
- </spoiler>
- </spoiler>
- <spoiler summary="Solution">
- [tutorial:1726G]
- </spoiler>
- <spoiler summary="Implementation">
- </spoiler>
- <h2>[problem:1726H]</h2>
- Idea: [user:anubhavdhar,2022-09-06] <br>
- Editorial: [user:Newtech66,2022-09-06], [user:anubhavdhar,2022-09-06]
- <spoiler summary="Hint 1">
- What role does the angles of the polygon play?
- </spoiler>
- <spoiler summary="Hint 2">
- Is there some overlap in the area of two such regions? How do we account for that?
- </spoiler>
- <spoiler summary="Solution">
- [tutorial:1726H]
- </spoiler>
- <spoiler summary="Implementation">
- </spoiler>
- ---
- <h2>Vote for the problems here!</h2>
- Feel free to vote for your opinion of each problem, and the best problem of the contest.
- <spoiler summary="Vote here">
- <spoiler summary="A">
- - Best problem: [likes:1,option1]
- - Good problem: [likes:1,option2]
- - Average problem: [likes:1,option3]
- - Bad problem: [likes:1,option4]
- - Did not solve: [likes:1,option5]
- </spoiler>
- <spoiler summary="B">
- - Best problem: [likes:2,option1]
- - Good problem: [likes:2,option2]
- - Average problem: [likes:2,option3]
- - Bad problem: [likes:2,option4]
- - Did not solve: [likes:2,option5]
- </spoiler>
- <spoiler summary="C">
- - Best problem: [likes:3,option1]
- - Good problem: [likes:3,option2]
- - Average problem: [likes:3,option3]
- - Bad problem: [likes:3,option4]
- - Did not solve: [likes:3,option5]
- </spoiler>
- <spoiler summary="D">
- - Best problem: [likes:4,option1]
- - Good problem: [likes:4,option2]
- - Average problem: [likes:4,option3]
- - Bad problem: [likes:4,option4]
- - Did not solve: [likes:4,option5]
- </spoiler>
- <spoiler summary="E">
- - Best problem: [likes:5,option1]
- - Good problem: [likes:5,option2]
- - Average problem: [likes:5,option3]
- - Bad problem: [likes:5,option4]
- - Did not solve: [likes:5,option5]
- </spoiler>
- <spoiler summary="F">
- - Best problem: [likes:6,option1]
- - Good problem: [likes:6,option2]
- - Average problem: [likes:6,option3]
- - Bad problem: [likes:6,option4]
- - Did not solve: [likes:6,option5]
- </spoiler>
- <spoiler summary="G">
- - Best problem: [likes:7,option1]
- - Good problem: [likes:7,option2]
- - Average problem: [likes:7,option3]
- - Bad problem: [likes:7,option4]
- - Did not solve: [likes:7,option5]
- </spoiler>
- <spoiler summary="H">
- - Best problem: [likes:8,option1]
- - Good problem: [likes:8,option2]
- - Average problem: [likes:8,option3]
- - Bad problem: [likes:8,option4]
- - Did not solve: [likes:8,option5]
- </spoiler>
- </spoiler>
Advertisement
Add Comment
Please, Sign In to add comment