A - Monitoring Peak

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君は工場の機械を N 日間モニタリングし、毎日の負荷レベルを 0 から 9 の 1 桁の数字で記録しました。記録は長さ N の文字列 S で表され、 S の i 文字目が i 日目の負荷レベルです。

N 日間で最も高かった負荷レベルを求めてください。

制約

  • 1 \leq N \leq 100
  • S は 0 から 9 の数字のみからなる長さ N の文字列

入力

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

N
S
  • 1 行目には、日数 N が与えられる。
  • 2 行目には、記録を表す文字列 S が与えられる。

出力

最も高かった負荷レベルを 1 行で出力せよ。


入力例 1

4
3715

出力例 1

7

入力例 2

2
44

出力例 2

4

入力例 3

4
4321

出力例 3

4

入力例 4

46
5001109154852658893042839798352710745703959824

出力例 4

9

入力例 5

6
562042

出力例 5

6

Score : 200 pts

Problem Statement

Takahashi monitored a factory machine for N days and recorded the load level each day as a single digit from 0 to 9. The record is represented by a string S of length N, where the i-th character of S is the load level on day i.

Find the highest load level observed over the N days.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting only of digits from 0 to 9.

Input

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

N
S
  • The first line contains the number of days N.
  • The second line contains the string S representing the record.

Output

Print the highest load level on a single line.


Sample Input 1

4
3715

Sample Output 1

7

Sample Input 2

2
44

Sample Output 2

4

Sample Input 3

4
4321

Sample Output 3

4

Sample Input 4

46
5001109154852658893042839798352710745703959824

Sample Output 4

9

Sample Input 5

6
562042

Sample Output 5

6
B - Seats in an Udon Restaurant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は饂飩屋に来ました。店内の座席は縦 H 行、横 W 列に並んでおり、座席表は H 個の文字列 S_1, S_2, \ldots, S_H で表されます。 S_i の j 文字目は上から i 行目、左から j 列目の座席の様子で、 o は人が座っていることを、 . は空席であることを表します。

高橋君は空席が最も多い行に座りたいです。空席が最も多い行は上から何行目かを求めてください。そのような行が複数ある場合は、そのうち最も上の行を答えてください。

制約

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i は o と . のみからなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H
  • 1 行目には、座席の行数 H と列数 W がスペース区切りで与えられる。
  • 2 行目から H 行にわたり、 1 + i 行目には上から i 行目の座席の様子を表す文字列 S_i が与えられる。

出力

空席が最も多い行が上から何行目かを 1 行で出力せよ。複数ある場合は最も上の行を出力せよ。


入力例 1

3 4
oo.o
..o.
ooo.

出力例 1

2

入力例 2

1 3
ooo

出力例 2

1

入力例 3

2 2
..
o.

出力例 3

1

入力例 4

3 2
.o
o.
..

出力例 4

3

入力例 5

4 4
oooo
o.oo
.o.o
.o..

出力例 5

4

Score : 233 pts

Problem Statement

Takahashi has come to an udon restaurant. The seats in the restaurant are arranged in H rows and W columns, and the seating chart is represented by H strings S_1, S_2, \ldots, S_H. The j-th character of S_i describes the seat in the i-th row from the top and the j-th column from the left: o means a person is sitting there, and . means the seat is empty.

Takahashi wants to sit in the row with the most empty seats. Find which row from the top has the most empty seats. If there are multiple such rows, answer the topmost one among them.

Constraints

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i is a string of length W consisting only of o and ..

Input

The input is given from standard input in the following format.

H W
S_1
S_2
\vdots
S_H
  • The first line contains the number of rows H and the number of columns W of seats, separated by a space.
  • For each of the next H lines, the (1 + i)-th line contains the string S_i representing the state of the seats in the i-th row from the top.

Output

Print, in one line, which row from the top has the most empty seats. If there are multiple such rows, print the topmost one.


Sample Input 1

3 4
oo.o
..o.
ooo.

Sample Output 1

2

Sample Input 2

1 3
ooo

Sample Output 2

1

Sample Input 3

2 2
..
o.

Sample Output 3

1

Sample Input 4

3 2
.o
o.
..

Sample Output 4

3

Sample Input 5

