A - Timeout

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

配点 : 150

問題文

高橋村の長老はすぐに寝てしまいます。具体的には、最後に肩を叩かれてから S+0.5 秒以上経過すると長老は寝てしまいます。

現在、長老は起きており、付き人がちょうど長老の肩を叩きました。

これから付き人はちょうど N 回長老の肩を叩きます。i 回目の肩叩きは現在から T_i 秒後に行われます。

長老が現在から T_N 秒後まで連続して起きているかを判定してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq S \leq 100
  • 1 \leq T_i \leq 1000 (1 \leq i \leq N)
  • T_i < T_{i+1} (1 \leq i \leq N-1)
  • 入力はすべて整数

入力

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

N S
T_1 T_2 \ldots T_N

出力

長老が現在から T_N 秒後まで連続して起きているならば Yes を、そうでないならば No を出力せよ。


入力例 1

5 10
6 11 21 22 30

出力例 1

Yes

付き人は時系列順に以下のように肩を叩きます。

  • 6 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 6 秒しか経過していないので起きています。
  • 11 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 5 秒しか経過していないので起きています。
  • 21 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 10 秒しか経過していないので起きています。
  • 22 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 1 秒しか経過していないので起きています。
  • 30 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 8 秒しか経過していないので起きています。

現在から 30 秒後まで長老は起きているので、Yes と出力します。


入力例 2

2 100
1 200

出力例 2

No

長老は現在から 101.5 秒後に寝てしまいます。そのため No と出力します。


入力例 3

10 22
47 81 82 95 117 146 165 209 212 215

出力例 3

No

Score : 150 points

Problem Statement

The elder of Takahashi Village falls asleep immediately. Specifically, if S+0.5 seconds or more have passed since the elder was last tapped on the shoulder, the elder falls asleep.

Currently, the elder is awake, and an attendant has just tapped the elder on the shoulder.

From now on, the attendant will tap the elder's shoulder exactly N times. The i-th shoulder tap will be performed T_i seconds from now.

Determine whether the elder remains awake continuously from now until T_N seconds later.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq S \leq 100
  • 1 \leq T_i \leq 1000 (1 \leq i \leq N)
  • T_i < T_{i+1} (1 \leq i \leq N-1)
  • All input values are integers.

Input

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

N S
T_1 T_2 \ldots T_N

Output

If the elder remains awake continuously from now until T_N seconds later, output Yes; otherwise, output No.


Sample Input 1

5 10
6 11 21 22 30

Sample Output 1

Yes

The attendant taps the shoulder in chronological order as follows:

  • Taps after 6 seconds. At this time, only 6 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
  • Taps after 11 seconds. At this time, only 5 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
  • Taps after 21 seconds. At this time, only 10 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
  • Taps after 22 seconds. At this time, only 1 second has passed since the elder was last tapped on the shoulder, so the elder is awake.
  • Taps after 30 seconds. At this time, only 8 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.

Since the elder is awake from now until 30 seconds later, output Yes.


Sample Input 2

2 100
1 200

Sample Output 2

No

The elder falls asleep 101.5 seconds from now. Therefore, output No.


Sample Input 3

10 22
47 81 82 95 117 146 165 209 212 215

Sample Output 3

No
B - Compression

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

配点 : 150

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A に含まれる数を重複を除いて小さい順に出力してください。

制約

  • 1\le N\le 100
  • 1\le A_i\le 100
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A に含まれる数を小さい順に C_1,C_2,\ldots , C_M として、以下の形式で出力せよ。

M
C_1 C_2 \ldots C_M

入力例 1

4
3 1 4 1

出力例 1

3
1 3 4

A=(3,1,4,1) に含まれる数は小さい順に 1,3,43 つです。したがって、上記のように出力してください。


入力例 2

3
7 7 7

出力例 2

1
7

入力例 3

8
19 5 5 19 5 19 4 19

出力例 3

3
4 5 19

Score : 150 points

Problem Statement

An integer sequence A=(A_1,A_2,\ldots,A_N) of length N is given.

