C - Candles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

数直線上に N 本のろうそくが置かれています。 左から i 番目のろうそくは座標 x_i に置かれています。 ただし、x_1 < x_2 < ... < x_N が成り立ちます。

最初、どのろうそくにも火が付いていません。 すぬけ君は、N 本のうち K 本のろうそくに火を付けることにしました。

今、すぬけ君は座標 0 にいます。 すぬけ君は、数直線上を左右に速度 1 で移動することができます。 また、自分と同じ座標のろうそくに火を付けることができます。 このとき、火を付けるのに掛かる時間は無視できます。

K 本のろうそくに火を付けるのに必要な最小の時間を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • x_i は整数である。
  • |x_i| \leq 10^8
  • x_1 < x_2 < ... < x_N

入力

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

N K
x_1 x_2 ... x_N

出力

K 本のろうそくに火を付けるのに必要な最小の時間を出力せよ。


入力例 1

5 3
-30 -10 10 20 50

出力例 1

40

次のように移動しながらろうそくに火を付ければよいです。

  • 座標 0 から -10 へ移動する。
  • 左から 2 番目のろうそくに火を付ける。
  • 座標 -10 から 10 へ移動する。
  • 左から 3 番目のろうそくに火を付ける。
  • 座標 10 から 20 へ移動する。
  • 左から 4 番目のろうそくに火を付ける。

入力例 2

3 2
10 20 30

出力例 2

20

入力例 3

1 1
0

出力例 3

0

座標 0 にろうそくが置かれていることもあります。


入力例 4

8 5
-9 -7 -4 -3 1 2 3 4

出力例 4

10

Score : 300 points

Problem Statement

There are N candles placed on a number line. The i-th candle from the left is placed on coordinate x_i. Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning. Snuke decides to light K of the N candles.

Now, he is at coordinate 0. He can move left and right along the line with speed 1. He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • x_i is an integer.
  • |x_i| \leq 10^8
  • x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.


Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

  • Move from coordinate 0 to -10.
  • Light the second candle from the left.
  • Move from coordinate -10 to 10.
  • Light the third candle from the left.
  • Move from coordinate 10 to 20.
  • Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0
  • There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10
D - Median of Medians

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

長さ M の数列 b中央値 を次のように定義します。

  • b を昇順にソートして得られる数列を b' とする。 このとき、b'M / 2 + 1 番目の要素の値を、b の中央値とする。 ここで、/ は小数点以下を切り捨てる除算である。

例えば、(10, 30, 20) の中央値は 20 であり、(10, 30, 20, 40) の中央値は 30 であり、(10, 10, 10, 20, 30) の中央値は 10 です。

すぬけ君は次のような問題を思いつきました。

長さ N の数列 a があります。 各 (l, r) (1 \leq l \leq r \leq N) について、a の連続部分列 (a_l, a_{l + 1}, ..., a_r) の中央値を m_{l, r} とします。 すべての (l, r) に対する m_{l, r} を並べ、新たに数列 m を作ります。 m の中央値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • a_i は整数である。
  • 1 \leq a_i \leq 10^9

入力

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

N
a_1 a_2 ... a_N

出力

m の中央値を出力せよ。


入力例 1

3
10 30 20

出力例 1

30

a のそれぞれの連続部分列の中央値は次のようになります。

  • (10) の中央値は 10
  • (30) の中央値は 30
  • (20) の中央値は 20
  • (10, 30) の中央値は 30
  • (30, 20) の中央値は 30
  • (10, 30, 20) の中央値は 20

よって、m = (10, 30, 20, 30, 30, 20) となり、m の中央値は 30 です。


入力例 2

1
10

出力例 2

10

入力例 3

10
5 9 5 9 8 9 3 5 4 3

出力例 3

8

Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

  • Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N. For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a. We will list m_{l, r} for all pairs (l, r) to create a new sequence m. Find the median of m.

