Jekyll2019-11-19T12:38:28+09:00http://kazune-lab.net/feed.xmlUnhappy Go Lucky!Documents by Kazune Takahashi.Kazune TakahashiA quiz for a travel in an airplane2019-11-11T00:00:00+09:002019-11-11T00:00:00+09:00http://kazune-lab.net/diary/2019/11/11/geom<h2 id="problem">Problem</h2> <blockquote class="twitter-tweet" data-lang="de"><p lang="ja" dir="ltr">初等幾何お好きならどぞ。 <a href="https://t.co/8ZgmuvEN9u">pic.twitter.com/8ZgmuvEN9u</a></p>&mdash; Kazune Takahashi (@kazune_lab) <a href="https://twitter.com/kazune_lab/status/1193482891168079874?ref_src=twsrc%5Etfw">10. November 2019</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <blockquote> <p>You are given an equilateral triangle $\triangle ABC$ on a plane. Let $X$ be a point inside $\triangle ABC$. Suppose that $\angle BXC = 120^\circ$ and $\bar{BX} : \bar{XC} = 2 : 3$. Calculate the ratio of the aria $\triangle ABX : \triangle ACX$.</p> </blockquote> <h2 id="solution">Solution</h2> <p>First, we argue on the side of $B$. Let $K$ be the point that satisfies $\angle XBK = 60^\circ$ and $\bar{BX} = \bar{BK}$. There is two possible points, but see the image below. We see that $\bar{BA} = \bar{BC}$ since $\triangle ABC$ is an equilateral triangle and $\angle KBA = \angle XBC$ since $\angle KBX = \angle ABC = 60^\circ$. Therefore it follows that $\triangle KBA \equiv \triangle XBC$. Thus we have $\angle XCB = \angle KAB$. In addition we have $\angle XBC + \angle XCB = 60^\circ = \angle ABC$. Thus we see that $\angle ABX = \angle KAB$. Hence it follows that $KA // BX$, which implies $\triangle ABX = \triangle KBX$. Note that $\triangle KBX$ is an equilateral triangle.</p> <p><img src="/images/2019-11-11-A.png" alt="For B side" /></p> <p>Second, we work the same thing for the side of $C$. Let $L$ be the point that satisfies $\angle XCL = 60^\circ$ and $\bar{CX} = \bar{CL}$. By the same argument, we see that $\triangle ACX = \triangle LCX$ and $\triangle LCX$ is an equilateral triangle.</p> <p><img src="/images/2019-11-11-B.png" alt="For C side" /></p> <p>Summing up these result, we conclude that $\triangle ABX : \triangle ACX = \triangle KBX : \triangle LCX = \bar{BX}^2 : \bar{XC}^2 = 4 : 9.$</p>Kazune TakahashiProblemNIKKEI Programming Contest 2019-22019-11-09T00:00:00+09:002019-11-09T00:00:00+09:00http://kazune-lab.net/contest/2019/11/09/nikkei2019-2-qual<p><a href="https://atcoder.jp/contests/nikkei2019-2-qual">第二回全国統一プログラミング王決定戦予選</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/nikkei2019-2-qual/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2019/1109_nikkei2019-2-qual">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---sum-of-two-integers"><a href="https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_a">A - Sum of Two Integers</a></h2> <p>Just flush $(N - 1) / 2$.</p> <h2 id="b---counting-of-trees"><a href="https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_b">B - Counting of Trees</a></h2> <h3 id="solution">Solution</h3> <p>First we check if $D = 0$ and $D[i] &gt; 0$ for $i &gt; 0$; otherwise the answer is $0$. We set</p> <blockquote> <p>$A[j] = \sharp \{ i \in N \mid D[i] = j \}$.</p> </blockquote> <p>We can forget the given integers of the vertexes. Just counting is enough. The answer is $\prod _ {i = 0} ^ {N - 1} A[i]^{A[i + 1]}.$</p> <h2 id="c---swaps"><a href="https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_c">C - Swaps</a></h2> <h3 id="observation">Observation</h3> <p>We argue on $0$-indexed.</p> <p>First, we forget the maximum number of the manipulations. Without loss of generality, we can assume $B _ 0 \leq B _ 1 \leq \dots \leq B _ {N - 1}$. The best strategy for $\{ A _ i \}$ is that we sort $\{ A _ i \}$ by the ascending order. This sorting takes at most $N - 1$ manipulations. We state this as the following theorem.</p> <blockquote> <p><strong>Theorem B.1</strong>: Let $p \in \mathfrak{S} _ N$. Let $G _ p = (V, E)$ be a graph with $V = N$ and $E = \{ (i, p _ i) \mid i \in N \}$. Then, the number of swaps we need to generate $p$ is $N - C$, where $C$ is the number of the connected components of $G$.</p> </blockquote> <p>Proof: First of all, we check the connected components of $G$ are cycles. We don’t need to swap the vertexes which are not on the same connected components. Thus we will work on one fixed connected component $K$ of $G$. First, we show that we need at least $\sharp K - 1$ swaps to make the component $K$. If $\sharp K = 1$, there is nothing to show. Thus suppose $\sharp K \geq 2$. When we swap $i, j \in K$, we color $e = (i, j)$. If we make less than $K - 1$ swaps, the colored edges won’t make a spanning tree, which means there is at least one vertex which has not been touched yet. This is impossible. Second, we show that we can make $K$ by $\sharp K - 1$ swaps. This is very straightforward. Just taking $\sharp K - 1$ edges on the cycles. Summing up these results, we can make $G$ by $\sharp G - C \times 1 = N - C$ swaps and this is the best result.</p> <p>Let $A’$ be the sorted version of $A$. If there exists $k \in N$ so that $A’[k] &gt; B[k]$, the answer is <code class="language-plaintext highlighter-rouge">No</code> immediately. Hereinafter we assume that $A’[i] \leq B[i]$ for $i \in N$. Let $p \in \mathfrak{S} _ N$ so that $p _ {A[i]} = A’[i]$. By Theorem B.1, if $G _ p$ is not one whole cycle, we can gain $A \mapsto A’$ by less than $N - 1$ swaps. The answer is <code class="language-plaintext highlighter-rouge">Yes</code>.</p> <h3 id="how-to-reduce-the-number-of-manipulations">How to reduce the number of manipulations</h3> <p>Hereinafter, we assume that $G _ p$ is a whole simple cycle. The sorting $A \mapsto A’$ needs $N - 1$ swaps. We have to give up $A \mapsto A’$. Then, how can we reduce the number of manipulations?</p> <p>We think of <strong>a sufficient condition</strong>.</p> <blockquote> <p><strong>Lemma B.2</strong>: Assume that $G _ p$ is a whole simple cycle. Then, if there exists $k \in N$ so that $A’ _ {k + 1} \leq B _ k$, the answer is <code class="language-plaintext highlighter-rouge">Yes</code>.</p> </blockquote> <p>Proof: We set $q \in \mathfrak{S} _ N$ as follows. $q _ i = \begin{cases} p _ {k + 1} &amp; i = k, \\\ p _ k &amp; i = k + 1, \\\ p _ i &amp; \text{otherwise}. \end{cases}$ We consider $G _ q$. Since $G _ p$ is a whole simple cycle, $G _ q$ consists of two “split” cycles divided by $k$ and $k + 1$. This can be made by $N - 2$ swaps and it satisfies the condition since $A’ _ {k + 1} \leq B _ k$. We conclude that the answer is <code class="language-plaintext highlighter-rouge">Yes</code>.</p> <p>And <strong>this is the necessary condition, too</strong>.</p> <blockquote> <p><strong>Lemma B.3</strong>: Assume that $G _ p$ is a whole simple cycle. Then, if $A’ _ {i + 1} &gt; B _ i$ for all $i \in (N - 1)$, the answer is <code class="language-plaintext highlighter-rouge">No</code>.</p> </blockquote> <p>Proof: We show that we have to use $A’ _ i$ for $B _ i$ to hold $A _ i \leq B _ i$ for each $i \in N$ by mathematical induction on $i$. For $i = 0, 1, \dots, N$, we consider which element of $\{ A’ _ i \}$ should be used for $B _ i$. Since $B _ i &lt; A’ _ {i + 1} \leq A’ _ {i + 2} &lt; \dots$, we have to use one element of $A’ _ 0, A’ _ 1, \dots, A’ _ i$. But the assumption of induction, we have already used $A’ _ 0, \dots, A’ _ {i - 1}$, which forces us to use $A’ _ i$ for $B _ i$. This means that, to hold $A _ i \leq B _ i$ for each $i \in N$ we must sort $A \mapsto A’$, which need $N - 1$ swaps. Therefore the answer is <code class="language-plaintext highlighter-rouge">No</code>.</p> <h2 id="d---shortest-path-on-a-line"><a href="https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_d">D - Shortest Path on a Line</a></h2> <h3 id="solution-1">Solution</h3> <p>Let $G = (N, E)$ be the original graph. Let $G’ = (N, E’)$ be a graph so that $E$ consists of the following edges; we use $(u, v, c) \in E’$ to denote the edge whose source is $u$ and whose destination is $v$ and whose cost is $c$.</p> <ul> <li>$(i + 1, i, 0)$ for each $i \in (N - 1)$.</li> <li>$(L _ j, R _ j, C _ j)$ for each $j \in M$.</li> </ul> <p>We show the following proposition later.</p> <blockquote> <p><strong>Proposition C.1</strong>: The minimum costs of the paths from $0$ to $N - 1$ on $G$ and $G’$ are the same.</p> </blockquote> <p>Thus the solution is just to use the Dijkstra method on $G’$.</p> <h3 id="proof-of-proposition-c1">Proof of Proposition C.1</h3> <p>Proof: Let $A$ and $A’$ be that minimum cost on $G$ and $G’$, respectively. First we show that $A \geq A’$. This is easy. We fix a minimum path $P$ from $0$ to $N - 1$. We can make a path $P’$ that attains the total cost being $A$ on $G’$. Just using $(L _ j, R _ j, C _ j)$ and returning back by $(i + 1, i, 0)$ are suffice.</p> <p>Second we show that $A = A’$. The edges of $E’ \setminus E$ are $(i + 1, i, 0)$ for each $i \in (N - 1)$. Let $d _ i$ be the distance between $0$ and $i$. We show that $\{ d _ i \}$ is a non-decreasing sequence to complete the proof. Let $0 \leq i &lt; j &lt; N$. We fix a minimum path $p _ j$ from $0$ to $j$. There are several steps. But the condition we are given in the problem statement guarantee that we can stop and change the final step smaller to reach $i$ by the cost less than or equal to $p _ j$. This means that $p _ j \leq p _ i$. This is what we want to show.</p> <h2 id="e---non-triangular-triplets"><a href="https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_e">E - Non-triangular Triplets</a></h2> <h3 id="observation-1">Observation</h3> <p>We omit “$K + \dots$” part to write the numbers.</p> <p>Consider one of the tuples $(A, B, C)$. To hold that $A + B \leq C$, The bigger $C$ is, the better. In addition the less $A$ and $B$ are, the better. Thus we use $2N, \dots, 3N - 1$ for $C$. Thus we work on how to assign $0, \dots, 2N - 1$ for $A + B$ side. Once we’ve assigned by the best strategy, we can check if the answer is proper or not easily.</p> <h3 id="what-is-the-best-strategy">What is the best strategy?</h3> <p>We will use $\{ 0, 1, \dots, 2N - 1 \}$ for $A$ and $B$. The sum is $N (2N - 1)$. We want to the best assignment, that means $A + B$ should be an arithmetic progression whose difference is $1$. We use $X$ to denote its start number. Thus the best strategy should be as follows: $A _ 0 + B _ 0 \leq X$, $A _ 1 + B _ 1 \leq X + 1$, $\dots$, $A _ {N - 1} + B _ {N - 1} \leq X + N - 1$. The sum of the right hand side is $XN + N(N - 1)/2$. Thus we have the lower bound of $X$ as follows. $XN + \frac{N (N - 1)}{2} \geq N (2N - 1), \ \ \ \text{ i.e. } \ \ \ X \geq \frac{3N - 1}{2}.$</p> <p>$X$ is an integer. We should divide the solution into two cases; $N$ is odd and $N$ is even.</p> <p>First, we consider the case $N$ is odd, that is, $N = 2M + 1$. Then we have $X \geq 3M + 1$. We search for the assignment that satisfies this equation. We can find the solution as follows. \begin{align} 0 &amp;+ &amp;&amp; 3M+1 &amp;= &amp;&amp; 3M+1 = X, \\\ M+1 &amp;+ &amp;&amp; 2M+1 &amp;= &amp;&amp; 3M+2 = X+1, \\\ 1 &amp;+ &amp;&amp; 3M+2 &amp;= &amp;&amp; 3M+3 = X+2, \\\ M+2 &amp;+ &amp;&amp; 2M+2 &amp;= &amp;&amp; 3M+4 = X+3, \\\ 2 &amp;+ &amp;&amp; 3M+3 &amp;= &amp;&amp; 3M+5 = X+4, \\\ \dots &amp;+ &amp;&amp; \dots &amp;= &amp;&amp; \dots, \\\ M-1 &amp;+ &amp;&amp; 2M+N-1 &amp;= &amp;&amp; 3M+N-2 = X+N-3, \\\ 2M &amp;+ &amp;&amp; M+N-1 &amp;= &amp;&amp; 3M+M-1 = X+N-2, \\\ M &amp;+ &amp;&amp; 2M+N &amp;= &amp;&amp; 3M+N = X+N-1. \\\ \end{align}</p> <p>Second, we consider the case $N$ is even, that is, $N = 2M$. Then we have $X \geq 3M - 1/2$. Actually, we can find the best solution as follows. \begin{align} 0 &amp;+ &amp;&amp; 3M &amp;= &amp;&amp; 3M \leq X, \\\ M &amp;+ &amp;&amp; 2M &amp;= &amp;&amp; 3M \leq X+1, \\\ 1 &amp;+ &amp;&amp; 3M+1 &amp;= &amp;&amp; 3M+2 \leq X+2, \\\ M+1 &amp;+ &amp;&amp; 2M+1 &amp;= &amp;&amp; 3M+2 \leq X+3, \\\ \dots &amp;+ &amp;&amp; \dots &amp;= &amp;&amp; \dots, \\\ M-1 &amp;+ &amp;&amp; 3M+M-1 &amp;= &amp;&amp; 3M+N-2 \leq X+N-2, \\\ 2M-1 &amp;+ &amp;&amp; 2M+M-1 &amp;= &amp;&amp; 3M+N-2 \leq X+N-1. \\\ \end{align}</p> <h2 id="f---mirror-frame"><a href="https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_f">F - Mirror Frame</a></h2> <h3 id="observation-2">Observation</h3> <p>First, we assign the unique number for each line. Observe what lines are assigned for a point $(i, j)$. We can see that exactly the two lines whose number are $x = \lvert i - j \rvert$ and $y = \min(i + j, 2N - i - j)$ are assigned for $(i, j)$.</p> <p><img src="/images/2019-11-09-F1.png" alt="N = 6" /></p> <p>We convert a point $(i, j)$ in this plane into an edge $(x, y)$ on the graph $G$. What is the graph $G$? $G$ is a pair of the perfect graphs. We convert the problem into another problem described as follows.</p> <blockquote> <p><strong>Constraints</strong>: We are given two perfect graphs. Each edge on the graph $G = (X, E)$ is assigned on$=1$, off $=0$ or <code class="language-plaintext highlighter-rouge">?</code>.<br /> <strong>Manipulation</strong>: Choose two vertexes $u, v$. We change on/off for all edges which contains either $u$ or $v$.<br /> <strong>Aim</strong>: By several manipulations, we want to make all edges be off.<br /> <strong>Answer</strong>: The number of assignments on all <code class="language-plaintext highlighter-rouge">?</code> for on/off so that we achieve the aim.</p> </blockquote> <h3 id="lemmas">Lemmas</h3> <p>Here we will solve a flipping problem. So we prepare $\mathbb{F} _ 2$-vector space. For all $e _ \lambda \in E$, we use $a _ \lambda \in \mathbb{F} _ 2$ to denote its state, i.e., $a _ \lambda = \begin{cases} 0 &amp; e _ \lambda \text{ is off}, \\\ 1 &amp; e _ \lambda \text{ is on}. \end{cases}$ Let $V$ be a vector space on $\mathbb{F} _ 2$ as follows. $V = \left\{ \sum _ {e _ \lambda \in E} a _ \lambda e _ \lambda \mid a _\lambda \in \mathbb{F} _ 2 \right\},$ where $\sum$ is a formatting sum, equipped with the normal $+$ and the normal scalar $\cdot$.</p> <p>We focus on the subspace $W \subset V$ generated by the manipulations.</p> <blockquote> <p><strong>Lemma F.1</strong>: Let $u, v, w \in X$. Then the triangle $uvw$ is included in $W$, i.e., $(u, v) + (v, w) + (w, u) \in W. \tag{F.1}$</p> </blockquote> <p>Proof: We do three manipulations on ($u$ and $v$), ($v$ and $w$) and ($w$ and $u$). Let $e \in E$. Then, $e$ is chosen $0$ times if neither of vertexes of $e$ is $u$, $v$ or $w$, $2$ times if exactly one of vertexes of $e$ is $u$, $v$ or $w$, and $3$ times if $e = (u, v)$, $(v, w)$ or $(w, u)$. Thus we have (F.1).</p> <blockquote> <p><strong>Lemma F.2</strong>: Suppose $\lvert X \rvert$ is even. Then, we have $W = V$.</p> </blockquote> <p>Proof: If $\lvert X \rvert \leq 2$, we have nothing to show. Suppose $\lvert X \rvert \geq 4$. Let $e _ 0 = (u, v) \in E$. For all $w \in E \setminus \{ u, v \}$, we apply Lemma F.1 for the triangles $uvw$. Note that in this process, $(u, v)$ is chosen a even-number times ($= \lvert X \rvert - 2$ times). Then, we manipulate on $u$ and $v$ to have $e \in W$. Then it follows that $W = V$ since $W$ is a linear subspace.</p> <p><img src="/images/2019-11-09-F2.png" alt="Lemma F.2" /></p> <blockquote> <p><strong>Lemma F.3</strong>: Suppose $\lvert X \rvert$ is odd. Then, $W$ consists of all sub-graphs of $G$ where the in-out-degree of any vertex is even.</p> </blockquote> <p>Proof: The manipulation changes the in-out-degree of any vertex by even number. So, if there is a vertex whose in-out-degree is odd, it won’t be included in $W$. On the contrary, let $g$ be a sub-graph of $G$ where the in-out-degree of any vertex is even. We check all vertexes in some fixed order. We visit $v _ k$ after we visit $v _ 0, \dots, v _ k$ whose in-out-degree is $0$. By our assumption, the in-out-degree of $v _ k$ is always even. We divide them into some pairs and make triangles by Lemma F.1. Then, the in-out-degree of $v _ k$ is zero. We can work iteratively to make all in-out-degrees be $0$. This means $g \in W$.</p> <p><img src="/images/2019-11-09-F3.png" alt="Lemma F.3" /></p> <h3 id="solution-2">Solution</h3> <p>The final answer is the multiplication of the answers of two graphs.</p> <p>By Lemma F.2, if $\lvert X \rvert$ is even, we can choose on/off for each <code class="language-plaintext highlighter-rouge">?</code> arbitrarily.</p> <p>If $\lvert X \rvert$ is even, things are complicated. We think of the forest of <code class="language-plaintext highlighter-rouge">?</code>-edges on $G$. We work on each connected component $C$ of the forest. First, we count the number of $1$-edges between $C$ and $G \setminus C$. If it is odd, the answer is $0$. We cannot achieve our goal in this case since all manipulations changes the in-out-degrees of all vertex by even number.</p> <p>If it is even, however, we can create solution by the same argument in Lemma F.3. How many possibilities are there? We take a spanning tree $T$ on $C$. We can choose on/off for each edges of $C \setminus T$ arbitrarily and then work on $T$. The statements of $T$ is determined iteratively by its leaves. Thus the possibility is $2 ^ {\lvert C \setminus T \rvert}$.</p> <h1 id="others">Others</h1>Kazune Takahashi第二回全国統一プログラミング王決定戦予選AtCoder Grand Contest 0402019-11-04T00:00:00+09:002019-11-04T00:00:00+09:00http://kazune-lab.net/contest/2019/11/04/agc040<p><a href="https://atcoder.jp/contests/agc040">AtCoder Grand Contest 040</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/agc040/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2019/1103_AGC040">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---"><a href="https://atcoder.jp/contests/agc040/tasks/agc040_a">A - &gt;&lt;</a></h2> <h3 id="solution">Solution</h3> <p>First, we initialize <code class="language-plaintext highlighter-rouge">vector&lt;ll&gt; V(N, 0LL);</code> as a candidate for good sequences. We divide this problems into two parts as follows.</p> <ul> <li>For all $i \in (N - 1)$ with $S[i] = “&lt;”$, we change $a _ {i + 1}$ so that $a _ i &lt; a _ {i + 1}$ in ascending order.</li> <li>For all $i \in (N - 1)$ with $S[i] = “&gt;”$, we change $a _ {i}$ so that $a _ i &gt; a _ {i + 1}$ in descending order.</li> </ul> <p>This greedy algorithm works. We can solve this problem in $O(2N) = O(N)$-time.</p> <h2 id="b---two-contests"><a href="https://atcoder.jp/contests/agc040/tasks/agc040_b">B - Two Contests</a></h2> <h3 id="solution-1">Solution</h3> <p>We focus on $x = \mathop{\mathrm{argmin} } _ i R _ i$ and $y = \mathop{\mathrm{argmax} } _ i L _ i$. If $x = y$, or $x$ and $y$ are included in the same contest, in this contest the number of the satisfied people must be $\max(0, R _ x - L _ y)$. Thus the best strategy is that we choose $z = \mathop{\mathrm{argmax} } _ {i \in N \setminus \{ x, y \} } R _ i - L _ i$ and let the other contest consist of only one problem $z$. Let $ans _ 0$ be the answer by this case.</p> <p>So we consider otherwise, that is, we consider, given $x \neq y$, the one contest $X$ includes $x$ and the other $Y$ includes $y$. Let’s see what is the best strategy here. Let $V = N \setminus \{ x, y \}$. and sort $I = \{ (L _ i, R _ i) \} _ {i \in V}$ in descending order of $R _ i$. We choose some ranges from $I$ to push into $Y$. What is the best strategy here?</p> <p>Let’s focus on $r = \min _ {i \in B} R _ i$. <strong>To make the argument simple, we fix $r$.</strong> For $e = (L _ i , R _ i) \in I$, If $R _ i \geq r$, we should push $e$ into $B$ because if we push $e$ into $B$, we don’t lose anyone satisfied, but if we push $e$ into $A$, we may or may not lose those, which is not the best strategy. On the other hand, if $R _ i &lt; r$, we must push $e$ into $A$; or otherwise it contradicts the definition of $r$. Therefore we conclude that our best strategy is that trying all $0 \leq k \leq N - 2$. We push $(L _ i, R _ i) \in I$ into $B$ if $i &lt; k$ and into $A$ if $i \geq k$.</p> <p>Let’s discuss the time complexity. Sorting $I = \{ (L _ i, R _ i) \} _ {i \in V}$ in descending order of $R _ i$ takes $O(N \log N)$-time. We can compute all $r _ k = \min _ {i &lt; k} R _ i$ and $l _ k = \max _ {i \geq k} L _ i$ by partial min/max method in $O(N)$-time. The answer is $\max \left( ans _ 0, \max _ {0 \leq k \leq N - 2} \left( \max\left(0, R _ x - l _ k \right) + \max\left(0, r _ k - L _ y \right) \right) \right).$</p> <h2 id="c---neither-ab-nor-ba"><a href="https://atcoder.jp/contests/agc040/tasks/agc040_c">C - Neither AB nor BA</a></h2> <h3 id="solution-2">Solution</h3> <p>We translate $s$ into $S$ as follows. $S[i] = \begin{cases} s[i] &amp; i \text{ is even}, \\\ B &amp; i \text{ is odd and } s[i] = A, \\\ A &amp; i \text{ is odd and } s[i] = B, \\\ C &amp; i \text{ is odd and } s[i] = C. \end{cases}$</p> <p>The map $s \mapsto S$ is a bijection. So we count the number of good $S$. In $S$, the forbidden pairs of characters are $AA$ and $BB$. Hereinafter, we use $A, B, C$ to denote the number of <code class="language-plaintext highlighter-rouge">A</code>, <code class="language-plaintext highlighter-rouge">B</code>, <code class="language-plaintext highlighter-rouge">C</code> in $S$, respectively.</p> <p>The number of the manipulations must be $N / 2$. Thus we see that, if $A &gt; N / 2$ or $B &gt; N / 2$, we cannot reduce $S$ into the empty string. However, otherwise, we can reduce $S$ into the empty string. Why? Let’s consider the case where $A = B = N / 2$. Then, we can find a place where <code class="language-plaintext highlighter-rouge">A</code> and <code class="language-plaintext highlighter-rouge">B</code> are adjacent to each other. We eliminate it. Then the problem becomes smaller than the original problem. We can continue this manipulation $N / 2$ times. Other cases are almost the same. We regard each <code class="language-plaintext highlighter-rouge">C</code> as <code class="language-plaintext highlighter-rouge">A</code> or <code class="language-plaintext highlighter-rouge">B</code> virtually so that $A = B = N / 2$. Then the same strategy works.</p> <p>Let $X$ be the number of $S$ that satisfies $A &gt; N / 2$. The case $B &gt; N / 2$ is the same. The answer is $3 ^ N - 2X$. Here, $X$ is computed as follows. $X = \sum _ {N / 2 + 1 \leq k \leq N} \begin{pmatrix} N \\ k \end{pmatrix} 2 ^ {N - k}.$</p> <h2 id="d---balance-beam"><a href="https://atcoder.jp/contests/agc040/tasks/agc040_d">D - Balance Beam</a></h2> <h3 id="observation">Observation</h3> <p>Let’s see this problem from the viewpoint of two graphs. Let the $x$-axis denote the time and the $y$-axis denote the distance from the starting point. We draw two graphs and let $I = \{ (A _ i, B _ i) \} _ {i \in N}$ be ordered as the following picture (from the official YouTube live).</p> <p><img src="/images/2019-11-04-D.png" alt="Graph of D" /></p> <p>Then, the problem will be translated as follows.</p> <blockquote> <p><strong>Problem D.1</strong>: Rearrange $I$ to minimize $(M, r) \in N \times [0, 1)$ so that there exists $k \in N$ so that $\sum _ {i = k} ^{N - 1} A _ i \leq \sum _ {i = k} ^ {M - 1} B _ i + r B _ M. \tag{D.1}$</p> </blockquote> <p>Let $C _ i = \max(A _ i, B _ i)$. We will show that Problem D.1 is equivalent to the following problem.</p> <blockquote> <p><strong>Problem D.2</strong>: Rearrange $I$ to minimize $(M, r) \in N \times [0, 1)$ so that $\sum _ {i = 0} ^{N - 1} A _ i \leq \sum _ {i = 0} ^ {M - 1} C _ i + r B _ M. \tag{D.2}$</p> </blockquote> <p>Let $(M, r) \in N \times [0, 1)$ satisfy the constraints of Problem D.1. There exists $k \in N$ satisfying (D.1). Then, (D.2) immediately follows by the definition of $C _ i$. Next, let $(M, r) \in N \times [0, 1)$ satisfy the constraints of Problem D.2. We can rearrange the order of $I$ so that there exists $1 \leq k \leq N$ so that $C _ i = \begin{cases} A _ i &amp; i &lt; k, \\\ B _ i &amp; i \geq k. \end{cases}$ Then, (D.2) becomes (D.1), which completes the proof.</p> <h3 id="solution-3">Solution</h3> <p>We can solve Problem D.2 to try all $B _ i$ for $b = B _ M$. Fix $b = B _ M$. Then, the best strategy to minimize $M$ is that we choose $C _ i$ in the descending order. We can do this greedy algorithm in $O(\log N)$-time for each $b$ by binary search if we prepare partial sums in $O(N)$-time. Then, we compute $r$ as follows. $r = \max \left(0, \frac{\sum _ {i = 0} ^ {N - 1} A _ i - \sum _ {i = 0} ^ {M - 1} C _ i}{B _ M} \right).$ In this case, the answer is $\frac{N - M - r}{N}.$ We compute the maximum of them.</p> <p>We have to care one type of corner cases. If we have $\sum _ {i = 0}^ {N - 1} A _ i = \sum _ {i = 0}^ {N - 1} C _ i,$ the answer is <code class="language-plaintext highlighter-rouge">0 1</code>. These cases are not necessarily corner cases if the implement is good, but be careful.</p> <h2 id="e---prefix-suffix-addition"><a href="https://atcoder.jp/contests/agc040/tasks/agc040_e">E - Prefix Suffix Addition</a></h2> <p>We use $a[i]$ instead of $A[i]$. I should have made correct pictures. We use $0$-indexed, but, without loss of generality, we insert $a = 0$ and $a[N + 1] = 0$.</p> <h3 id="preliminary">Preliminary</h3> <p>We are given two operations. But <strong>first of all, we consider only “Operation 1”</strong>. How many times we need to meet the objective? Of course we can construct arbitrary $\{ a[i] \}$ by $N$ times. Just make $a[i]$ for each manipulation. But we may or may not reduce the number of it. Let’s consider the following sequence.</p> <p>$0, 2, 1, 3, 4, 6, 5, 0.$</p> <p>Here we point out “$2, 1$”. We need at least a pair of operations to make this since $2 &gt; 1$. We can find two more points: $6 &gt; 5 &gt; 0$. So we have to use the operation at least three times.</p> <p>On the other hand, three times is suffice to make this sequence. We can construct as follows. \begin{align} &amp; 0, 2, \\\ &amp; 0, 0, 1, 3, 4, 6, \\\ &amp; 0, 0, 0, 0, 0, 0, 5. \end{align}</p> <p>Generally speaking, we can show that, to obtain the sequence $\{ a[i] \}$, we need exactly $x$ operations, where $x$ is the number of $i$ with $a[i] &gt; a[i + 1]$.</p> <p>We can say the same thing for “Operation 2”. we need $y$ times, where $y$ is the count of “$&lt;$”.</p> <h3 id="translate-the-problem-into-another">Translate the problem into another</h3> <p>By the observation above, we see that all we have to do is solve the following problem.</p> <blockquote> <p><strong>Problem E.1</strong>: We will split each $a[i]$ into two nonnegative integers $b[i]$ and $c[i]$ with $a[i] = b[i] + c[i]$. Find the minimum count of $i$ with $b[i] &gt; b[i + 1]$ plus $i$ with $c[i] &lt; c[i + 1]$.</p> </blockquote> <p>We regard an “$i$ with $b[i] &gt; b[i + 1]$” and an “$i$ with $c[i] &lt; c[i + 1]$” as a penalty. We regard the number of the accumulated penalties as a distance.</p> <p>It is natural for us to convert Problem E.1 into DP. Obviously, we can forget the details $j &lt; i$ when considering $i \mapsto i + 1$. How much information do we need? Before estimating the time or memory complexity, we observe the transaction.</p> <h3 id="details-of-dp">Details of DP</h3> <p>To say the conclusion first, we possess the states by <code class="language-plaintext highlighter-rouge">vector&lt;map&lt;int, int&gt;&gt; M;</code>.</p> <blockquote> <p>$M[i][h] =$ the minimum distance between $0$ and $i$ with the height $b[i] = h$; $c[i] = a[i] - h$.</p> </blockquote> <h4 id="initial-state">Initial State</h4> <p>We start by $M = 0$; otherwise, $M[i] = \emptyset$.</p> <h4 id="answer">Answer</h4> <p>The answer is $M[N + 1]$.</p> <h4 id="transaction">Transaction</h4> <p>Here we promise if $M[i][k]$ is empty, we skip it of course. In addition, we regard $M[i + 1][k] = \infty$ if it is empty.</p> <p>For $i = 0, \dots, N$, for $(h, d) \in M[i]$, we have two types of transaction; the case $a[i] \leq a[i + 1]$ and the case $a[i] \geq a[i + 1]$.</p> <p>For both cases, we will split $a[i + 1] = b[i + 1] + c[i + 1]$; $h’ = b[i + 1]$. Remember that if $h &gt; h’$, we have $1$ penalty; if $a[i] - h &lt; a[i + 1] - h’$, we have $1$ penalty. So we see that $h’$ should be the lowest as long as we have the same distance. <strong>Just memorizing the lowest height is suffice.</strong></p> <p>Let’s see the former case $a[i] \leq a[i + 1]$, which is the easier case.</p> <p>\begin{align} M[i + 1][a[i + 1] - (a[i] - h)] &amp; \gets \min(M[i + 1][a[i + 1] - (a[i] - h)], d), \\\ M[i + 1][h] &amp; \gets \min(M[i + 1][h], d + 1), \\\ M[i + 1] &amp; \gets \min(M[i + 1], d + 2). \end{align}</p> <p><img src="/images/2019-11-04-E1.png" alt="E high" /></p> <p>The latter case $a[i] \geq a[i + 1]$ is a little bit complicated.</p> <p>\begin{align} M[i + 1][h] &amp; \gets \min(M[i + 1][h], d) &amp; &amp; \text{ if } h \leq a[i + 1], \\\ M[i + 1][k] &amp; \gets \min(M[i + 1][k], d + 1) &amp; &amp; \text{ where } k = \max(0, a[i + 1] - (a[i] - h)), \\\ M[i + 1] &amp; \gets \min(M[i + 1], d + 2). &amp; &amp; \end{align}</p> <p><img src="/images/2019-11-04-E2.png" alt="E low" /></p> <p>After the all transaction for $M[i] \mapsto M[i + 1]$, <strong>we erase unneeded elements on $M[i + 1]$</strong>. We consider $(h, d) \in M[i + 1]$ in the ascending order. If we find a pair $\{ (h, d), (h’, d’) \}$ with $h \leq h’$ and $d \geq d’$, we can erase $(h’, d’)$ since $(h, d)$ is a better state than it.</p> <h3 id="time-complexity">Time complexity</h3> <p>Now we can show the following lemma.</p> <blockquote> <p><strong>Lemma E.2</strong>: For each $i$, $M[i]$ has at most three elements: $M[i] = \{ (0, d + 2), (h _ 1, d + 1), (h _ 2, d) \}, \tag{E.1}$ with $0 \leq h _ 1 \leq h _ 2 \leq A[i]$.</p> </blockquote> <p>Proof: We show this lemma by mathematical induction on $i$. If $i = 0$, it is done immediately. Suppose the case $i$ is true. From $(h _ 2, d) \in M[i]$ in (E.1), we create $(0, d + 2) \in M[i + 1]$. we cannot create $(h’, d’)$ with $d’ &lt; d$ by the transaction rule and our current assumption. Then, after erasing, we have at most three elements in $M[i + 1]$ where $d$, $d + 1$ and $d + 2$ as its second value, which completes the proof.</p> <p>So each transaction takes $O(1)$-time. The total time complexity is $O(N)$.</p> <h2 id="f---two-pieces"><a href="https://atcoder.jp/contests/agc040/tasks/agc040_f">F - Two Pieces</a></h2> <h3 id="preliminary-1">Preliminary</h3> <p>First, here we promise that the two pieces are indistinguishable and the two manipulation for $(x, x + 1) \to (x + 1, x + 1)$ are indistinguishable, too.</p> <p>Instead of considering $(A, B)$ with $A \leq B$, we consider the pair of nonnegative integers $(x, d)$ where $x = B$ and $d = B - A$. Then, there are three manipulations here.</p> <ol> <li>$x \mathbin{ {+} {+} }$; $d \mathbin{ {+} {+} }$;</li> <li>$d \mathbin{ {-} {-} }$; only when $d \geq 2$.</li> <li>$d = 0$;</li> </ol> <p>They are distinguishable because we have restricted the manipulation 2. when $d \geq 2$. We use $X, Y, Z$ to denote the number of the manipulations of 1., 2., 3., respectively. We immediately see that $X + Y + Z = N$ and $X = B$.</p> <h3 id="observation-1">Observation</h3> <p>Let’s consider <strong>the transition of $d$ without the manipulation 3.</strong> We fix $Y$, which runs $0 \leq Y \leq \min(A, N - B, B - 1)$. We have to make these $X + Y$ elements in a good order, whose number is equal to $\mathrm{Catalan}(X - 1, Y)$, so as not to break the condition for the manipulation 2., that is, $d \geq 2$. We draw a picture of the transition of $d$ as below. Let $D$ be the resulting $d$, that is, $D = B - A$. The final height of $d$ in the picture is equal to $X - Y$.</p> <p><img src="/images/2019-11-04-F.png" alt="Graph" /></p> <p>Then, where can we insert the manipulations 3? The number of those is $Z$. First, we must insert at least one element into the <strong>final</strong> point where the height of $d$ is equal to $X - Y - D$ to make the resulting $d$ is equal to $D$. Why must it be final? Because otherwise we will break the rule of $d \geq 2$ for the manipulation 3. Second, we may insert any elements into the <strong>final</strong> point where the height of $d$ is less than or equal to $X - Y - D$. Why it must be final is the same before.</p> <p>Therefore, what we have to do is divide $Z - 1$ elements into $X - Y - D + 1$ parts. The count of that possible division is as follows. $\begin{pmatrix} (X - Y - D + 1) + (Z - 1) - 1 \\\ (X - Y - D + 1) - 1 \end{pmatrix}. \tag{F.1}$ Here, we promise that out of range is $0$.</p> <h3 id="corner-cases">Corner Cases</h3> <p>We have to be careful for two types of the corner cases.</p> <p>One is simple. If $A = B = 0$, we just flush $1$ instead of $0$.</p> <p>The other is rather difficult. Consider what happens given $Z = 0$. In normal cases, the count is $0$ because we need at least one element to adjust the resulting $d$. But if $X - Y = D$, we don’t need any element for manipulation 3. So in this case, instead of (F.1) being $0$, we use $1$.</p> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Grand Contest 040HACK TO THE FUTURE 2020 予選2019-11-02T00:00:00+09:002019-11-02T00:00:00+09:00http://kazune-lab.net/contest/2019/11/02/future-contest-2020-qual<p><a href="https://atcoder.jp/contests/future-contest-2020-qual">HACK TO THE FUTURE 2020 予選</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/future-contest-2020-qual/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2019/1102_future-contest-2020-qual">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---ロボットの誘導"><a href="https://atcoder.jp/contests/future-contest-2020-qual/tasks/future_contest_2020_qual_a">A - ロボットの誘導</a></h2> <blockquote class="twitter-tweet" data-lang="ja"><p lang="en" dir="ltr"><a href="https://twitter.com/hashtag/HTTF?src=hash&amp;ref_src=twsrc%5Etfw">#HTTF</a> First I thought that minus scores are appeared in the standings, which was actually wrong. AC solutions with minus scores were regarded as just WAs. Thus, I made up my mind to get the minimum score, that is, 1.</p>&mdash; Kazune Takahashi (@kazune_lab) <a href="https://twitter.com/kazune_lab/status/1190635112192331776?ref_src=twsrc%5Etfw">2019年11月2日</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <blockquote class="twitter-tweet" data-conversation="none" data-lang="ja"><p lang="en" dir="ltr">Then, I focused on the total score is just the sum of the 50 cases. The 47 cases were hidden, but fixed. I decided that I obtained minus scores on the other 49 cases which was totally roughly -50,000 points and then reach the plus score on the first case which was not hidden.</p>&mdash; Kazune Takahashi (@kazune_lab) <a href="https://twitter.com/kazune_lab/status/1190635117972049921?ref_src=twsrc%5Etfw">2019年11月2日</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <blockquote class="twitter-tweet" data-conversation="none" data-lang="ja"><p lang="en" dir="ltr">Actually I succeeded in gaining the minus score, -55,791 points as the sum of the 49 cases then I found a solution for 58,212 points for the first example by a randomly way. Then I filled unneeded panels to lose my point down to 55,792 points. Thus the total score was 1.</p>&mdash; Kazune Takahashi (@kazune_lab) <a href="https://twitter.com/kazune_lab/status/1190635119217823744?ref_src=twsrc%5Etfw">2019年11月2日</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <h1 id="others">Others</h1>Kazune TakahashiHACK TO THE FUTURE 2020 予選A quiz from Iroha-chan2019-10-24T00:00:00+09:002019-10-24T00:00:00+09:00http://kazune-lab.net/diary/2019/10/24/iroha<h2 id="problem">Problem</h2> <blockquote class="twitter-tweet" data-lang="ja"><p lang="ja" dir="ltr">そういえば、OpenCupで出てきた問題が面白かったので紹介しておきますね。ギャグ問題枠ですけどね<br /><br />AliceとBobがそれぞれN個ずつの整数A,Bを持っています。二人が交互に整数を一つずつ捨てていき、お互いが残り1つになった時に、Aliceは差を最大化、Bobは最小化したいです。最終的な差を求めなさい。</p>&mdash; 古寺いろは@競プロ応援アカウント (@codera_iroha) <a href="https://twitter.com/codera_iroha/status/1187218314520485888?ref_src=twsrc%5Etfw">2019年10月24日</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <blockquote class="twitter-tweet" data-conversation="none" data-lang="ja"><p lang="ja" dir="ltr">制約は、Nが10万以下。A,Bの要素は10億以下くらいでしょうか？<br />例を挙げると、<br />A = {4, 6, 11, 18}, B = {1, 10, 11, 20}<br />とかで、例えば、A:4, B:1, A:6, B:10, A:11, B:20の順に捨てると、残りが18と20になって差が2ですね。この入出力例の本当の答えは4なのですけど。</p>&mdash; 古寺いろは@競プロ応援アカウント (@codera_iroha) <a href="https://twitter.com/codera_iroha/status/1187218955603066880?ref_src=twsrc%5Etfw">2019年10月24日</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <p>We call 「捨てる」 “burning” in English here.</p> <h2 id="solution">Solution</h2> <p>Hereinafter, $A$ and $B$ are multi-sets. Let $f \colon A \to B$ a function so that</p> <blockquote> <p>$f(a) = \mathop{\mathrm{argmin}} _ {b \in B} \lvert a - b \rvert$. If there are several $b \in B$ that attain the minimum, we can choose any of them.</p> </blockquote> <p>Firstly, we consider the best strategy for A. It’s very simple. For each turn, A should burn $a’ = \mathop{\mathrm{argmin}} _ {a \in A} \lvert a - f(a) \rvert$; otherwise, if A still holds $a’$ at the final state, B can keep $b’ = f(a’)$ so that B can attain $\lvert a’ - b’ \rvert = \min _ {a \in A} \lvert a - f(a) \rvert = \min _ {a \in A} \min _ {b \in B} \lvert a - b \rvert \leq \lvert a - b \rvert$ for any $a \in A$ and $b \in B$ at the moment, which is not the best strategy for A.</p> <p>Secondly, we consider the best strategy for B. It’s very simple, too. Just after the turn of A, it follows that $\sharp A &lt; \sharp B$. Thus $f(A) \neq B$ but $f(A) \subset B$. Thus there exists an element $b’ \in B \setminus f(A)$. B should burn $b’$. Let’s see what happens when B burn something form $B$. For any $a \in A$, $\lvert a - f(a) \rvert$ is no less than before since $B$ becomes smaller. If B burns $b’$, B does not change $f(a)$ at all, which is the best strategy for B.</p> <p>Finally, we come to the conclusion. A burns $a’ = \mathop{\mathrm{argmin}} _ {a \in A} \lvert a - f(a) \rvert$ for each time and B burns an element of $B$ so that no $f(a)$ is changed. Therefore, A burns elements of $A$ in the ascending order of $\lvert f(a) - a \rvert$. Thus the answer is $\max _ {a \in A} \lvert f(a) - a \rvert$ for the first $A$. We can compute all $f(a)$ for $O(\lvert A \rvert)$ since the candidates of $f(a)$ is just two element for each $a$.</p> <h2 id="others">Others</h2> <p>I considered this problem in my rest time for lunch, but I came up with the solution above in over 100 minutes. Very late! But fortunately, a colleague of mine was working on his own branch and I was waiting for his work and my work at the moment was reading source codes. So I wrote this article in working time.</p>Kazune TakahashiProblemI donated to KyoAni2019-10-23T00:00:00+09:002019-10-23T00:00:00+09:00http://kazune-lab.net/diary/2019/10/23/kyoani<p><a href="/diary/2019/07/26/laborify/">As I promised before</a>, I donated the fee for my article to Kyoto Animation.</p> <p><img src="/images/2019-10-23-kyoani.png" alt="donate" /></p> <p>I was so late to donate to KyoAni. This was not because the payment from Laborify had been late (actually it had been done so quickly), but because I didn’t have enough money at that time. Now I have some money to live. Thus I decided to donate at the moment.</p> <p>As you know, the donation is taken away from national tax. It is until Oct. 31th, 2019. If you also want to donate to KyoAni, I recommend you to as soon as possible. The following is the explanation how to donate without tax.</p> <ul> <li><a href="https://www.pref.kyoto.jp/chiiki/news/higaisyagienkin.html">京都府京都市伏見区で発生した放火事件に係る被害者義援金の受入れについて</a></li> </ul>Kazune TakahashiAs I promised before, I donated the fee for my article to Kyoto Animation.Kyoto University Programming Contest 20192019-10-13T00:00:00+09:002019-10-13T00:00:00+09:00http://kazune-lab.net/contest/2019/10/13/kupc2019<p><a href="https://atcoder.jp/contests/kupc2019">Kyoto University Programming Contest 2019</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/kupc2019/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2019/1013_KUPC2019">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---november-festival"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_a">A - November Festival</a></h2> <h2 id="b---ナップサック問題"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_b">B - ナップサック問題</a></h2> <h2 id="c---てんびんばかり"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_c">C - てんびんばかり</a></h2> <h2 id="d---maximin-game"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_d">D - Maximin Game</a></h2> <h2 id="e---根付き森二人用ゲーム"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_e">E - 根付き森二人用ゲーム</a></h2> <h2 id="f---カズマ王国の陥落"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_f">F - カズマ王国の陥落</a></h2> <h2 id="g---abcのg問題"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_g">G - ABCのG問題</a></h2> <h2 id="h---123パズル"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_h">H - 123パズル</a></h2> <h2 id="i---encodedecode-2019"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_i">I - encode/decode 2019</a></h2> <h2 id="j---link-cut-tworee"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_j">J - Link-cut tworee</a></h2> <h2 id="k---one-or-all"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_k">K - One or All</a></h2> <h2 id="l---タケノコ"><a href="https://atcoder.jp/contests/kupc2019/tasks/kupc2019_l">L - タケノコ</a></h2> <h1 id="others">Others</h1>Kazune TakahashiKyoto University Programming Contest 2019A memo for my upgrading to macOS Catalina2019-10-12T00:00:00+09:002019-10-12T00:00:00+09:00http://kazune-lab.net/diary/2019/10/12/catalina<p>I started upgrading my mac to macOS Catalina in 12th Oct. 2019.</p> <h2 id="initial-step">Initial step</h2> <p>I downloaded 8GB and started installing. After starting, I went to bed.</p> <p>After my getting up, it needed to be rebooted. Then, I waited for completing the install.</p> <ul> <li><a href="https://support.apple.com/en-us/HT204108">If your Mac is stuck on the ‘Setting Up Your Mac’ screen</a></li> </ul> <h2 id="routine-work">Routine work</h2> <h3 id="xcode-command-line-tool">Xcode command line tool</h3> <p>I just executed <code class="language-plaintext highlighter-rouge">xcode-select --install</code>.</p> <h3 id="tex">TeX</h3> <ul> <li><a href="https://texwiki.texjp.org/?ヒラギノフォント#macos-hiragino-setup">macOS Catalina / macOS Mojave / macOS High Sierra / macOS Sierra / OS X El Capitan に付属するヒラギノフォントのセットアップ</a></li> </ul> <h3 id="homebrew">HomeBrew</h3> <p>Seeing <code class="language-plaintext highlighter-rouge">brew doctor</code>, I followed the suggestion.</p> <h3 id="disable-全角スペース">Disable 全角スペース</h3> <ul> <li><a href="https://qiita.com/on0z/items/a9b4033cc088627b1728">macOS Catalina (Mojave, High Sierra, Sierra)で日本語入力時に常にスペースを半角に</a></li> </ul> <h3 id="ruby">Ruby</h3> <p>There was no Ruby interpreter except the system. Thus I reinstalled the latest Ruby by <code class="language-plaintext highlighter-rouge">rbenv</code>. I executed <code class="language-plaintext highlighter-rouge">rbenv install 2.6.5</code>.</p>Kazune TakahashiI started upgrading my mac to macOS Catalina in 12th Oct. 2019.AtCoder Grand Contest 0392019-10-05T00:00:00+09:002019-10-05T00:00:00+09:00http://kazune-lab.net/contest/2019/10/05/agc039<p><a href="https://atcoder.jp/contests/agc039">AtCoder Grand Contest 039</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/agc039/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2019/1005_AGC039">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---connection-and-disconnection"><a href="https://atcoder.jp/contests/agc039/tasks/agc039_a">A - Connection and Disconnection</a></h2> <h3 id="basic-strategy">Basic Strategy</h3> <p>Suppose we have a function $f(S)$ that flushes the correct answer. Then, we can <em>basically</em> see that the answer is described as the following form: $f(KS) = A(K - 1) + B$, where $A, B$ is a constant.</p> <p>This is seen through the construction of $f$. We write $N = \lvert S \rvert$. For $i = 1, \dots, N - 1$, if $S[i - 1] = S[i]$, we change $S[i]$ for some character that must be different from $S[i + 1]$, such as <code class="language-plaintext highlighter-rouge">#</code>. This greedy method works well if there exists $j$ so that $S[j - 1] \neq S[j]$. We divide $S$ into $T + U$ and we have $f(KS) = f(T) + f((K - 1) (U + T) ) + f(U) = f(T) + f(U) + (K - 1) f(U + T).$ So we set $B = f(T) + f(U) = f(S)$ and $A = f(U + T)$.</p> <p>To write the code, it is easy for us to compute $B = f(S)$ and $A + B = f(2S)$.</p> <h3 id="corner-cases">Corner Cases</h3> <p>If the string is composed only by one character, the strategy above doesn’t work. We can see this problem by the single character <code class="language-plaintext highlighter-rouge">S = "a"</code>. We have $f(a) = 0$, $f(aa) = 1$, $f(aaa) = 1$, $f(aaaa) = 2$, $f(aaaaa) = 2$, and so on.</p> <p>In this case the answer is $NK/2$.</p> <h2 id="b---graph-partition"><a href="https://atcoder.jp/contests/agc039/tasks/agc039_b">B - Graph Partition</a></h2> <h3 id="when-can-we-construct-the-solution">When can we construct the solution?</h3> <p>First, let us show the following lemma.</p> <blockquote> <p><strong>Lemma B.1</strong>: We can construct the solution if and only if the graph is a bipartite graph.</p> </blockquote> <p>Proof. Suppose we have constructed the solution $\{ V _ i \}$. We color each $e \in V _ i$ red if $i$ is odd and blue if $i$ is even. Then, the constraints confirm that each edge must connect a red vertex and a blue vertex. That means the graph is bipartite. Suppose the graph is a bipartite graph. Then, we can easily construct $V _ 0$ which contains all red vertexes and $V _ 1$ which contains all blue vertexes.</p> <p>So we have to decide if the graph is a bipartite graph or not. If not, the answer is <code class="language-plaintext highlighter-rouge">-1</code>.</p> <h3 id="the-answer">The answer</h3> <p>We show the next lemma.</p> <blockquote> <p><strong>Lemma B.2</strong>: Suppose the graph is a bipartite graph. Then, the answer is the diameter $+1$.</p> </blockquote> <p>Proof. Let $D$ be the diameter. By Lemma B.1, we can construct $\{ V _ i \}$. Assume to the contrary that the answer is greater than $D + 1$. Then, we can take a path whose length is greater than or equal to the answer by the graph’s connectedness. This contradicts the definition of the diameter. Thus the answer must be less than or equal to $D + 1$. In addition, we can construct $V _ 0, \dots, V _ D$ as follows. We take a pass $v _ 0, \dots, v _ D$ that attain the diameter. We set $V _ i$ as follows.</p> <blockquote> <p>$V _ i =$ the set of the vertexes whose distance with $v _ 0$ is $i$.</p> </blockquote> <p>Obviously, $v _ i \in V _ i$, especially $v _ D \in V _ D$. For any edge $e = (u, v)$, the difference of the distance between $u$ and $v _ 0$ and the distance between $v$ and $v _ 0$ is less than or equal to $1$. If it were $0$, it would break the bipartite property. Thus the difference is $1$, which satisfies the constraints.</p> <h3 id="how-to-compute-the-diameter">How to compute the diameter</h3> <p>It is well-known that how to compute the diameter of a tree. We just do DFS twice and the time complexity is $O(N)$. But this problem, the graph is not necessarily a tree. We can compute, however, its diameter by definition: $D = \max _ {u, v} d(u, v).$ We use the Warshall-Floyd algorithm to calculate $d(u, v)$ for all $u$ and $v$ in $O(N^3)$-time.</p> <h2 id="c---division-by-two-with-something"><a href="https://atcoder.jp/contests/agc039/tasks/agc039_c">C - Division by Two with Something</a></h2> <h3 id="examine-some-samples">Examine some samples</h3> <p>The manipulation is described in binary as follows.</p> <ul> <li>$[…]0$ -&gt; $1[…]$,</li> <li>$[…]1$ -&gt; $0[…]$.</li> </ul> <p>Thus every binary whose size is $N$ returns its initial state by $2N$ times. But we want to know the minimum number of manipulations. The period seems to be critical. Let’s see two cases.</p> <ul> <li>$1010$ -&gt; $1101$ -&gt; $0110$ -&gt; $1011$ -&gt; $0101$ (the conjugation of the initial state) -&gt; -&gt; -&gt; -&gt; $1010$. We have taken $8$ times now.</li> <li>$111000111$ -&gt; $011100011$ -&gt; $001110001$ -&gt; $000111000$ (the conjugation of the initial state) -&gt; -&gt; -&gt; $111000111$. We have taken $6$ times this time.</li> </ul> <p>Therefore, we see the following.</p> <blockquote> <p><strong>Proposition C.1</strong>: Let $T = S \bar{S} S \bar{S} S \dots S$ be a binary whose length is $N$. Then, $T$ returns by manipulating $2 \lvert S \rvert$ times.</p> </blockquote> <h3 id="solution">Solution</h3> <p>For $K = 1, \dots, N$, we check $N \% K = 0$ and $K$ is odd. If so, let $L = N/K$ and $S$ be the prefix of $X$ whose length is $L$.</p> <p>First, we consider $S$ as a binary number. Then, for $S’ = 0, \dots, S - 1$, we consider $T = S’ \bar{S’} S’ \dots S’$ by repeating $S$ and $S’$ for $K$ times. It follows that $T &lt; X$, both as strings and as binaries. Thus we have obtained $c = S$ binaries which needs $2K$ times manipulations.</p> <p>Second, we consider $T = S \bar{S} S \dots S$. If $T \leq X$, we have another good binary, so the count will be added by $c \mathbin{ {+} {=} } 1$; otherwise nothing more is needed.</p> <p>We hold the results $(L, c)$ above as <code class="language-plaintext highlighter-rouge">map&lt;int, mint&gt;</code>. There are “double-count problems” here. We can solve this by very easy way. For example, we count $S = 010101$ as $6$ period, it must have been counted as $2$, and every $2$-period $S$ have been counted as $4, 6, 8, 10, \dots$. So by the ascending order of $L$, we subtract the counts for $2K, 3K, 4K, \dots$ by that number. This is the same way as the sieve of Eratosthenes. We have eliminated “double-count problems”.</p> <p>The answer is $\sum _ {(L, c) \in map} 2Lc.$ The time complexity is $O(PN + N \log \log N)$, where $P$ is the number of the $K$ so that $N \% K = 0$ and $K$ is odd. Since $N \leq 2 \times 10^5$, $P$ is very small. Actually it holds that $P \leq 72$.</p> <h2 id="d---incenters"><a href="https://atcoder.jp/contests/agc039/tasks/agc039_d">D - Incenters</a></h2> <h3 id="terms">Terms</h3> <table> <thead> <tr> <th>日本語</th> <th>English</th> </tr> </thead> <tbody> <tr> <td>内心</td> <td>incenter</td> </tr> <tr> <td>重心</td> <td>centroid</td> </tr> <tr> <td>外心</td> <td>circumcenter</td> </tr> <tr> <td>垂心</td> <td>orthocenter</td> </tr> <tr> <td>傍心</td> <td>excenter</td> </tr> <tr> <td>二等分線</td> <td>bisection</td> </tr> </tbody> </table> <h3 id="solution-1">Solution</h3> <p>Let $O = (0, 0)$ be the central point of the unit circle $K$. On $K$, we take three different points $A, B, C$. Let $I$ be the incenter of $\triangle ABC$. Let $A’$ be the central point of the arc $BC$ without $A$. We define $B’, C’$ as the similar way. Then, we see that $I$ is the orthocenter of $\triangle A’B’C’$. Note that $O$ is the circumcenter of $\triangle A’B’C’$, too. Let $G$ be the centroid of $\triangle A’B’C’$. Here, we use the Euler line, i.e. $3\vec{OG} = \vec{OI}$. Therefore, we conclude that $\vec{OI} = 3\vec{OG} = \vec{OA’} + \vec{OB’} + \vec{OC’}. \tag{C.1}$</p> <p>We fix $B$ and $C$. Then $A’$ is also fixed, provided that the relative positions of $A, B, C$ are the same. We count how many points are on the two arcs $BC$ respectively and add them up to $A’$. This solution works in $O(N^2)$-time.</p> <h2 id="e---pairing-points"><a href="https://atcoder.jp/contests/agc039/tasks/agc039_e">E - Pairing Points</a></h2> <h2 id="f---min-product-sum"><a href="https://atcoder.jp/contests/agc039/tasks/agc039_f">F - Min Product Sum</a></h2> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Grand Contest 039AtCoder Beginner Contest 1422019-09-28T00:00:00+09:002019-09-28T00:00:00+09:00http://kazune-lab.net/contest/2019/09/28/abc142<p><a href="https://atcoder.jp/contests/abc142">AtCoder Beginner Contest 142</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/abc142/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2019/1012_ABC142">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---odds-of-oddness"><a href="https://atcoder.jp/contests/abc142/tasks/abc142_a">A - Odds of Oddness</a></h2> <p>Just calculate by <code class="language-plaintext highlighter-rouge">for</code>-loop.</p> <h2 id="b---roller-coaster"><a href="https://atcoder.jp/contests/abc142/tasks/abc142_b">B - Roller Coaster</a></h2> <p>Just calculate by <code class="language-plaintext highlighter-rouge">for</code>-loop, too.</p> <h2 id="c---go-to-school"><a href="https://atcoder.jp/contests/abc142/tasks/abc142_c">C - Go to School</a></h2> <p>We use <code class="language-plaintext highlighter-rouge">vector&lt;tuple&lt;int, int&gt;&gt; V;</code>. We insert $(A _ i, i)$into $V$ and sort it.</p> <h2 id="d---disjoint-set-of-common-divisors"><a href="https://atcoder.jp/contests/abc142/tasks/abc142_d">D - Disjoint Set of Common Divisors</a></h2> <h2 id="e---get-everything"><a href="https://atcoder.jp/contests/abc142/tasks/abc142_e">E - Get Everything</a></h2> <h2 id="f---pure"><a href="https://atcoder.jp/contests/abc142/tasks/abc142_f">F - Pure</a></h2> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Beginner Contest 142