Output the numbers contained in A in ascending order, removing duplicates.

Constraints

  • 1\le N\le 100
  • 1\le A_i\le 100
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Let C_1,C_2,\ldots , C_M be the numbers contained in A in ascending order. Output in the following format:

M
C_1 C_2 \ldots C_M

Sample Input 1

4
3 1 4 1

Sample Output 1

3
1 3 4

The numbers contained in A=(3,1,4,1) are 1,3,4 in ascending order, totalling 3 distinct numbers. Therefore, output as shown above.


Sample Input 2

3
7 7 7

Sample Output 2

1
7

Sample Input 3

8
19 5 5 19 5 19 4 19

Sample Output 3

3
4 5 19
C - Not All Covered

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

配点 : 300

問題文

AtCoder 王国には 1 から N まで番号がつけられた N 個の城壁があります。また、 M 個の砲台があります。

砲台 iL_i 以上 R_i 以下の番号の城壁を守っています。

ある砲台を破壊すると、その砲台が守っていた城壁はその砲台により守られなくなります。

どの砲台にも守られていない城壁が存在する状態にするためには、最小で何個の砲台を破壊する必要がありますか?

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • 入力はすべて整数

入力

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

N M
L_1 R_1
L_2 R_2
 \vdots 
L_M R_M

出力

砲台に守られていない城壁が存在する状態にするために破壊する必要がある砲台の個数の最小値を出力せよ。


入力例 1

10 4
1 6
4 5
5 10
7 10

出力例 1

1

砲台 1 を破壊するとどの砲台も城壁 3 を守っていない状態になります。 また、砲台を 1 個も破壊しなければ全ての城壁がどれかの砲台に守られている状態になります。 そのため、1 を出力します。


入力例 2

5 2
1 2
3 4

出力例 2

0

どの砲台も城壁 5 を守っていないため、砲台を 1 個も破壊しないでも砲台に守られていない城壁が存在する状態になります。そのため、0 を出力します。


入力例 3

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

出力例 3

3

Score : 300 points

Problem Statement

In the AtCoder Kingdom, there are N castle walls numbered from 1 through N. There are also M turrets.

Turret i guards castle walls numbered from L_i through R_i.

When a turret is destroyed, the castle walls that were guarded by that turret are no longer guarded by that turret.

What is the minimum number of turrets that need to be destroyed so that at least one castle wall is not guarded by any turret?

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • All input values are integers.

Input

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

N M
L_1 R_1
L_2 R_2
 \vdots 
L_M R_M

Output

Output the minimum number of turrets that need to be destroyed so that at least one castle wall is not guarded by any turret.


Sample Input 1

10 4
1 6
4 5
5 10
7 10

Sample Output 1

1

If turret 1 is destroyed, no turret guards castle wall 3. Also, if no turrets are destroyed, all castle walls are guarded by some turret. Therefore, output 1.


Sample Input 2

5 2
1 2
3 4

Sample Output 2

0

Since no turret guards castle wall 5, there already exists a castle wall not guarded by any turret without destroying any turrets. Therefore, output 0.


Sample Input 3

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

Sample Output 3

3
D - Flip to Gather

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

配点 : 400

問題文

長さ N01 のみからなる文字列 S が与えられます。

あなたは以下の操作を何回でも( 0 回でもよい)行うことができます。

  • 1 \leq i \leq N を満たす整数 i を選び、S_i0 のときは 1 に、1 のときは 0 に変更する。

あなたの目標は S に含まれる 1高々 1の区間をなすようにすることです。目標を達成するために必要な操作回数の最小値を求めてください。

より厳密には以下の条件をともに満たすような整数の組 (l,r) が存在するような S にすることが目標です。目標を達成するために必要な操作回数の最小値を求めてください。

  • 1 \leq l \leq r \leq N+1
  • 1 \leq i \leq N を満たす整数 i に対し、S_i= 1l \leq i < r は同値である。

