Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は、 N 個のタンクが横一列に並んだ水道システムを管理しています。各タンクには 1 から N までの番号が左から順についており、タンク i には現在 A_i リットルの水が入っています。
高橋君は、このシステムの水量を調整するために、以下の操作を何回でも( 0 回でもよい)行うことができます。操作ごとに選ぶタンクの組は自由に変えられます。
操作: 隣り合う 2 つのタンク i と i + 1 ( 1 \leq i \leq N - 1 )を選び、一方のタンクから他方のタンクへ水を 1 リットル移す。すなわち、次のいずれかを行う。
- タンク i の水を 1 リットル減らし、タンク i + 1 の水を 1 リットル増やす。
- タンク i + 1 の水を 1 リットル減らし、タンク i の水を 1 リットル増やす。
ただし、各タンクの水量はどの操作の実行後においても 0 以上でなければなりません。すなわち、水が 0 リットルのタンクから水を減らす操作は行えません。
この操作では水の総量は変化しないことに注意してください。
高橋君は、操作を繰り返すことで、すべてのタンクの水量を等しくしたいと考えています。これが達成可能かどうかを判定してください。可能な場合は Yes を、不可能な場合は No を出力してください。
制約
- 1 \leq N \leq 10^5
- 0 \leq A_i \leq 10^9
- 入力はすべて整数である
入力
N A_1 A_2 \ldots A_N
- 1 行目には、タンクの個数を表す整数 N が与えられる。
- 2 行目には、各タンクに入っている水の量を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
すべてのタンクの水量を等しくすることが可能な場合は Yes を、不可能な場合は No を 1 行で出力せよ。
入力例 1
3 1 2 3
出力例 1
Yes
入力例 2
4 1 2 3 5
出力例 2
No
入力例 3
6 0 1000000000 500000000 200000000 100000000 200000000
出力例 3
No
Score : 200 pts
Problem Statement
Takahashi manages a water system consisting of N tanks arranged in a horizontal row. The tanks are numbered from 1 to N from left to right, and tank i currently contains A_i liters of water.
To adjust the water levels in this system, Takahashi can perform the following operation any number of times (including zero times). The pair of tanks chosen can be different for each operation.
Operation: Choose two adjacent tanks i and i + 1 (1 \leq i \leq N - 1), and transfer 1 liter of water from one tank to the other. Specifically, perform one of the following:
- Decrease the water in tank i by 1 liter and increase the water in tank i + 1 by 1 liter.
- Decrease the water in tank i + 1 by 1 liter and increase the water in tank i by 1 liter.
However, the water level in each tank must remain 0 or more after every operation. That is, you cannot decrease the water in a tank that has 0 liters of water.
Note that this operation does not change the total amount of water.
Takahashi wants to make the water levels of all tanks equal by repeating operations. Determine whether this is achievable. If it is possible, output Yes; otherwise, output No.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq A_i \leq 10^9
- All input values are integers
Input
N A_1 A_2 \ldots A_N
- The first line contains an integer N, representing the number of tanks.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the amount of water in each tank.
Output
If it is possible to make the water levels of all tanks equal, output Yes; otherwise, output No, in a single line.
Sample Input 1
3 1 2 3
Sample Output 1
Yes
Sample Input 2
4 1 2 3 5
Sample Output 2
No
Sample Input 3
6 0 1000000000 500000000 200000000 100000000 200000000
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は、麓から山頂を目指して登山をしています。
麓から山頂までの登山道は N 個の区間に分かれており、各区間にはそれぞれ登りの厳しさを表す難易度が設定されています。 i 番目 (1 \leq i \leq N) の区間の難易度は D_i です。
高橋君は体力 S を持った状態で登山を開始し、区間を 1 番目から N 番目まで順にすべて通過します。途中で体力が 0 以下になっても、登山を中断することなく最後まで進み続けます。
高橋君には「バテた状態」と「バテていない状態」があり、登山開始時はバテていない状態です。一度バテた状態になると、その後体力がいくら回復しても、バテた状態が解除されることはありません。
各区間 i (1 \leq i \leq N) を通過する際、以下の処理がこの順に行われます。
- 体力の消費: 高橋君がバテていない状態であれば体力が D_i だけ減少し、バテた状態であれば体力が 2 \times D_i だけ減少します。
- バテ判定: 体力消費後の体力が 0 以下であり、かつ現在バテていない状態であれば、高橋君はバテた状態に変わります。(すでにバテた状態であれば、何も起こりません。)
- 山小屋での回復: 区間 i を通過した直後に山小屋がある場合、その山小屋の回復量の分だけ体力が増加します。山小屋がない場合、何も起こりません。体力に上限はなく、初期体力を超えて回復することもあります。
登山道の途中にはいくつかの山小屋があります。山小屋は M 個(0 個の場合もあります)あり、 j 番目 (1 \leq j \leq M) の山小屋は区間 P_j を通過した直後に位置しており、体力を R_j だけ回復できます。山小屋は区間 1 から区間 N-1 の通過直後にのみ存在し得ます。区間 N(最後の区間)の通過直後には山小屋はありません。
高橋君がすべての N 区間を通過して山頂に到着したとき、残っている体力を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq N - 1
- 1 \leq S \leq 10^9
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq N - 1 (1 \leq j \leq M)
- P_j はすべて異なる
- 1 \leq R_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数
入力
N M S D_1 D_2 \ldots D_N P_1 R_1 P_2 R_2 \vdots P_M R_M
- 1 行目には、区間の数 N 、山小屋の数 M 、初期体力 S が、スペース区切りで与えられる。
- 2 行目には、各区間の難易度 D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。
- 3 行目から M 行にわたって、山小屋の情報が与えられる。 M = 0 の場合、この部分は存在しない。
- 2 + j 行目には、 j 番目の山小屋が位置する区間番号 P_j と、回復量 R_j がスペース区切りで与えられる。山小屋の情報は P_j の昇順で与えられるとは限らない。
出力
高橋君がすべての区間を通過し終えたときの残り体力を 1 行で出力せよ。体力は負の値になることもある。
入力例 1
3 1 10 3 4 2 2 3
出力例 1
4
入力例 2
3 0 5 3 4 2
出力例 2
-6
入力例 3
6 3 20 5 8 3 7 6 4 1 4 3 10 5 3
出力例 3
4
入力例 4
10 4 15 3 5 7 2 8 6 4 9 1 3 2 5 4 3 6 8 8 2
出力例 4
-38
入力例 5
1 0 1 1
出力例 5
0
Score : 266 pts
Problem Statement
Takahashi is climbing from the base of a mountain toward the summit.
The trail from the base to the summit is divided into N sections, each with a difficulty rating representing the severity of the climb. The difficulty of the i-th section (1 \leq i \leq N) is D_i.
Takahashi starts the climb with stamina S and passes through all sections in order from the 1-st to the N-th. Even if his stamina drops to 0 or below during the climb, he continues onward without stopping until the end.
Takahashi can be in either an "exhausted state" or a "non-exhausted state." He starts the climb in a non-exhausted state. Once he becomes exhausted, he remains exhausted for the rest of the climb, regardless of how much stamina he recovers afterward.
When passing through each section i (1 \leq i \leq N), the following steps are performed in this order:
- Stamina consumption: If Takahashi is in a non-exhausted state, his stamina decreases by D_i. If he is in an exhausted state, his stamina decreases by 2 \times D_i.
- Exhaustion check: If his stamina after consumption is 0 or below and he is currently in a non-exhausted state, Takahashi becomes exhausted. (If he is already exhausted, nothing happens.)
- Mountain hut recovery: If there is a mountain hut immediately after passing through section i, his stamina increases by the recovery amount of that hut. If there is no hut, nothing happens. There is no upper limit on stamina, and it can exceed the initial stamina.
There are several mountain huts along the trail. There are M huts (possibly 0), and the j-th hut (1 \leq j \leq M) is located immediately after passing through section P_j, providing a stamina recovery of R_j. Mountain huts can only exist immediately after sections 1 through N-1. There is no mountain hut immediately after section N (the last section).
Determine Takahashi's remaining stamina when he arrives at the summit after passing through all N sections.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq N - 1
- 1 \leq S \leq 10^9
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq N - 1 (1 \leq j \leq M)
- All P_j are distinct
- 1 \leq R_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers
Input
N M S D_1 D_2 \ldots D_N P_1 R_1 P_2 R_2 \vdots P_M R_M
- The first line contains the number of sections N, the number of mountain huts M, and the initial stamina S, separated by spaces.
- The second line contains the difficulties D_1, D_2, \ldots, D_N of each section, separated by spaces.
- The following M lines contain information about the mountain huts. If M = 0, this part does not exist.
- The (2 + j)-th line contains the section number P_j where the j-th mountain hut is located and the recovery amount R_j, separated by spaces. The mountain hut information is not necessarily given in ascending order of P_j.
Output
Output in a single line the remaining stamina of Takahashi after he has passed through all sections. The stamina may be a negative value.
Sample Input 1
3 1 10 3 4 2 2 3
Sample Output 1
4
Sample Input 2
3 0 5 3 4 2
Sample Output 2
-6
Sample Input 3
6 3 20 5 8 3 7 6 4 1 4 3 10 5 3
Sample Output 3
4
Sample Input 4
10 4 15 3 5 7 2 8 6 4 9 1 3 2 5 4 3 6 8 8 2
Sample Output 4
-38
Sample Input 5
1 0 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は自分の部屋の本棚を整理することにしました。本棚には N 冊の本があり、i 番目(1 \leq i \leq N)の本の厚さは V_i です。異なる番号の 2 冊の本の厚さが同じである場合もあります。
高橋君は、N 冊の本すべてをちょうど 1 回ずつ使い、任意の順番で一列に並べ替えようとしています。隣り合う本の厚さの差が大きいと見た目が悪いと感じるため、隣り合う本の厚さの差の絶対値の総和ができるだけ小さくなるように並べ替えたいと考えました。
具体的には、並べ替えた結果、左から k 番目の本の厚さを A_k とします。ここで (A_1, A_2, \ldots, A_N) は (V_1, V_2, \ldots, V_N) を並べ替えたものです。このとき、
\sum_{k=1}^{N-1} |A_k - A_{k+1}|
の値の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq V_i \leq 10^9
- 入力はすべて整数である
入力
N V_1 V_2 \ldots V_N
- 1 行目には、本の冊数を表す整数 N が与えられる。
- 2 行目には、各本の厚さを表す N 個の整数 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。
出力
隣り合う本の厚さの差の絶対値の総和の最小値を整数として 1 行で出力せよ。
入力例 1
4 3 1 4 2
出力例 1
3
入力例 2
7 10 50 30 20 40 5 25
出力例 2
45
入力例 3
10 1000000000 1 500000000 250000000 750000000 100 999999999 2 123456789 987654321
出力例 3
999999999
Score : 300 pts
Problem Statement
Takahashi has decided to organize the bookshelf in his room. The bookshelf contains N books, and the thickness of the i-th book (1 \leq i \leq N) is V_i. Two books with different indices may have the same thickness.
Takahashi wants to rearrange all N books into a single row in any order, using each book exactly once. Since a large difference in thickness between adjacent books looks unappealing, he wants to rearrange the books so that the sum of absolute differences of thicknesses between adjacent books is minimized.
Specifically, let A_k denote the thickness of the k-th book from the left after rearranging. Here, (A_1, A_2, \ldots, A_N) is a permutation of (V_1, V_2, \ldots, V_N). Find the minimum value of:
\sum_{k=1}^{N-1} |A_k - A_{k+1}|
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq V_i \leq 10^9
- All input values are integers.
Input
N V_1 V_2 \ldots V_N
- The first line contains an integer N, representing the number of books.
- The second line contains N integers V_1, V_2, \ldots, V_N separated by spaces, representing the thickness of each book.
Output
Print the minimum value of the sum of absolute differences of thicknesses between adjacent books as an integer on a single line.
Sample Input 1
4 3 1 4 2
Sample Output 1
3
Sample Input 2
7 10 50 30 20 40 5 25
Sample Output 2
45
Sample Input 3
10 1000000000 1 500000000 250000000 750000000 100 999999999 2 123456789 987654321
Sample Output 3
999999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は長い廊下を歩いて移動しようとしています。
この廊下は数直線上の閉区間 [0, L] として表されます。高橋君は座標 0 にあるスタート地点から、座標 L にあるゴール地点まで廊下をまっすぐ歩きます。
廊下には N 個のWi-Fiルーターが設置されています。i 番目のルーター (1 \leq i \leq N) は座標 X_i に設置されていて、そこから距離 R_i 以内(距離がちょうど R_i の地点を含む)の範囲に電波を届けています。すなわち、i 番目のルーターがカバーする範囲は閉区間 [X_i - R_i, X_i + R_i] です。なお、複数のルーターが同じ座標に設置されていることもあります。
高橋君はスマートフォンでオンライン通話をしながら移動しているため、廊下上のすべての地点でWi-Fiに接続されている必要があります。
閉区間 [0, L] のすべての座標が、N 個のルーターのうち少なくとも1つのカバー範囲に含まれているかどうかを判定してください。ルーターのカバー範囲が閉区間 [0, L] の外側にはみ出している場合、そのはみ出した部分は判定に影響しません。閉区間 [0, L] 内の部分が適切にカバーされていれば十分です。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L \leq 10^9
- 0 \leq X_i \leq L
- 1 \leq R_i \leq 10^9
- 入力はすべて整数である
入力
N L X_1 R_1 X_2 R_2 \vdots X_N R_N
- 1 行目には、ルーターの個数を表す整数 N と、ゴール地点の座標を表す整数 L が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各ルーターの情報が与えられる。
- 1 + i 行目では、i 番目のルーターの座標を表す整数 X_i と、電波が届く範囲の半径を表す整数 R_i が、スペース区切りで与えられる。
出力
閉区間 [0, L] のすべての座標がいずれかのルーターのカバー範囲に含まれている場合は Yes を、そうでない場合は No を出力してください。
入力例 1
3 10 2 3 5 2 8 3
出力例 1
Yes
入力例 2
2 20 5 5 18 3
出力例 2
No
入力例 3
5 1000000000 100000000 150000000 300000000 100000000 500000000 150000000 700000000 150000000 900000000 150000000
出力例 3
Yes
Score : 366 pts
Problem Statement
Takahashi is trying to walk along a long corridor.
This corridor is represented as the closed interval [0, L] on a number line. Takahashi walks straight through the corridor from the start point at coordinate 0 to the goal point at coordinate L.
There are N Wi-Fi routers installed in the corridor. The i-th router (1 \leq i \leq N) is installed at coordinate X_i and transmits a signal to all points within distance R_i from it (including points at exactly distance R_i). That is, the coverage area of the i-th router is the closed interval [X_i - R_i, X_i + R_i]. Note that multiple routers may be installed at the same coordinate.
Since Takahashi is making an online call on his smartphone while moving, he needs to be connected to Wi-Fi at every point along the corridor.
Determine whether every coordinate in the closed interval [0, L] is covered by at least one of the N routers. If a router's coverage area extends beyond the closed interval [0, L], the portion outside the interval does not affect the determination. It is sufficient that the portion within the closed interval [0, L] is properly covered.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L \leq 10^9
- 0 \leq X_i \leq L
- 1 \leq R_i \leq 10^9
- All input values are integers
Input
N L X_1 R_1 X_2 R_2 \vdots X_N R_N
- The first line contains an integer N representing the number of routers and an integer L representing the coordinate of the goal point, separated by a space.
- From the 2nd line to the (N + 1)-th line, the information of each router is given.
- The (1 + i)-th line contains an integer X_i representing the coordinate of the i-th router and an integer R_i representing the radius of its signal range, separated by a space.
Output
If every coordinate in the closed interval [0, L] is covered by at least one router's coverage area, print Yes; otherwise, print No.
Sample Input 1
3 10 2 3 5 2 8 3
Sample Output 1
Yes
Sample Input 2
2 20 5 5 18 3
Sample Output 2
No
Sample Input 3
5 1000000000 100000000 150000000 300000000 100000000 500000000 150000000 700000000 150000000 900000000 150000000
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は図書館の司書として、N 冊の本を本棚に配架する作業を担当することになりました。
本棚には M 個の空きスペースが一列に並んでおり、左から順にスペース 1、スペース 2、\ldots、スペース M と番号が振られています。スペース j には「耐荷重」を表す整数値 C_j が設定されており、各スペースには最大 1 冊の本しか置くことができません。
本には 1 から N までの番号が付けられており、本 i には「重さ」を表す整数値 W_i が与えられています。
高橋君は、本 1、本 2、\ldots、本 N の順に、1 冊ずつ以下の手順で配架場所を決めていきます。
- まだ本が置かれていないスペースのうち、耐荷重がその本の重さ以上(すなわち C_j \geq W_i)であるスペースを候補とする。
- 候補が 1 つ以上存在する場合、その中で最も番号が小さいスペースにその本を置く。本を置いたスペースは使用済みとなり、以降の本の配架先としては選べなくなる。
- 候補が存在しない場合、その本の配架を断念する。断念した本は本棚に置かれず、以降再検討されることもない。
すべての本について配架場所を検討し終わった後、本棚に置くことができた本の冊数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数
入力
N M W_1 W_2 \ldots W_N C_1 C_2 \ldots C_M
- 1 行目には、本の冊数を表す N と、本棚のスペース数を表す M が、スペース区切りで与えられる。
- 2 行目には、各本の重さ W_1, W_2, \ldots, W_N が、スペース区切りで与えられる。
- 3 行目には、各スペースの耐荷重 C_1, C_2, \ldots, C_M が、スペース区切りで与えられる。
出力
本棚に置くことができた本の冊数を 1 行で出力してください。
入力例 1
4 5 3 5 2 7 4 6 3 8 2
出力例 1
4
入力例 2
6 5 5 3 8 2 10 1 7 4 9 3 6
出力例 2
5
入力例 3
10 8 5 1 9 3 7 6 2 4 8 10 6 3 10 5 8 2 7 4
出力例 3
8
Score : 433 pts
Problem Statement
Takahashi, working as a librarian, has been assigned the task of shelving N books onto a bookshelf.
The bookshelf has M empty spaces arranged in a row, numbered from left to right as space 1, space 2, \ldots, space M. Each space j has an integer value C_j representing its "weight capacity," and each space can hold at most 1 book.
The books are numbered from 1 to N, and book i has an integer value W_i representing its "weight."
Takahashi determines the placement for each book one at a time, in the order book 1, book 2, \ldots, book N, using the following procedure:
- Among the spaces that do not yet have a book placed on them, consider as candidates those whose weight capacity is at least the weight of the book (i.e., C_j \geq W_i).
- If there is at least one candidate, place the book on the candidate space with the smallest number. The space where a book is placed becomes occupied and can no longer be selected for subsequent books.
- If there are no candidates, give up on shelving that book. A book that is given up on is not placed on the bookshelf and is never reconsidered.
After considering the placement for all books, determine the number of books that were successfully placed on the bookshelf.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_j \leq 10^9 (1 \leq j \leq M)
- All inputs are integers
Input
N M W_1 W_2 \ldots W_N C_1 C_2 \ldots C_M
- The first line contains N, the number of books, and M, the number of spaces on the bookshelf, separated by a space.
- The second line contains the weights of each book W_1, W_2, \ldots, W_N, separated by spaces.
- The third line contains the weight capacities of each space C_1, C_2, \ldots, C_M, separated by spaces.
Output
Print the number of books that were successfully placed on the bookshelf, on a single line.
Sample Input 1
4 5 3 5 2 7 4 6 3 8 2
Sample Output 1
4
Sample Input 2
6 5 5 3 8 2 10 1 7 4 9 3 6
Sample Output 2
5
Sample Input 3
10 8 5 1 9 3 7 6 2 4 8 10 6 3 10 5 8 2 7 4
Sample Output 3
8