Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君はウェブサービスの運用担当者です。彼の管理するシステムには N 台のサーバーがあり、明日から M 日間にわたる大規模キャンペーンが始まります。
キャンペーン期間中は、毎日ちょうど 1 件の重い処理リクエストが発生します。高橋君は M 日間の各日について、そのリクエストを N 台のサーバーのうちいずれか 1 台に必ず割り当てなければなりません。同じサーバーを複数の日に繰り返し選ぶこともできます。
各サーバー i( 1 \leq i \leq N )には「処理可能回数」 A_i が設定されています。キャンペーン期間全体を通じて、サーバー i に割り当てるリクエストの合計回数が A_i 回以下であれば、そのサーバーは正常に動作します。一方、A_i 回を超えるリクエストを割り当てると、そのサーバーはダウンしてしまいます。高橋君はどのサーバーもダウンさせてはなりません。
高橋君がうまくリクエストの割り当て先を選ぶことで、どのサーバーもダウンさせずに M 日間のキャンペーンを乗り切ることができるでしょうか?
乗り切ることが可能な割り当て方が存在するなら Yes を、存在しないなら No を出力してください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^9
- 1 \leq A_i \leq 10^9( 1 \leq i \leq N )
- 入力はすべて整数
入力
N M A_1 A_2 \ldots A_N
- 1 行目には、サーバーの台数を表す整数 N と、キャンペーンの日数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各サーバーの処理可能回数を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
どのサーバーもダウンさせずに M 日間のキャンペーンを乗り切ることが可能な割り当て方が存在するなら Yes を、存在しないなら No を 1 行で出力せよ。
入力例 1
3 10 3 4 2
出力例 1
No
入力例 2
5 100 10 20 30 15 25
出力例 2
Yes
入力例 3
4 1000000000 250000000 250000000 250000000 250000000
出力例 3
Yes
Score : 200 pts
Problem Statement
Takahashi is a web service operations manager. His system has N servers, and a large-scale campaign spanning M days starts tomorrow.
During the campaign period, exactly 1 heavy processing request occurs each day. For each of the M days, Takahashi must assign that request to exactly 1 of the N servers. The same server may be chosen on multiple days.
Each server i (1 \leq i \leq N) has a "processing capacity" A_i. If the total number of requests assigned to server i throughout the entire campaign period is at most A_i, that server operates normally. On the other hand, if more than A_i requests are assigned to it, that server will go down. Takahashi must not let any server go down.
Can Takahashi choose request assignments wisely enough to get through the M-day campaign without any server going down?
If there exists an assignment that allows all M days to be completed without any server going down, output Yes; otherwise, output No.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^9
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All inputs are integers
Input
N M A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of servers and an integer M representing the number of campaign days, separated by a space.
- The second line contains N integers A_1, A_2, \ldots, A_N representing the processing capacity of each server, separated by spaces.
Output
If there exists an assignment that allows all M days of the campaign to be completed without any server going down, output Yes; otherwise, output No on a single line.
Sample Input 1
3 10 3 4 2
Sample Output 1
No
Sample Input 2
5 100 10 20 30 15 25
Sample Output 2
Yes
Sample Input 3
4 1000000000 250000000 250000000 250000000 250000000
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君はピアニストを目指しており、今日は N 曲の練習曲を 1 番目から N 番目まで順番に演奏することにしました。
i 番目の曲には整数の難易度 D_i が設定されています。各曲の演奏にかかる時間は以下のルールで決まります。
- 1 番目の曲の演奏には D_1 分かかります。
- 2 番目以降の曲(2 \leq i \leq N)については、直前に演奏した曲との難易度の関係によって演奏時間が変わります。
- 直前の曲より難易度が真に大きい場合(D_{i-1} < D_i)、前の曲の演奏でウォーミングアップができているため、かかる時間は \lfloor D_i / 2 \rfloor 分に短縮されます。ここで \lfloor x \rfloor は x を超えない最大の整数を表します(すなわち、D_i を 2 で割って小数点以下を切り捨てた値です)。
- 直前の曲の難易度以下である場合(D_{i-1} \geq D_i)、短縮は適用されず D_i 分かかります。
高橋君がすべての曲を 1 番目から N 番目まで順番に演奏するとき、合計でかかる時間は何分でしょうか。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq D_i \leq 10^9
- 入力はすべて整数
入力
N D_1 D_2 \ldots D_N
- 1 行目には、曲の数を表す整数 N が与えられる。
- 2 行目には、各曲の難易度を表す N 個の整数 D_1, D_2, \ldots, D_N がスペース区切りで与えられる。
出力
高橋君がすべての曲を演奏するのにかかる合計時間(分)を 1 行に出力せよ。
入力例 1
5 3 5 4 8 10
出力例 1
18
入力例 2
7 1 2 3 4 5 6 7
出力例 2
13
入力例 3
10 100 50 200 200 300 150 400 500 600 100
出力例 3
1600
Score : 233 pts
Problem Statement
Takahashi aspires to become a pianist, and today he has decided to play N practice pieces in order from the 1-st to the N-th.
The i-th piece has an integer difficulty D_i. The time required to perform each piece is determined by the following rules:
- The 1-st piece takes D_1 minutes to perform.
- For the 2-nd piece onward (2 \leq i \leq N), the performance time depends on the relationship between the difficulty of the current piece and the immediately preceding piece.
- If the difficulty is strictly greater than the previous piece (D_{i-1} < D_i), then the previous piece served as a warm-up, so the time is reduced to \lfloor D_i / 2 \rfloor minutes. Here, \lfloor x \rfloor denotes the largest integer not exceeding x (that is, the value obtained by dividing D_i by 2 and rounding down).
- If the difficulty is less than or equal to the previous piece (D_{i-1} \geq D_i), no reduction is applied and the piece takes D_i minutes.
When Takahashi performs all pieces in order from the 1-st to the N-th, how many minutes does it take in total?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq D_i \leq 10^9
- All inputs are integers.
Input
N D_1 D_2 \ldots D_N
- The first line contains an integer N representing the number of pieces.
- The second line contains N integers D_1, D_2, \ldots, D_N separated by spaces, representing the difficulty of each piece.
Output
Print in one line the total time (in minutes) it takes for Takahashi to perform all the pieces.
Sample Input 1
5 3 5 4 8 10
Sample Output 1
18
Sample Input 2
7 1 2 3 4 5 6 7
Sample Output 2
13
Sample Input 3
10 100 50 200 200 300 150 400 500 600 100
Sample Output 3
1600
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は山岳ガイドです。ある山岳地帯に N 個の山小屋が一列に並んでおり、左から i 番目の山小屋(以下、山小屋 i と呼びます)の標高は A_i メートルです。
隣接する山小屋の間には、標高差が小さい場合に限り連絡路が整備されています。具体的には、山小屋 i と山小屋 i+1 について、標高の差の絶対値が K 以下であるとき(すなわち |A_i - A_{i+1}| \leq K であるとき)、それらの山小屋の間には連絡路があり、直接行き来することができます。標高の差の絶対値が K より大きい場合、連絡路はなく、直接行き来することはできません。なお、隣接していない山小屋の間には連絡路はありません。
連絡路でつながった山小屋を順にたどることで互いに行き来できる山小屋の極大な集合を エリア と呼びます。連絡路は隣接する山小屋の間にしか存在しないため、各エリアに属する山小屋の番号は必ず連続した区間をなします。形式的には、エリアとは山小屋の番号の連続区間 [l, r](1 \leq l \leq r \leq N)であって、以下の 2 つの条件をともに満たすもののことです。
- 区間内のすべての隣接する山小屋間に連絡路がある。すなわち、l \leq i \leq r-1 なるすべての整数 i について |A_i - A_{i+1}| \leq K が成り立つ。(l = r の場合、この条件は自動的に満たされます。)
- 区間は極大である。すなわち、l \geq 2 ならば |A_{l-1} - A_l| > K であり、r \leq N-1 ならば |A_r - A_{r+1}| > K である。
N 個の山小屋はそれぞれちょうど 1 つのエリアに属します。
高橋君は Q 個の質問に答えたいです。j 番目の質問では、山小屋 L_j と山小屋 R_j が同じエリアに属するかどうかを判定してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j < R_j \leq N (1 \leq j \leq Q)
- 入力はすべて整数である。
入力
N K Q A_1 A_2 \ldots A_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- 1 行目には、山小屋の数を表す整数 N、連絡路が整備される標高差の上限を表す整数 K(標高差が K 以下のとき連絡路がある)、質問の数を表す整数 Q が、スペース区切りで与えられる。
- 2 行目には、左から順に各山小屋の標高を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 続く Q 行にわたって、各質問が与えられる。
- そのうち j 行目(入力全体の 2 + j 行目)では、j 番目の質問で対象となる山小屋の番号 L_j と R_j がスペース区切りで与えられる。
出力
Q 行出力せよ。j 行目には、j 番目の質問について、山小屋 L_j と山小屋 R_j が同じエリアに属するならば Yes を、そうでなければ No を出力せよ。
入力例 1
6 3 3 10 13 15 25 27 26 1 3 2 5 4 6
出力例 1
Yes No Yes
入力例 2
9 5 5 100 102 110 112 111 105 103 200 198 1 2 1 3 3 5 6 9 8 9
出力例 2
Yes No Yes No Yes
入力例 3
12 4 6 50 52 54 56 100 98 96 94 92 90 50 52 1 4 1 5 5 10 10 11 11 12 3 12
出力例 3
Yes No Yes No Yes No
Score : 333 pts
Problem Statement
Takahashi is a mountain guide. In a certain mountainous region, N mountain huts are lined up in a row. The i-th mountain hut from the left (hereafter called hut i) is at an elevation of A_i meters.
Between adjacent mountain huts, trails are maintained only when the elevation difference is small. Specifically, for hut i and hut i+1, if the absolute difference in elevation is at most K (that is, |A_i - A_{i+1}| \leq K), there is a trail between them and one can travel directly between them. If the absolute difference in elevation is greater than K, there is no trail and one cannot travel directly between them. There are no trails between non-adjacent mountain huts.
A maximal set of mountain huts that can be reached from one another by following a sequence of trails is called an area. Since trails only exist between adjacent huts, the hut numbers belonging to each area always form a contiguous interval. Formally, an area is a contiguous interval [l, r] (1 \leq l \leq r \leq N) of hut numbers that satisfies both of the following conditions:
- There is a trail between all adjacent huts within the interval. That is, for all integers i such that l \leq i \leq r-1, |A_i - A_{i+1}| \leq K holds. (When l = r, this condition is automatically satisfied.)
- The interval is maximal. That is, if l \geq 2 then |A_{l-1} - A_l| > K, and if r \leq N-1 then |A_r - A_{r+1}| > K.
Each of the N mountain huts belongs to exactly one area.
Takahashi wants to answer Q questions. For the j-th question, determine whether hut L_j and hut R_j belong to the same area.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j < R_j \leq N (1 \leq j \leq Q)
- All input values are integers.
Input
N K Q A_1 A_2 \ldots A_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- The first line contains three space-separated integers: N representing the number of mountain huts, K representing the maximum elevation difference for a trail to exist (a trail exists when the elevation difference is at most K), and Q representing the number of questions.
- The second line contains space-separated integers A_1, A_2, \ldots, A_N representing the elevations of the mountain huts from left to right.
- The following Q lines each contain a question.
- The j-th of these lines (the (2 + j)-th line of the entire input) contains space-separated hut numbers L_j and R_j for the j-th question.
Output
Output Q lines. On the j-th line, print Yes if hut L_j and hut R_j belong to the same area for the j-th question, and No otherwise.
Sample Input 1
6 3 3 10 13 15 25 27 26 1 3 2 5 4 6
Sample Output 1
Yes No Yes
Sample Input 2
9 5 5 100 102 110 112 111 105 103 200 198 1 2 1 3 3 5 6 9 8 9
Sample Output 2
Yes No Yes No Yes
Sample Input 3
12 4 6 50 52 54 56 100 98 96 94 92 90 50 52 1 4 1 5 5 10 10 11 11 12 3 12
Sample Output 3
Yes No Yes No Yes No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は採掘場で働いています。採掘場には N 個の岩が一列に並んでおり、これらをすべて破壊する必要があります。
i 番目の岩は硬度 H_i を持っています。高橋君は 1 番目の岩から順に N 番目の岩まで、1つずつ処理していきます。ある岩を破壊しない限り、次の岩の処理に移ることはできません。
高橋君は毎ターン、以下の2つの行動のうちいずれか1つを選んで実行します。
- ハンマーで叩く: 現在処理中の岩の硬度を 1 減らす。このとき 1 ターンを消費する。
- ダイナマイトを使う: 現在処理中の岩の硬度を即座に 0 にする。このとき 1 ターンを消費する。ただし、ダイナマイトは合計で K 個しか持っておらず、使用するたびに 1 個消費される。残りが 0 個のときはこの行動を選ぶことはできない。
岩の硬度が 0 になると、その岩は破壊され、次の岩の処理に移ります。すべての岩を破壊すると作業完了です。
高橋君が最適に行動したとき、すべての岩を破壊するのに必要な最小ターン数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq H_i \leq 10^9
- 入力はすべて整数
入力
N K H_1 H_2 \ldots H_N
- 1 行目には、岩の個数を表す整数 N と、ダイナマイトの個数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各岩の硬度を表す整数 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
出力
すべての岩を破壊するために必要な最小ターン数を 1 行で出力してください。
入力例 1
3 1 5 2 8
出力例 1
8
入力例 2
5 2 10 3 7 4 6
出力例 2
15
入力例 3
8 3 100 5 200 8 150 3 50 300
出力例 3
169
Score : 366 pts
Problem Statement
Takahashi works at a quarry. There are N rocks lined up in a row at the quarry, and he needs to destroy all of them.
The i-th rock has hardness H_i. Takahashi processes the rocks one by one in order from the 1-st rock to the N-th rock. He cannot move on to processing the next rock until the current rock is destroyed.
Each turn, Takahashi chooses and performs exactly one of the following two actions:
- Strike with a hammer: Reduce the hardness of the rock currently being processed by 1. This consumes 1 turn.
- Use dynamite: Instantly reduce the hardness of the rock currently being processed to 0. This consumes 1 turn. However, Takahashi only has K sticks of dynamite in total, and each use consumes 1 stick. This action cannot be chosen when he has 0 sticks remaining.
When a rock's hardness becomes 0, that rock is destroyed, and he moves on to processing the next rock. The work is complete once all rocks are destroyed.
Find the minimum number of turns required to destroy all rocks when Takahashi acts optimally.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq H_i \leq 10^9
- All inputs are integers
Input
N K H_1 H_2 \ldots H_N
- The first line contains an integer N representing the number of rocks and an integer K representing the number of dynamite sticks, separated by a space.
- The second line contains integers H_1, H_2, \ldots, H_N representing the hardness of each rock, separated by spaces.
Output
Print the minimum number of turns required to destroy all rocks in a single line.
Sample Input 1
3 1 5 2 8
Sample Output 1
8
Sample Input 2
5 2 10 3 7 4 6
Sample Output 2
15
Sample Input 3
8 3 100 5 200 8 150 3 50 300
Sample Output 3
169
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は N 社が出展する展示会の運営を担当しています。会場には円形に配置された N 個のブースがあり、時計回りに 1 から N まで番号が付けられています。円形配置のため、ブース k とブース k+1( 1 \leq k \leq N-1 )が隣接しているほか、ブース N とブース 1 も隣接しています。したがって、隣接するブースのペアはちょうど N 組あります。
N 社の企業にも 1 から N まで番号が付けられており、最初、企業 i はブース i に配置されています( 1 \leq i \leq N )。各ブースにはちょうど 1 社が入り、各企業はちょうど 1 つのブースを使用します。
展示会では、隣接するブースのペアそれぞれについて、そこに配置された 2 社がコラボレーション企画を行います。企業 i と企業 j がコラボレーションした場合の集客効果は C_{i,j} で表されます( C_{i,j} = C_{j,i} )。
高橋君は、異なる 2 社の企業を選び、それぞれが現在使用しているブースを互いに交換する操作を、0 回以上 K 回以下行うことができます。各操作で選ぶ 2 社に制限はなく、ある企業が複数回の操作で選ばれることや、同じ企業のペアが複数回選ばれることも許されます。
すべての操作を終えた後の配置を考えます。操作後にブース k に配置されている企業を p_k とすると、(p_1, p_2, \ldots, p_N) は (1, 2, \ldots, N) の順列になります。このとき、隣接する N 組のブースのペアの集客効果の合計は
\sum_{k=1}^{N-1} C_{p_k, p_{k+1}} + C_{p_N, p_1}
です。操作の仕方を最適に選んだとき、この合計の最大値を求めてください。
制約
- 3 \leq N \leq 8
- 0 \leq K \leq \frac{N(N-1)}{2}
- 0 \leq C_{i,j} \leq 10^6
- C_{i,j} = C_{j,i}
- C_{i,i} = 0
- 入力はすべて整数
入力
N K
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
\vdots
C_{N,1} C_{N,2} \ldots C_{N,N}
- 1 行目には、企業およびブースの数を表す整数 N と、操作の最大回数を表す整数 K が、スペース区切りで与えられる。
- 続く N 行には、集客効果を表す N \times N の行列 C が与えられる。このうち i 行目( 1 \leq i \leq N )には、企業 i と各企業との集客効果 C_{i,1}, C_{i,2}, \ldots, C_{i,N} がスペース区切りで与えられる。
- C_{i,j} = C_{j,i} および C_{i,i} = 0 が保証される。
出力
0 回以上 K 回以下の操作を行った後に達成できる、隣接するブースの全ペアの集客効果の合計の最大値を 1 行で出力せよ。
入力例 1
3 0 0 10 30 10 0 20 30 20 0
出力例 1
60
入力例 2
4 1 0 5 50 100 5 0 5 100 50 5 0 50 100 100 50 0
出力例 2
255
入力例 3
6 3 0 10 80 50 30 100 10 0 90 20 70 40 80 90 0 60 10 50 50 20 60 0 100 30 30 70 10 100 0 80 100 40 50 30 80 0
出力例 3
470
Score : 433 pts
Problem Statement
Takahashi is in charge of organizing an exhibition where N companies are exhibiting. The venue has N booths arranged in a circle, numbered 1 to N in clockwise order. Due to the circular arrangement, booth k and booth k+1 (1 \leq k \leq N-1) are adjacent, and booth N and booth 1 are also adjacent. Therefore, there are exactly N pairs of adjacent booths.
The N companies are also numbered 1 to N, and initially, company i is placed in booth i (1 \leq i \leq N). Each booth contains exactly one company, and each company uses exactly one booth.
At the exhibition, for each pair of adjacent booths, the two companies placed there will hold a collaboration event. The audience-drawing effect when company i and company j collaborate is represented by C_{i,j} (C_{i,j} = C_{j,i}).
Takahashi can perform the following operation 0 or more times, up to K times: choose two distinct companies and swap the booths they are currently using. There are no restrictions on which two companies are chosen for each operation; a company may be chosen in multiple operations, and the same pair of companies may be chosen multiple times.
Consider the arrangement after all operations are completed. Let p_k denote the company placed in booth k after the operations. Then (p_1, p_2, \ldots, p_N) is a permutation of (1, 2, \ldots, N). The total audience-drawing effect over all N pairs of adjacent booths is
$\sum_{k=1}^{N-1} C_{p_k, p_{k+1}} + C_{p_N, p_1}$
Find the maximum value of this total when the operations are chosen optimally.
Constraints
- 3 \leq N \leq 8
- 0 \leq K \leq \frac{N(N-1)}{2}
- 0 \leq C_{i,j} \leq 10^6
- C_{i,j} = C_{j,i}
- C_{i,i} = 0
- All inputs are integers
Input
N K
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
\vdots
C_{N,1} C_{N,2} \ldots C_{N,N}
- The first line contains two space-separated integers: N, the number of companies and booths, and K, the maximum number of operations.
- The following N lines contain the N \times N matrix C representing the audience-drawing effects. The i-th of these lines (1 \leq i \leq N) contains the space-separated values C_{i,1}, C_{i,2}, \ldots, C_{i,N}, representing the audience-drawing effects between company i and each other company.
- It is guaranteed that C_{i,j} = C_{j,i} and C_{i,i} = 0.
Output
Print on a single line the maximum total audience-drawing effect over all pairs of adjacent booths that can be achieved after performing 0 or more and at most K operations.
Sample Input 1
3 0 0 10 30 10 0 20 30 20 0
Sample Output 1
60
Sample Input 2
4 1 0 5 50 100 5 0 5 100 50 5 0 50 100 100 50 0
Sample Output 2
255
Sample Input 3
6 3 0 10 80 50 30 100 10 0 90 20 70 40 80 90 0 60 10 50 50 20 60 0 100 30 30 70 10 100 0 80 100 40 50 30 80 0
Sample Output 3
470