なお、有限回の操作で必ず目標が達成できることが証明できます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 20000
  • 1 \leq N \leq 2 \times 10^5
  • S01 のみからなる長さ N の文字列
  • 各入力ファイルについて、すべてのテストケースの N の総和は 2 \times 10^5 以下である。
  • T,N は整数

入力

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

T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T

\textrm{case}_ii 番目のテストケースを表し、以下の形式で与えられる。

N
S

出力

T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。


入力例 1

3
5
10011
10
1111111111
7
0000000

出力例 1

1
0
0

1 番目のテストケースにおいて、S_10 に変更する操作を行なうと、11 個の区間をなすようになります。また、S のままでは条件を満たしていません。したがって、答えは 1 です。

2 番目のテストケースにおいて、S01 個もないので、操作を行う必要はありません。したがって答えは 0 です。

3 番目のテストケースにおいて、S11 個もないので、操作を行う必要はありません。したがって答えは 0 です。


入力例 2

5
2
01
10
1000010011
12
111100010011
3
111
8
00010101

出力例 2

0
2
3
0
2

Score : 400 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.

You can perform the following operation any number of times (possibly zero):

  • Choose an integer i satisfying 1 \leq i \leq N, and change S_i from 0 to 1, or from 1 to 0.

Your goal is to make the 1s in S form at most one interval. Find the minimum number of operations required to achieve the goal.

More precisely, the goal is to make S such that there exists a pair of integers (l,r) satisfying both of the following conditions. Find the minimum number of operations required to achieve the goal.

  • 1 \leq l \leq r \leq N+1.
  • S_i= 1 and l \leq i < r are equivalent for each integer i satisfying 1 \leq i \leq N.

It can be proved that the goal can always be achieved with a finite number of operations.

T test cases are given, so solve each.

Constraints

  • 1 \leq T \leq 20000
  • 1 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • For each input file, the sum of N over all test cases is at most 2 \times 10^5.
  • T,N are integers.

Input

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

T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T

\textrm{case}_i represents the i-th test case and is given in the following format:

N
S

Output

Output T lines. The i-th line (1 \leq i \leq T) should contain the answer for the i-th test case.


Sample Input 1

3
5
10011
10
1111111111
7
0000000

Sample Output 1

1
0
0

In the first test case, if we perform the operation to change S_1 to 0, the 1s will form one interval. Also, the initial S does not satisfy the condition. Therefore, the answer is 1.

In the second test case, there are no 0s in S, so no operations need to be performed. Therefore, the answer is 0.

In the third test case, there are no 1s in S, so no operations need to be performed. Therefore, the answer is 0.


Sample Input 2

5
2
01
10
1000010011
12
111100010011
3
111
8
00010101

Sample Output 2

0
2
3
0
2
E - Minimum OR Path

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

配点 : 450

問題文

頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の自己ループを持たない連結な無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を双方向に結んでおり、ラベル w_i がつけられています。

頂点 1 から頂点 N までの単純パス(同じ頂点を 2 度以上通らないパス)のうち、パスに含まれる辺につけられたラベルの総ビット単位 \mathrm{OR} としてあり得る最小値を求めてください。

ビット単位 \mathrm{OR} 演算とは

