A - 山脈のジグザグ

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

配点 : 233

問題文

高橋君は登山が趣味で、ある山脈の地形を調査しています。

この山脈には N 個の地点が一列に並んでおり、各地点 i1 \leq i \leq N)の標高は R_i です。高橋君は、この標高列の中に「ジグザグ」な箇所がいくつあるかを数えたいと考えています。

ここで「ジグザグ」とは、次のように定義されます。連続する 3 つの地点 i, i+1, i+21 \leq i \leq N - 2)について、以下のいずれかの条件を満たすとき、地点 i から始まる 3 地点の組は「ジグザグ」であるといいます。

  • R_i < R_{i+1} かつ R_{i+1} > R_{i+2}(標高が上がってから下がるパターン)
  • R_i > R_{i+1} かつ R_{i+1} < R_{i+2}(標高が下がってから上がるパターン)

すなわち、中央の地点の標高が両隣より高い(山頂型)か、両隣より低い(谷底型)場合がジグザグに該当します。隣接する地点の標高が等しい場合は、いずれの条件も満たさないため、ジグザグには該当しません。

ジグザグである 3 地点の組の総数、すなわち上の条件を満たす i1 \leq i \leq N - 2)の個数を求めてください。

制約

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq R_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である。

入力

N
R_1 R_2 \ldots R_N
  • 1 行目には、地点の数を表す整数 N が与えられる。
  • 2 行目には、各地点の標高を表す整数 R_1, R_2, \ldots, R_N がスペース区切りで与えられる。

出力

ジグザグである 3 地点の組の総数を 1 行で出力せよ。


入力例 1

5
1 3 2 4 1

出力例 1

3

入力例 2

8
1 2 3 4 5 6 7 8

出力例 2

0

入力例 3

10
10 5 8 3 7 2 9 1 6 4

出力例 3

8

Score : 233 pts

Problem Statement

Takahashi enjoys mountain climbing as a hobby and is surveying the terrain of a certain mountain range.

This mountain range has N points arranged in a line, and the elevation of each point i (1 \leq i \leq N) is R_i. Takahashi wants to count how many "zigzag" portions exist in this sequence of elevations.

A "zigzag" is defined as follows. For three consecutive points i, i+1, i+2 (1 \leq i \leq N - 2), the triplet of points starting at point i is called a "zigzag" if it satisfies either of the following conditions:

  • R_i < R_{i+1} and R_{i+1} > R_{i+2} (a pattern where elevation goes up then down)
  • R_i > R_{i+1} and R_{i+1} < R_{i+2} (a pattern where elevation goes down then up)

In other words, a zigzag occurs when the elevation of the middle point is higher than both neighbors (peak type) or lower than both neighbors (valley type). If adjacent points have equal elevations, neither condition is satisfied, so it does not count as a zigzag.

Find the total number of zigzag triplets, that is, the number of values of i (1 \leq i \leq N - 2) that satisfy the above conditions.

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq R_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers.

Input

N
R_1 R_2 \ldots R_N
  • The first line contains an integer N representing the number of points.
  • The second line contains integers R_1, R_2, \ldots, R_N representing the elevation of each point, separated by spaces.

Output

Output the total number of zigzag triplets in a single line.


Sample Input 1

5
1 3 2 4 1

Sample Output 1

3

Sample Input 2

8
1 2 3 4 5 6 7 8

Sample Output 2

0

Sample Input 3

10
10 5 8 3 7 2 9 1 6 4

Sample Output 3

8
B - 果物の収穫

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

配点 : 333

問題文

高橋君は果樹園を経営しています。果樹園には N 本の果物の木が一列に並んでおり、左から順に 1 番目、 2 番目、…、 N 番目と番号がついています。

i 番目の木には A_i 個の果物が実っています。

今年は人手不足のため、高橋君はすべての木を収穫することができず、連続するちょうど K 本の木を選んで収穫作業を行うことにしました。すなわち、ある整数 l1 \leq l \leq N - K + 1)を選び、l 番目から l + K - 1 番目までの K 本の木から果物を収穫します。選んだ K 本の木からはそれぞれすべての果物を収穫しなければならず(一部だけ収穫することはできません)、選ばなかった木からは果物を収穫しません。

