AtCoder Beginner Contest 144

Updated:

AtCoder Beginner Contest 144

Source codesPermalink

SolutionsPermalink

A - 9x9Permalink

If A9 and B9, flush AB. Otherwise, 1.

B - 81Permalink

Try all possibilities.

C - Walk on Multiplication TablePermalink

We can try all possibilities. Since the distance between (1,1) and (i,j) is equal to that between (1,1) and (j,i), we can assume that ij without loss of generality. Therefore, it suffice to try i106+10.

D - Water BottlePermalink

SolutionPermalink

This is a elementary trigonometric function problem.

First of all, we use y=x/a to convert this problem into 2-dimensional problem.

Let θ be the resulting angle. We consider the limit of θ.

If yab/2, we have y=S(θ)=ab12a2tanθ.

If yab/2, we have y=S(θ)=b22tanθ.

Whether we use (D.1) or (D.2), we can determine θ by binary search since the greater θ is, the smaller S(θ) is. At first we set lb=ϵ and ub=π/2ϵ for some fixed small ϵ and it suffice to try 100 times.

ImplementPermalink

To count times, we use a variable which will not appear inside {}, e.g. for (auto q{0}; q < 100; ++q).

E - GluttonyPermalink

ObservationPermalink

Let xi be the consumption coefficient of Member i and yi be the difficulty of the food Member i eats. Then, the total score is S=maxiNxiyi We want to minimize S. How should we assign foods to each Member i? This is obvious: we sort {xi} in ascending order and {yi} in descending order.

Then, we count the trainings into account. The less score we aim, the more training we need. Thus, this can be solved by binary search.

SolutionPermalink

Let ng=1 and ok=1012+10. We prepare

f(k)= how many trainings we need to achieve the score k.

This is calculated as follows. f(k)=iN(xikyi).

Here is an example of the implement.

  auto f = [&](ll k) {
    ll cnt{0};
    for (auto i = 0; i < N; ++i)
    {
      cnt += max(0LL, A[i] - k / F[i]);
    }
    return cnt <= K;
  };

Then, Meguru’s binary search works as follows.

  ll ok{1'000'000'000'010LL};
  ll ng{-1LL};
  while (abs(ok - ng) > 1)
  {
    ll t{(ok + ng) / 2};
    if (f(t))
    {
      ok = t;
    }
    else
    {
      ng = t;
    }
  }
  cout << ok << endl;

F - Fork in the RoadPermalink

Assume 0-indexed. Note that this graph is a DAG since it is guaranteed that si<ti.

ObservationPermalink

Focusing on the differencePermalink

Let Pu be the set of the paths from 0 to u. Let Qv be the set of the paths from u to N1. We regard Pu and Qv as the set such as (p,l)Pu, where p is the probability in which we take the path and l be the length of the path. Later, it will be critical to distinguish original probability and conditional probability. Thus here we write down the strict definitions.

Notation F.1: We regard (p,l)Pu or (p,l)Qv as the set of the probability and the length of the path. More precisely,

  1. For (p,l)Pu, p is the original probability in which we choose this path starting from 0.
  2. For (p,l)Qv, p is the conditional probability in which we choose this path starting from v.

Here, we consider the path from u to v. We want to change the probability in which we choose this path. How much it affects the total expectation of the whole length in which Takahashi-kun will take?

Let δ be the difference of the probability and Δ be the difference of the whole expectation. Then we have Δ=(p,l)Pu(p,l)Qvpδp(l+1+l). Here, we define αu=(p,l)Pup,βv=(p,l)Qvp,Su=(p,l)Pupl,Tv=(p,l)Qvpl. Then, we have Δ=δ(βvSu+αuTv+αuβv).

How to obtain the answer?Permalink

The expectation of the total length is total=S[N1]. We can measure the efficiency blocking the passages (u,v) as follows.

Let V[u] be the set of the passages from u. Suppose that we have reached u. Then, the probability Takahashi-kun chooses each passage will be 1/|V[u]|. If |V[u]|=1, Aoki-kun cannot block it. Hereinafter we assume |V[u]|2. Assume that Aoki-kun has blocked the passage (u,v). We can regard this move as follows. The probability in which Takahashi-kun chooses (u,v) will be 0. And those related to other passages will be 1/(|V[u]|1). Both of these effects are measured by (F.2), since these manipulations will not affect each other.

To simplify the implement of the solution, first we sum up the effects of 1/(|V[u]|)1/(|V[u]|1). Then we add to it the effect 1/(|V[u]|1)0.

DP partPermalink

We have to calculate the four DP tables. The definitions are written in (F.1). The “answers” are the DP tables themselves. So we have to determine the initial state and the transition of each table.

αPermalink

Initial statePermalink

α[0]=1, otherwise 0.

TransitionPermalink

For i=0,,N2, for jV[i], we execute the following. α[j]+=1|V[i]|α[i].

ImplementPermalink
  void calc_alpha()
  {
    alpha[0] = 1.0;
    for (auto i = 0; i < N - 1; ++i)
    {
      for (auto j : V[i])
      {
        alpha[j] += prob(i) * alpha[i];
      }
    }
  }

βPermalink

By definition of p and β. β[v] is the probability in which we reach N1 starting from v when we have reached v. This is obviously 1 thank to the constraints. The result is β[v]=1 for all v.

SuPermalink

Initial statePermalink

S[u]=0 for all u.

TransitionPermalink

For i=0,,N2, for jV[i], we execute the following. S[j]+=α[i]1|V[i]|×1+1|V[i]|×S[i]. This is because, for the former part, we choose (i,j) in the probability α[i]×1/|V[i]| and, for the latter part, we add the expectation of the length through 0i to S[j].

ImplementPermalink
  void calc_S()
  {
    for (auto i = 0; i < N - 1; ++i)
    {
      for (auto j : V[i])
      {
        S[j] += alpha[i] * prob(i) * 1.0 + prob(i) * S[i];
      }
    }
  }

TvPermalink

Initial statePermalink

T[v]=0 for all v.

TransitionPermalink

For i=N2,,0, we can calculate T[i] as follows. T[i]=jV[i]1|V[i]|(1+T[j]).

ImplementPermalink
  void calc_T()
  {
    for (auto i = N - 2; i >= 0; --i)
    {
      for (auto j : V[i])
      {
        T[i] += prob(i) * (1.0 + T[j]);
      }
    }
  }

OthersPermalink