Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020

Updated:

日立製作所 社会システム事業部 プログラミングコンテスト 2020

Source codesPermalink

SolutionsPermalink

A - Hitachi StringPermalink

There are just 5 strings.

B - Nice ShoppingPermalink

The minimum is achieved either by choosing the cheapest refrigerator and the cheapest oven range or by using the discount coupons.

C - ThREEPermalink

SolutionPermalink

The condition ab=0 or a+b=0 is equivalent to (a,b)(1,1),(2,2) in Z/3Z.

A tree is a bipartite graph. We color each vertex by red or blue. Then, two vertexes whose distance is 3 must be colored differently.

Therefore, it is suffice that we assign numbers for vertexes so that there are no pair of red/blue vertex and assigned for (1,1) or (2,2). This is done by the following way.

  • The case both red and blue N/3: We assign 1 for red side and 2 for blue side as much as possible. Then assign 0 for others.
  • The case red <N/3: We assign 0 for red side. Then any numbers for others.
  • The case blue <N/3: Same as red.

D - Manga MarketPermalink

ObservationPermalink

Let ci=ai+1 and di=ai+bi+1. Then, the i-th shop is the machine that leaps from the time t to the time fi(t)=cit+di.

First, we show the following lemma.

Lemma D.1: Fix the indexes I of the shops that Takahashi-kun will visit. Then, the minimum time is achieved in the descending order of {fi}iI with the weight (ci1)/di. Here, the division is the operator in Q.

Proof: Assume to the contrary that the minimum time is achieved in another order of {fi}iI with the weight (ci1)/di. Then, there is at least one adjacent pair fi and fj with (ci1)/di<(cj1)/dj. We see that, in the current situation, we reach i-th shop at the time t and then go to j-th shop. The resulting time is T=fjfi(t)=cicjt+cjdi+dj. Swapping this order, however, we obtain the resulting time as follows. T=fifj(t)=cjcit+cidj+di. Then, we have TT=cjdi+djcidjdi>0, which contradicts the assumption. This completes the proof.

Therefore, we can solve this problem by DP in O(N2)-time.

SolutionPermalink

For speeding-up, we divide the shops into two sets; ci2 and ci=1. Note that, by Lemma D.1, we always consider visiting the shops with ci=1 after the shops with ci2.

For the shops with ci2, we try the DP above. Thanks to the constraint ci2, we just possess the second key up to C=32log2T.

Then, we visit some shops with ci=1 for each j=0,,C1. Obviously, we try di’s ascending order. This part can be done by partial_sum and upper_bound. The time complexity is O(NlogT+NlogN).

DefinitionPermalink

dp[i][j]= the minimum time we take to visit j stores considering up to the i-th store.

Initial StatePermalink

dp[0][0]=0, otherwise .

AnswerPermalink

dp[M][j] for j=0,,C1. Then, we try the shops with ci=1.

TransitionPermalink

For i=0,,M1, for j=0,,C1, we execute the following. dp[i+1][j]min(dp[i][j],fi(dp[i][j1])) Here, out-of-range will be considered as .

E - Odd Sum RectanglesPermalink

ObservationPermalink

We work on the 2-dimensional partial-sum grid instead of the original grid, i.e., V[i][j]=xiyjans[x][y]. We consider that all numbers are in F2. We will work on F2-vector space. V is 2N×2M. Since the elements are in F2, we can arbitrary put elements in F2 in each cell except on the 0-th line and column.

In the viewpoint of V, we consider 0i0<i1<2N and 0j0<j1<2M to see whether the sum of the four cells (i0,j0), (i1,j0), (i0,j1) and (i1,j1) is either 1 or 3, or not. We want to maximize the number of the 4-tuple whose sum is either 1 or 3. Thus, we think of the i0-th row and the i1-th row as two binaries. If we can achieve the Hamming distance between any two binaries is exactly 2M1, it attains its maximum.

SolutionPermalink

Without loss of generalities, we assume that NM. If we can construct the solution for the case N=M, We can create the solution for the case N<M since we can extract arbitrary 2N rows from the original solution. Hereinafter we assume that N=M.

We define xiF2M2 for iN as follows.

xi(j)= the i-th binary digit of j.

Let W=x0,,xN1. There are 2N elements in W.

We show the following lemma.

Lemma E.1: Let v,wW with vw. Then, the Hamming distance between v and w is 2M1.

Proof: We use d to denote the Hamming distance. We see d(v,w)=d(vw,0). Therefore, without the loss of generalities, we can assume w=0. Then, what we want to show is d(v,0)={i2Nv[i]=1}=2M1 for v0. Let v=iNaixi, where aiF2 for iN. Since v0, we can assume a0=1 by rearranging the order of xi. Let vk=ikaixi for k=1,,N. We will show d(vk,0)=2M1 by mathematical induction on k for finite version. For k=1, it is done since there are 2M1 elements whose i-th binary digit is 1, and the same for 0. We assume the condition is satisfied for k. If ak=0, it follows that d(vk+1,0)=d(vk,0). If ak=1, we consider the effect of xk. Let Xl={i2Nvk[i]=l} for l{0,1}. By the assumption, X0=X1=2M1. For both X0 and X1, Exactly the half elements has 1 as their k-th binary digit. Therefore, 2M2 elements are changed from 1 to 0 while 2M2 elements are changed from 0 to 1. Thus d(vk,0)=2M1. This completes the proof.

We note that W=2N from the binary point of view. Therefore, the conclusion is that it is suffice to set V[i][j]=popcount(i&j)F2. and we compute the following. ans[i][j]=V[i+1][j+1]V[i+1][j]V[i][j+1]+V[i][j].

F - Preserve DiameterPermalink

OthersPermalink