A - Five Variables

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

5 つの変数 x_1, x_2, x_3, x_4, x_5 があります。

最初、変数 x_i には整数 i が代入されていました。

すぬけくんは、これらの変数の中から 1 つを選んで、その変数に 0 を代入する操作を行いました。

すぬけくんがこの操作を行ったあとの 5 つの変数の値が与えられます。

すぬけくんが 0 を代入した変数がどれであったかを答えてください。

制約

  • 入力として与えられる x_1, x_2, x_3, x_4, x_5 の値は、すぬけくんが操作を行ったあとのものとしてありえるものである。

入力

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

x_1 x_2 x_3 x_4 x_5

出力

すぬけくんが 0 を代入した変数が x_i であるとき、整数 i を出力せよ。


入力例 1

0 2 3 4 5

出力例 1

1

この場合、すぬけくんが 0 を代入した変数は x_1 なので、1 を出力します。


入力例 2

1 2 0 4 5

出力例 2

3

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

  • The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.


Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.


Sample Input 2

1 2 0 4 5

Sample Output 2

3
B - Crane and Turtle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

庭に何匹かの動物がいます。これらはそれぞれ、2 本の足を持つ鶴か 4 本の足を持つ亀のいずれかです。

高橋くんは、「庭の動物の総数は X 匹で、それらの足の総数は Y 本である」と発言しています。この発言が正しいような鶴と亀の数の組合せが存在するか判定してください。

制約

  • 1 \leq X \leq 100
  • 1 \leq Y \leq 100
  • 入力中のすべての値は整数である。

入力

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

X Y

出力

発言が正しいような鶴と亀の数の組合せが存在すれば Yes、なければ No と出力せよ。


入力例 1

3 8

出力例 1

Yes

「庭にいる動物の総数は 3 匹で、それらの動物の足の総数は 8 本である」という発言は、鶴が 2 羽、亀が 1 匹いる場合に正しいため、発言が正しいような鶴と亀の数の組合せは存在します。


入力例 2

2 100

出力例 2

No

この発言が正しいような鶴と亀の数の組合せは存在しません。


入力例 3

1 2

出力例 3

Yes

鶴と亀のうち一方のみが存在する場合も考慮します。

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

  • 1 \leq X \leq 100
  • 1 \leq Y \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.


Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.


Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.


Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.

C - Forbidden List

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 X と、長さ N の整数列 p_1, \ldots, p_N が与えられます。

整数列 p_1, \ldots, p_N に含まれない整数 (正とは限らない) のうち X に最も近いもの、つまり X との差の絶対値が最小のものを求めてください。そのような整数が複数存在する場合は、そのうち最も小さいものを答えてください。

制約

  • 1 \leq X \leq 100
  • 0 \leq N \leq 100
  • 1 \leq p_i \leq 100
  • p_1, \ldots, p_N はすべて異なる。
  • 入力中のすべての値は整数である。

入力

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

X N
p_1 ... p_N

出力

答えを出力せよ。


入力例 1

6 5
4 7 10 6 5

出力例 1

8

整数列 4, 7, 10, 6, 5 に含まれない整数のうち、最も 6 に近いものは 8 です。


入力例 2

10 5
4 7 10 6 5

出力例 2

9

整数列 4, 7, 10, 6, 5 に含まれない整数のうち、最も 10 に近いものは 911 です。このうち小さい方である 9 を出力します。


入力例 3

100 0

出力例 3

100

N = 0 の場合、入力の 2 行目は空行となります。また、この場合のように、X 自身も答えとなりえます。

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

  • 1 \leq X \leq 100
  • 0 \leq N \leq 100
  • 1 \leq p_i \leq 100
  • p_1, \ldots, p_N are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.


Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.


Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.


Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.

D - Not Divisible

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の数列 A が与えられます。

次の性質を満たす整数 i \left(1 \leq i \leq N \right) の数を答えてください。

  • i \neq j である任意の整数 j \left(1 \leq j \leq N\right) について A_iA_j で割り切れない

制約

  • 入力は全て整数
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

5
24 11 8 3 16

出力例 1

3

問の性質を満たすのは 2 , 3 , 4 です。


入力例 2

4
5 5 5 5

出力例 2

0

同じ数が存在する場合に注意してください。


入力例 3

10
33 18 45 28 8 19 89 86 2 4

出力例 3

5

Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

  • For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.


Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.


Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5
E - Smart Infants