高橋君は作業の負担をできるだけ減らしたいため、収穫する果物の個数の合計をできるだけ少なくしたいと考えています。

連続する K 本の木を適切に選んだとき、収穫する果物の個数の合計の最小値を求めてください。

制約

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N K
A_1 A_2 \ldots A_N

1 行目には、果樹園にある木の本数を表す整数 N と、収穫作業を行う連続した木の本数を表す整数 K が、スペース区切りで与えられる。

2 行目には、各木に実っている果物の個数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

連続する K 本の木を適切に選んだときの、収穫する果物の個数の合計の最小値を 1 行で出力せよ。


入力例 1

5 3
4 2 1 3 5

出力例 1

6

入力例 2

8 4
10 5 8 3 2 7 4 6

出力例 2

16

入力例 3

10 5
100 200 50 80 120 30 60 90 150 70

出力例 3

340

Score : 333 pts

Problem Statement

Takahashi runs an orchard. The orchard has N fruit trees lined up in a row, numbered 1, 2, \ldots, N from left to right.

The i-th tree bears A_i fruits.

Due to a labor shortage this year, Takahashi cannot harvest all the trees, so he has decided to select exactly K consecutive trees to harvest. Specifically, he chooses an integer l (1 \leq l \leq N - K + 1) and harvests fruits from the K trees numbered l through l + K - 1. He must harvest all fruits from each of the K chosen trees (he cannot partially harvest a tree), and he does not harvest any fruits from the trees he did not choose.

Takahashi wants to minimize his workload, so he wants to minimize the total number of fruits harvested.

Find the minimum possible total number of fruits harvested when the K consecutive trees are chosen optimally.

Constraints

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All inputs are integers.

Input

N K
A_1 A_2 \ldots A_N

The first line contains the integer N representing the number of trees in the orchard and the integer K representing the number of consecutive trees to harvest, separated by a space.

The second line contains the integers A_1, A_2, \ldots, A_N representing the number of fruits on each tree, separated by spaces.

Output

Print in one line the minimum total number of fruits harvested when the K consecutive trees are chosen optimally.


Sample Input 1

5 3
4 2 1 3 5

Sample Output 1

6

Sample Input 2

8 4
10 5 8 3 2 7 4 6

Sample Output 2

16

Sample Input 3

10 5
100 200 50 80 120 30 60 90 150 70

Sample Output 3

340
C - 予算内での買い物

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

配点 : 366

問題文

高橋君はショッピングモールにやってきました。モールには N 種類の商品が売られており、 i 番目の商品の価格は C_i 円で、高橋君にとっての満足度は V_i です。

高橋君は現在 S 円を持っていますが、このうち T 円は生活費として残しておかなければなりません。つまり、商品の購入に使える金額の合計は S - T 円以下でなければなりません。

各商品は最大 1 個しか購入できません。高橋君が得られる満足度の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 2000
  • 1 \leq T \leq S \leq 10^5
  • 1 \leq C_i \leq 10^5 (1 \leq i \leq N)
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N S T
C_1 V_1
C_2 V_2
:
C_N V_N
  • 1 行目には、商品の種類数を表す N 、高橋君の所持金を表す S 、生活費として残す金額を表す T が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各商品の情報が与えられる。
  • 1 + i 行目では、 i 番目の商品の価格を表す C_i と、その商品の満足度を表す V_i が、スペース区切りで与えられる。

出力

高橋君が得られる満足度の合計の最大値を 1 行で出力せよ。


入力例 1

3 100 30
30 50
40 60
50 80

出力例 1

110

入力例 2

3 50 50
10 100
20 200
30 300

出力例 2

0

入力例 3

8 500 100
100 200
150 300
200 500
80 150
120 250
250 600
50 100
180 400

出力例 3

900

入力例 4

15 10000 3000
500 1200
1200 3500
800 2000
1500 4000
300 700
2000 5500
700 1800
900 2400
1100 3000
600 1500
1800 4800
400 1000
1300 3600
250 600
950 2600

出力例 4

19200

入力例 5

