AtCoder Beginner Contest 167
Updated:
Source codesPermalink
SolutionsPermalink
A - RegistrationPermalink
Execute T.pop_back();
and check if S=T or not.
B - Easy Linear ProgrammingPermalink
Do as they indicate us to. Use auto tmp{min(a, k)};
and execute a -= tmp;
and k -= tmp;
.
C - Skill UpPermalink
Apply brute-force method by bitwise-operation.
D - TeleporterPermalink
SolutionPermalink
This is a problem about a functional graph. Assume 0-indexed.
We start 0 and then we will be involved in a cycle. We preserve when we visited v in visited[v]. Let t be the length of the cycle and l be the steps we need to be involved that cycle. We update visited and will see duplicated number n. We determine t=n−visited[v] and l=visited[v].
If k≤n, we have found the answer. Otherwise, we update k←(k−r)%t+r and search visited for the answer.
Another featuresPermalink
- We can determine t in O(1)-space and O(N)-time by Floyd’s tortoise and hare.
- NOMURA Programming Competition 2020 D - Urban Planning
E - Colorful BlocksPermalink
Assume 0-indexed.
SolutionPermalink
Let 0≤x≤min(K,N−1)=K be the number of the pairs of the same-colored adjacent blocks.
For each such pairs, we think of removing the right block. The number of ways of the removed blocks is (N−1x). Here, note that we cannot remove the 0-th block.
The (N−x) remaining blocks is colored so that all of the adjacent blocks are differently colored. Therefore, the number of ways to color the remaining blocks is M(M−1)N−x−1.
Hence, the answer is K∑x=0(N−1x)M(M−1)N−x−1.
F - Bracket SequencingPermalink
SolutionPermalink
We regard (
as +1 and )
as −1. We regard each string si as a pair of numbers (δi,mi), where Δi is the resulting cumulated sum and mi is the minimum value of constructive cumulated sums.
We think of connecting these strings. Let X be the cumulated sum so far. We can connect (δi,mi) if X+mi≥0. After that X←X+δi. In the final state, X=0 is needed. Note that the final value of X is not changed if we change the order of strings.
The following proposition gives us a solution.
Proposition F.1: The answer is “Yes” if and only if we arrange (δi,mi) as follows.
- We divide {(δi,mi)} into two sets: S+={(δi,mi)∣δi≥0},S−={(δi,mi)∣δi<0}.
- We sort S+ in mi’s descending order.
- We sort S− in mi−δi’s ascending order.
- Connect S+ with S−.
Proof of Proposition F.1Permalink
Lemma F.2: Assume that {(δi,mi)} satisfies the condition. Let S+ and S− defined as (F.1), preserving the order in each set. We connect S+ with S−. After this change of order, it satisfies the condition, too.
Proof: For each (δ−i,mi)∈S−, let J(i) be the number of (δ+j,nj)∈S+ where (δ+j,nj) is originally put before (δ−i,mi). For each (δ+j,nj)∈S+, let I(j) be the number of (δ−i,mi)∈S− where (δ−i,mi) is originally put before (δ+j,nj).
We have to check if, for any (δ,m)∈S+∪S−, it follows that X+m≥0 or not.
Let (δ+j,nj)∈S+. Then, we would like to verify the following. X=∑0≤k<jδ+k; X+nj≥0 From the assumption, it follows that X′=∑0≤k<jδ+k+∑0≤k<I(j)δ−k; X′+nj≥0. This is because the order inside S+ is preserved. Therefore, this inequality is satisfied since X′≤X.
Let (δ−i,mi)∈S−. Then, we would like to verify the following. X=∑0≤k<|S+|δ+k∑0≤k<iδ−k; X+mi≥0 From the assumption, it follows that X′=∑0≤k<J(i)δ+k∑0≤k<iδ−k; X′+mi≥0 This is because the order inside S− is preserved. Therefore, this inequality is also satisfied since X′≤X. We complete the proof.
Lemma F.3: Let S+ and S− be defined as (F.1). Suppose that the order generated by connecting S+ and S− satisfies the condition. We sort S+ by mi’s descending order and connect S+ with S−. Then, it also satisfies the condition.
Proof: For elements in S−, the condition is same, regardless of the order of S+. Let S+ be sorted as above. For each i=0,1,…,|S+|−1, we show that (δ+i,mi) is valid. Assume to the contrary that (δ+i,mi) is not valid. Then, it holds that X=∑0≤k<iδ+k; X+mi<0. By assumption, it follows that mk≤mi for k≥i. We go back to the original order. We take first (i+1) elements from S+. It contains (δ+k,mk) with k≥i. Let (δ+k,mk) be first-appeared element of them in the original order. Then, it holds that X′≤X. Thus, we have the following. X′+mk≤X+mk≤X+mi<0, which contradicts the fact that the original order satisfies the condition. We complete the proof.
Lemma F.4: Let S+ and S− be defined as (F.1). Suppose that the order generated by connecting S+ and S− satisfies the condition. We sort S− by mi−δi’s ascending order and connect S+ with S−. Then, it also satisfies the condition.
Proof: We think of the reverse version of this problem. We reverse si and we regard (
as −1 and )
as +1. Note that this manipulation is equivalent to the manipulation that we reverse the chart of cumulated sum. Then, we see that δi becomes −δi and mi becomes mi−δi.
We apply Lemma F.4 for this version to see that we sort S− in reversed-descending order of new mi, i.e., ascending order of mi−δi.
Proof of Proposition F.1: This is just a direct consequence from Lemmas F.2, F.3 and F.4.