実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君と青木君は、数直線上を歩いています。
時刻 0 に、高橋君は座標 X に、青木君は座標 Y にいます。2 人はこれから T 分間、毎分同時に移動します。
高橋君の移動は長さ T の文字列 A で、青木君の移動は長さ T の文字列 B で表されます。A の i 文字目(1 \leq i \leq T)は、高橋君の時刻 i-1 から時刻 i への移動を、B の i 文字目は、青木君の同じ時間帯の移動を表します。各文字の意味は次の通りです。
L: 座標が 1 減少する(負の方向に移動する)R: 座標が 1 増加する(正の方向に移動する)S: 移動しない(座標は変わらない)
各整数時刻 i(1 \leq i \leq T)について、時刻 i における 2 人の座標が等しいとき、時刻 i に 出会った といいます。移動の途中ですれ違う場合は出会いに含めず、また時刻 0 も対象に含みません。
T 分の間に 2 人が出会う回数、すなわち、時刻 i における 2 人の座標が等しいような i(1 \leq i \leq T)の個数を求めてください。
制約
- 1 \leq T \leq 10^6
- -10^9 \leq X \leq 10^9
- -10^9 \leq Y \leq 10^9
- T, X, Y は整数である。
- A は
L,R,Sからなる長さ T の文字列である。 - B は
L,R,Sからなる長さ T の文字列である。
入力
T X Y A B
- 1 行目には、移動する時間を表す整数 T(分)、高橋君の初期座標を表す整数 X、青木君の初期座標を表す整数 Y が、スペース区切りで与えられる。
- 2 行目には、高橋君の移動を表す文字列 A が与えられる。
- 3 行目には、青木君の移動を表す文字列 B が与えられる。
出力
T 分の間に 2 人が出会う回数を、1 行で出力せよ。
入力例 1
4 0 2 RRSS SSLL
出力例 1
1
入力例 2
2 0 1 RS LS
出力例 2
0
入力例 3
12 0 3 RRSRLSRSLSRS SSSLSRSSLRSS
出力例 3
6
入力例 4
30 -10 15 RRSLSLRSSRRRSLSLRSSRRRSLSLRSSR SLRSRSLLSRSLRSRSLLSRSLRSRSLLSR
出力例 4
0
入力例 5
1 0 0 R L
出力例 5
0
Score : 266 pts
Problem Statement
Takahashi and Aoki are walking on a number line.
At time 0, Takahashi is at coordinate X and Aoki is at coordinate Y. From now on, the two will move simultaneously every minute for T minutes.
Takahashi's movement is described by a string A of length T, and Aoki's movement is described by a string B of length T. The i-th character of A (1 \leq i \leq T) represents Takahashi's movement from time i-1 to time i, and the i-th character of B represents Aoki's movement during the same time period. The meaning of each character is as follows:
L: The coordinate decreases by 1 (move in the negative direction)R: The coordinate increases by 1 (move in the positive direction)S: Stay in place (the coordinate does not change)
For each integer time i (1 \leq i \leq T), if the coordinates of the two people are equal at time i, we say they met at time i. Passing each other during movement does not count as a meeting, and time 0 is also not considered.
Find the number of times the two people meet during the T minutes, that is, the number of i (1 \leq i \leq T) such that the coordinates of the two people are equal at time i.
Constraints
- 1 \leq T \leq 10^6
- -10^9 \leq X \leq 10^9
- -10^9 \leq Y \leq 10^9
- T, X, Y are integers.
- A is a string of length T consisting of
L,R,S. - B is a string of length T consisting of
L,R,S.
Input
T X Y A B
- The first line contains the integer T (in minutes) representing the duration of movement, the integer X representing Takahashi's initial coordinate, and the integer Y representing Aoki's initial coordinate, separated by spaces.
- The second line contains the string A representing Takahashi's movement.
- The third line contains the string B representing Aoki's movement.
Output
Output the number of times the two people meet during the T minutes, in a single line.
Sample Input 1
4 0 2 RRSS SSLL
Sample Output 1
1
Sample Input 2
2 0 1 RS LS
Sample Output 2
0
Sample Input 3
12 0 3 RRSRLSRSLSRS SSSLSRSSLRSS
Sample Output 3
6
Sample Input 4
30 -10 15 RRSLSLRSSRRRSLSLRSSRRRSLSLRSSR SLRSRSLLSRSLRSRSLLSRSLRSRSLLSR
Sample Output 4
0
Sample Input 5
1 0 0 R L
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は工場で N 台の機械のメンテナンスを担当しています。高橋君は同時に複数の機械のメンテナンスを行うことはできず、N 台すべての機械をちょうど 1 回ずつメンテナンスしなければなりません。
i 番目の機械をメンテナンスするには、ちょうど T_i 秒の作業時間がかかります。また、各機械のメンテナンスが完了した直後に、高橋君は使用した工具の洗浄・整備(以下「工具整備」と呼びます)を行う必要があります。i 番目の機械に対する工具整備にはちょうど R_i 秒かかります。工具整備にかかる時間は機械ごとに決まっており、次にどの機械を作業するかには依存しません。メンテナンスと工具整備はすべて逐次的に行い、並行して作業することはできません。工具整備が完了するまで、次の機械のメンテナンスを開始することはできません。
ただし、最後にメンテナンスする機械については、その後に作業する機械がないため、工具整備を省略できます(省略するほうが完了時刻が早くなるため、必ず省略します)。
機械間の移動時間は無視でき、ある作業が終わった直後に次の作業を開始できるものとします。
すなわち、メンテナンスする順番を (p_1, p_2, \ldots, p_N)((1, 2, \ldots, N) の順列)としたとき、すべてのメンテナンスが完了するまでの所要時間は
\left(\sum_{k=1}^{N-1} (T_{p_k} + R_{p_k})\right) + T_{p_N}
です。
高橋君は N 台の機械を好きな順番でメンテナンスすることができます。時刻 0 に作業を開始するとき、すべての機械のメンテナンスが完了する最も早い時刻を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq T_i \leq 10^9
- 1 \leq R_i \leq 10^9
- 入力はすべて整数
入力
N T_1 R_1 T_2 R_2 \vdots T_N R_N
- 1 行目には、機械の台数を表す整数 N が与えられる。
- 2 行目から N + 1 行目には、各機械の情報が与えられる。
- 1 + i 行目には、i 番目の機械のメンテナンスにかかる作業時間 T_i と、その機械のメンテナンス後に必要な工具整備時間 R_i が、スペース区切りで与えられる。
出力
すべての機械のメンテナンスが完了する最も早い時刻を 1 行で出力してください。
入力例 1
3 5 3 3 7 8 2
出力例 1
21
入力例 2
5 10 4 7 9 3 1 6 9 2 5
出力例 2
47
入力例 3
8 100 80 200 500 150 60 300 200 50 90 400 100 250 300 180 150
出力例 3
2610
Score : 300 pts
Problem Statement
Takahashi is in charge of maintaining N machines at a factory. Takahashi cannot maintain multiple machines simultaneously and must maintain all N machines exactly once each.
Maintaining the i-th machine takes exactly T_i seconds of work time. Additionally, immediately after completing the maintenance of each machine, Takahashi must perform cleaning and servicing of the tools used (hereafter called "tool servicing"). Tool servicing for the i-th machine takes exactly R_i seconds. The time required for tool servicing is determined by each machine and does not depend on which machine is worked on next. All maintenance and tool servicing must be performed sequentially and cannot be done in parallel. The next machine's maintenance cannot begin until tool servicing is complete.
However, for the last machine to be maintained, since there is no machine to work on afterward, tool servicing can be skipped (and since skipping it results in an earlier completion time, it is always skipped).
Travel time between machines is negligible, and the next task can begin immediately after the previous one finishes.
That is, if the order of maintenance is (p_1, p_2, \ldots, p_N) (a permutation of (1, 2, \ldots, N)), the total time required to complete all maintenance is
\left(\sum_{k=1}^{N-1} (T_{p_k} + R_{p_k})\right) + T_{p_N}
Takahashi can maintain the N machines in any order he likes. If he starts working at time 0, find the earliest time at which all machine maintenance is completed.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq T_i \leq 10^9
- 1 \leq R_i \leq 10^9
- All inputs are integers
Input
N T_1 R_1 T_2 R_2 \vdots T_N R_N
- The first line contains an integer N representing the number of machines.
- Lines 2 through N + 1 contain information about each machine.
- The (1 + i)-th line contains the work time T_i required to maintain the i-th machine and the tool servicing time R_i required after maintaining that machine, separated by a space.
Output
Print the earliest time at which all machine maintenance is completed, on a single line.
Sample Input 1
3 5 3 3 7 8 2
Sample Output 1
21
Sample Input 2
5 10 4 7 9 3 1 6 9 2 5
Sample Output 2
47
Sample Input 3
8 100 80 200 500 150 60 300 200 50 90 400 100 250 300 180 150
Sample Output 3
2610
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は登山が趣味で、ある山脈の縦走ルートを計画しています。この山脈には N 個の地点が 1 から N の順に一列に並んでおり、各地点 i(1 \leq i \leq N)の標高は A_i メートルです。
高橋君は、1 \leq l \leq r \leq N を満たす整数の組 (l, r) を 1 つ選び、地点 l から地点 r まで番号の昇順に各地点を順番に通過するルートを歩きます。このルートに含まれる地点の数は r - l + 1 です。
このルートの総高低差を、ルート上で隣接する地点間の標高差の絶対値の合計、すなわち
\sum_{i=l}^{r-1} |A_{i+1} - A_i|
と定義します。なお、l = r の場合はこの和は空和となるため、総高低差は 0 です。
高橋君はトレーニングのために、総高低差が K 以上となるルートを歩きたいと考えています。しかし、時間に余裕がないため、条件を満たすルートの中で含まれる地点の数が最も少ないものを選びたいです。
総高低差が K 以上となるルートが存在するならば、そのようなルートに含まれる地点の数の最小値を求めてください。存在しない場合は -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 0 \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 を出力せよ。
入力例 1
5 5 1 4 2 5 3
出力例 1
3
入力例 2
3 100 1 2 3
出力例 2
-1
入力例 3
10 15 0 10 3 8 1 9 2 7 4 6
出力例 3
3
入力例 4
20 500 0 100 0 100 0 100 0 100 0 100 0 100 0 100 0 100 0 100 0 100
出力例 4
6
入力例 5
1 1 1000000000
出力例 5
-1
Score : 366 pts
Problem Statement
Takahashi enjoys mountain climbing and is planning a traverse route through a mountain range. The mountain range has N points arranged in a line, numbered from 1 to N, and the elevation of each point i (1 \leq i \leq N) is A_i meters.
Takahashi chooses a pair of integers (l, r) satisfying 1 \leq l \leq r \leq N, and walks a route that passes through each point in order from point l to point r in ascending order of their numbers. The number of points included in this route is r - l + 1.
The total elevation change of this route is defined as the sum of the absolute differences in elevation between adjacent points along the route, namely:
\sum_{i=l}^{r-1} |A_{i+1} - A_i|
Note that when l = r, this sum is an empty sum, so the total elevation change is 0.
Takahashi wants to walk a route with a total elevation change of at least K for training purposes. However, since he is short on time, he wants to choose a route that satisfies the condition while containing the fewest number of points.
If a route with a total elevation change of at least K exists, find the minimum number of points included in such a route. If no such route exists, output -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 0 \leq A_i \leq 10^9
- All input values are integers.
Input
N K A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of points and an integer K representing the lower bound on the total elevation change, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the elevation of each point, separated by spaces.
Output
If a route with a total elevation change of at least K exists, output the minimum number of points included in such a route on a single line. If no such route exists, output -1.
Sample Input 1
5 5 1 4 2 5 3
Sample Output 1
3
Sample Input 2
3 100 1 2 3
Sample Output 2
-1
Sample Input 3
10 15 0 10 3 8 1 9 2 7 4 6
Sample Output 3
3
Sample Input 4
20 500 0 100 0 100 0 100 0 100 0 100 0 100 0 100 0 100 0 100 0 100
Sample Output 4
6
Sample Input 5
1 1 1000000000
Sample Output 5
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は図書館の司書として働いています。今日は利用者から返却された N 冊の本を、それぞれ正しい書棚の位置に戻さなければなりません。
図書館は数直線上にあり、カウンターは座標 0 の位置にあります。i 番目の本 (1 \leq i \leq N) を戻すべき書棚は座標 X_i の位置にあります。書棚はカウンターから見て正の方向にも負の方向にも存在する可能性があります。また、異なる本の戻し先が同じ座標であることもあります。
高橋君は最初カウンター(座標 0)にいます。高橋君は一度に最大 K 冊の本を持って運ぶことができます。すべての本を戻すまで、以下の手順を繰り返します。
- カウンター(座標 0)にて、まだ戻していない本の中から 1 冊以上 K 冊以下を好きに選んで持ち出す。
- 数直線上を自由に移動し、持っている本をそれぞれ対応する書棚の座標に到達した時点で降ろす。訪れる書棚の順番は自由に決めてよい。正の方向の書棚と負の方向の書棚を同じ往復の中で訪れてもよい。
- 持ち出した本をすべて正しい書棚に降ろし終えたら、カウンター(座標 0)に戻る。この時点で1回の往復が完了する。
持ち出した本は、その往復中にすべて正しい書棚へ降ろさなければなりません。本をカウンターに持ち帰って次の往復に回すことはできません。最後の往復を含め、毎回必ずカウンターに戻ります。
1回の往復における移動距離は、その往復中に数直線上で実際に移動した距離の合計です。たとえば、座標 0 から座標 5 に移動し、そこから座標 -3 に移動し、座標 0 に戻った場合、移動距離は 5 + 8 + 3 = 16 です。
すべての往復の移動距離の合計を 総移動距離 と呼びます。すべての本を正しい書棚に戻し終えてカウンターに戻るまでの、総移動距離の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- -10^9 \leq X_i \leq 10^9
- X_i \neq 0 (書棚はカウンターの位置にはない)
- 入力はすべて整数である
入力
N K X_1 X_2 \ldots X_N
- 1 行目には、本の冊数を表す整数 N と、一度に持てる本の最大冊数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、i 番目の本を戻すべき書棚の座標を表す整数 X_1, X_2, \ldots, X_N が、スペース区切りで与えられる。
出力
すべての本を戻し終えてカウンターに戻るまでの最小の総移動距離を整数で 1 行に出力せよ。
入力例 1
3 2 1 3 5
出力例 1
12
入力例 2
5 2 -3 -1 2 4 6
出力例 2
22
入力例 3
8 3 -7 -5 -2 1 3 4 8 10
出力例 3
40
Score : 400 pts
Problem Statement
Takahashi works as a librarian at a library. Today, he must return N books that were returned by patrons to their correct positions on the shelves.
The library is on a number line, and the counter is located at coordinate 0. The shelf where the i-th book (1 \leq i \leq N) should be placed is at coordinate X_i. Shelves can exist in both the positive and negative directions from the counter. Different books may need to be returned to the same coordinate.
Takahashi starts at the counter (coordinate 0). He can carry at most K books at a time. He repeats the following procedure until all books have been returned:
- At the counter (coordinate 0), choose between 1 and K books (inclusive) from among the books not yet returned, and take them out.
- Move freely along the number line, and drop off each book when reaching the coordinate of its corresponding shelf. The order in which shelves are visited is free to choose. It is allowed to visit shelves in both the positive and negative directions within the same trip.
- After all books taken out have been placed on their correct shelves, return to the counter (coordinate 0). This completes one round trip.
All books taken out must be placed on their correct shelves during that round trip. Books cannot be brought back to the counter to be carried on a later trip. After every round trip, including the last one, Takahashi must return to the counter.
The travel distance of one round trip is the total distance actually traveled along the number line during that trip. For example, if Takahashi moves from coordinate 0 to coordinate 5, then from there to coordinate -3, and then back to coordinate 0, the travel distance is 5 + 8 + 3 = 16.
The sum of travel distances across all round trips is called the total travel distance. Find the minimum total travel distance to return all books to their correct shelves and return to the counter.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- -10^9 \leq X_i \leq 10^9
- X_i \neq 0 (no shelf is at the counter's position)
- All input values are integers
Input
N K X_1 X_2 \ldots X_N
- The first line contains an integer N representing the number of books and an integer K representing the maximum number of books that can be carried at once, separated by a space.
- The second line contains integers X_1, X_2, \ldots, X_N representing the coordinates of the shelves where each book should be returned, separated by spaces.
Output
Print the minimum total travel distance to return all books and come back to the counter, as a single integer on one line.
Sample Input 1
3 2 1 3 5
Sample Output 1
12
Sample Input 2
5 2 -3 -1 2 4 6
Sample Output 2
22
Sample Input 3
8 3 -7 -5 -2 1 3 4 8 10
Sample Output 3
40
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君と青木君は N 個の仕事を番号 1 から N で管理しています。
i 番目の仕事には、担当者を表す記号 S_i と作業コスト P_i が記録されています。S_i が T のとき担当者は高橋君、A のとき担当者は青木君です。
ここで、担当の 反転 とは、T を A に、A を T に変更することを指します。
Q 個の問い合わせが与えられます。
j 番目の問い合わせでは、次の操作を高々 1 回行うことができます。操作を行わないことも許されます。
操作: L_j \leq l \leq r \leq R_j を満たす整数の組 (l, r) を 1 つ選び、l 番目から r 番目までのすべての仕事について担当を反転する。
操作後(または操作を行わなかった場合)における、L_j 番目から R_j 番目までの仕事のうち青木君(A)が担当する仕事の作業コストの合計を考えます。操作の仕方を最適に選んだとき、この合計の最小値を求めてください。
各問い合わせは独立です。ある問い合わせでの操作は、元の担当・コストの情報や他の問い合わせには影響しません。
制約
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- S_i は
TまたはA - 1 \leq P_i \leq 10^9
- 1 \leq L_j \leq R_j \leq N
- N, Q, P_i, L_j, R_j は整数
入力
N Q S_1 P_1 S_2 P_2 \vdots S_N P_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- 1 行目には、仕事の個数を表す N と、問い合わせの個数を表す Q が、スペース区切りで与えられる。
- 続く N 行では、各仕事の情報が与えられる。
- このうち i 行目には、i 番目の仕事の担当者を表す S_i と、その仕事の作業コストを表す P_i が、スペース区切りで与えられる。
- S_i は
TまたはAである。 - 続く Q 行では、問い合わせの情報が与えられる。
- このうち j 行目には、j 番目の問い合わせで対象となる範囲の左端 L_j と右端 R_j が、スペース区切りで与えられる。
出力
Q 行出力してください。
j 行目には、j 番目の問い合わせに対する答えを整数で出力してください。
入力例 1
5 4 A 3 T 5 A 2 T 4 A 6 1 5 2 4 3 3 4 5
出力例 1
5 0 0 0
入力例 2
6 5 T 7 T 1 T 4 A 10 A 2 T 8 1 3 4 6 1 6 2 5 5 5
出力例 2
0 0 0 0 0
入力例 3
12 8 A 8 T 3 T 12 A 5 A 20 T 7 A 1 T 15 T 6 A 9 T 4 A 11 1 12 1 6 4 10 7 12 2 2 5 9 3 11 8 10
出力例 3
29 8 10 5 0 1 10 0
入力例 4
25 15 T 100 A 250 T 30 A 400 A 50 T 600 T 70 A 800 T 90 A 1000 A 110 T 1200 T 130 A 1400 T 150 A 1600 T 170 T 1800 A 190 A 2000 T 210 A 2200 T 230 A 2400 T 250 1 25 1 10 6 20 11 25 3 17 8 8 14 19 2 24 5 15 16 23 7 13 20 25 1 1 12 12 9 21
出力例 4
4680 790 3540 2670 2020 0 340 4680 1470 1810 90 440 0 0 3230
入力例 5
1 1 A 1000000000 1 1
出力例 5
0
Score : 433 pts
Problem Statement
Takahashi and Aoki manage N tasks numbered from 1 to N.
For the i-th task, a symbol S_i representing the person in charge and a work cost P_i are recorded. When S_i is T, the person in charge is Takahashi; when it is A, the person in charge is Aoki.
Here, flipping the assignment means changing T to A and A to T.
Q queries are given.
For the j-th query, you may perform the following operation at most once. It is also permitted to not perform the operation.
Operation: Choose a pair of integers (l, r) satisfying L_j \leq l \leq r \leq R_j, and flip the assignment of all tasks from the l-th to the r-th.
Consider the total work cost of tasks assigned to Aoki (A) among the tasks from the L_j-th to the R_j-th after the operation (or if no operation is performed). Find the minimum value of this total when the operation is chosen optimally.
Each query is independent. An operation in one query does not affect the original assignment/cost information or other queries.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- S_i is
TorA - 1 \leq P_i \leq 10^9
- 1 \leq L_j \leq R_j \leq N
- N, Q, P_i, L_j, R_j are integers
Input
N Q S_1 P_1 S_2 P_2 \vdots S_N P_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- The first line contains N, the number of tasks, and Q, the number of queries, separated by a space.
- The following N lines give the information of each task.
- The i-th of these lines contains S_i, representing the person in charge of the i-th task, and P_i, representing the work cost of that task, separated by a space.
- S_i is
TorA. - The following Q lines give the information of the queries.
- The j-th of these lines contains the left endpoint L_j and right endpoint R_j of the range for the j-th query, separated by a space.
Output
Print Q lines.
On the j-th line, print the answer to the j-th query as an integer.
Sample Input 1
5 4 A 3 T 5 A 2 T 4 A 6 1 5 2 4 3 3 4 5
Sample Output 1
5 0 0 0
Sample Input 2
6 5 T 7 T 1 T 4 A 10 A 2 T 8 1 3 4 6 1 6 2 5 5 5
Sample Output 2
0 0 0 0 0
Sample Input 3
12 8 A 8 T 3 T 12 A 5 A 20 T 7 A 1 T 15 T 6 A 9 T 4 A 11 1 12 1 6 4 10 7 12 2 2 5 9 3 11 8 10
Sample Output 3
29 8 10 5 0 1 10 0
Sample Input 4
25 15 T 100 A 250 T 30 A 400 A 50 T 600 T 70 A 800 T 90 A 1000 A 110 T 1200 T 130 A 1400 T 150 A 1600 T 170 T 1800 A 190 A 2000 T 210 A 2200 T 230 A 2400 T 250 1 25 1 10 6 20 11 25 3 17 8 8 14 19 2 24 5 15 16 23 7 13 20 25 1 1 12 12 9 21
Sample Output 4
4680 790 3540 2670 2020 0 340 4680 1470 1810 90 440 0 0 3230
Sample Input 5
1 1 A 1000000000 1 1
Sample Output 5
0