A - Approximation

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 150

問題文

正整数 A と正の奇数 B が与えられます。

実数 \dfrac AB との差が最小となる整数を出力してください。

ただし、制約のもとでそのような整数がただ一つ存在することが証明できます。

制約

  • 1\leq A\leq407
  • 1\leq B\leq407
  • B は奇数
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

A B

出力

\dfrac AB との差が最小となる整数を出力せよ。


入力例 1

4 7

出力例 1

1

\dfrac AB=\dfrac47=0.5714\ldots です。 \dfrac AB1 との差は \dfrac37=0.4285\ldots で、これより差が小さい整数はありません。

よって、1 を出力してください。


入力例 2

407 29

出力例 2

14

\dfrac AB=\dfrac{407}{29}=14.0344\ldots です。 \dfrac AB14 との差は \dfrac1{29}=0.0344\ldots で、これより差が小さい整数はありません。

よって、14 を出力してください。


入力例 3

22 11

出力例 3

2

\dfrac AB が整数である場合もあります。

Score : 150 points

Problem Statement

You are given a positive integer A and a positive odd integer B.

Output the integer whose difference from the real number \dfrac AB is the smallest.

It can be proved that, under the constraints, such an integer is unique.

Constraints

  • 1 \le A \le 407
  • 1 \le B \le 407
  • B is odd.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Output the integer that minimizes the difference from \dfrac AB.


Sample Input 1

4 7

Sample Output 1

1

We have \dfrac AB = \dfrac47 = 0.5714\ldots. The difference between \dfrac AB and 1 is \dfrac37 = 0.4285\ldots, and no integer has a smaller difference.

Thus, print 1.


Sample Input 2

407 29

Sample Output 2

14

We have \dfrac AB = \dfrac{407}{29} = 14.0344\ldots. The difference between \dfrac AB and 14 is \dfrac1{29} = 0.0344\ldots, and no integer has a smaller difference.

Thus, print 14.


Sample Input 3

22 11

Sample Output 3

2

\dfrac AB may itself be an integer.

B - Pawn on a Grid

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

上下左右に広がる HW 列のマス目があり、各マスにはコマが置かれているか、何も置かれていないかのどちらかです。

マス目の状態は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって表され、
S_ij 文字目が # のとき上から i 行目かつ左から j 列目のマスにはコマが置かれていることを、
S_ij 文字目が . のとき上から i 行目かつ左から j 列目のマスには何も置かれていないことを表しています。

マス目上のマスのうち、コマが置かれているようなものの個数を求めてください。

制約

  • 1\leq H,W \leq 10
  • H,W は整数
  • S_i#. のみからなる長さ W の文字列

入力

入力は以下の形式で標準入力から与えられる。

H W
S_1
S_2
\vdots
S_H

出力

コマが置かれているマスの個数を整数で出力せよ。


入力例 1

3 5
#....
.....
.##..

出力例 1

3
  • 上から 1 行目かつ左から 1 列目のマス
  • 上から 3 行目かつ左から 2 列目のマス
  • 上から 3 行目かつ左から 3 列目のマス

の計 3 つのマスにコマが置かれているため、3 を出力します。


入力例 2

1 10
..........

出力例 2

0

どのマスにもコマは置かれていないため、0 を出力します。


入力例 3

6 5
#.#.#
....#
..##.
####.
..#..
#####

出力例 3

16

Score : 100 points

Problem Statement

There is a grid with H rows from top to bottom and W columns from left to right. Each square has a piece placed on it or is empty.

The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W.
If the j-th character of S_i is #, the square at the i-th row from the top and j-th column from the left has a piece on it;
if the j-th character of S_i is ., the square at the i-th row from the top and j-th column from the left is empty.

How many squares in the grid have pieces on them?

Constraints

  • 1\leq H,W \leq 10
  • H and W are integers.
  • S_i is a string of length W consisting of # and ..

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Print the number of squares with pieces as an integer.


Sample Input 1

3 5
#....
.....
.##..

Sample Output 1

3

The following three squares have pieces on them:

  • the square at the 1-st row from the top and 1-st column from the left;
  • the square at the 3-rd row from the top and 2-nd column from the left;
  • the square at the 3-rd row from the top and 3-rd column from the left.

Thus, 3 should be printed.


Sample Input 2

1 10
..........

Sample Output 2

0

Since no square has a piece on it, 0 should be printed.


Sample Input 3

6 5
#.#.#
....#
..##.
####.
..#..
#####

Sample Output 3

16
C - Reverse Proxy

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

1,2,\dots,N の番号が付いた N 個の箱があります。最初は全ての箱が空です。

これから Q 個のボールが順番にやってきます。
高橋君は、数列 X=(X_1,X_2,\dots,X_Q) に従ってボールを箱に入れます。
具体的には、 i 番目にやってきたボールに次の処理を行います。

  • X_i \ge 1 である場合 : このボールを、箱 X_i に入れる。
  • X_i = 0 である場合 : このボールを、現在入っているボールが最も少ない箱のうち番号が最小である箱に入れる。