4 4
oooo
o.oo
.o.o
.o..

Sample Output 5

4
C - Travel Plan

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は N 日間の旅行を計画しています。旅行の i 日目 (1 \le i \le N) にかかる費用は A_i 円です。

高橋君は予算を調整するため、旅行の日程のうち連続する区間を取りやめることにしました。具体的には、以下の 1 + \frac{N(N+1)}{2} 通りの方法の中から 1 つを選びます。

  • 予定を 1 日も取りやめない。このとき、かかる費用の合計は \sum_{i=1}^N A_i 円となります。
  • 1 \le l \le r \le N を満たす整数の組 (l, r) を 1 つ選び、l 日目から r 日目までの予定を取りやめる。このとき、かかる費用の合計は \sum_{i=1}^N A_i - \sum_{i=l}^r A_i 円となります。

上記の方法のうち、旅行を実施する日にかかる費用の合計がちょうど K 円となるものが何通りあるかを求めてください。

制約

  • 1 \le N \le 10^6
  • 0 \le K \le 10^{15}
  • 1 \le A_i \le 10^9 (1 \le i \le N)
  • 入力はすべて整数

入力

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

N K
A_1 A_2 \dots A_N

出力

旅行を実施する日にかかる費用の合計がちょうど K 円となるような、予定の取りやめ方の総数を 1 行に出力せよ。


入力例 1

4 5
2 3 1 4

出力例 1

2

入力例 2

3 8
1 2 3

出力例 2

0

入力例 3

10 6
2 1 3 1 2 1 3 2 1 2

出力例 3

2

入力例 4

15 2500000000
500000000 300000000 200000000 800000000 200000000 400000000 600000000 1000000000 100000000 900000000 700000000 300000000 500000000 400000000 600000000

出力例 4

2

入力例 5

1 0
100

出力例 5

1

Score : 333 pts

Problem Statement

Takahashi is planning an N-day trip. The cost for the i-th day (1 \le i \le N) of the trip is A_i yen.

To adjust his budget, Takahashi has decided to cancel a contiguous range of days from the trip's schedule. Specifically, he will choose one of the following 1 + \frac{N(N+1)}{2} options:

  • Do not cancel any days. In this case, the total cost will be \sum_{i=1}^N A_i yen.
  • Choose a pair of integers (l, r) satisfying 1 \le l \le r \le N, and cancel the schedule from day l through day r. In this case, the total cost will be \sum_{i=1}^N A_i - \sum_{i=l}^r A_i yen.

Find the number of options among the above such that the total cost for the days the trip takes place is exactly K yen.