1 1 1
1 1000000000

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi has come to a shopping mall. The mall sells N types of items, where the i-th item has a price of C_i yen and a satisfaction value of V_i for Takahashi.

Takahashi currently has S yen, but he must keep T yen as living expenses. In other words, the total amount spent on purchasing items must be at most S - T yen.

Each item can be purchased at most once. Find the maximum total satisfaction Takahashi can obtain.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq T \leq S \leq 10^5
  • 1 \leq C_i \leq 10^5 (1 \leq i \leq N)
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers

Input

N S T
C_1 V_1
C_2 V_2
:
C_N V_N
  • The first line contains N, the number of item types, S, Takahashi's total money, and T, the amount to keep as living expenses, separated by spaces.
  • From the 2nd line to the (N + 1)-th line, the information for each item is given.
  • The (1 + i)-th line contains C_i, the price of the i-th item, and V_i, the satisfaction value of that item, separated by spaces.

Output

Print the maximum total satisfaction Takahashi can obtain, in a single line.


Sample Input 1

3 100 30
30 50
40 60
50 80

Sample Output 1

110

Sample Input 2

3 50 50
10 100
20 200
30 300

Sample Output 2

0

Sample Input 3

8 500 100
100 200
150 300
200 500
80 150
120 250
250 600
50 100
180 400

Sample Output 3

900

Sample Input 4

15 10000 3000
500 1200
1200 3500
800 2000
1500 4000
300 700
2000 5500
700 1800
900 2400
1100 3000
600 1500
1800 4800
400 1000
1300 3600
250 600
950 2600

Sample Output 4

19200

Sample Input 5

1 1 1
1 1000000000

Sample Output 5

0
D - 花束の仕分け

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

配点 : 400

問題文

高橋君は花屋でアルバイトをしています。今日は N 本の花を K 個の花束に仕分ける作業を任されました。

各花 i1 \leq i \leq N)には「茎の長さ」を表す整数 A_i が定められています。N 本の花すべてを K 個の花束のいずれかちょうど 1 つに割り当てます。ただし、各花束に入れられる花の本数は 0 本以上 M 本以下です。花が 1 本も入っていない花束があっても構いません。

同じ花束に入れる花どうしで茎の長さの差が大きいと、見栄えが悪くなってしまいます。そこで高橋君は、非負整数 D を用いた次の条件を満たすように花を仕分けたいと考えています。

  • 花が 2 本以上入っているどの花束についても、その花束に含まれる花の茎の長さの最大値と最小値の差が D 以下である。

花が 0 本または 1 本のみの花束については、この条件は自動的に満たされます。

D が小さいほど各花束の見栄えは良くなりますが、D を小さくしすぎると K 個の花束ではすべての花を仕分けられなくなることがあります。一方、D を十分大きくすれば、各花束の容量制約 M 本以下のみを考えればよくなり、K \times M \geq N が保証されているため、必ず仕分けることができます。

すべての N 本の花を上記の条件を満たすように K 個の花束に割り当てることが可能となる D の最小値を求めてください。A_i はすべて整数であるため、答えも非負整数となります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq M \leq N
  • K \times M \geq N
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数である。

入力

N K M
A_1 A_2 \ldots A_N
  • 1 行目には、花の本数を表す整数 N、花束の個数を表す整数 K、各花束に入れられる花の最大本数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各花の茎の長さを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

すべての N 本の花を条件を満たすように割り当てられる D の最小値を 1 行で出力せよ。


入力例 1

5 2 3
1 3 5 8 10

出力例 1

4

入力例 2

4 2 2
1 5 10 20

出力例 2

10

入力例 3

10 3 4
2 5 8 11 15 20 25 30 33 37

出力例 3

10

入力例 4

20 5 5
3 7 12 18 25 31 38 42 50 55 63 70 78 85 90 96 100 108 115 120

出力例 4

20

入力例 5

1 1 1
1000000000

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi works part-time at a flower shop. Today he has been assigned the task of sorting N flowers into K bouquets.