それぞれのボールをどの箱に入れたかを求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le Q \le 100
  • 0 \le X_i \le N

入力

入力は以下の形式で標準入力から与えられる。

N Q
X_1 X_2 \dots X_Q

出力

i 番目にやってきたボールを箱 B_i に入れたとき、次の形式に従って出力せよ。

B_1 B_2 \dots B_Q

入力例 1

4 5
2 0 3 0 0

出力例 1

2 1 3 4 1

箱が 4 つあり、ボールは 5 個やってきます。

  • 最初、全ての箱は空です。
    • 各箱に入っているボールの数は箱 1 から順に 0,0,0,0 個です。
  • X_1=2 なので、 1 番目にやってきたボールを箱 2 に入れます。
    • 各箱に入っているボールの数は箱 1 から順に 0,1,0,0 個となります。
  • X_2=0 なので、 2 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
    • 各箱に入っているボールの数は箱 1 から順に 1,1,0,0 個となります。
  • X_3=3 なので、 3 番目にやってきたボールを箱 3 に入れます。
    • 各箱に入っているボールの数は箱 1 から順に 1,1,1,0 個となります。
  • X_4=0 なので、 4 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 4 に入れます。
    • 各箱に入っているボールの数は箱 1 から順に 1,1,1,1 個となります。
  • X_5=0 なので、 5 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
    • 各箱に入っているボールの数は箱 1 から順に 2,1,1,1 個となります。

各ボールを、やってきた順に箱 2,1,3,4,1 に入れました。よって、 2 1 3 4 1 と出力します。


入力例 2

3 7
1 1 0 0 0 0 0

出力例 2

1 1 2 3 2 3 1

入力例 3

6 20
4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0

出力例 3

4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6

Score : 200 points

Problem Statement

There are N boxes numbered 1,2,\dots,N. Initially, all boxes are empty.

Q balls will come in order.
Takahashi will put the balls into the boxes according to the sequence X=(X_1,X_2,\dots,X_Q).
Specifically, he performs the following process for the i-th ball:

  • If X_i \ge 1: Put this ball into box X_i.
  • If X_i = 0: Put this ball into the box with the smallest number among those containing the fewest balls.

Find which box each ball was put into.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le Q \le 100
  • 0 \le X_i \le N

Input

The input is given from Standard Input in the following format:

N Q
X_1 X_2 \dots X_Q

Output

If the i-th ball was put into box B_i, output in the following format:

B_1 B_2 \dots B_Q

Sample Input 1

4 5
2 0 3 0 0

Sample Output 1

2 1 3 4 1

There are 4 boxes, and 5 balls come.

  • Initially, all boxes are empty.
    • The numbers of balls in box 1,2,3,4 are 0,0,0,0, respectively.
  • Since X_1=2, put the 1st ball into box 2.
    • The numbers of balls in box 1,2,3,4 are 0,1,0,0, respectively.
  • Since X_2=0, put the 2nd ball into box 1, which has the smallest number among those containing the fewest balls.
    • The numbers of balls in box 1,2,3,4 are 1,1,0,0, respectively.
  • Since X_3=3, put the 3rd ball into box 3.
    • The numbers of balls in box 1,2,3,4 are 1,1,1,0, respectively.
  • Since X_4=0, put the 4th ball into box 4, which has the smallest number among those containing the fewest balls.
    • The numbers of balls in box 1,2,3,4 are 1,1,1,1, respectively.
  • Since X_5=0, put the 5th ball into box 1, which has the smallest number among those containing the fewest balls.
    • The numbers of balls in box 1,2,3,4 are 2,1,1,1, respectively.

The balls were put into boxes 2,1,3,4,1 in order. Thus, output 2 1 3 4 1.


Sample Input 2

3 7
1 1 0 0 0 0 0

Sample Output 2

1 1 2 3 2 3 1

Sample Input 3

6 20
4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0

Sample Output 3

4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
D - Greedy Draft

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

N 人の客がおり、1 から N までの番号が付けられています。また、M 本の缶ジュースがあり、1 から M までの番号が付けられています。

i (1 \leq i \leq N) は長さ L_i の希望リストを持っています。客 i の希望リストの先頭から j 番目 (1 \leq j \leq L_i) は缶ジュース X_{i,j} です。任意の客 i に対して、客 i の希望リストに載っている番号 X_{i, 1}, \dots, X_{i, L_i} は相異なります。

これから客 1, \dots, N が番号の小さいほうから順に、以下にしたがって自分が飲む飲料を選びます。

  • その時点で誰にも選ばれていない缶ジュースの番号が自分の希望リストに存在する場合、そのうち先頭に最も近い番号の缶ジュースを選ぶ。そうでない場合は水を選ぶ。