Constraints

  • 1 \le N \le 10^6
  • 0 \le K \le 10^{15}
  • 1 \le A_i \le 10^9 (1 \le i \le 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_N

Output

Print the number of ways to cancel the schedule such that the total cost for the days the trip takes place is exactly K yen in a single line.


Sample Input 1

4 5
2 3 1 4

Sample Output 1

2

Sample Input 2

3 8
1 2 3

Sample Output 2

0

Sample Input 3

10 6
2 1 3 1 2 1 3 2 1 2

Sample Output 3

2

Sample Input 4

15 2500000000
500000000 300000000 200000000 800000000 200000000 400000000 600000000 1000000000 100000000 900000000 700000000 300000000 500000000 400000000 600000000

Sample Output 4

2

Sample Input 5

1 0
100

Sample Output 5

1
D - Alternating Movement Robot

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は H 行 W 列のマス目からなるテストコースで、1 台の自律走行ロボットを動かしています。

上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

各マス (i, j) の状態は整数 A_{i, j} で表されます。A_{i, j} = 0 のときマス (i, j) は空きマスであり進入可能です。A_{i, j} = 1 のときマス (i, j) には障害物があり進入できません。

マス (1, 1) およびマス (H, W) は空きマスであることが保証されています。

ロボットは最初マス (1, 1) に置かれています。ロボットはマス目の範囲内で、上下左右に隣接する進入可能なマスへ 1 マス移動することを繰り返して、マス (H, W) を目指します。移動の途中で同じマスを複数回訪れても構いません。

ただし、このロボットは機構の制約により、縦方向(上または下)への移動と、横方向(左または右)への移動を交互に行わなければなりません。

より正確には、移動は以下の条件を満たす必要があります。

  • 1 回目の移動では、縦方向・横方向のどちらへ移動してもよい。
  • 直前に縦方向へ移動した場合、次の移動では横方向へ移動しなければならない。
  • 直前に横方向へ移動した場合、次の移動では縦方向へ移動しなければならない。

ロボットがマス (1, 1) からマス (H, W) に到達可能であるか判定し、到達可能な場合は必要な移動回数の最小値を求めてください。

制約

  • 2 \leq H \leq 1000
  • 2 \leq W \leq 1000
  • A_{i, j} \in \{0, 1\}(1 \leq i \leq H,\ 1 \leq j \leq W)
  • A_{1, 1} = 0
  • A_{H, W} = 0
  • 入力はすべて整数

入力

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

H W
A_{1, 1} A_{1, 2} \dots A_{1, W}
A_{2, 1} A_{2, 2} \dots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \dots A_{H, W}

出力

ロボットがマス (H, W) に到達するために必要な移動回数の最小値を 1 行で出力してください。マス (H, W) に到達できない場合は NONE を出力してください。


入力例 1

3 3
0 1 0
0 0 0
0 0 0

出力例 1

4

入力例 2

3 3
0 0 0
0 1 0
0 0 0

出力例 2

NONE

入力例 3

5 5
0 0 1 1 1
1 0 0 1 1
1 0 0 1 1
1 0 0 0 0
1 1 0 0 0

出力例 3

12

入力例 4

8 8
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0

出力例 4

22

入力例 5

2 2
0 0
0 0

出力例 5

2

Score : 400 pts

Problem Statement

Takahashi is operating an autonomous robot on a test course consisting of a grid with H rows and W columns.

Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

The state of each cell (i, j) is represented by an integer A_{i, j}. If A_{i, j} = 0, cell (i, j) is an empty cell and can be entered. If A_{i, j} = 1, cell (i, j) contains an obstacle and cannot be entered.

It is guaranteed that cells (1, 1) and (H, W) are empty cells.

The robot is initially placed at cell (1, 1). It aims to reach cell (H, W) by repeatedly moving 1 cell to an adjacent passable cell in the up, down, left, or right direction within the grid. It is allowed to visit the same cell multiple times during the movement.

However, due to mechanical constraints, the robot must alternate between moving vertically (up or down) and moving horizontally (left or right).

More precisely, the moves must satisfy the following conditions:

  • In the first move, the robot may move in either the vertical or horizontal direction.
  • If the previous move was in the vertical direction, the next move must be in the horizontal direction.
  • If the previous move was in the horizontal direction, the next move must be in the vertical direction.

Determine whether the robot can reach cell (H, W) from cell (1, 1), and if it is possible, find the minimum number of moves required.

Constraints

  • 2 \leq H \leq 1000
  • 2 \leq W \leq 1000
  • A_{i, j} \in \{0, 1\} (1 \leq i \leq H,\ 1 \leq j \leq W)
  • A_{1, 1} = 0
  • A_{H, W} = 0
  • All input values are integers.

Input

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

H W
A_{1, 1} A_{1, 2} \dots A_{1, W}
A_{2, 1} A_{2, 2} \dots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \dots A_{H, W}

Output

Print the minimum number of moves required for the robot to reach cell (H, W) in a single line. If it is impossible to reach cell (H, W), print NONE.


Sample Input 1

3 3
0 1 0
0 0 0
0 0 0

Sample Output 1

4

Sample Input 2

3 3
0 0 0
0 1 0
0 0 0

Sample Output 2

NONE

Sample Input 3

5 5
0 0 1 1 1
1 0 0 1 1
1 0 0 1 1
1 0 0 0 0
1 1 0 0 0

Sample Output 3

12

Sample Input 4

8 8
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0

Sample Output 4

22

Sample Input 5

2 2
0 0
0 0

Sample Output 5

2
E - Comfort of the Mountain Trail

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は、山岳ガイドとして登山道の管理を担当しています。登山道には N 個のチェックポイントが一列に並んでおり、左から順に 1, 2, \ldots, N と番号が付いています。各チェックポイント i の標高は A_i です。急激な標高変化は登山者の体力を大きく消耗させるため、高橋君は登山者の疲労度を管理したいと考えています。

登山道の管理中、以下の 2 種類のイベントが合計 Q 回発生します。

  • イベント 1:地形の変化により、チェックポイント p の標高が v に変更される。
  • イベント 2:青木君が登山に来て、チェックポイント l からチェックポイント r までを歩く予定であると高橋君に伝える。高橋君は、青木君がこの区間で経験する疲労度を計算して伝えなければならない。

イベント 2 における疲労度の計算方法を、以下で正確に定めます。

区間 [l, r] に含まれる隣接ペアの集合を S = \{(i, i+1) \mid l \le i \le r-1\} とし、各隣接ペア (i, i+1) の標高差の絶対値を d_i = |A_i - A_{i+1}| とします。

  1. S が空(すなわち l = r)の場合、疲労度は 0 です。
  2. S が空でない場合、M_1 = \max_{(i,i+1) \in S} d_i とします。
  • M_1 \le K ならば、疲労度は M_1 です。
  • M_1 > K ならば、青木君は迂回路を使い、S の中から ちょうど 1 つ の隣接ペアを選んで回避(ショートカット)できます。青木君は疲労度を最小にするように回避する隣接ペアを最適に選びます。すなわち、疲労度は \min_{(j, j+1) \in S} \left( \max_{(i,i+1) \in S \setminus \{(j,j+1)\}} d_i \right) です。ただし、S の要素数が 1(すなわち r = l+1)の場合、回避後に隣接ペアが残らないため疲労度は 0 とします。

各イベント 2 について、上記で定まる疲労度を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • イベント 1 において、1 \leq p \leq N かつ 0 \leq v \leq 10^9
  • イベント 2 において、1 \leq l \leq r \leq N
  • 入力はすべて整数である。

入力

N Q K
A_1 A_2 \ldots A_N
query_1
query_2
\vdots
query_Q
  • 1 行目には、チェックポイントの数 N 、イベントの数 Q 、閾値 K がスペース区切りで与えられる。
  • 2 行目には、各チェックポイントの初期標高 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
  • 3 行目から Q 行にわたり、各イベントの情報が与えられる。
  • イベント 1 の場合: 1 p v (チェックポイント p の標高を v に更新する)
  • イベント 2 の場合: 2 l r (チェックポイント l からチェックポイント r まで歩く際の疲労度を求める)

出力

イベント 2 が発生するたびに、青木君が経験する疲労度を 1 行に 1 つずつ出力せよ。


入力例 1

5 8 5
10 13 20 18 25
2 1 5
2 2 4
2 3 3
1 3 15
2 1 5
1 5 16
2 4 5
2 1 2

出力例 1

7
2
0
3
2
3

入力例 2

4 7 10
0 4 9 15
2 1 4
2 1 1
1 2 20
2 1 3
1 4 5
2 2 4
2 3 4

出力例 2

6
0
11
4
4

入力例 3

10 15 7
5 12 3 8 20 21 10 30 25 27
2 1 10
2 3 6
1 5 9
2 1 6
2 5 5
1 7 35
2 6 8
1 1 100
2 1 3
2 4 10
1 10 0
2 8 10
1 2 100
2 1 2
2 1 10

出力例 3

12
5
9
0
5
9
12
5
0
25

入力例 4

20 25 50
100 180 160 300 290 400 405 1000 950 970 30 35 500 490 495 800 100 120 130 700
2 1 20
2 1 2
2 10 15
1 8 930
2 7 10
1 11 900
2 9 12
2 12 12
1 1 0
2 1 5
1 20 1000
2 16 20
1 17 850
2 15 18
1 5 100
2 3 6
2 6 14
1 13 910
2 11 14
1 2 0
2 1 3
2 18 20
1 10 200
2 8 11
2 1 20

出力例 4

700
0
465
20
70
0
140
700
305
200
525
865
0
10
700
870

入力例 5

2 10 0
0 1000000000
2 1 1
2 1 2
1 2 0
2 1 2
1 1 1000000000
2 1 2
1 2 1000000000
2 1 2
1 1 0
2 1 2

出力例 5

0
0
0
0
0
0

Score : 433 pts

Problem Statement

Takahashi is a mountain guide responsible for managing a mountain trail. There are N checkpoints arranged in a line along the trail, numbered 1, 2, \ldots, N from left to right. The elevation of each checkpoint i is A_i. Because steep changes in elevation significantly drain climbers' stamina, Takahashi wants to manage climbers' fatigue levels.

During the trail's management, a total of Q events of the following two types occur:

  • Event 1: Due to changes in terrain, the elevation of checkpoint p is changed to v.
  • Event 2: Aoki comes hiking and tells Takahashi that he plans to walk from checkpoint l to checkpoint r. Takahashi must compute and report the fatigue level that Aoki will experience on this segment.

The method for calculating the fatigue level in Event 2 is defined precisely below.

Let S = \{(i, i+1) \mid l \le i \le r-1\} be the set of adjacent pairs contained in the interval [l, r], and let d_i = |A_i - A_{i+1}| be the absolute difference in elevation for each adjacent pair (i, i+1).

  1. If S is empty (i.e., l = r), the fatigue level is 0.
  2. If S is non-empty, let M_1 = \max_{(i,i+1) \in S} d_i.
  • If M_1 \le K, the fatigue level is M_1.
  • If M_1 > K, Aoki can use a detour to bypass (shortcut) exactly one adjacent pair from S. Aoki chooses which adjacent pair to bypass optimally to minimize the fatigue level. That is, the fatigue level is \min_{(j, j+1) \in S} \left( \max_{(i,i+1) \in S \setminus \{(j,j+1)\}} d_i \right). However, if the size of S is 1 (i.e., r = l+1), no adjacent pairs remain after the bypass, so the fatigue level is 0.

For each Event 2, find the fatigue level determined as described above.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • In Event 1, 1 \leq p \leq N and 0 \leq v \leq 10^9.
  • In Event 2, 1 \leq l \leq r \leq N.
  • All input values are integers.

Input

N Q K
A_1 A_2 \ldots A_N
query_1
query_2
\vdots
query_Q
  • The first line contains the number of checkpoints N, the number of events Q, and the threshold K, separated by spaces.
  • The second line contains the initial elevations of the checkpoints A_1, A_2, \ldots, A_N, separated by spaces.
  • The next Q lines provide the information for each event.
  • For Event 1: 1 p v (update the elevation of checkpoint p to v)
  • For Event 2: 2 l r (find the fatigue level when walking from checkpoint l to checkpoint r)

Output

Whenever an Event 2 occurs, print the fatigue level experienced by Aoki, one per line.


Sample Input 1

5 8 5
10 13 20 18 25
2 1 5
2 2 4
2 3 3
1 3 15
2 1 5
1 5 16
2 4 5
2 1 2

Sample Output 1

7
2
0
3
2
3

Sample Input 2

4 7 10
0 4 9 15
2 1 4
2 1 1
1 2 20
2 1 3
1 4 5
2 2 4
2 3 4

Sample Output 2

6
0
11
4
4

Sample Input 3

10 15 7
5 12 3 8 20 21 10 30 25 27
2 1 10
2 3 6
1 5 9
2 1 6
2 5 5
1 7 35
2 6 8
1 1 100
2 1 3
2 4 10
1 10 0
2 8 10
1 2 100
2 1 2
2 1 10

Sample Output 3

12
5
9
0
5
9
12
5
0
25

Sample Input 4

20 25 50
100 180 160 300 290 400 405 1000 950 970 30 35 500 490 495 800 100 120 130 700
2 1 20
2 1 2
2 10 15
1 8 930
2 7 10
1 11 900
2 9 12
2 12 12
1 1 0
2 1 5
1 20 1000
2 16 20
1 17 850
2 15 18
1 5 100
2 3 6
2 6 14
1 13 910
2 11 14
1 2 0
2 1 3
2 18 20
1 10 200
2 8 11
2 1 20

Sample Output 4

700
0
465
20
70
0
140
700
305
200
525
865
0
10
700
870

Sample Input 5

2 10 0
0 1000000000
2 1 1
2 1 2
1 2 0
2 1 2
1 1 1000000000
2 1 2
1 2 1000000000
2 1 2
1 1 0
2 1 2

Sample Output 5

0
0
0
0
0
0