Constraints

  • 1 \leq N \leq 10^5
  • a_i is an integer.
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.


Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

  • The median of (10) is 10.
  • The median of (30) is 30.
  • The median of (20) is 20.
  • The median of (10, 30) is 30.
  • The median of (30, 20) is 30.
  • The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.


Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8
E - Ribbons on Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

N を偶数とします。

N 頂点の木があります。 頂点には 1, 2, ..., N と番号が振られています。 各 i (1 \leq i \leq N - 1) について、i 番目の辺は頂点 x_iy_i を結んでいます。

すぬけ君は、次のようにして木をリボンで飾りつけようと思っています。

まず、N 個の頂点を N / 2 組のペアに分けます。 このとき、各頂点はちょうど 1 つのペアに属さなければなりません。 次に、各ペア (u, v) について、1 本のリボンを u-v 間の最短パスに含まれるすべての辺を通るように張ります。

すぬけ君はペアの分け方を工夫し、「どの辺にも少なくとも 1 本のリボンが張られている」という条件が成り立つようにしようとしています。 条件が成り立つようなペアの分け方は何通りでしょうか? 10^9 + 7 で割った余りを求めてください。 ただし、2 通りのペアの分け方が異なるとは、あるペアが一方には含まれるが他方には含まれないことを言います。

制約

  • N は偶数である。
  • 2 \leq N \leq 5000
  • 1 \leq x_i, y_i \leq N
  • 与えられるグラフは木である。

入力

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

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

出力

条件が成り立つようなペアの分け方は何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

4
1 2
2 3
3 4

出力例 1

2

ペアの分け方は次図の 3 通りであり、右側の 2 通りが条件を満たします。


入力例 2

4
1 2
1 3
1 4

出力例 2

3

ペアの分け方は次図の 3 通りであり、すべて条件を満たします。


入力例 3

6
1 2
1 3
3 4
1 5
5 6

出力例 3

10

入力例 4

10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

出力例 4

672

Score : 900 points

Problem Statement

Let N be an even number.

There is a tree with N vertices. The vertices are numbered 1, 2, ..., N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Snuke would like to decorate the tree with ribbons, as follows.

First, he will divide the N vertices into N / 2 pairs. Here, each vertex must belong to exactly one pair. Then, for each pair (u, v), put a ribbon through all the edges contained in the shortest path between u and v.

Snuke is trying to divide the vertices into pairs so that the following condition is satisfied: "for every edge, there is at least one ribbon going through it." How many ways are there to divide the vertices into pairs, satisfying this condition? Find the count modulo 10^9 + 7. Here, two ways to divide the vertices into pairs are considered different when there is a pair that is contained in one of the two ways but not in the other.

Constraints

  • N is an even number.
  • 2 \leq N \leq 5000
  • 1 \leq x_i, y_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print the number of the ways to divide the vertices into pairs, satisfying the condition, modulo 10^9 + 7.


Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2

There are three possible ways to divide the vertices into pairs, as shown below, and two satisfy the condition: the middle one and the right one.


Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

3

There are three possible ways to divide the vertices into pairs, as shown below, and all of them satisfy the condition.


Sample Input 3

6
1 2
1 3
3 4
1 5
5 6

Sample Output 3

10

Sample Input 4

10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

672
F - Robots and Exits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

数直線上に N 体のロボットと M 個の出口があります。 これらの N + M 個の座標はすべて整数であり、すべて相異なります。 各 i (1 \leq i \leq N) について、左から i 番目のロボットの座標は x_i です。 また、各 j (1 \leq j \leq M) について、左から j 番目の出口の座標は y_j です。

すぬけ君は、次の 2 種類の操作を好きな順序で繰り返し行い、ロボットを一斉に動かすことができます。

  • 数直線上のすべてのロボットの座標を -1 する。
  • 数直線上のすべてのロボットの座標を +1 する。

各ロボットは、いずれかの出口と重なった瞬間、その出口を通って数直線上から消えます。 すぬけ君は、すべてのロボットが消えるまで操作を行い続けます。