非負整数 A, B のビット単位 \mathrm{OR}A\ \mathrm{OR}\ B は以下のように定義されます。

  • A\ \mathrm{OR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも片方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{OR}\ 5 = 7 となります (二進表記すると: 011\ \mathrm{OR}\ 101 = 111)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{OR}(\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 2\le N\le 2\times 10^5
  • N-1\le M\le 2\times 10^5
  • 1\le u_i < v_i\le N
  • 0\le w_i< 2^{30}
  • 与えられるグラフは連結である
  • 入力される値は全て整数

入力

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

1,3,5 を順番に通り頂点 1,2,3,4 を順番に通ると総ビット単位 \mathrm{OR}1\ \mathrm{OR}\ 2\ \mathrm{OR}\ 3=3 となります。

総ビット単位 \mathrm{OR}3 より小さくすることはできないので、 3 を出力してください。


入力例 2

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

出力例 2

4

グラフには多重辺が存在する場合もあります。


入力例 3

8 12
4 5 16691344
5 7 129642441
2 7 789275447
3 8 335307651
3 5 530163333
5 6 811293773
3 8 333712701
1 2 2909941
2 3 160265478
5 7 465414272
1 3 903373004
6 7 408299562

出力例 3

468549631

Score : 450 points

Problem Statement

You are given a connected undirected graph with N vertices and M edges without self-loops, where vertices are numbered from 1 to N and edges are numbered from 1 to M. Edge i connects vertices u_i and v_i bidirectionally and has a label w_i.

Among the simple paths (paths that do not visit the same vertex more than once) from vertex 1 to vertex N, find the minimum possible value of the bitwise \mathrm{OR} of all labels on edges included in the path.

What is bitwise \mathrm{OR} operation?

The bitwise \mathrm{OR} of non-negative integers A and B, A\ \mathrm{OR}\ B, is defined as follows:

  • The digit in the 2^k (k \geq 0) place of A\ \mathrm{OR}\ B in binary representation is 1 if at least one of the digits in the 2^k place of A and B in binary representation is 1, and 0 otherwise.
For example, 3\ \mathrm{OR}\ 5 = 7 (in binary: 011\ \mathrm{OR}\ 101 = 111).
In general, the bitwise \mathrm{OR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k), and it can be proven that this does not depend on the order of p_1, p_2, p_3, \dots p_k.

Constraints

  • 2\le N\le 2\times 10^5
  • N-1\le M\le 2\times 10^5
  • 1\le u_i < v_i\le N
  • 0\le w_i< 2^{30}
  • The given graph is connected.
  • All input values are integers.

Input

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

Output

Output the answer.


Sample Input 1

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

Sample Output 1

3

By traversing edges 1,3,5 in order and visiting vertices 1,2,3,4 in order, the total bitwise \mathrm{OR} is 1\ \mathrm{OR}\ 2\ \mathrm{OR}\ 3=3.

It is impossible to make the total bitwise \mathrm{OR} smaller than 3, so output 3.


Sample Input 2

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

Sample Output 2

4

The graph may contain multi-edges.


Sample Input 3

8 12
4 5 16691344
5 7 129642441
2 7 789275447
3 8 335307651
3 5 530163333
5 6 811293773
3 8 333712701
1 2 2909941
2 3 160265478
5 7 465414272
1 3 903373004
6 7 408299562

Sample Output 3

468549631
F - Athletic

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

配点 : 500

問題文

1 から N までの番号がつけられた N 個の足場が一列に並んでいます。足場 i(1 \leq i \leq N) の高さは H_i です。

高橋君は足場に乗って移動する遊びをすることにしました。 最初、高橋君は整数 i(1 \leq i \leq N) を自由に選び、足場 i に乗ります。

高橋君はある時点で足場 i に乗っている時、以下の条件を満たす整数 j(1 \leq j \leq N) を選び足場 j に移動することができます。

  • H_j \leq H_i - D かつ 1 \leq |i-j| \leq R

高橋君は移動を行えなくなるまで移動を繰り返すとき、移動できる回数の最大値を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq D,R \leq N
  • H(1,2,\ldots,N) の順列
  • 入力はすべて整数

入力

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

N D R
H_1 H_2 \ldots H_N

出力

答えを出力せよ。


入力例 1

5 2 1
5 3 1 4 2

出力例 1

2

高橋君は初めに足場 1 に乗り、以下のように足場を移動することができます。

  • 1 回目の移動: H_2 \leq H_1 -D かつ |2-1| \leq R であるため足場 2 に移動することができる。足場 1 から足場 2 に移動する。
  • 2 回目の移動: H_3 \leq H_2 -D かつ |3-2| \leq R であるため足場 3 に移動することができる。足場 2 から足場 3 に移動する。
  • 足場 3 の高さは 1 であるため、これ以上移動できない。

以上のように 2 回移動することができます。また、どのように移動する足場を選んでも 3 回以上移動することはできません。よって、2 を出力します。


入力例 2

13 3 2
13 7 10 1 9 5 4 11 12 2 8 6 3

出力例 2

3

Score : 500 points

Problem Statement

There are N scaffolds numbered from 1 to N arranged in a line. The height of scaffold i(1 \leq i \leq N) is H_i.

Takahashi decides to play a game of moving on the scaffolds. Initially, he freely chooses an integer i(1 \leq i \leq N) and gets on scaffold i.

When he is on scaffold i at some point, he can choose an integer j(1 \leq j \leq N) satisfying the following condition and move to scaffold j:

  • H_j \leq H_i - D and 1 \leq |i-j| \leq R.

Find the maximum number of moves he can make when he repeats moving until he can no longer move.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq D,R \leq N
  • H is a permutation of (1,2,\ldots,N).
  • All input values are integers.

Input

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

N D R
H_1 H_2 \ldots H_N

Output

Output the answer.


Sample Input 1

5 2 1
5 3 1 4 2

Sample Output 1

2

Takahashi initially gets on scaffold 1 and can move between the scaffolds as follows:

  • First move: Since H_2 \leq H_1 -D and |2-1| \leq R, he can move to scaffold 2. Move from scaffold 1 to scaffold 2.
  • Second move: Since H_3 \leq H_2 -D and |3-2| \leq R, he can move to scaffold 3. Move from scaffold 2 to scaffold 3.
  • Since the height of scaffold 3 is 1, he can no longer move.

As shown above, he can move 2 times. Also, no matter how he chooses the scaffolds to move to, he cannot move 3 or more times. Therefore, output 2.


Sample Input 2

13 3 2
13 7 10 1 9 5 4 11 12 2 8 6 3

Sample Output 2

3
G - A/B < p/q < C/D

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

配点 : 625

問題文

\displaystyle \frac AB < \frac CD を満たす正整数 A,B,C,D が与えられます。

以下の条件を満たす最小の正整数 q を求めてください。

  • ある正整数 p が存在し、 \displaystyle \frac AB <\frac pq < \frac CD が成立する。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\le T\le 2\times 10^5
  • 1\le A,B,C,D\le 10^{18}
  • \displaystyle\frac AB < \frac CD
  • 入力される値は全て整数

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

ただし、 \text{case}_ii 個目のテストケースを表す。

各テストケースは以下の形式で与えられる。

A B C D

出力

T 行出力せよ。 i 行目には i 番目のテストケースの答えを出力せよ。


入力例 1

5
3 2 2 1
5 2 8 3
1 2 2 1
60 191 11 35
40 191 71 226

出力例 1

3
5
1
226
4

1 番目のテストケースについて考えます。

例えば p=5,q=3 とすると \displaystyle \frac32<\frac53<\frac21 が成立するので q=3 は条件を満たすことが分かります。

3 未満の正整数で条件を満たす q は存在しないので、 1 番目のテストケースの答えは 3 になります。

Score : 625 points

Problem Statement

You are given positive integers A,B,C,D satisfying \displaystyle \frac AB < \frac CD.

Find the smallest positive integer q satisfying the following condition:

  • There exists a positive integer p such that \displaystyle \frac AB <\frac pq < \frac CD.

T test cases are given, so solve each.

Constraints

  • 1\le T\le 2\times 10^5
  • 1\le A,B,C,D\le 10^{18}
  • \displaystyle\frac AB < \frac CD
  • All input values are integers.

Input

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Here, \text{case}_i represents the i-th test case.

Each test case is given in the following format:

A B C D

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
3 2 2 1
5 2 8 3
1 2 2 1
60 191 11 35
40 191 71 226

Sample Output 1

3
5
1
226
4

Consider the first test case.

For example, if p=5,q=3, then \displaystyle \frac32<\frac53<\frac21, so q=3 satisfies the condition.

There is no positive integer less than 3 that satisfies the condition for q, so the answer for the first test case is 3.