実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君は、筋力トレーニングのために N 種類のエクササイズを準備しています。それぞれのエクササイズには 1 から N までの番号が付けられており、エクササイズ i のスタミナの初期値は A_i です。
各エクササイズには 残りスタミナ が設定されており、トレーニング開始前の時点では、エクササイズ i の残りスタミナは初期値 A_i に等しいです。
高橋君は毎日ちょうど1回、以下の手順でトレーニングを行います。
- N 種類のエクササイズのうち、残りスタミナが K 以上であるものの中から 1つ を選ぶ。残りスタミナが K 以上であれば、過去に何度選んだエクササイズであっても再び選ぶことができる。
- 選んだエクササイズの残りスタミナをちょうど K だけ減らす。選ばれなかったエクササイズの残りスタミナは変化しない。
手順 1 において、残りスタミナが K 以上のエクササイズが1つも存在しない場合、その日のトレーニングは行えず、それ以降もトレーニングを続けることはできません。
高橋君は M 日間のトレーニングを計画しています。すなわち、上の手順を合計 M 回行う必要があります。
高橋君がエクササイズの選び方をうまく決めることで、M 日間すべてトレーニングを行うことができるかどうかを判定してください。できる場合は Yes を、できない場合は No を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^9
- 1 \leq K \leq 10^9
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数
入力
N M K A_1 A_2 \ldots A_N
- 1 行目には、エクササイズの種類数を表す整数 N、トレーニングしたい日数を表す整数 M、1回のトレーニングで消費する残りスタミナの量を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各エクササイズのスタミナの初期値を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
高橋君が M 日間、毎日トレーニングを続けることができる場合は Yes を、できない場合は No を1行で出力してください。
入力例 1
3 5 2 4 3 5
出力例 1
Yes
入力例 2
3 6 2 4 3 5
出力例 2
No
入力例 3
5 5 1000000000 1000000000 1000000000 1000000000 999999999 1000000000
出力例 3
No
Score : 233 pts
Problem Statement
Takahashi is preparing N types of exercises for strength training. Each exercise is numbered from 1 to N, and exercise i has an initial stamina value of A_i.
Each exercise has a remaining stamina value. Before training begins, the remaining stamina of exercise i is equal to its initial value A_i.
Every day, Takahashi performs training exactly once according to the following procedure:
- From the N types of exercises, choose one whose remaining stamina is at least K. As long as the remaining stamina is at least K, an exercise can be chosen again regardless of how many times it has been chosen in the past.
- Decrease the remaining stamina of the chosen exercise by exactly K. The remaining stamina of exercises that were not chosen does not change.
In step 1, if there is no exercise with remaining stamina of at least K, training cannot be performed that day, and no further training can be done from that point onward.
Takahashi is planning M days of training. That is, he needs to perform the above procedure a total of M times.
Determine whether Takahashi can complete training on all M days by choosing exercises wisely. If it is possible, output Yes; otherwise, output No.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^9
- 1 \leq K \leq 10^9
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All inputs are integers
Input
N M K A_1 A_2 \ldots A_N
- The first line contains three space-separated integers: N, the number of types of exercises; M, the number of days Takahashi wants to train; and K, the amount of remaining stamina consumed per training session.
- The second line contains space-separated integers A_1, A_2, \ldots, A_N, representing the initial stamina values of each exercise.
Output
If Takahashi can continue training every day for M days, output Yes; otherwise, output No, on a single line.
Sample Input 1
3 5 2 4 3 5
Sample Output 1
Yes
Sample Input 2
3 6 2 4 3 5
Sample Output 2
No
Sample Input 3
5 5 1000000000 1000000000 1000000000 1000000000 999999999 1000000000
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は車で高速道路を走って目的地まで向かうことになりました。
この高速道路には、入口から目的地へ向かう途中に N 個の料金所が順番に設置されています。i 番目の料金所は入口から距離 D_i メートルの位置にあります。目的地は入口から距離 G メートルの位置にあり、すべての料金所よりも入口から遠い場所にあります。
高橋君の車は常に毎秒 1 メートルの一定速度で走行します。料金所に到達すると停止して料金を支払う必要があり、i 番目の料金所では停止してから支払いを完了し再出発するまでに T_i 秒かかります。料金所以外で停止することはありません。
高橋君は ETC カードを持っており、利用区間を 必ずちょうど1つ 設定しなければなりません。具体的には、ある整数 i(1 \leq i \leq N-K+1)を選び、i 番目から i+K-1 番目までの番号が連続する ちょうど K 個 の料金所を ETC 利用区間として指定します。ETC 利用区間に指定された K 個の料金所では停止せずにそのまま通過でき、支払い時間はかかりません。それ以外の料金所では通常どおり停止して支払いを行います。
高橋君が高速道路の入口を出発してから目的地に到達するまでにかかる時間は、走行時間(入口から目的地までの距離が G メートル、速度が毎秒 1 メートルであるため G 秒)に、ETC 利用区間以外の各料金所での支払い時間の合計を加えたものです。ETC 利用区間の選び方を最適にしたとき、この所要時間の最小値を求めてください。
制約
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq G \leq 10^9
- 1 \leq D_i < G (1 \leq i \leq N)
- D_1 < D_2 < \cdots < D_N
- 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数である
入力
N K G D_1 T_1 D_2 T_2 \vdots D_N T_N
- 1 行目には、料金所の数を表す整数 N、ETC 利用区間として指定する料金所の数を表す整数 K、入口から目的地までの距離を表す整数 G が、スペース区切りで与えられる。
- 1 + i 行目(1 \leq i \leq N)には、i 番目の料金所の入口からの距離 D_i と、その料金所での支払いにかかる時間 T_i が、スペース区切りで与えられる。
出力
高橋君が高速道路の入口から目的地まで到達するのにかかる最短時間を 1 行で出力せよ。
入力例 1
3 2 100 10 5 30 10 60 3
出力例 1
103
入力例 2
5 3 1000 100 20 200 50 400 30 600 40 800 10
出力例 2
1030
入力例 3
7 4 500000000 1000 100000000 50000 200000000 100000 150000000 200000 300000000 300000 50000000 400000 250000000 450000 100000000
出力例 3
900000000
Score : 333 pts
Problem Statement
Takahashi is driving on a highway to reach his destination.
Along this highway, there are N toll booths placed in order between the entrance and the destination. The i-th toll booth is located at a distance of D_i meters from the entrance. The destination is located at a distance of G meters from the entrance, and it is farther from the entrance than all toll booths.
Takahashi's car always travels at a constant speed of 1 meter per second. When he reaches a toll booth, he must stop and pay the toll; at the i-th toll booth, it takes T_i seconds from stopping to completing the payment and departing again. He does not stop anywhere other than at toll booths.
Takahashi has an ETC card and must set exactly one ETC usage section. Specifically, he chooses an integer i (1 \leq i \leq N-K+1) and designates exactly K consecutively numbered toll booths from the i-th to the (i+K-1)-th as the ETC usage section. At the K toll booths designated as the ETC usage section, he can pass through without stopping, incurring no payment time. At all other toll booths, he stops and pays as usual.
The time it takes for Takahashi to travel from the highway entrance to the destination is the sum of the travel time (since the distance from the entrance to the destination is G meters and the speed is 1 meter per second, this is G seconds) and the total payment time at all toll booths outside the ETC usage section. Find the minimum possible total time when the ETC usage section is chosen optimally.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq G \leq 10^9
- 1 \leq D_i < G (1 \leq i \leq N)
- D_1 < D_2 < \cdots < D_N
- 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N K G D_1 T_1 D_2 T_2 \vdots D_N T_N
- The first line contains three space-separated integers: N, the number of toll booths; K, the number of toll booths to designate as the ETC usage section; and G, the distance from the entrance to the destination.
- The (1 + i)-th line (1 \leq i \leq N) contains two space-separated values: D_i, the distance of the i-th toll booth from the entrance, and T_i, the time required for payment at that toll booth.
Output
Output in a single line the minimum time it takes for Takahashi to travel from the highway entrance to the destination.
Sample Input 1
3 2 100 10 5 30 10 60 3
Sample Output 1
103
Sample Input 2
5 3 1000 100 20 200 50 400 30 600 40 800 10
Sample Output 2
1030
Sample Input 3
7 4 500000000 1000 100000000 50000 200000000 100000 150000000 200000 300000000 300000 50000000 400000 250000000 450000 100000000
Sample Output 3
900000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は居酒屋に来ています。メニューには N 品の料理があり、i 番目の料理には「おいしさ」 A_i と「こってり度」 B_i が定められています。高橋君はこれらの料理の中から任意の組み合わせを選んで注文できます。各料理は最大 1 回まで注文でき、1 品も注文しないことも許されます(その場合、満足度は 0 です)。
高橋君はこってりした料理が好きですが、注文した料理のこってり度の合計が K を超えると、超過したこってり度 1 あたり満足度が D だけ下がってしまいます。
具体的には、高橋君の最終的な満足度は次の式で計算されます。
\text{満足度} = \left(\text{注文した料理のおいしさの合計}\right) - D \times \max\!\left(0,\ \text{注文した料理のこってり度の合計} - K\right)
高橋君が得られる満足度の最大値を求めてください。
制約
- 1 \leq N \leq 19
- 1 \leq K \leq 10^6
- 1 \leq D \leq 10^6
- 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq B_i \leq 10^6 (1 \leq i \leq N)
- 入力はすべて整数である
入力
N K D A_1 B_1 A_2 B_2 \vdots A_N B_N
- 1 行目には、料理の数 N、こってり度の許容上限 K、超過 1 あたりの満足度減少量 D が、スペース区切りで与えられる。
- 2 行目から N+1 行目には、各料理のおいしさとこってり度が与えられる。
- 1+i 行目には、i 番目の料理のおいしさ A_i とこってり度 B_i がスペース区切りで与えられる。
出力
高橋君が得られる満足度の最大値を整数で 1 行に出力せよ。
入力例 1
3 5 2 4 2 5 3 10 6
出力例 1
9
入力例 2
2 1 10 3 5 4 6
出力例 2
0
入力例 3
8 15 3 10 4 7 5 8 6 14 8 5 3 11 7 6 2 13 9
出力例 3
30
入力例 4
19 50 4 12 5 7 4 20 10 18 9 5 2 11 6 9 5 14 7 6 3 25 13 8 4 16 8 10 6 13 7 21 11 4 1 17 9 15 8 19 10
出力例 4
104
入力例 5
1 1 1000000 1000000 1000000
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is at an izakaya (Japanese-style pub). The menu has N dishes, and the i-th dish has a "deliciousness" value A_i and a "richness" value B_i. Takahashi can order any combination of these dishes. Each dish can be ordered at most once, and it is also allowed to order no dishes at all (in which case, the satisfaction is 0).
Takahashi likes rich dishes, but if the total richness of the ordered dishes exceeds K, his satisfaction decreases by D for each unit of excess richness.
Specifically, Takahashi's final satisfaction is calculated by the following formula:
\text{Satisfaction} = \left(\text{Total deliciousness of ordered dishes}\right) - D \times \max\!\left(0,\ \text{Total richness of ordered dishes} - K\right)
Find the maximum satisfaction Takahashi can achieve.
Constraints
- 1 \leq N \leq 19
- 1 \leq K \leq 10^6
- 1 \leq D \leq 10^6
- 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq B_i \leq 10^6 (1 \leq i \leq N)
- All inputs are integers
Input
N K D A_1 B_1 A_2 B_2 \vdots A_N B_N
- The first line contains the number of dishes N, the richness tolerance limit K, and the satisfaction decrease per unit of excess D, separated by spaces.
- From the 2nd line to the (N+1)-th line, the deliciousness and richness of each dish are given.
- The (1+i)-th line contains the deliciousness A_i and richness B_i of the i-th dish, separated by a space.
Output
Print the maximum satisfaction Takahashi can achieve as an integer on a single line.
Sample Input 1
3 5 2 4 2 5 3 10 6
Sample Output 1
9
Sample Input 2
2 1 10 3 5 4 6
Sample Output 2
0
Sample Input 3
8 15 3 10 4 7 5 8 6 14 8 5 3 11 7 6 2 13 9
Sample Output 3
30
Sample Input 4
19 50 4 12 5 7 4 20 10 18 9 5 2 11 6 9 5 14 7 6 3 25 13 8 4 16 8 10 6 13 7 21 11 4 1 17 9 15 8 19 10
Sample Output 4
104
Sample Input 5
1 1 1000000 1000000 1000000
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は、ある通信システムのログを解析しています。このシステムでは、 0 と 1 のみからなる長さ N のビット列 S が記録されています。
高橋君は、この信号ログの中から「ほぼ同じ」区間のペアを見つけたいと考えています。
S の連続部分文字列を、開始位置 l と終了位置 r の組 (l, r) ( 1 \leq l \leq r \leq N )で表します。これは S の l 文字目から r 文字目までの連続した部分を指し、その長さは r - l + 1 です。
2つの連続部分文字列の組 ((l_1, r_1), (l_2, r_2)) が「ほぼ同じ」ペアであるとは、以下の条件をすべて満たすことを言います。
- (l_1, r_1) \neq (l_2, r_2) である。すなわち、 l_1 \neq l_2 または r_1 \neq r_2 である。
- r_1 - l_1 = r_2 - l_2 である。すなわち、2つの連続部分文字列の長さが等しい。
- 2つの連続部分文字列の長さを L とする。先頭から i 番目の文字同士( 1 \leq i \leq L )を比較したとき、文字が異なる位置 i がちょうど 1 つである(すなわち、ハミング距離がちょうど 1 である)。
ペアは 順序なし で数えます。すなわち、 ((l_1, r_1), (l_2, r_2)) と ((l_2, r_2), (l_1, r_1)) は同一のペアとみなします。
なお、部分文字列は位置の組 (l, r) で区別します。 (l_1, r_1) \neq (l_2, r_2) であれば、たとえ取り出した文字列の内容が一致していても、それらは異なる部分文字列として扱います。
条件を満たすペアの個数を求めてください。
制約
- 1 \leq N \leq 5000
- N は整数である。
- S は
0と1のみからなる長さ N の文字列である。
入力
N S
- 1 行目には、ビット列の長さを表す整数 N が与えられる。
- 2 行目には、
0と1のみからなる長さ N の文字列 S が与えられる。
出力
条件を満たす順序なしペアの個数を整数として 1 行で出力せよ。
入力例 1
3 101
出力例 1
2
入力例 2
4 0011
出力例 2
7
入力例 3
20 01101001011010010110
出力例 3
298
入力例 4
50 01001101011010001001110101100110100101101001101011
出力例 4
2136
入力例 5
1 0
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi is analyzing the log of a communication system. In this system, a bit string S of length N consisting only of 0 and 1 is recorded.
Takahashi wants to find pairs of "nearly identical" intervals in this signal log.
A contiguous substring of S is represented by a pair of starting position l and ending position r, denoted (l, r) (1 \leq l \leq r \leq N). This refers to the contiguous portion from the l-th character to the r-th character of S, and its length is r - l + 1.
A pair of two contiguous substrings ((l_1, r_1), (l_2, r_2)) is called a "nearly identical" pair if and only if all of the following conditions are satisfied:
- (l_1, r_1) \neq (l_2, r_2). That is, l_1 \neq l_2 or r_1 \neq r_2.
- r_1 - l_1 = r_2 - l_2. That is, the two contiguous substrings have equal length.
- Let L be the length of the two contiguous substrings. When comparing the i-th characters from the beginning (1 \leq i \leq L), there is exactly 1 position i where the characters differ (that is, the Hamming distance is exactly 1).
Pairs are counted as unordered. That is, ((l_1, r_1), (l_2, r_2)) and ((l_2, r_2), (l_1, r_1)) are considered the same pair.
Note that substrings are distinguished by their position pair (l, r). Even if the actual string contents are identical, if (l_1, r_1) \neq (l_2, r_2), they are treated as different substrings.
Find the number of pairs that satisfy the conditions.
Constraints
- 1 \leq N \leq 5000
- N is an integer.
- S is a string of length N consisting only of
0and1.
Input
N S
- The first line contains an integer N representing the length of the bit string.
- The second line contains a string S of length N consisting only of
0and1.
Output
Output the number of unordered pairs satisfying the conditions as a single integer on one line.
Sample Input 1
3 101
Sample Output 1
2
Sample Input 2
4 0011
Sample Output 2
7
Sample Input 3
20 01101001011010010110
Sample Output 3
298
Sample Input 4
50 01001101011010001001110101100110100101101001101011
Sample Output 4
2136
Sample Input 5
1 0
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は通信会社のエンジニアとして、道路沿いの電波状況の調査を担当しています。
調査対象の道路は一直線で、道路沿いには N 基の電波塔が建てられています。道路は数直線として表され、 i 番目の電波塔は座標 X_i に建てられており、出力 B_i を持っています。
座標 p における「信号強度」は、すべての電波塔からの寄与の合計として次のように定義されます。
f(p) = \sum_{i=1}^{N} \max(0,\ B_i - |p - X_i|)
すなわち、各電波塔 i は座標 p に対して \max(0,\ B_i - |p - X_i|) だけの信号強度を届けます。電波塔から離れるほど信号は弱くなり、距離が出力 B_i 以上になると届かなくなります。
高橋君の同僚である青木君は、 Q 個の区間について信号強度の調査を行います。各調査 j では区間 [L_j, R_j] が与えられるので、その区間内の整数座標における f(p) の最大値を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 0 \leq X_i \leq 2 \times 10^5
- 1 \leq B_i \leq 2 \times 10^5
- 0 \leq L_j \leq R_j \leq 2 \times 10^5
- 入力はすべて整数である。
入力
N Q X_1 B_1 X_2 B_2 : X_N B_N L_1 R_1 L_2 R_2 : L_Q R_Q
- 1 行目には、電波塔の数を表す N と、調査の数を表す Q が、スペース区切りで与えられる。
- 2 行目から N 行では、各電波塔の情報が与えられる。
- 1 + i 行目では、 i 番目の電波塔の座標 X_i と出力 B_i が、スペース区切りで与えられる。
- 続く Q 行では、各調査の情報が与えられる。
- 1 + N + j 行目では、 j 番目の調査の区間の左端 L_j と右端 R_j が、スペース区切りで与えられる。
出力
Q 行出力せよ。 j 行目には、 j 番目の調査に対する答え、すなわち区間 [L_j, R_j] 内の整数座標における f(p) の最大値を出力せよ。
入力例 1
1 2 5 3 0 10 0 3
出力例 1
3 1
入力例 2
3 3 2 3 5 4 10 2 0 10 6 10 9 12
出力例 2
4 3 2
入力例 3
5 5 0 5 10 3 20 8 15 6 25 4 0 30 12 18 0 5 20 25 0 0
出力例 3
9 9 5 9 5
Score : 466 pts
Problem Statement
Takahashi is an engineer at a telecommunications company, responsible for investigating radio conditions along a road.
The road under investigation is a straight line, and N radio towers are built along the road. The road is represented as a number line, where the i-th radio tower is located at coordinate X_i and has power output B_i.
The "signal strength" at coordinate p is defined as the sum of contributions from all radio towers:
f(p) = \sum_{i=1}^{N} \max(0,\ B_i - |p - X_i|)
That is, each radio tower i delivers a signal strength of \max(0,\ B_i - |p - X_i|) to coordinate p. The signal weakens as one moves farther from the tower, and it no longer reaches once the distance is at least B_i.
Takahashi's colleague Aoki will conduct signal strength surveys for Q intervals. For each survey j, an interval [L_j, R_j] is given. Find the maximum value of f(p) over all integer coordinates p within that interval.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 0 \leq X_i \leq 2 \times 10^5
- 1 \leq B_i \leq 2 \times 10^5
- 0 \leq L_j \leq R_j \leq 2 \times 10^5
- All input values are integers.
Input
N Q X_1 B_1 X_2 B_2 : X_N B_N L_1 R_1 L_2 R_2 : L_Q R_Q
- The first line contains N, the number of radio towers, and Q, the number of surveys, separated by a space.
- The following N lines give information about each radio tower.
- The (1 + i)-th line contains the coordinate X_i and the power output B_i of the i-th radio tower, separated by a space.
- The following Q lines give information about each survey.
- The (1 + N + j)-th line contains the left endpoint L_j and the right endpoint R_j of the interval for the j-th survey, separated by a space.
Output
Output Q lines. The j-th line should contain the answer to the j-th survey, that is, the maximum value of f(p) over all integer coordinates p within the interval [L_j, R_j].
Sample Input 1
1 2 5 3 0 10 0 3
Sample Output 1
3 1
Sample Input 2
3 3 2 3 5 4 10 2 0 10 6 10 9 12
Sample Output 2
4 3 2
Sample Input 3
5 5 0 5 10 3 20 8 15 6 25 4 0 30 12 18 0 5 20 25 0 0
Sample Output 3
9 9 5 9 5