すべてのロボットが消えた後、各ロボットがどの出口を通ったかという組合せは何通りありうるでしょうか? 10^9 + 7 で割った余りを求めてください。 ただし、2 通りの組合せが異なるとは、あるロボットが存在し、そのロボットの通った出口が異なることを言います。

制約

  • 1 \leq N, M \leq 10^5
  • 1 \leq x_1 < x_2 < ... < x_N \leq 10^9
  • 1 \leq y_1 < y_2 < ... < y_M \leq 10^9
  • 与えられる座標はすべて整数である。
  • 与えられる座標はすべて相異なる。

入力

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

N M
x_1 x_2 ... x_N
y_1 y_2 ... y_M

出力

すべてのロボットが消えた後、各ロボットがどの出口を通ったかという組合せは何通りありうるか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

2 2
2 3
1 4

出力例 1

3

左から i 番目のロボットをロボット i と呼び、左から j 番目の出口を出口 j と呼びます。 (ロボット 1 が通った出口, ロボット 2 が通った出口) の組合せとしてありうるものは、次の 3 通りです。

  • (出口 1, 出口 1)
  • (出口 1, 出口 2)
  • (出口 2, 出口 2)

入力例 2

3 4
2 5 10
1 3 7 13

出力例 2

8

各ロボットごと独立に通る出口を決められるので、組合せは 2^3 = 8 通りです。


入力例 3

4 1
1 2 4 5
3

出力例 3

1

どのロボットも出口 1 を通ります。


入力例 4

4 5
2 5 7 11
1 3 6 9 13

出力例 4

6

入力例 5

10 10
4 13 15 18 19 20 21 22 25 27
1 5 11 12 14 16 23 26 29 30

出力例 5

22

Score : 900 points

Problem Statement

There are N robots and M exits on a number line. The N + M coordinates of these are all integers and all distinct. For each i (1 \leq i \leq N), the coordinate of the i-th robot from the left is x_i. Also, for each j (1 \leq j \leq M), the coordinate of the j-th exit from the left is y_j.

Snuke can repeatedly perform the following two kinds of operations in any order to move all the robots simultaneously:

  • Increment the coordinates of all the robots on the number line by 1.
  • Decrement the coordinates of all the robots on the number line by 1.

Each robot will disappear from the number line when its position coincides with that of an exit, going through that exit. Snuke will continue performing operations until all the robots disappear.

When all the robots disappear, how many combinations of exits can be used by the robots? Find the count modulo 10^9 + 7. Here, two combinations of exits are considered different when there is a robot that used different exits in those two combinations.

Constraints

  • 1 \leq N, M \leq 10^5
  • 1 \leq x_1 < x_2 < ... < x_N \leq 10^9
  • 1 \leq y_1 < y_2 < ... < y_M \leq 10^9
  • All given coordinates are integers.
  • All given coordinates are distinct.

Input

Input is given from Standard Input in the following format:

N M
x_1 x_2 ... x_N
y_1 y_2 ... y_M

Output

Print the number of the combinations of exits that can be used by the robots when all the robots disappear, modulo 10^9 + 7.


Sample Input 1

2 2
2 3
1 4

Sample Output 1

3

The i-th robot from the left will be called Robot i, and the j-th exit from the left will be called Exit j. There are three possible combinations of exits (the exit used by Robot 1, the exit used by Robot 2) as follows:

  • (Exit 1, Exit 1)
  • (Exit 1, Exit 2)
  • (Exit 2, Exit 2)

Sample Input 2

3 4
2 5 10
1 3 7 13

Sample Output 2

8

The exit for each robot can be chosen independently, so there are 2^3 = 8 possible combinations of exits.


Sample Input 3

4 1
1 2 4 5
3

Sample Output 3

1

Every robot uses Exit 1.


Sample Input 4

4 5
2 5 7 11
1 3 6 9 13

Sample Output 4

6

Sample Input 5

10 10
4 13 15 18 19 20 21 22 25 27
1 5 11 12 14 16 23 26 29 30

Sample Output 5

22