Time Limit: 3.5 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder に参加している幼児が N 人おり、1 から N の番号が付けられています。また、幼稚園が 2\times 10^5 校あり、1 から 2\times 10^5 の番号が付けられています。 幼児 i のレートは A_i であり、はじめ幼稚園 B_i に所属しています。

これから Q 回にわたって、転園が行われます。 j 回目の転園では、幼児 C_j の所属を幼稚園 D_j に変更します。

ここで、「平等さ」を、AtCoderに参加している幼児が一人以上いるような幼稚園それぞれについて園内で最もレートの高い幼児のレートを求め、その最小値として得られる値とします。

Q 回それぞれの転園後の平等さを求めてください。

制約

  • 1 \leq N,Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq C_j \leq N
  • 1 \leq B_i,D_j \leq 2 \times 10^5
  • 入力はすべて整数である。
  • j 回目の転園の前後で幼児 C_j の所属は異なる。

入力

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

N Q
A_1 B_1
A_2 B_2
:
A_N B_N
C_1 D_1
C_2 D_2
:
C_Q D_Q

出力

Q 行出力せよ。 j 行目には、j 回目の転園の後の平等さを出力せよ。


入力例 1

6 3
8 1
6 2
9 3
1 1
2 2
1 3
4 3
2 1
1 2

出力例 1

6
2
6

はじめ、幼稚園 1 には幼児 1, 4、幼稚園 2 には幼児 2, 5、幼稚園 3 には幼児 3, 6 が所属しています。

1 回目の転園で幼児 4 の所属を幼稚園 3 に変更すると、幼稚園 1 には幼児 1、幼稚園 2 には幼児 2, 5、幼稚園 3 には幼児 3, 4, 6 が所属している状態になります。幼稚園 1 で最もレートの高い幼児のレートは 8、幼稚園 2 では 6、幼稚園 3 では 9 です。これらの最小値は 6 であるので、1 行目には 6 を出力します。

2 回目の転園で 2 番目の幼児の所属を幼稚園 1 に変更すると、幼稚園 1 には幼児 1, 2、幼稚園 2 には幼児 5、幼稚園 3 には幼児 3, 4, 6 が所属している状態になります。幼稚園 1 で最もレートの高い幼児のレートは 8、幼稚園 2 では 2、幼稚園 3 では 9 です。これらの最小値は 2 であるので、2 行目には 2 を出力します。

3 回目の転園で 1 番目の幼児の所属を幼稚園 2 に変更すると、幼稚園 1 には幼児 2、幼稚園 2 には幼児 1, 5、幼稚園 3 には幼児 3, 4, 6 が所属している状態になります。幼稚園 1 で最もレートの高い幼児のレートは 6、幼稚園 2 では 8、幼稚園 3 では 9 です。これらの最小値は 6 であるので、3 行目には 6 を出力します。


入力例 2

2 2
4208 1234
3056 5678
1 2020
2 2020

出力例 2

3056
4208

Score : 500 points

Problem Statement

There are N infants registered in AtCoder, numbered 1 to N, and 2\times 10^5 kindergartens, numbered 1 to 2\times 10^5. Infant i has a rating of A_i and initially belongs to Kindergarten B_i.

From now on, Q transfers will happen. After the j-th transfer, Infant C_j will belong to Kindergarten D_j.

Here, we define the evenness as follows. For each kindergarten with one or more infants registered in AtCoder, let us find the highest rating of an infant in the kindergarten. The evenness is then defined as the lowest among those ratings.

For each of the Q transfers, find the evenness just after the transfer.

Constraints

  • 1 \leq N,Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq C_j \leq N
  • 1 \leq B_i,D_j \leq 2 \times 10^5
  • All values in input are integers.
  • In the j-th transfer, Infant C_j changes the kindergarten it belongs to.

Input

Input is given from Standard Input in the following format:

N Q
A_1 B_1
A_2 B_2
:
A_N B_N
C_1 D_1
C_2 D_2
:
C_Q D_Q

Output

Print Q lines. The j-th line should contain the evenness just after the j-th transfer.


Sample Input 1

6 3
8 1
6 2
9 3
1 1
2 2
1 3
4 3
2 1
1 2

Sample Output 1

6
2
6

Initially, Infant 1, 4 belongs to Kindergarten 1, Infant 2, 5 belongs to Kindergarten 2, and Infant 3, 6 belongs to Kindergarten 3.

