AtCoder Beginner Contest 167

Updated:

AtCoder Beginner Contest 167

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=nvisited[v] and l=visited[v].

If kn, we have found the answer. Otherwise, we update k(kr)%t+r and search visited for the answer.

Another featuresPermalink

E - Colorful BlocksPermalink

Assume 0-indexed.

SolutionPermalink

Let 0xmin(K,N1)=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 (N1x). Here, note that we cannot remove the 0-th block.

The (Nx) 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(M1)Nx1.

Hence, the answer is Kx=0(N1x)M(M1)Nx1.

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+mi0. After that XX+δ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)δi0},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+m0 or not.

Let (δ+j,nj)S+. Then, we would like to verify the following. X=0k<jδ+k;    X+nj0 From the assumption, it follows that X=0k<jδ+k+0k<I(j)δk;    X+nj0. This is because the order inside S+ is preserved. Therefore, this inequality is satisfied since XX.

Let (δi,mi)S. Then, we would like to verify the following. X=0k<|S+|δ+k0k<iδk;    X+mi0 From the assumption, it follows that X=0k<J(i)δ+k0k<iδk;    X+mi0 This is because the order inside S is preserved. Therefore, this inequality is also satisfied since XX. 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=0k<iδ+k; X+mi<0. By assumption, it follows that mkmi for ki. We go back to the original order. We take first (i+1) elements from S+. It contains (δ+k,mk) with ki. Let (δ+k,mk) be first-appeared element of them in the original order. Then, it holds that XX. Thus, we have the following. X+mkX+mkX+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.

F reverse

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.

OthersPermalink