Each flower i (1 \leq i \leq N) has an integer A_i representing its "stem length." All N flowers must be assigned to exactly one of the K bouquets. However, the number of flowers in each bouquet must be between 0 and M, inclusive. It is acceptable for a bouquet to contain no flowers at all.

If flowers in the same bouquet have a large difference in stem length, the bouquet will look unappealing. Therefore, Takahashi wants to sort the flowers so that the following condition is satisfied using a non-negative integer D:

  • For every bouquet containing 2 or more flowers, the difference between the maximum and minimum stem lengths of the flowers in that bouquet is at most D.

For bouquets containing 0 or 1 flowers, this condition is automatically satisfied.

The smaller D is, the better each bouquet looks, but if D is too small, it may become impossible to sort all the flowers into K bouquets. On the other hand, if D is sufficiently large, only the capacity constraint of at most M flowers per bouquet needs to be considered, and since K \times M \geq N is guaranteed, it is always possible to sort the flowers.

Find the minimum value of D such that it is possible to assign all N flowers to K bouquets while satisfying the above condition. Since all A_i are integers, the answer is also a non-negative integer.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq M \leq N
  • K \times M \geq N
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

N K M
A_1 A_2 \ldots A_N
  • The first line contains three space-separated integers: N representing the number of flowers, K representing the number of bouquets, and M representing the maximum number of flowers per bouquet.
  • The second line contains space-separated integers A_1, A_2, \ldots, A_N representing the stem length of each flower.

Output

Print in one line the minimum value of D such that all N flowers can be assigned while satisfying the condition.


Sample Input 1

5 2 3
1 3 5 8 10

Sample Output 1

4

Sample Input 2

4 2 2
1 5 10 20

Sample Output 2

10

Sample Input 3

10 3 4
2 5 8 11 15 20 25 30 33 37

Sample Output 3

10

Sample Input 4

20 5 5
3 7 12 18 25 31 38 42 50 55 63 70 78 85 90 96 100 108 115 120

Sample Output 4

20

Sample Input 5

1 1 1
1000000000

Sample Output 5

0
E - 迷子の子猫たち

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

配点 : 433

問題文

高橋君は HW 列のグリッドで表される街を歩いています。グリッドの各マスは通行可能な道(.)または建物(#)のいずれかです。マスの座標は (r, c)r 行目、c 列目、ともに 1 から始まる)で表されます。

高橋君の初期位置はグリッド上で @ と表されるマスにあります。街のあちこちに迷子の子猫が K 匹おり、子猫のいるマスはグリッド上で F と表されています。また、街には動物保護施設が 1 箇所あり、グリッド上で G と表されています。これらのマス(@, F, G)はすべて通行可能であり、どの 2 つも異なる位置にあります。

高橋君は現在いるマスから上下左右に隣接する通行可能なマスへ 1 歩で移動することができます。斜め方向への移動やグリッドの外側への移動はできません。同じマスを何度通ってもかまいません。

高橋君が子猫のいるマスに移動すると、その子猫は自動的に保護され、以降は高橋君と一緒に移動します。子猫のいるマスに移動した際に保護を拒否することはできず、一度保護した子猫を途中で手放すこともできません。子猫を保護する順序に制約はありません。すでに保護済みの子猫がいたマスを再度訪れた場合、何も起こりません。

高橋君の目標は、すべての迷子の子猫を保護した状態で動物保護施設のマスにいることです。途中で動物保護施設のマスを通過することは自由ですが、最終的にすべての子猫を保護した状態で動物保護施設のマスにいる必要があります。

高橋君が初期位置から出発して、すべての子猫を保護し動物保護施設に到達するために必要な総移動歩数の最小値を求めてください。なお、出発時点での移動歩数は 0 です。すべての子猫を保護して動物保護施設に到達することが不可能な場合は -1 を出力してください。

制約

  • 1 \leq H \leq 200
  • 1 \leq W \leq 200
  • 1 \leq K \leq 10
  • グリッド中に @ はちょうど 1 個存在する
  • グリッド中に F はちょうど K 個存在する
  • グリッド中に G はちょうど 1 個存在する
  • S_i1 \leq i \leq H)は ., #, @, F, G のみからなる長さ W の文字列である
  • H, W, K は整数である

入力