After the 1-st transfer that makes Infant 4 belong to Kindergarten 3, Infant 1 belongs to Kindergarten 1, Infant 2, 5 belong to Kindergarten 2, and Infant 3, 4, 6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1, 2, 3 are 8, 6, 9, respectively. The lowest among them is 6, so the 1-st line in the output should contain 6.

After the 2-nd transfer that makes Infant 2 belong to Kindergarten 1, Infant 1, 2 belong to Kindergarten 1, Infant 5 belongs to Kindergarten 2, and Infant 3, 4, 6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1, 2, 3 are 8, 2, 9, respectively. The lowest among them is 2, so the 2-nd line in the output should contain 2.

After the 3-rd transfer that makes Infant 1 belong to Kindergarten 2, Infant 2 belongs to Kindergarten 1, Infant 1, 5 belong to Kindergarten 2, and Infant 3, 4, 6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1, 2, 3 are 6, 8, 9, respectively. The lowest among them is 6, so the 3-rd line in the output should contain 6.


Sample Input 2

2 2
4208 1234
3056 5678
1 2020
2 2020

Sample Output 2

3056
4208
F - Pond Skater

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

アメンボのすぬけ君は南北 H マス東西 W マスの長方形の形をしたグリッド状の池に住んでいます。北から i 番目、西から j 番目のマスをマス (i,j) とします。

いくつかのマスには蓮の葉が浮かんでおり、すぬけ君はそれらのマスには入ることができません。 c_{ij}@ のときマス (i,j) に蓮の葉が浮かんでいること、.のときそうでないことを表します。

すぬけ君は一回水をかくことで東西南北のいずれかの方向に 1 マス以上 K マス以下移動することができます。 移動の途中に蓮の葉のあるマスがあってはいけません。また、蓮の葉のあるマスや池の外に移動することもできません。

すぬけ君がマス (x_1,y_1) から (x_2,y_2) まで移動するのに最小で何回水をかく必要があるか求めてください。 (x_1,y_1) から (x_2,y_2) まで移動することができない場合、そのことを指摘してください。

制約

  • 1 \leq H,W,K \leq 10^6
  • H \times W \leq 10^6
  • 1 \leq x_1,x_2 \leq H
  • 1 \leq y_1,y_2 \leq W
  • x_1 \neq x_2 または y_1 \neq y_2
  • c_{i,j}. または @
  • c_{x_1,y_1} = .
  • c_{x_2,y_2} = .
  • 入力される数はすべて整数である。

入力

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

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

出力

すぬけ君がマス (x_1,y_1) から (x_2,y_2) まで移動するのに必要な最小の水かきの回数を出力せよ。 (x_1,y_1) から (x_2,y_2) まで移動することができない場合、-1を出力せよ。


入力例 1

3 5 2
3 2 3 4
.....
.@..@
..@..

出力例 1

5

はじめ、すぬけ君はマス (3,2) にいます。 以下のように 5 回水をかくことでマス (3,4) まで移動することができます。

  • マス (3,2) から西に 1 マス進み、マス (3,1) に移動する。

  • マス (3,1) から北に 2 マス進み、マス (1,1) に移動する。

  • マス (1,1) から東に 2 マス進み、マス (1,3) に移動する。

  • マス (1,3) から東に 1 マス進み、マス (1,4) に移動する。

  • マス (1,4) から南に 2 マス進み、マス (3,4) に移動する。


入力例 2

1 6 4
1 1 1 6
......

出力例 2

2

入力例 3

3 3 1
2 1 2 3
.@.
.@.
.@.

出力例 3

-1

Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

  • 1 \leq H,W,K \leq 10^6
  • H \times W \leq 10^6
  • 1 \leq x_1,x_2 \leq H
  • 1 \leq y_1,y_2 \leq W
  • x_1 \neq x_2 or y_1 \neq y_2.
  • c_{i,j} is . or @.
  • c_{x_1,y_1} = .
  • c_{x_2,y_2} = .
  • All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.


Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2). He can reach the square (3, 4) by making five strokes as follows:

  • From (3, 2), go west one square to (3, 1).

  • From (3, 1), go north two squares to (1, 1).

  • From (1, 1), go east two squares to (1, 3).

  • From (1, 3), go east one square to (1, 4).

  • From (1, 4), go south two squares to (3, 4).


Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1