それぞれの客がどの飲料を得るかを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq L_i \leq M (1 \leq i \leq N)
  • 1 \leq X_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq L_i)
  • X_{i, 1}, \dots, X_{i, L_i} は相異なる (1 \leq i \leq N)
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N M
L_1
X_{1,1} X_{1,2} \cdots X_{1,L_1}
L_2
X_{2,1} X_{2,2} \cdots X_{2,L_2}
\vdots
L_N
X_{N,1} X_{N,2} \cdots X_{N,L_N}

出力

N 行出力せよ。i 行目 (1 \leq i \leq N) には、客 i が缶ジュースを得る場合はその番号を、水を得る場合は 0 を出力せよ。


入力例 1

4 5
3
3 1 2
3
3 2 1
2
2 3
4
2 5 3 1

出力例 1

3
2
0
5

1 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 3,1,2 です。このうち先頭に最も近いのは 3 なので、客 1 は缶ジュース 3 を選びます。

2 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 2,1 です。このうち先頭に最も近いのは 2 なので、客 2 は缶ジュース 2 を選びます。

3 の希望リストにある番号について、対応する缶ジュースはすべてその時点で誰かに選ばれています。よって客 3 は水を選びます。

4 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 5,1 です。このうち先頭に最も近いのは 5 なので、客 4 は缶ジュース 5 を選びます。


入力例 2

6 5
1
3
2
3 5
5
5 3 1 4 2
5
5 1 3 4 2
5
3 4 1 5 2
5
5 1 3 2 4

出力例 2

3
5
1
4
2
0

Score : 200 points

Problem Statement

There are N customers numbered 1 to N, and M canned juices numbered 1 to M.

Customer i (1 \leq i \leq N) has a wish list of length L_i. The j-th item (1 \leq j \leq L_i) from the top of customer i's wish list is canned juice X_{i,j}. For any customer i, the numbers X_{i, 1}, \dots, X_{i, L_i} on customer i's wish list are distinct.

Customers 1, \dots, N, in this order, will now choose their beverages, following the procedure below.

  • If the customer's wish list contains a canned juice that has not yet been chosen by anyone at that point, they choose the canned juice whose number appears earliest in their wish list. Otherwise, they choose water.

Determine which beverage each customer gets.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq L_i \leq M (1 \leq i \leq N)
  • 1 \leq X_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq L_i)
  • X_{i, 1}, \dots, X_{i, L_i} are distinct. (1 \leq i \leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
L_1
X_{1,1} X_{1,2} \cdots X_{1,L_1}
L_2
X_{2,1} X_{2,2} \cdots X_{2,L_2}
\vdots
L_N
X_{N,1} X_{N,2} \cdots X_{N,L_N}

Output

Output N lines. The i-th line (1 \leq i \leq N) should contain the number of the canned juice customer i gets if they get one, or 0 if customer i gets water.


Sample Input 1

4 5
3
3 1 2
3
3 2 1
2
2 3
4
2 5 3 1

Sample Output 1

3
2
0
5

Among the numbers on customer 1's wish list, the canned juices not yet chosen by anyone are 3, 1, 2. The one appearing earliest in the list is 3, so customer 1 chooses canned juice 3.

Among the numbers on customer 2's wish list, the canned juices not yet chosen by anyone are 2, 1. The one appearing earliest in the list is 2, so customer 2 chooses canned juice 2.

For the numbers on customer 3's wish list, all corresponding canned juices have already been chosen by someone at that point. Thus, customer 3 chooses water.

Among the numbers on customer 4's wish list, the canned juices not yet chosen by anyone are 5, 1. The one appearing earliest in the list is 5, so customer 4 chooses canned juice 5.


Sample Input 2

6 5
1
3
2
3 5
5
5 3 1 4 2
5
5 1 3 4 2
5
3 4 1 5 2
5
5 1 3 2 4

Sample Output 2

3
5
1
4
2
0
E - Socks 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350

問題文

高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さ|i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。

残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。

制約

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \dots A_K

出力

奇妙さの総和の最小値を整数として出力せよ。


入力例 1

4 2
1 3

出力例 1

2

以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。

1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4)3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。


入力例 2

5 1
2

出力例 2

0

(1,1),(3,3),(4,4),(5,5)4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。


入力例 3

8 5
1 2 4 7 8

出力例 3

2

Score : 350 points

Problem Statement

Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.

Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.

Constraints

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_K

Output

Print the minimum total weirdness as an integer.


Sample Input 1

4 2
1 3

Sample Output 1

2

Below, let (i,j) denote a pair of a sock of color i and a sock of color j.

There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.


Sample Input 2

5 1
2

Sample Output 2

0

The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).


Sample Input 3

8 5
1 2 4 7 8

Sample Output 3

2