H W K
S_1
S_2
\vdots
S_H
  • 1 行目には、グリッドの行数を表す整数 H、列数を表す整数 W、迷子の子猫の数を表す整数 K が、スペース区切りで与えられる。
  • 2 行目から H 行にわたって、グリッドの各行を表す文字列 S_i1 \leq i \leq H)が与えられる。
  • S_iW 文字の文字列であり、各文字は以下のいずれかである。
  • . : 通行可能な空きマス
  • # : 建物(通行不可)
  • @ : 高橋君の初期位置(通行可能)
  • F : 迷子の子猫の位置(通行可能、グリッド全体でちょうど K 個存在する)
  • G : 動物保護施設の位置(通行可能、グリッド全体でちょうど 1 個存在する)

出力

すべての子猫を保護して動物保護施設に到達するために必要な最小の総移動歩数を 1 行で出力せよ。不可能な場合は -1 を出力せよ。


入力例 1

3 5 2
@.F.G
.....
..F..

出力例 1

8

入力例 2

3 5 1
@.#F.
..#..
..#.G

出力例 2

-1

入力例 3

7 10 3
@........F
.########.
.#......#.
.#..F...#.
.#......#.
.########.
G........F

出力例 3

-1

Score : 433 pts

Problem Statement

Takahashi is walking through a city represented by a grid with H rows and W columns. Each cell of the grid is either a passable road (.) or a building (#). The coordinates of a cell are represented as (r, c) (row r, column c, both 1-indexed).

Takahashi's initial position is the cell marked @ on the grid. There are K lost kittens scattered around the city, and the cells where kittens are located are marked F on the grid. There is also exactly one animal shelter in the city, marked G on the grid. All of these cells (@, F, G) are passable, and no two of them occupy the same position.

Takahashi can move one step at a time from his current cell to an adjacent passable cell in one of the four cardinal directions (up, down, left, right). Diagonal movement and movement outside the grid are not allowed. He may pass through the same cell any number of times.

When Takahashi moves to a cell where a kitten is located, that kitten is automatically rescued and will travel with Takahashi from that point on. He cannot refuse to rescue a kitten when entering its cell, nor can he release a kitten once rescued. There are no restrictions on the order in which kittens are rescued. If he revisits a cell where a kitten has already been rescued, nothing happens.

Takahashi's goal is to be at the animal shelter cell with all lost kittens rescued. He may freely pass through the animal shelter cell along the way, but ultimately he must be at the animal shelter cell with all kittens rescued.

Find the minimum total number of steps required for Takahashi to start from his initial position, rescue all kittens, and reach the animal shelter. The number of steps at the start is 0. If it is impossible to rescue all kittens and reach the animal shelter, output -1.

Constraints

  • 1 \leq H \leq 200
  • 1 \leq W \leq 200
  • 1 \leq K \leq 10
  • There is exactly 1 @ in the grid
  • There are exactly K Fs in the grid
  • There is exactly 1 G in the grid
  • S_i (1 \leq i \leq H) is a string of length W consisting only of ., #, @, F, G
  • H, W, K are integers

Input

H W K
S_1
S_2
\vdots
S_H
  • The first line contains three space-separated integers: H representing the number of rows, W representing the number of columns, and K representing the number of lost kittens.
  • The following H lines each contain a string S_i (1 \leq i \leq H) representing a row of the grid.
  • S_i is a string of W characters, where each character is one of the following:
  • . : A passable empty cell
  • # : A building (impassable)
  • @ : Takahashi's initial position (passable)
  • F : A lost kitten's position (passable, exactly K in the entire grid)
  • G : The animal shelter's position (passable, exactly 1 in the entire grid)

Output

Output in one line the minimum total number of steps required to rescue all kittens and reach the animal shelter. If it is impossible, output -1.


Sample Input 1

3 5 2
@.F.G
.....
..F..

Sample Output 1

8

Sample Input 2

3 5 1
@.#F.
..#..
..#.G

Sample Output 2

-1

Sample Input 3

7 10 3
@........F
.########.
.#......#.
.#..F...#.
.#......#.
.########.
G........F

Sample Output 3

-1