実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君はRPGゲームをプレイしています。
このゲームには N 個の部屋が一列に並んだダンジョンがあります。部屋は入口側から奥に向かって 1, 2, \ldots, N と番号付けられており、各部屋にはモンスターが1体ずつ待ち構えています。部屋 i (1 \leq i \leq N) にいるモンスターの強さは V_i です。
高橋君は初期の強さが W のスライムを操作して、このダンジョンを攻略します。スライムは部屋 1 から部屋 N まで、1, 2, \ldots, N の順に各部屋をちょうど1回ずつ訪れます。途中で引き返したり、部屋を飛ばしたりすることはできません。
スライムがある部屋を訪れた時点での強さを「現在の強さ」と呼びます。スライムの現在の強さは、初期の強さ W に、その部屋を訪れるまでに吸収した全てのモンスターの強さの合計を加えた値です。
スライムが部屋 i を訪れたとき、以下の2つの場合のうちいずれか一方が起こります。
- モンスターの強さ V_i がスライムの現在の強さ以下である場合(V_i がスライムの現在の強さと等しい場合を含む)、スライムはそのモンスターを吸収します。スライムの強さに V_i が即座に加算され、加算後の値が次の部屋以降での現在の強さとなります。
- モンスターの強さ V_i がスライムの現在の強さより真に大きい場合、スライムはそのモンスターを吸収できず、スライムの強さは変化しません。
スライムが部屋 1 から部屋 N まで全ての部屋を順に訪れ終わった後の、スライムの強さを求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq W \leq 10^9
- 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数
- 答えは 2^{63} 未満に収まることが保証されます(符号付き 64 ビット整数に収まります)。ただし、符号付き 32 ビット整数の範囲(2^{31}-1 以下)を超える場合があります。
入力
N W V_1 V_2 \cdots V_N
- 1 行目には、部屋の数を表す整数 N と、スライムの初期の強さを表す整数 W が、スペース区切りで与えられる。
- 2 行目には、各部屋にいるモンスターの強さを表す整数 V_1, V_2, \ldots, V_N が、スペース区切りで与えられる。
出力
ダンジョン攻略後のスライムの強さを整数で 1 行に出力せよ。
入力例 1
5 3 2 4 1 3 7
出力例 1
20
入力例 2
8 5 3 6 2 10 4 8 1 20
出力例 2
59
入力例 3
10 1 1 3 2 5 100 4 6 3 2 1000000000
出力例 3
19
Score : 233 pts
Problem Statement
Takahashi is playing an RPG game.
In this game, there is a dungeon consisting of N rooms arranged in a row. The rooms are numbered 1, 2, \ldots, N from the entrance toward the back, and each room has one monster waiting inside. The strength of the monster in room i (1 \leq i \leq N) is V_i.
Takahashi controls a slime with an initial strength of W to conquer this dungeon. The slime visits each room exactly once in order from room 1 to room N, visiting them in the order 1, 2, \ldots, N. It cannot turn back or skip rooms.
The "current strength" of the slime at the time it visits a room is defined as the initial strength W plus the total strength of all monsters absorbed before visiting that room.
When the slime visits room i, exactly one of the following two cases occurs:
- If the monster's strength V_i is less than or equal to the slime's current strength (including the case where V_i equals the slime's current strength), the slime absorbs the monster. V_i is immediately added to the slime's strength, and the value after the addition becomes the current strength for subsequent rooms.
- If the monster's strength V_i is strictly greater than the slime's current strength, the slime cannot absorb the monster, and the slime's strength does not change.
Find the slime's strength after it has visited all rooms from room 1 to room N in order.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq W \leq 10^9
- 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
- It is guaranteed that the answer fits in less than 2^{63} (it fits in a signed 64-bit integer). However, it may exceed the range of a signed 32-bit integer (2^{31}-1 or less).
Input
N W V_1 V_2 \cdots V_N
- The first line contains an integer N representing the number of rooms and an integer W representing the slime's initial strength, separated by a space.
- The second line contains integers V_1, V_2, \ldots, V_N representing the strength of the monster in each room, separated by spaces.
Output
Output the slime's strength after conquering the dungeon as an integer on a single line.
Sample Input 1
5 3 2 4 1 3 7
Sample Output 1
20
Sample Input 2
8 5 3 6 2 10 4 8 1 20
Sample Output 2
59
Sample Input 3
10 1 1 3 2 5 100 4 6 3 2 1000000000
Sample Output 3
19
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は気象観測所で働いており、毎日の平均気温を記録しています。気温は 1 から 9 までの 9 段階の整数値で簡易的に記録されています。
ある日、高橋君は過去の連続する N 日間の気温データを分析することにしました。高橋君は、連続する K 日間の気温の合計値に注目し、すべての連続 K 日間について合計値を計算したうえで、その中での最大値と最小値の差を求めたいと考えています。この差が大きいほど、期間内の気温変動が激しかったことを意味します。
具体的には、N 日間の気温の記録が A_1, A_2, \dots, A_N として与えられたとき、連続する K 日間の気温の合計値は N - K + 1 個存在します。すなわち、i = 1, 2, \dots, N - K + 1 に対して、
S_i = \sum_{j=0}^{K-1} A_{i+j}
を計算します。このとき、S_1, S_2, \dots, S_{N-K+1} の最大値と最小値の差を求めてください。
制約
- 1 \leq K \leq N \leq 10^5
- 1 \leq A_i \leq 9
- 入力はすべて整数である。
入力
N K A_1 A_2 \dots A_N
- 1 行目には、観測日数を表す整数 N と、注目する連続日数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各日の気温を表す整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
出力
S_1, S_2, \dots, S_{N-K+1} の最大値と最小値の差を 1 行で出力せよ。
入力例 1
5 3 2 5 3 1 4
出力例 1
2
入力例 2
10 4 3 1 4 1 5 9 2 6 5 3
出力例 2
13
入力例 3
20 5 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9
出力例 3
8
Score : 300 pts
Problem Statement
Takahashi works at a meteorological observatory and records the daily average temperature. Temperatures are recorded as simplified integer values ranging from 1 to 9 (9 levels).
One day, Takahashi decided to analyze temperature data from a consecutive N-day period in the past. Takahashi focuses on the total temperature over consecutive K-day windows, and wants to calculate the total for every consecutive K-day window, then find the difference between the maximum and minimum values among them. A larger difference indicates more intense temperature fluctuations during the period.
Specifically, given the temperature records for N days as A_1, A_2, \dots, A_N, there are N - K + 1 total values of consecutive K-day windows. That is, for i = 1, 2, \dots, N - K + 1, we compute:
S_i = \sum_{j=0}^{K-1} A_{i+j}
Find the difference between the maximum and minimum values among S_1, S_2, \dots, S_{N-K+1}.
Constraints
- 1 \leq K \leq N \leq 10^5
- 1 \leq A_i \leq 9
- All input values are integers.
Input
N K A_1 A_2 \dots A_N
- The first line contains an integer N representing the number of observation days and an integer K representing the number of consecutive days of interest, separated by a space.
- The second line contains integers A_1, A_2, \dots, A_N representing the temperature for each day, separated by spaces.
Output
Print the difference between the maximum and minimum values of S_1, S_2, \dots, S_{N-K+1} on a single line.
Sample Input 1
5 3 2 5 3 1 4
Sample Output 1
2
Sample Input 2
10 4 3 1 4 1 5 9 2 6 5 3
Sample Output 2
13
Sample Input 3
20 5 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
東西に伸びる一本道に沿って N 個の都市があります。i 番目の都市は西端から距離 X_i の位置にあります(都市は必ずしも西から東の順に番号付けされているとは限りません)。
高橋君はこれらの都市間に双方向の通信回線を敷設し、災害に強い通信ネットワークを構築しようとしています。通信回線は任意の2都市間に直接敷設でき、都市 i と都市 j の間に通信回線を敷設するコストは |X_i - X_j| です。ただし、次の条件があります。
- 同じ2都市間に複数の通信回線を敷設することはできない。
- 各通信回線はその両端の2都市のみを接続する。地理的に2都市の間に位置する他の都市であっても、その回線には接続されない。
構築するネットワークは、次の災害耐性条件を満たす必要があります。
- 任意の 1 つの都市 v を選んだとき、都市 v およびその都市 v に直接接続するすべての通信回線をネットワークから取り除いても、残りのすべての都市が互いに通信できる。
ここで「互いに通信できる」とは、残りの都市と残りの通信回線からなるネットワークにおいて、任意の2都市間で、直接の通信回線または他の都市を中継してたどることで到達可能であることを意味します。
この条件を満たすネットワークを構築するために必要な通信回線の敷設コストの総和の最小値を求めてください。
制約
- 3 \leq N \leq 10^6
- 0 \leq X_i \leq 10^9
- X_i \neq X_j (i \neq j)
- 入力はすべて整数
入力
N X_1 X_2 \cdots X_N
- 1 行目には、都市の個数を表す整数 N が与えられる。
- 2 行目には、各都市の西端からの距離を表す整数 X_1, X_2, \ldots, X_N がスペース区切りで与えられる。
出力
条件を満たすネットワークを構築するために必要な敷設コストの総和の最小値を整数で出力してください。
入力例 1
4 0 2 5 9
出力例 1
18
入力例 2
5 10 1 7 3 20
出力例 2
38
入力例 3
10 42 5 100 17 63 88 29 74 1 56
出力例 3
198
入力例 4
25 1000 250 4300 120 9800 7600 3150 640 8750 5200 60 7000 1500 2400 9999 3600 8100 4700 5900 9300 2050 6800 7350 1110 5550
出力例 4
19878
入力例 5
3 0 1000000000 500000000
出力例 5
2000000000
Score : 366 pts
Problem Statement
There are N cities along a straight road running from west to east. The i-th city is located at distance X_i from the western end (the cities are not necessarily numbered from west to east).
Takahashi is trying to build a disaster-resistant communication network by installing bidirectional communication lines between these cities. A communication line can be installed directly between any two cities, and the cost of installing a communication line between city i and city j is |X_i - X_j|. However, the following conditions apply:
- Multiple communication lines cannot be installed between the same pair of cities.
- Each communication line connects only the two cities at its endpoints. Even if another city is geographically located between the two cities, it is not connected to that line.
The constructed network must satisfy the following disaster resilience condition:
- For any single city v, even if city v and all communication lines directly connected to city v are removed from the network, all remaining cities can still communicate with each other.
Here, "can communicate with each other" means that in the network consisting of the remaining cities and remaining communication lines, any two cities can be reached from one another either through a direct communication line or by relaying through other cities.
Find the minimum total cost of installing communication lines needed to construct a network satisfying this condition.
Constraints
- 3 \leq N \leq 10^6
- 0 \leq X_i \leq 10^9
- X_i \neq X_j (i \neq j)
- All inputs are integers
Input
N X_1 X_2 \cdots X_N
- The first line contains an integer N representing the number of cities.
- The second line contains integers X_1, X_2, \ldots, X_N separated by spaces, representing the distance of each city from the western end.
Output
Output as an integer the minimum total installation cost needed to construct a network satisfying the condition.
Sample Input 1
4 0 2 5 9
Sample Output 1
18
Sample Input 2
5 10 1 7 3 20
Sample Output 2
38
Sample Input 3
10 42 5 100 17 63 88 29 74 1 56
Sample Output 3
198
Sample Input 4
25 1000 250 4300 120 9800 7600 3150 640 8750 5200 60 7000 1500 2400 9999 3600 8100 4700 5900 9300 2050 6800 7350 1110 5550
Sample Output 4
19878
Sample Input 5
3 0 1000000000 500000000
Sample Output 5
2000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君の会社では、社員旅行のお弁当を注文することになりました。お弁当は、メインおかず・サイドおかず・デザートの 3 種類のパーツを組み合わせて作るオーダーメイド方式です。
メインおかずには A 種類、サイドおかずには B 種類、デザートには C 種類の選択肢があります。お弁当は(メインおかず、サイドおかず、デザート)の 3 つ組で表されます。メインおかず・サイドおかず・デザートはそれぞれ独立に選べるため、3 つ組の組み合わせとしてお弁当は全部で A \times B \times C 種類存在し、そのどれでも注文可能です。
社員旅行には N 人の社員が参加します。せっかくのオーダーメイドなので、どの 2 人も同じお弁当にならないように、N 人全員にそれぞれ異なるお弁当を 1 つずつ割り当てたいと考えています。
ここで、N 人の社員はそれぞれ区別します。すなわち、選ばれた N 個のお弁当の集合が同じであっても、どの社員にどのお弁当を割り当てるかが異なれば、異なる割り当て方として数えます。
このとき、N 人への割り当て方は何通りあるかを求めてください。
ただし、答えは非常に大きくなることがあるため、10^9 + 7 で割った余りを出力してください。
制約
- 1 \leq N \leq 10^6
- 1 \leq A \leq 10^6
- 1 \leq B \leq 10^6
- 1 \leq C \leq 10^6
- N \leq A \times B \times C(すなわち、お弁当の総数は社員の人数以上であることが保証される)
- 入力はすべて整数である
入力
N A B C
社員の人数を表す N、メインおかずの種類数を表す A、サイドおかずの種類数を表す B、デザートの種類数を表す C が、スペース区切りで 1 行に与えられる。
出力
N 人全員に互いに異なるお弁当を割り当てる方法の数を 10^9 + 7 で割った余りを 1 行で出力せよ。
入力例 1
2 2 1 2
出力例 1
12
入力例 2
3 2 2 1
出力例 2
24
入力例 3
10 3 3 3
出力例 3
590793709
入力例 4
100000 100 100 100
出力例 4
431436174
入力例 5
1 1 1 1
出力例 5
1
Score : 400 pts
Problem Statement
At Takahashi's company, they need to order bento boxes for the company trip. Each bento is made in a custom-order style by combining 3 types of parts: a main dish, a side dish, and a dessert.
There are A choices for the main dish, B choices for the side dish, and C choices for the dessert. A bento is represented as a triplet (main dish, side dish, dessert). Since the main dish, side dish, and dessert can each be chosen independently, there are A \times B \times C types of bento in total, and any of them can be ordered.
N employees will participate in the company trip. Since the bentos are custom-made, they want to assign a distinct bento to each of the N employees so that no two employees receive the same bento.
Here, the N employees are distinguished from each other. That is, even if the set of N chosen bentos is the same, if the assignment of which employee gets which bento differs, it is counted as a different assignment.
Determine how many ways there are to assign bentos to the N employees.
Since the answer can be very large, output the remainder when divided by 10^9 + 7.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq A \leq 10^6
- 1 \leq B \leq 10^6
- 1 \leq C \leq 10^6
- N \leq A \times B \times C (that is, it is guaranteed that the total number of bentos is at least the number of employees)
- All inputs are integers
Input
N A B C
N representing the number of employees, A representing the number of main dish choices, B representing the number of side dish choices, and C representing the number of dessert choices are given on a single line separated by spaces.
Output
Output on a single line the number of ways to assign mutually distinct bentos to all N employees, modulo 10^9 + 7.
Sample Input 1
2 2 1 2
Sample Output 1
12
Sample Input 2
3 2 2 1
Sample Output 2
24
Sample Input 3
10 3 3 3
Sample Output 3
590793709
Sample Input 4
100000 100 100 100
Sample Output 4
431436174
Sample Input 5
1 1 1 1
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は洞窟を探検しています。洞窟は H 行 W 列のマス目で表されます。
各マスは次のいずれかです。
S: 高橋君の開始地点G: 出口O: 通行可能な通常の通路V: 通行可能だが、水に浸かった通路X: 通行できない壁
V のマスだけを上下左右の隣接でつないだときの連結成分を「地下水路」と呼びます。
高橋君は、次の行動を任意の順に行うことができます。
- 上下左右に隣接する通行可能なマスへ通常移動する。通常移動には 1 分かかる。
- 通常移動の移動先が
Vの場合、水に浸かりながら進むために追加で 1 分かかる。 - 同じ
Vのマスや同じ地下水路に複数回入った場合でも、通常移動でVに入るたびに追加で 1 分かかる。 - 現在いるマスが
Vの場合、同じ地下水路に属する任意のVのマスへ、水流に乗って 0 分で移動できる。 - この移動は通常移動ではないため、移動先が
Vであっても追加の 1 分はかからない。
開始地点 S から出口 G まで移動するために必要な最小時間を求めてください。
到達できない場合は NO を出力してください。
制約
- 1 \leq H \leq 10^5
- 1 \leq W \leq 10^5
- H \times W \leq 2 \times 10^5
- C_i は
S,G,O,V,Xのみからなる長さ W の文字列である Sはマス目全体にちょうど 1 つ存在するGはマス目全体にちょうど 1 つ存在する
入力
H W C_1 C_2 : C_H
- 1 行目には、洞窟の縦の長さを表す H と、横の長さを表す W が、スペース区切りで与えられる。
- 続く H 行では、洞窟の各行を表す文字列 C_i が与えられる。
- 1 + i 行目では、上から i 行目の状態を表す長さ W の文字列 C_i が与えられる。
- C_i は
S,G,O,V,Xのみからなる。 SとGはそれぞれちょうど 1 つずつ存在する。
出力
S から G まで移動できる場合は、必要な最小時間を整数で出力してください。移動できない場合は NO を出力してください。
入力例 1
3 5 SOVVG OOXXO OOOOO
出力例 1
4
入力例 2
3 3 SXX XXX XXG
出力例 2
NO
入力例 3
8 12 SOVVOOOXOOOO XOXXOXOXOXXO XOVVVXOVVVXO XOXOXXOXOXOO XOXOOOOVOXVX XOVXXXXVOXVX XOOOOVVVOXVO XXXXOOOOOOGX
出力例 3
16
入力例 4
15 20 SOVVVVVOOOOOOOOOOOOO OXXXXXXOXOOOXOOOXXXO OOVVVXXOXVVOXOVVVVVO OXXVXXXOXVXXXXOOXXVO OXXVVVVOXVOOOOXOXXVO OOOXXXOOXVXXXXXOXXVO XVVOOOOXVVVVVVXOXXVO OVXXXXXXOXXXXOXOXXVO OVVVVVVOOOOXXOXOOOVO OXXXXXXOXXOXXOXVXXVO OOOOOOOOVVOOOOXVXXVO OXXXXXOXXXXXXXVVXXVO OVVVVVOOOOOOOOOVVVVO OXXXXXXXXXXXXXXOXXVO OOOOOOOOOOXXXXOOOVVG
出力例 4
15
入力例 5
1 2 SG
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is exploring a cave. The cave is represented as a grid with H rows and W columns.
Each cell is one of the following:
S: Takahashi's starting pointG: The exitO: A passable normal corridorV: A passable but water-filled corridorX: An impassable wall
A connected component formed by connecting only V cells through up/down/left/right adjacency is called an "underground waterway."
Takahashi can perform the following actions in any order:
- Make a normal move to an adjacent passable cell (up, down, left, or right). A normal move takes 1 minute.
- If the destination of a normal move is a
Vcell, it takes an additional 1 minute due to wading through water. - Even if he enters the same
Vcell or the same underground waterway multiple times, the additional 1 minute is incurred each time he enters aVcell via a normal move. - If the current cell is
V, he can ride the water current to move to anyVcell belonging to the same underground waterway in 0 minutes. - Since this movement is not a normal move, the additional 1 minute is not incurred even though the destination is a
Vcell.
Find the minimum time required to move from the starting point S to the exit G.
If it is impossible to reach the exit, output NO.
Constraints
- 1 \leq H \leq 10^5
- 1 \leq W \leq 10^5
- H \times W \leq 2 \times 10^5
- C_i is a string of length W consisting only of
S,G,O,V,X - There is exactly one
Sin the entire grid - There is exactly one
Gin the entire grid
Input
H W C_1 C_2 : C_H
- The first line contains H, the vertical length of the cave, and W, the horizontal length of the cave, separated by a space.
- The following H lines each contain a string C_i representing a row of the cave.
- The (1 + i)-th line contains a string C_i of length W representing the state of the i-th row from the top.
- C_i consists only of
S,G,O,V,X. - There is exactly one
Sand exactly oneG.
Output
If it is possible to move from S to G, output the minimum time required as an integer. If it is impossible, output NO.
Sample Input 1
3 5 SOVVG OOXXO OOOOO
Sample Output 1
4
Sample Input 2
3 3 SXX XXX XXG
Sample Output 2
NO
Sample Input 3
8 12 SOVVOOOXOOOO XOXXOXOXOXXO XOVVVXOVVVXO XOXOXXOXOXOO XOXOOOOVOXVX XOVXXXXVOXVX XOOOOVVVOXVO XXXXOOOOOOGX
Sample Output 3
16
Sample Input 4
15 20 SOVVVVVOOOOOOOOOOOOO OXXXXXXOXOOOXOOOXXXO OOVVVXXOXVVOXOVVVVVO OXXVXXXOXVXXXXOOXXVO OXXVVVVOXVOOOOXOXXVO OOOXXXOOXVXXXXXOXXVO XVVOOOOXVVVVVVXOXXVO OVXXXXXXOXXXXOXOXXVO OVVVVVVOOOOXXOXOOOVO OXXXXXXOXXOXXOXVXXVO OOOOOOOOVVOOOOXVXXVO OXXXXXOXXXXXXXVVXXVO OVVVVVOOOOOOOOOVVVVO OXXXXXXXXXXXXXXOXXVO OOOOOOOOOOXXXXOOOVVG
Sample Output 4
15
Sample Input 5
1 2 SG
Sample Output 5
1