Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は横一列に N 個のマスが並んだ布を、縞模様に染めます。
左から順に、最初の K マスを赤、次の K マスを青、次の K マスを赤、…と、 K マスごとに赤と青を交互に染めていきます。 N が K の倍数でない場合、最後の縞は K マスより短くなります。
染め終わった布の様子を、左から順に、赤いマスを R 、青いマスを B で表した長さ N の文字列として出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq K \leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K
- 1 行目には、マスの数 N と縞の幅 K がスペース区切りで与えられる。
出力
染め終わった布の様子を表す長さ N の文字列を 1 行で出力せよ。
入力例 1
6 2
出力例 1
RRBBRR
入力例 2
6 3
出力例 2
RRRBBB
入力例 3
4 1
出力例 3
RBRB
入力例 4
99 1
出力例 4
RBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBR
入力例 5
6 1
出力例 5
RBRBRB
Score : 200 pts
Problem Statement
Takahashi is dyeing a cloth consisting of N cells arranged in a horizontal row in a striped pattern.
From left to right, he paints the first K cells red, the next K cells blue, the next K cells red, and so on, alternating between red and blue every K cells. If N is not a multiple of K, the last stripe will be shorter than K cells.
Print the resulting cloth as a string of length N, from left to right, using R for red cells and B for blue cells.
Constraints
- 1 \leq N \leq 100
- 1 \leq K \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K
- The first line contains the number of cells N and the stripe width K, separated by a space.
Output
Print one line containing the string of length N representing the dyed cloth.
Sample Input 1
6 2
Sample Output 1
RRBBRR
Sample Input 2
6 3
Sample Output 2
RRRBBB
Sample Input 3
4 1
Sample Output 3
RBRB
Sample Input 4
99 1
Sample Output 4
RBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBR
Sample Input 5
6 1
Sample Output 5
RBRBRB
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君はある商品の相場を N 日間記録しました。 i 日目の価格は A_i 円でした。
2 日目から N 日目までの各日について、前日と比べて価格が上がった日を「上昇日」、下がった日を「下落日」、同じだった日を「横ばい日」と呼びます。
上昇日、下落日、横ばい日がそれぞれ何日あったかを求めてください。
制約
- 2 \leq N \leq 100
- 1 \leq A_i \leq 10000
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
- 1 行目には、記録した日数 N が与えられる。
- 2 行目には、各日の価格 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
上昇日の数、下落日の数、横ばい日の数を、この順にスペース区切りで 1 行に出力せよ。
入力例 1
3 100 120 90
出力例 1
1 1 0
入力例 2
4 10 20 30 40
出力例 2
3 0 0
入力例 3
3 50 40 30
出力例 3
0 2 0
入力例 4
3 1301 2362 8372
出力例 4
2 0 0
入力例 5
2 2 5
出力例 5
1 0 0
Score : 233 pts
Problem Statement
Takahashi recorded the market price of a certain product for N days. The price on the i-th day was A_i yen.
For each day from day 2 to day N, we call a day an *up day* if the price increased compared to the previous day, a *down day* if it decreased, and a *flat day* if it stayed the same.
Find the number of up days, down days, and flat days.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 10000
- All input values are integers.
Input
The input is given from standard input in the following format:
N A_1 A_2 \ldots A_N
- The first line contains the number of recorded days N.
- The second line contains the prices A_1, A_2, \ldots, A_N for each day, separated by spaces.
Output
Print the number of up days, the number of down days, and the number of flat days in this order, separated by spaces, on a single line.
Sample Input 1
3 100 120 90
Sample Output 1
1 1 0
Sample Input 2
4 10 20 30 40
Sample Output 2
3 0 0
Sample Input 3
3 50 40 30
Sample Output 3
0 2 0
Sample Input 4
3 1301 2362 8372
Sample Output 4
2 0 0
Sample Input 5
2 2 5
Sample Output 5
1 0 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は H 行 W 列のグリッド上に並んだマスからなる街を散策しています。上から i 行目、左から j 列目のマスを (i, j) と表します。行番号は北から南へ、列番号は西から東へ大きくなります。
各マスには道案内の標識が立てられており、南向きの矢印 v または東向きの矢印 > のいずれかが表示されています。i 行目の各マスの標識は文字列 S_i で表され、S_i の j 文字目がマス (i, j) の標識に対応します。
高橋君は北西の角にあるマス (1, 1) からスタートし、以下のルールに従って繰り返し移動します。
- 現在マス (i, j) にいるとき、そのマスの標識が
v(南向き)ならば、マス (i+1, j) へ移動しようとします。 - 現在マス (i, j) にいるとき、そのマスの標識が
>(東向き)ならば、マス (i, j+1) へ移動しようとします。
移動先がグリッドの外である場合、高橋君はその移動を行わず、現在いるマスで停止します。具体的には、現在マス (i, j) にいるとき、南へ移動しようとして i + 1 > H となる場合、または東へ移動しようとして j + 1 > W となる場合に、高橋君は停止します。
移動は常に南または東への 1 マスのみであるため、高橋君は (1, 1) から南方向に最大 H - 1 回、東方向に最大 W - 1 回しか移動できず、スタートから H + W - 2 回以内の移動で必ず停止します。
高橋君が停止したマスの座標を求めてください。
制約
- 1 \leq H \leq 500
- 1 \leq W \leq 500
- H, W は整数
- S_i は
vおよび>のみからなる長さ W の文字列である
入力
H W S_1 S_2 \vdots S_H
- 1 行目には、グリッドの行数を表す整数 H と列数を表す整数 W が、スペース区切りで与えられる。
- 続く H 行にわたって、グリッドの各行の情報が文字列として与えられる。i 行目 (1 \leq i \leq H) の情報は入力の i + 1 行目に与えられ、長さ W の文字列 S_i である。S_i の j 文字目は、マス (i, j) に表示された標識を表し、
v(南向き)または>(東向き)のいずれかである。
出力
高橋君が停止したマスの行番号と列番号を、スペース区切りで 1 行で出力せよ。すなわち、停止したマスが (r, c) であるとき、r と c をスペース区切りで出力せよ。
入力例 1
3 3 v>v vv> >>>
出力例 1
3 3
入力例 2
3 4 >>>v vv>v >v>>
出力例 2
3 4
入力例 3
5 6 v>>v>> >v>>v> v>v>>v >>vv>> vv>>v>
出力例 3
5 5
入力例 4
8 10 >>v>>>v>>> v>v>>v>v>> >>v>v>>>v> v>>>v>v>>v >v>>v>>>v> v>v>>>v>>v >>v>v>>v>> v>>>v>>>>>
出力例 4
8 10
入力例 5
1 1 v
出力例 5
1 1
Score : 266 pts
Problem Statement
Takahashi is exploring a town consisting of cells arranged in a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is denoted as (i, j). Row numbers increase from north to south, and column numbers increase from west to east.
Each cell has a directional signpost displaying either a south-pointing arrow v or an east-pointing arrow >. The signposts for the cells in the i-th row are represented by the string S_i, where the j-th character of S_i corresponds to the signpost at cell (i, j).
Takahashi starts at cell (1, 1) in the northwest corner and repeatedly moves according to the following rules:
- If he is currently at cell (i, j) and the signpost on that cell is
v(south-pointing), he attempts to move to cell (i+1, j). - If he is currently at cell (i, j) and the signpost on that cell is
>(east-pointing), he attempts to move to cell (i, j+1).
If the destination is outside the grid, Takahashi does not make that move and stops at his current cell. Specifically, if he is at cell (i, j) and attempts to move south but i + 1 > H, or attempts to move east but j + 1 > W, Takahashi stops.
Since each move is always exactly one cell to the south or east, Takahashi can move at most H - 1 times southward and at most W - 1 times eastward from (1, 1), so he will always stop within H + W - 2 moves from the start.
Determine the coordinates of the cell where Takahashi stops.
Constraints
- 1 \leq H \leq 500
- 1 \leq W \leq 500
- H, W are integers
- S_i is a string of length W consisting only of
vand>
Input
H W S_1 S_2 \vdots S_H
- The first line contains an integer H representing the number of rows and an integer W representing the number of columns of the grid, separated by a space.
- The following H lines provide the information for each row of the grid as a string. The information for the i-th row (1 \leq i \leq H) is given on the (i + 1)-th line of the input as a string S_i of length W. The j-th character of S_i represents the signpost displayed at cell (i, j), which is either
v(south-pointing) or>(east-pointing).
Output
Output the row number and column number of the cell where Takahashi stops, separated by a space, on a single line. That is, if the cell where he stops is (r, c), output r and c separated by a space.
Sample Input 1
3 3 v>v vv> >>>
Sample Output 1
3 3
Sample Input 2
3 4 >>>v vv>v >v>>
Sample Output 2
3 4
Sample Input 3
5 6 v>>v>> >v>>v> v>v>>v >>vv>> vv>>v>
Sample Output 3
5 5
Sample Input 4
8 10 >>v>>>v>>> v>v>>v>v>> >>v>v>>>v> v>>>v>v>>v >v>>v>>>v> v>v>>>v>>v >>v>v>>v>> v>>>v>>>>>
Sample Output 4
8 10
Sample Input 5
1 1 v
Sample Output 5
1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君のクラスでは、N 人の生徒がテストを受けました。各生徒 i(1 \leq i \leq N)のテストの点数は A_i です。
先生は、他の生徒と比べて特に優秀な成績を収めた生徒を表彰したいと考えています。具体的には、正の整数 K が与えられ、ある生徒の点数が自分以外の生徒の点数の平均値の K 倍以上であるとき、その生徒を「突出している」と呼ぶことにします。
より正確には、S = A_1 + A_2 + \cdots + A_N としたとき、生徒 i が「突出している」とは、
A_i \times (N - 1) \geq K \times (S - A_i)
を満たすことを言います。
ここで S - A_i は生徒 i 以外の N - 1 人の点数の合計です。N \geq 2 のとき、この不等式の両辺を N - 1 で割ると「A_i が自分以外の平均点の K 倍以上」という条件と同値になります。N = 1 のときは、両辺がともに 0 となるため、唯一の生徒は点数や K の値によらず常にこの条件を満たします。
「突出している」生徒の人数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- 0 \leq A_i \leq 10^9(1 \leq i \leq N)
- 入力はすべて整数である。
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、生徒の人数を表す整数 N と、表彰の基準となる倍率を表す正の整数 K が、スペース区切りで与えられる。
- 2 行目には、各生徒の点数を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
「突出している」生徒の人数を 1 行で出力せよ。
入力例 1
5 2 10 2 3 1 4
出力例 1
1
入力例 2
4 3 5 5 5 5
出力例 2
0
入力例 3
10 5 100 3 2 1 4 2 3 1 2 2
出力例 3
1
入力例 4
20 2 50 50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 4
2
入力例 5
1 1000000000 0
出力例 5
1
Score : 300 pts
Problem Statement
In Takahashi's class, N students took a test. The test score of each student i (1 \leq i \leq N) is A_i.
The teacher wants to recognize students who achieved particularly excellent results compared to the other students. Specifically, given a positive integer K, a student is called "outstanding" if their score is at least K times the average score of all other students.
More precisely, let S = A_1 + A_2 + \cdots + A_N. Student i is "outstanding" if and only if:
A_i \times (N - 1) \geq K \times (S - A_i)
Here, S - A_i is the total score of the N - 1 students other than student i. When N \geq 2, dividing both sides of this inequality by N - 1 gives the equivalent condition "A_i is at least K times the average score of the other students." When N = 1, both sides become 0, so the sole student always satisfies this condition regardless of their score or the value of K.
Find the number of "outstanding" students.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 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 students and a positive integer K representing the multiplier criterion for recognition, separated by a space.
- The second line contains N integers A_1, A_2, \ldots, A_N representing each student's score, separated by spaces.
Output
Print the number of "outstanding" students in a single line.
Sample Input 1
5 2 10 2 3 1 4
Sample Output 1
1
Sample Input 2
4 3 5 5 5 5
Sample Output 2
0
Sample Input 3
10 5 100 3 2 1 4 2 3 1 2 2
Sample Output 3
1
Sample Input 4
20 2 50 50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 4
2
Sample Input 5
1 1000000000 0
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君はある会社の部長です。部署には N 人の社員が所属しており、i 番目 (1 \leq i \leq N) の社員にはスキルポイント S_i が定められています。
ある日、M 件のプロジェクトが同時に立ち上がりました。j 番目 (1 \leq j \leq M) のプロジェクトには、優先度 P_j と、遂行に必要な社員の人数 R_j が設定されています。高橋君は、すべてのプロジェクトに対して必要な人数ちょうどの社員を割り当てたいと考えています。ただし、各社員は高々 1 件のプロジェクトにしか割り当てることができず、どのプロジェクトにも割り当てられない社員がいてもかまいません。
社内の方針として、割り当ては以下のルールに従って順番に行います:
- まず、M 件のプロジェクトを優先度の値が大きい順に並べる。優先度の値が同じプロジェクトが複数ある場合は、番号(j の値)が小さい方を先にする。
- この順番に従い、先頭のプロジェクトから 1 件ずつ社員の割り当てを行う。
- あるプロジェクトに社員を割り当てる際、まだどのプロジェクトにも割り当てられていない社員の人数がそのプロジェクトの必要人数 R_j に満たない場合、割り当て全体が失敗となり、処理を中断する(既に割り当て済みのプロジェクトの結果も含め、すべて無効となる)。
- 人数が足りている場合は、まだどのプロジェクトにも割り当てられていない社員の中から、スキルポイントが高い順に R_j 人を選んで割り当てる。スキルポイントが等しい社員が複数いる場合は、そのうち誰を選んでもよい。
注記: スキルポイントが等しい社員の選び方によらず、すべてのプロジェクトに割り当てられるスキルポイントの合計値はそれぞれ一意に定まります。これは、同じスキルポイントを持つ社員のうち誰を現在のプロジェクトに選んでも、残りの社員の集合(をスキルポイントの多重集合として見たもの)が変わらないためです。
上記のルールに従って割り当てを行ったとき、すべてのプロジェクトに必要な人数の社員を割り当てることが可能かどうかを判定してください。可能な場合は、各プロジェクトに割り当てられた社員のスキルポイントの合計値をそれぞれ求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
- 1 \leq R_j \leq N (1 \leq j \leq M)
- 入力はすべて整数である。
注意: R_1 + R_2 + \cdots + R_M \leq N は保証されません。必要人数の合計が社員数を超える場合、割り当ては途中で失敗する可能性があります。
入力
N M S_1 S_2 \ldots S_N P_1 R_1 P_2 R_2 \vdots P_M R_M
- 1 行目には、社員の人数 N と、プロジェクトの件数 M が、スペース区切りで与えられる。
- 2 行目には、各社員のスキルポイント S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。S_i は i 番目の社員のスキルポイントである。
- 続く M 行にわたって、各プロジェクトの情報が与えられる。
- そのうち j 行目 (1 \leq j \leq M)、すなわち入力全体の第 (2 + j) 行目では、j 番目のプロジェクトの優先度 P_j と、遂行に必要な社員の人数 R_j が、スペース区切りで与えられる。
出力
上記のルールに従って割り当てを行った結果、割り当てが失敗する場合は、-1 を 1 行で出力せよ。
すべてのプロジェクトへの割り当てが成功する場合は、M 行出力せよ。j 行目 (1 \leq j \leq M) には、j 番目のプロジェクトに割り当てられた社員のスキルポイントの合計値を出力せよ。
入力例 1
5 2 10 20 30 40 50 5 2 10 2
出力例 1
50 90
入力例 2
3 2 1 2 3 1 2 2 2
出力例 2
-1
入力例 3
10 3 5 15 25 35 45 55 65 75 85 95 3 3 7 2 7 4
出力例 3
75 180 240
入力例 4
15 4 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1 2 5 3 5 4 10 3
出力例 4
900 3300 3000 4200
入力例 5
1 1 1000000000 1 1
出力例 5
1000000000
Score : 333 pts
Problem Statement
Takahashi is a department manager at a company. There are N employees in his department, and the i-th employee (1 \leq i \leq N) has a skill point value of S_i.
One day, M projects are launched simultaneously. The j-th project (1 \leq j \leq M) has a priority P_j and requires exactly R_j employees to carry it out. Takahashi wants to assign exactly the required number of employees to every project. However, each employee can be assigned to at most one project, and it is acceptable for some employees to not be assigned to any project.
According to company policy, assignments are made sequentially following these rules:
- First, sort the M projects in decreasing order of priority value. If multiple projects have the same priority value, the one with the smaller index (j value) comes first.
- Following this order, assign employees to one project at a time, starting from the first project.
- When assigning employees to a project, if the number of employees not yet assigned to any project is less than the required number R_j for that project, the entire assignment fails and processing is terminated (the results of all previously assigned projects are also invalidated).
- If there are enough employees, select the R_j employees with the highest skill points from those not yet assigned to any project. If multiple employees have equal skill points, any of them may be chosen.
Note: Regardless of how ties among employees with equal skill points are broken, the total skill points assigned to each project is uniquely determined. This is because no matter which employees with the same skill points are chosen for the current project, the remaining set of employees (viewed as a multiset of skill points) remains unchanged.
Following the rules above, determine whether it is possible to assign the required number of employees to all projects. If it is possible, find the total skill points of the employees assigned to each project.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
- 1 \leq R_j \leq N (1 \leq j \leq M)
- All input values are integers.
Note: It is NOT guaranteed that R_1 + R_2 + \cdots + R_M \leq N. If the total number of required employees exceeds the number of employees, the assignment may fail partway through.
Input
N M S_1 S_2 \ldots S_N P_1 R_1 P_2 R_2 \vdots P_M R_M
- The first line contains the number of employees N and the number of projects M, separated by a space.
- The second line contains the skill points S_1, S_2, \ldots, S_N of each employee, separated by spaces. S_i is the skill point of the i-th employee.
- The following M lines contain information about each project.
- The j-th of these lines (1 \leq j \leq M), which is the (2 + j)-th line of the entire input, contains the priority P_j and the required number of employees R_j for the j-th project, separated by a space.
Output
If the assignment fails following the rules above, output -1 on a single line.
If the assignment to all projects succeeds, output M lines. The j-th line (1 \leq j \leq M) should contain the total skill points of the employees assigned to the j-th project.
Sample Input 1
5 2 10 20 30 40 50 5 2 10 2
Sample Output 1
50 90
Sample Input 2
3 2 1 2 3 1 2 2 2
Sample Output 2
-1
Sample Input 3
10 3 5 15 25 35 45 55 65 75 85 95 3 3 7 2 7 4
Sample Output 3
75 180 240
Sample Input 4
15 4 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1 2 5 3 5 4 10 3
Sample Output 4
900 3300 3000 4200
Sample Input 5
1 1 1000000000 1 1
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君はゲームの中でダンジョンを探索しています。
高橋君の体力には最大値 M が定められており、ダンジョンの入り口(初期状態)での体力は M、所持コイン数は 0 枚です。
ダンジョンには 1 から N までの番号が付けられた N 個の部屋と、1 から N - 1 までの番号が付けられた N - 1 個の休憩所があります。
高橋君は、部屋 1 から始めて以下の順番で交互に進み、部屋 N の攻略を目指します。
\text{部屋 } 1 \to \text{休憩所 } 1 \to \text{部屋 } 2 \to \text{休憩所 } 2 \to \dots \to \text{休憩所 } N-1 \to \text{部屋 } N
各部屋および各休憩所でのイベントは以下の通りです。
- 部屋 i(1 \le i \le N)の攻略:
部屋 i を攻略すると、体力を A_i 消費します。攻略する直前の体力が A_i 未満である場合、その部屋を攻略できず探索は失敗となります(攻略直後の体力がちょうど 0 になることは許容されます)。
- 休憩所 i(1 \le i \le N - 1)での行動:
高橋君は、以下の 2 つの行動のうちどちらかちょうど一方を選択して行わなければなりません。
- 泉の水を飲む: 体力を B_i 回復する。ただし、回復後の体力が最大値 M を超える場合、体力は M になる。
- 宝箱を開ける: コインを C_i 枚獲得する。体力は変化しない。
高橋君が適切に行動を選択して部屋 N まで攻略を完了させたとき、最終的に獲得できるコインの総数の最大値を求めてください。
なお、適切に行動を選択することで、部屋 N まで攻略を完了できる方法が少なくとも 1 つ存在することが保証されます。
制約
- 2 \le N \le 3000
- 1 \le M \le 3000
- 1 \le A_i \le M(1 \le i \le N)
- 1 \le B_i \le M(1 \le i \le N - 1)
- 1 \le C_i \le 10^9(1 \le i \le N - 1)
- 適切に行動を選択することで、部屋 N まで攻略を完了できる方法が少なくとも 1 つ存在する。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1 A_2 \dots A_N
B_1 C_1
B_2 C_2
\vdots
B_{N-1} C_{N-1}
- 1 行目には、部屋の数を表す整数 N と、最大体力を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各部屋の攻略で消費する体力を表す N 個の整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
- 続く N - 1 行のうち i 行目(1 \le i \le N - 1)には、休憩所 i における体力の回復量を表す整数 B_i と、獲得できるコインの枚数を表す整数 C_i が、スペース区切りで与えられる。
出力
高橋君が獲得できるコインの総数の最大値を 1 行で出力せよ。
入力例 1
4 10 4 5 4 3 5 10 6 30 4 15
出力例 1
30
入力例 2
3 10 2 2 2 1 50 1 70
出力例 2
120
入力例 3
8 15 7 8 4 10 5 6 9 3 8 50 6 120 10 30 5 200 7 80 9 150 4 90
出力例 3
210
入力例 4
20 50 25 30 15 40 20 35 10 25 30 20 45 15 30 25 35 20 10 40 30 15 20 1000000000 35 850000000 15 500000000 40 999999999 30 750000000 25 300000000 45 900000000 20 600000000 35 450000000 50 1000000000 15 200000000 30 800000000 40 650000000 25 400000000 35 950000000 20 350000000 45 880000000 30 550000000 50 1000000000
出力例 4
650000000
入力例 5
2 1 1 1 1 1
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is exploring a dungeon in a game.
Takahashi has a maximum health of M. At the entrance of the dungeon (initial state), his health is M, and he has 0 coins.
The dungeon has N rooms numbered 1 to N, and N - 1 rest areas numbered 1 to N - 1.
Takahashi starts from Room 1 and alternates between rooms and rest areas in the following order, aiming to clear Room N:
\text{Room } 1 \to \text{Rest Area } 1 \to \text{Room } 2 \to \text{Rest Area } 2 \to \dots \to \text{Rest Area } N-1 \to \text{Room } N
The events at each room and rest area are as follows:
- Clearing Room i (1 \le i \le N):
Clearing Room i consumes A_i health. If his health immediately before clearing the room is strictly less than A_i, he cannot clear the room and the exploration fails (having exactly 0 health immediately after clearing is allowed).
- Action at Rest Area i (1 \le i \le N - 1):
Takahashi must choose and perform exactly one of the following two actions:
- Drink water from the spring: Restores B_i health. However, if the health after recovery exceeds the maximum value M, it becomes M.
- Open the treasure chest: Obtains C_i coins. Health does not change.
Find the maximum total number of coins Takahashi can ultimately obtain when he chooses his actions appropriately and successfully clears up to Room N.
It is guaranteed that there is at least one way to choose actions to clear up to Room N.
Constraints
- 2 \le N \le 3000
- 1 \le M \le 3000
- 1 \le A_i \le M (1 \le i \le N)
- 1 \le B_i \le M (1 \le i \le N - 1)
- 1 \le C_i \le 10^9 (1 \le i \le N - 1)
- There exists at least one way to choose actions to clear up to Room N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1 A_2 \dots A_N
B_1 C_1
B_2 C_2
\vdots
B_{N-1} C_{N-1}
- The first line contains two space-separated integers N and M, representing the number of rooms and the maximum health, respectively.
- The second line contains N space-separated integers A_1, A_2, \dots, A_N, representing the health consumed to clear each room.
- The i-th of the following N - 1 lines (1 \le i \le N - 1) contains two space-separated integers B_i and C_i, representing the amount of health restored and the number of coins obtained at Rest Area i, respectively.
Output
Print the maximum total number of coins Takahashi can obtain in a single line.
Sample Input 1
4 10 4 5 4 3 5 10 6 30 4 15
Sample Output 1
30
Sample Input 2
3 10 2 2 2 1 50 1 70
Sample Output 2
120
Sample Input 3
8 15 7 8 4 10 5 6 9 3 8 50 6 120 10 30 5 200 7 80 9 150 4 90
Sample Output 3
210
Sample Input 4
20 50 25 30 15 40 20 35 10 25 30 20 45 15 30 25 35 20 10 40 30 15 20 1000000000 35 850000000 15 500000000 40 999999999 30 750000000 25 300000000 45 900000000 20 600000000 35 450000000 50 1000000000 15 200000000 30 800000000 40 650000000 25 400000000 35 950000000 20 350000000 45 880000000 30 550000000 50 1000000000
Sample Output 4
650000000
Sample Input 5
2 1 1 1 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
AtCoder王国には、1 から N までの番号が付けられた N 個の街があります。
街 i (1 \le i \le N) には特産品があり、その価値は A_i です。
王国には 1 から M までの番号が付けられた M 本の道路があり、道路 j (1 \le j \le M) は街 U_j と街 V_j を双方向に結んでいます。
街 u から 0 本以上の道路を経由して街 v へ到達できるとき、街 u から街 v へ「移動可能」であるといいます(したがって、すべての街は自分自身へ移動可能です)。
各街 i (1 \le i \le N) について、街 i から移動可能な街(街 i 自身を含む)にある特産品の価値の最大値を求めてください。
制約
- 1 \le N \le 2 \times 10^5
- 0 \le M \le 2 \times 10^5
- N + M + 2 \le 2 \times 10^5(入力と出力の行数の合計は 2 \times 10^5 以下)
- 1 \le A_i \le 10^9 (1 \le i \le N)
- 1 \le U_j, V_j \le N (1 \le j \le M)
- 同じ 2 つの街を結ぶ道路が複数存在することや、街とそれ自身を結ぶ道路が存在することもある(多重辺や自己ループを含む可能性がある)。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
M = 0 の場合、道路を表す行は与えられない。
出力
出力は N 行からなる。
i 行目 (1 \le i \le N) には、街 i から移動可能な街にある特産品の価値の最大値を出力してください。
入力例 1
4 2 10 30 20 5 1 2 3 4
出力例 1
30 30 20 20
入力例 2
5 4 50 100 200 40 40 1 1 1 2 2 1 4 5
出力例 2
100 100 200 40 40
入力例 3
10 9 15 28 999999999 42 15 1000000000 8 300 42 77 1 3 3 5 2 4 4 6 6 8 8 2 7 9 9 10 7 10
出力例 3
999999999 1000000000 999999999 1000000000 999999999 1000000000 77 1000000000 77 77
入力例 4
25 26 72 15 88 43 91 105 2 34 999 12 56 78 90 23 45 67 89 12 345 678 901 234 567 890 123 1 2 2 3 3 4 4 5 5 1 2 6 6 7 8 9 9 10 10 11 11 12 12 13 13 9 14 15 15 16 16 17 17 18 18 19 19 20 14 18 15 19 21 22 22 23 23 24 24 21 21 23
出力例 4
105 105 105 105 105 105 105 999 999 999 999 999 999 678 678 678 678 678 678 678 901 901 901 901 123
入力例 5
1 0 1000000000
出力例 5
1000000000
Score : 366 pts
Problem Statement
In the Kingdom of AtCoder, there are N towns numbered 1 to N.
Town i (1 \le i \le N) has a local specialty with a value of A_i.
There are M roads in the kingdom numbered 1 to M, and road j (1 \le j \le M) connects town U_j and town V_j bidirectionally.
Town v is said to be "reachable" from town u if one can reach town v from town u by traversing zero or more roads (thus, every town is reachable from itself).
For each town i (1 \le i \le N), find the maximum value of a specialty among all towns reachable from town i (including town i itself).
Constraints
- 1 \le N \le 2 \times 10^5
- 0 \le M \le 2 \times 10^5
- N + M + 2 \le 2 \times 10^5 (The total number of lines in the input and output is at most 2 \times 10^5)
- 1 \le A_i \le 10^9 (1 \le i \le N)
- 1 \le U_j, V_j \le N (1 \le j \le M)
- There may be multiple roads connecting the same pair of towns, or roads connecting a town to itself (the graph may contain multi-edges and self-loops).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
If M = 0, the lines representing roads are not given.
Output
Print N lines.
The i-th line (1 \le i \le N) should contain the maximum value of a specialty in a town reachable from town i.
Sample Input 1
4 2 10 30 20 5 1 2 3 4
Sample Output 1
30 30 20 20
Sample Input 2
5 4 50 100 200 40 40 1 1 1 2 2 1 4 5
Sample Output 2
100 100 200 40 40
Sample Input 3
10 9 15 28 999999999 42 15 1000000000 8 300 42 77 1 3 3 5 2 4 4 6 6 8 8 2 7 9 9 10 7 10
Sample Output 3
999999999 1000000000 999999999 1000000000 999999999 1000000000 77 1000000000 77 77
Sample Input 4
25 26 72 15 88 43 91 105 2 34 999 12 56 78 90 23 45 67 89 12 345 678 901 234 567 890 123 1 2 2 3 3 4 4 5 5 1 2 6 6 7 8 9 9 10 10 11 11 12 12 13 13 9 14 15 15 16 16 17 17 18 18 19 19 20 14 18 15 19 21 22 22 23 23 24 24 21 21 23
Sample Output 4
105 105 105 105 105 105 105 999 999 999 999 999 999 678 678 678 678 678 678 678 901 901 901 901 123
Sample Input 5
1 0 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君が主催するプログラミングコンテストでは、 N 人の参加者を 1 つ以上のチームに分けて団体戦を行います。参加者には 1 から N までの番号が付けられており、参加者 i のレートは A_i です。
高橋君は、以下の条件をすべて満たすように参加者をチームに分割することにしました。
- すべての参加者はちょうど 1 つのチームに所属する。
- 各チームには K 人以上の参加者が所属する。
チーム内の実力のばらつきを小さくするため、あるチームの「不均衡度」を、そのチームに所属する参加者のレートの最大値と最小値の差として定義します。
すべてのチームの不均衡度の合計として達成可能な最小値を求めてください。
ただし、条件を満たす分割が存在しない場合は、 -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で与えられる。
N K A_1 A_2 \dots A_N
- 1 行目には、参加者の人数を表す整数 N と、各チームの人数の下限を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各参加者のレートを表す N 個の整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
出力
すべてのチームの不均衡度の合計として達成可能な最小値を 1 行で出力せよ。
条件を満たす分割が存在しない場合は、 -1 を出力せよ。
入力例 1
5 2 10 1 4 15 3
出力例 1
8
入力例 2
3 5 100 200 150
出力例 2
-1
入力例 3
12 3 25 100 22 102 105 30 200 205 201 28 203 101
出力例 3
18
入力例 4
30 4 48 12 99 35 14 88 52 91 19 33 85 41 18 95 38 72 50 22 67 93 45 78 61 29 83 55 69 75 63 31
出力例 4
64
入力例 5
1 1 1000000000
出力例 5
0
Score : 400 pts
Problem Statement
In a programming contest hosted by Takahashi, N participants will be divided into one or more teams to compete in a team competition. The participants are numbered 1 to N, and the rating of participant i is A_i.
Takahashi has decided to divide the participants into teams such that all of the following conditions are satisfied:
- Every participant belongs to exactly one team.
- Each team has at least K participants.
To reduce the disparity in skill within teams, the "imbalance" of a team is defined as the difference between the maximum and minimum ratings among the participants belonging to that team.
Find the minimum possible sum of the imbalances of all teams.
If no division satisfies the conditions, print -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given in the following format:
N K A_1 A_2 \dots A_N
- The first line contains an integer N representing the number of participants and an integer K representing the lower bound on the number of participants in each team, separated by a space.
- The second line contains N space-separated integers A_1, A_2, \dots, A_N representing the ratings of the participants.
Output
Print the minimum possible sum of the imbalances of all teams in a single line.
If no division satisfies the conditions, print -1.
Sample Input 1
5 2 10 1 4 15 3
Sample Output 1
8
Sample Input 2
3 5 100 200 150
Sample Output 2
-1
Sample Input 3
12 3 25 100 22 102 105 30 200 205 201 28 203 101
Sample Output 3
18
Sample Input 4
30 4 48 12 99 35 14 88 52 91 19 33 85 41 18 95 38 72 50 22 67 93 45 78 61 29 83 55 69 75 63 31
Sample Output 4
64
Sample Input 5
1 1 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は、ドローンを使って拠点 1 から拠点 N へ荷物を配送します。
ドローンの飛行高度は 0 以上 K 以下の整数で表されます。はじめ、ドローンは拠点 1 にいて、高度は S です。最終的に拠点 N に到着し、高度をちょうど T にしたいです。
ドローンには高度制限装置が搭載されており、出発前に許容する高度範囲を設定する必要があります。高橋君は出発前に整数 U, V(0 \leq U \leq V \leq K)を 1 組選びます。高度範囲を [U, V] に設定すると、装置の調整に G \times (V - U) の時間がかかります。
全行程を通じて、ドローンの高度は常に U 以上かつ V 以下でなければなりません。したがって、U \leq S \leq V かつ U \leq T \leq V が必要です。一度設定した高度範囲は途中で変更できません。
拠点どうしを結ぶ M 本の一方通行の航路があります。同じ拠点の組に対して複数の航路が存在することもあります。航路 j は拠点 A_j から拠点 B_j へ向かっています。
高度が x のとき、航路 j は L_j \leq x \leq R_j を満たす場合に限り通ることができ、通過にかかる時間は
C_j + D_j \times |x - H_j|
です。ここで H_j は航路 j の基準高度であり、必ずしも [L_j, R_j] の範囲内にあるとは限りません。航路を通過しても高度は変わりません。
各拠点 i(拠点 1 と拠点 N を含む)に滞在しているとき、次の操作をそれぞれ何回でも任意の順序で行えます。
- 高度を 1 上げる。この操作には P_i の時間がかかる。
- 高度を 1 下げる。この操作には Q_i の時間がかかる。
ただし、操作によって高度が U 未満または V を超えてはなりません。
ドローンは同じ拠点を複数回訪れても、同じ航路を複数回利用してもかまいません。
高橋君は、高度範囲 [U, V] の設定、拠点での高度調整、航路の選択を自由に行い、総所要時間を最小化したいです。ここで、総所要時間は、高度範囲の調整時間 G \times (V - U) と、拠点での高度調整にかかった時間の合計と、航路の通過にかかった時間の合計をすべて足し合わせたものです。
拠点 1 で高度 S の状態から始め、拠点 N で高度 T の状態にするために必要な総所要時間の最小値を求めてください。達成する方法が存在しない場合は NA を出力してください。
制約
- 2 \leq N \leq 1000
- 0 \leq M \leq 5000
- 1 \leq K \leq 10
- 0 \leq G \leq 10^6
- 0 \leq S \leq K
- 0 \leq T \leq K
- 0 \leq P_i \leq 10^6 (1 \leq i \leq N)
- 0 \leq Q_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq A_j \leq N,\ 1 \leq B_j \leq N,\ A_j \neq B_j (1 \leq j \leq M)
- 0 \leq L_j \leq R_j \leq K (1 \leq j \leq M)
- 0 \leq H_j \leq K (1 \leq j \leq M)
- 0 \leq C_j \leq 10^6 (1 \leq j \leq M)
- 0 \leq D_j \leq 10^6 (1 \leq j \leq M)
- 入力はすべて整数である
入力
N M K G S T P_1 Q_1 P_2 Q_2 \vdots P_N Q_N A_1 B_1 L_1 R_1 C_1 H_1 D_1 A_2 B_2 L_2 R_2 C_2 H_2 D_2 \vdots A_M B_M L_M R_M C_M H_M D_M
- 1 行目には、拠点の数 N、航路の数 M、高度の最大値 K、高度範囲の幅 1 あたりの調整時間 G がスペース区切りで与えられる。
- 2 行目には、初期高度 S と目標高度 T がスペース区切りで与えられる。
- 続く N 行では、各拠点での高度調整にかかる時間が与えられる。
- 2+i 行目には、拠点 i で高度を 1 上げる時間 P_i と、高度を 1 下げる時間 Q_i がスペース区切りで与えられる。
- 続く M 行では、各航路の情報が与えられる。
- 2+N+j 行目には、航路 j の始点 A_j、終点 B_j、通行可能な高度範囲の下限 L_j、上限 R_j、基本時間 C_j、基準高度 H_j、高度のずれに対する追加時間係数 D_j がスペース区切りで与えられる。
- M=0 の場合、航路の情報を表す行は与えられない。
出力
拠点 1 で高度 S の状態から始め、拠点 N で高度 T の状態にするために必要な総所要時間の最小値を 1 行に出力してください。達成する方法が存在しない場合は NA を出力してください。
入力例 1
3 3 4 5 1 3 10 10 2 2 10 10 1 2 1 3 20 2 3 2 3 2 4 15 3 2 1 3 1 4 100 2 1
出力例 1
52
入力例 2
3 2 3 10 1 2 5 5 5 5 5 5 1 2 0 3 10 1 1 3 2 0 3 10 1 1
出力例 2
NA
入力例 3
6 7 6 15 0 6 20 5 3 30 25 25 4 4 15 15 10 10 1 2 0 2 50 1 10 1 3 0 4 80 2 5 2 4 1 5 40 3 8 3 4 2 6 30 4 2 4 5 3 6 45 5 6 2 5 0 3 120 2 1 5 6 4 6 25 6 3
出力例 3
287
入力例 4
15 22 10 100 3 7 50 30 20 20 15 40 35 10 25 25 45 15 10 50 30 30 20 15 40 25 15 35 25 20 30 40 10 10 50 50 1 2 2 5 120 3 15 1 3 0 4 200 2 10 2 4 3 6 85 4 20 2 5 1 5 150 2 12 3 5 2 7 110 5 8 3 6 0 3 180 1 25 4 7 4 8 90 6 15 4 8 2 6 130 3 10 5 8 3 7 75 5 18 5 9 1 6 160 4 14 6 9 0 5 140 2 22 7 10 5 9 105 7 12 8 10 4 8 80 6 16 8 11 3 7 115 4 20 9 11 2 6 95 5 15 9 12 1 8 170 3 10 10 13 6 10 130 8 25 10 14 5 9 85 7 14 11 14 4 8 100 6 18 12 14 2 7 125 5 12 13 15 7 10 110 8 20 14 15 5 10 90 7 15
出力例 4
1055
入力例 5
2 2 3 0 0 3 0 100 100 0 1 2 2 3 50 0 10 1 2 1 1 0 3 5
出力例 5
80
Score : 433 pts
Problem Statement
Takahashi is delivering a package from base 1 to base N using a drone.
The flight altitude of the drone is represented by an integer between 0 and K, inclusive. Initially, the drone is at base 1 at an altitude of S. He wants to eventually arrive at base N with an altitude of exactly T.
The drone is equipped with an altitude limiter device, and an allowed altitude range must be configured before departure. Takahashi chooses a pair of integers U, V (0 \leq U \leq V \leq K) before departure. Setting the altitude range to [U, V] takes G \times (V - U) time to adjust the device.
Throughout the entire trip, the drone's altitude must always be at least U and at most V. Therefore, U \leq S \leq V and U \leq T \leq V are required. The configured altitude range cannot be changed en route.
There are M one-way flight paths connecting bases. There may be multiple flight paths between the same pair of bases. Flight path j goes from base A_j to base B_j.
When the altitude is x, flight path j can be traversed if and only if L_j \leq x \leq R_j, and the time required to traverse it is
C_j + D_j \times |x - H_j|
Here, H_j is the reference altitude of flight path j, and is not necessarily within the range [L_j, R_j]. Traversing a flight path does not change the altitude.
While staying at each base i (including base 1 and base N), the following operations can each be performed any number of times in any order:
- Increase the altitude by 1. This operation takes P_i time.
- Decrease the altitude by 1. This operation takes Q_i time.
However, the operations must not cause the altitude to become less than U or exceed V.
The drone may visit the same base multiple times and traverse the same flight path multiple times.
Takahashi wants to minimize the total required time by freely choosing the altitude range [U, V], the altitude adjustments at bases, and the flight paths to take. Here, the total required time is the sum of the time to adjust the altitude range G \times (V - U), the total time spent adjusting altitude at bases, and the total time spent traversing flight paths.
Find the minimum total required time to start from base 1 at altitude S and reach base N at altitude T. If it is impossible to achieve this, print NA.
Constraints
- 2 \leq N \leq 1000
- 0 \leq M \leq 5000
- 1 \leq K \leq 10
- 0 \leq G \leq 10^6
- 0 \leq S \leq K
- 0 \leq T \leq K
- 0 \leq P_i \leq 10^6 (1 \leq i \leq N)
- 0 \leq Q_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq A_j \leq N,\ 1 \leq B_j \leq N,\ A_j \neq B_j (1 \leq j \leq M)
- 0 \leq L_j \leq R_j \leq K (1 \leq j \leq M)
- 0 \leq H_j \leq K (1 \leq j \leq M)
- 0 \leq C_j \leq 10^6 (1 \leq j \leq M)
- 0 \leq D_j \leq 10^6 (1 \leq j \leq M)
- All input values are integers.
Input
N M K G S T P_1 Q_1 P_2 Q_2 \vdots P_N Q_N A_1 B_1 L_1 R_1 C_1 H_1 D_1 A_2 B_2 L_2 R_2 C_2 H_2 D_2 \vdots A_M B_M L_M R_M C_M H_M D_M
- The first line contains the number of bases N, the number of flight paths M, the maximum altitude K, and the adjustment time G per unit width of the altitude range, separated by spaces.
- The second line contains the initial altitude S and the target altitude T, separated by spaces.
- The following N lines give the time required for altitude adjustment at each base.
- The (2+i)-th line contains the time P_i to increase altitude by 1 and the time Q_i to decrease altitude by 1 at base i, separated by spaces.
- The following M lines give the information of each flight path.
- The (2+N+j)-th line contains the start base A_j, the end base B_j, the lower bound L_j and upper bound R_j of the passable altitude range, the base time C_j, the reference altitude H_j, and the additional time coefficient for altitude deviation D_j of flight path j, separated by spaces.
- If M=0, no lines representing flight path information are given.
Output
Print in one line the minimum total required time to start from base 1 at altitude S and reach base N at altitude T. If it is impossible to achieve this, print NA.
Sample Input 1
3 3 4 5 1 3 10 10 2 2 10 10 1 2 1 3 20 2 3 2 3 2 4 15 3 2 1 3 1 4 100 2 1
Sample Output 1
52
Sample Input 2
3 2 3 10 1 2 5 5 5 5 5 5 1 2 0 3 10 1 1 3 2 0 3 10 1 1
Sample Output 2
NA
Sample Input 3
6 7 6 15 0 6 20 5 3 30 25 25 4 4 15 15 10 10 1 2 0 2 50 1 10 1 3 0 4 80 2 5 2 4 1 5 40 3 8 3 4 2 6 30 4 2 4 5 3 6 45 5 6 2 5 0 3 120 2 1 5 6 4 6 25 6 3
Sample Output 3
287
Sample Input 4
15 22 10 100 3 7 50 30 20 20 15 40 35 10 25 25 45 15 10 50 30 30 20 15 40 25 15 35 25 20 30 40 10 10 50 50 1 2 2 5 120 3 15 1 3 0 4 200 2 10 2 4 3 6 85 4 20 2 5 1 5 150 2 12 3 5 2 7 110 5 8 3 6 0 3 180 1 25 4 7 4 8 90 6 15 4 8 2 6 130 3 10 5 8 3 7 75 5 18 5 9 1 6 160 4 14 6 9 0 5 140 2 22 7 10 5 9 105 7 12 8 10 4 8 80 6 16 8 11 3 7 115 4 20 9 11 2 6 95 5 15 9 12 1 8 170 3 10 10 13 6 10 130 8 25 10 14 5 9 85 7 14 11 14 4 8 100 6 18 12 14 2 7 125 5 12 13 15 7 10 110 8 20 14 15 5 10 90 7 15
Sample Output 4
1055
Sample Input 5
2 2 3 0 0 3 0 100 100 0 1 2 2 3 50 0 10 1 2 1 1 0 3 5
Sample Output 5
80
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は数直線上でコマを使ったゲームをしています。最初、数直線上の座標 0 にコマが置かれており、数直線上に障害物は 1 つもありません。
これから、i = 1, 2, \dots, N の順に以下のイベントが発生します。
- 座標 X_i に障害物が設置される。設置された障害物は以降その場に残り続ける(すでに障害物がある座標に設置された場合も状態は変わらない)。
- コマの現在座標が X_i である場合、コマを以下の条件で定まる整数座標 Y へ移動させる。そうでない場合、コマは移動させない。
- 現在障害物が置かれていない座標、すなわち Y \notin \{X_1, X_2, \dots, X_i\} を満たす座標を選ぶ。
- そのような座標のうち、現在のコマの座標 X_i との距離 |Y - X_i| が最小となる座標を選ぶ。
- 最小距離を d として、条件を満たす座標が X_i - d と X_i + d の 2 つ存在する場合は、より値が大きい X_i + d を選ぶ。
これにより、移動先 Y は常に一意に定まります。
すべてのイベントが終了した時点で、コマが置かれている座標を求めてください。
制約
- 1 \leq N \leq 10^5
- -10^9 \leq X_i \leq 10^9(1 \leq i \leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 X_2 \vdots X_N
出力
すべてのイベントが終了した時点で、コマが置かれている座標を 1 行で出力せよ。
入力例 1
4 0 1 5 2
出力例 1
3
入力例 2
2 10 -5
出力例 2
0
入力例 3
10 0 2 3 1 -2 -1 10 -3 -4 0
出力例 3
-5
入力例 4
25 1 2 3 4 5 -1 -2 -3 -4 0 -6 -7 -5 1000000000 -1000000000 999999999 -8 -9 -10 500 -500 -11 1 2 -12
出力例 4
-13
入力例 5
1 0
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is playing a game with a piece on a number line. Initially, the piece is placed at coordinate 0 on the number line, and there is not even 1 obstacle on the number line.
The following events occur in order for i = 1, 2, \dots, N:
- An obstacle is placed at coordinate X_i. The placed obstacle remains there indefinitely (if an obstacle is placed at a coordinate that already has an obstacle, the state does not change).
- If the current coordinate of the piece is X_i, move the piece to the integer coordinate Y determined by the following conditions. Otherwise, do not move the piece.
- Choose a coordinate where an obstacle is not currently placed, that is, a coordinate satisfying Y \notin \{X_1, X_2, \dots, X_i\}.
- Among such coordinates, choose one that minimizes the distance |Y - X_i| to the piece's current coordinate X_i.
- Letting d be the minimum distance, if there are 2 coordinates satisfying the conditions, X_i - d and X_i + d, choose the larger one, X_i + d.
This always uniquely determines the destination Y.
Find the coordinate where the piece is located after all events have finished.
Constraints
- 1 \leq N \leq 10^5
- -10^9 \leq X_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \vdots X_N
Output
Print the coordinate where the piece is located after all events have finished in 1 line.
Sample Input 1
4 0 1 5 2
Sample Output 1
3
Sample Input 2
2 10 -5
Sample Output 2
0
Sample Input 3
10 0 2 3 1 -2 -1 10 -3 -4 0
Sample Output 3
-5
Sample Input 4
25 1 2 3 4 5 -1 -2 -3 -4 0 -6 -7 -5 1000000000 -1000000000 999999999 -8 -9 -10 500 -500 -11 1 2 -12
Sample Output 4
-13
Sample Input 5
1 0
Sample Output 5
1
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君の家の庭には、N 枚の板が下端を揃えて隙間なく横一列に並んだフェンスがあります。
左から i 番目 (1 \le i \le N) の板の横幅は 1、高さは A_i です。
高橋君は、このフェンスからはみ出さないように長方形のポスターを 1 枚貼りたいと考えています。
具体的には、以下の条件を満たすようにポスターを貼ることにしました。
- 連続する板の区間 [L, R] (1 \le L \le R \le N) および正の整数 H (1 \le H \le \min_{L \le i \le R} A_i) を選ぶ。
- 選んだ区間に、横幅 R - L + 1、高さ H の長方形のポスターを貼る。このとき、ポスターの面積は (R - L + 1) \times H となる。
高橋君は、面積が S 以上であるようなポスターを貼りたいと考えています。
条件を満たすポスターの貼り方が存在するか判定し、存在するならば、ポスターの横幅 R - L + 1 として考えられる最小値を出力してください。
条件を満たす貼り方が存在しない場合は、代わりに -1 を出力してください。
制約
- 1 \le N \le 5 \times 10^5
- 1 \le S \le 5 \times 10^{14}
- 1 \le A_i \le 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S A_1 A_2 \dots A_N
出力
面積が S 以上となるポスターの横幅 R - L + 1 の最小値を 1 行で出力してください。そのような選び方が存在しない場合は、-1 を出力してください。
入力例 1
5 8 2 4 4 3 1
出力例 1
2
入力例 2
4 20 3 2 4 2
出力例 2
-1
入力例 3
10 15 3 5 2 7 6 8 4 2 1 5
出力例 3
3
入力例 4
24 2500000000 300000000 450000000 200000000 800000000 750000000 700000000 720000000 500000000 100000000 600000000 650000000 600000000 400000000 350000000 800000000 700000000 200000000 500000000 550000000 500000000 600000000 400000000 300000000 100000000
出力例 4
4
入力例 5
1 1000000000 1000000000
出力例 5
1
Score : 466 pts
Problem Statement
In Takahashi's garden, there is a fence consisting of N boards arranged in a row from left to right without any gaps, with their bottom edges aligned.
The i-th board from the left (1 \le i \le N) has a width of 1 and a height of A_i.
Takahashi wants to put up a single rectangular poster without it extending beyond the fence.
Specifically, he decides to place the poster such that the following conditions are satisfied:
- Choose a contiguous range of boards [L, R] (1 \le L \le R \le N) and a positive integer H (1 \le H \le \min_{L \le i \le R} A_i).
- Place a rectangular poster of width R - L + 1 and height H over the chosen range. The area of the poster will be (R - L + 1) \times H.
Takahashi wants to place a poster with an area of at least S.
Determine whether there is a way to place a poster that satisfies the conditions. If it exists, print the minimum possible width R - L + 1 of the poster.
If no such placement exists, print -1 instead.
Constraints
- 1 \le N \le 5 \times 10^5
- 1 \le S \le 5 \times 10^{14}
- 1 \le A_i \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N S A_1 A_2 \dots A_N
Output
Print the minimum possible width R - L + 1 of a poster with an area of at least S in a single line. If no such placement exists, print -1.
Sample Input 1
5 8 2 4 4 3 1
Sample Output 1
2
Sample Input 2
4 20 3 2 4 2
Sample Output 2
-1
Sample Input 3
10 15 3 5 2 7 6 8 4 2 1 5
Sample Output 3
3
Sample Input 4
24 2500000000 300000000 450000000 200000000 800000000 750000000 700000000 720000000 500000000 100000000 600000000 650000000 600000000 400000000 350000000 800000000 700000000 200000000 500000000 550000000 500000000 600000000 400000000 300000000 100000000
Sample Output 4
4
Sample Input 5
1 1000000000 1000000000
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
高橋君は、 N 個の区画からなる登山道をハイキングしようとしています。
i 番目の区画( 1 \leq i \leq N )には、景観の美しさ S_i と、静けさ B_i が定められています。
高橋君は、連続する 1 個以上の区画を選んでハイキングします。
具体的には、整数 l, r( 1 \leq l \leq r \leq N )を決めて、第 l 区画から第 r 区画までを歩きます。
このとき、ハイキングの 満足度 を次のように定義します。
S_l + S_{l+1} + \cdots + S_r + \min(B_l, B_{l+1}, \ldots, B_r)
すなわち、歩いた区画すべての景観の美しさの合計に、歩いた区画すべての静けさの最小値を加えたものです。
高橋君は、満足度が K 以上になるように歩く範囲を選びたいと考えています。
満足度が K 以上となる (l, r) の組の個数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^{14}
- 1 \leq S_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力はすべて整数
入力
N K S_1 B_1 S_2 B_2 \vdots S_N B_N
1 行目には、区画の数 N と、必要な満足度の下限 K がスペース区切りで与えられる。
続く N 行のうち i 行目( 1 \leq i \leq N )には、 i 番目の区画の景観の美しさ S_i と静けさ B_i がスペース区切りで与えられる。
出力
満足度が K 以上になる (l, r) の組の個数を 1 行で出力せよ。
入力例 1
5 10 3 4 2 5 6 1 1 7 4 3
出力例 1
6
入力例 2
4 50 5 3 4 2 3 5 2 1
出力例 2
0
入力例 3
12 25 8 6 3 10 5 4 7 8 2 3 9 5 4 12 6 2 1 9 10 7 3 6 5 1
出力例 3
38
入力例 4
30 250 12 80 25 60 7 100 18 45 30 70 5 20 22 90 14 35 40 55 11 75 9 15 33 65 16 85 28 25 6 95 21 50 13 40 37 30 19 110 8 10 45 58 24 72 10 38 31 88 17 22 26 68 4 120 35 44 15 78 29 18
出力例 4
191
入力例 5
1 200000000000000 1000000000 1000000000
出力例 5
0
Score : 500 pts
Problem Statement
Takahashi is going to hike along a trail consisting of N sections.
The i-th section (1 \leq i \leq N) has a scenic beauty value S_i and a tranquility value B_i.
Takahashi will select one or more consecutive sections to hike through.
Specifically, he chooses integers l, r (1 \leq l \leq r \leq N) and walks from section l to section r.
The satisfaction of the hike is defined as follows:
S_l + S_{l+1} + \cdots + S_r + \min(B_l, B_{l+1}, \ldots, B_r)
That is, the sum of the scenic beauty values of all sections walked, plus the minimum tranquility value among all sections walked.
Takahashi wants to choose a range to walk such that the satisfaction is at least K.
Find the number of pairs (l, r) for which the satisfaction is at least K.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^{14}
- 1 \leq S_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All inputs are integers
Input
N K S_1 B_1 S_2 B_2 \vdots S_N B_N
The first line contains the number of sections N and the required lower bound on satisfaction K, separated by a space.
The following N lines each contain, on the i-th line (1 \leq i \leq N), the scenic beauty S_i and tranquility B_i of the i-th section, separated by a space.
Output
Output in one line the number of pairs (l, r) for which the satisfaction is at least K.
Sample Input 1
5 10 3 4 2 5 6 1 1 7 4 3
Sample Output 1
6
Sample Input 2
4 50 5 3 4 2 3 5 2 1
Sample Output 2
0
Sample Input 3
12 25 8 6 3 10 5 4 7 8 2 3 9 5 4 12 6 2 1 9 10 7 3 6 5 1
Sample Output 3
38
Sample Input 4
30 250 12 80 25 60 7 100 18 45 30 70 5 20 22 90 14 35 40 55 11 75 9 15 33 65 16 85 28 25 6 95 21 50 13 40 37 30 19 110 8 10 45 58 24 72 10 38 31 88 17 22 26 68 4 120 35 44 15 78 29 18
Sample Output 4
191
Sample Input 5
1 200000000000000 1000000000 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
整数 D(0 \leq D \leq 9)が与えられます。まず、D を用いて非負整数に対する「なめらかな数」を定義します。
非負整数 v の標準的な十進表記とは、v \geq 1 のときは先頭に 0 を付けない十進表記、v = 0 のときは 0 を指します。非負整数 v がなめらかな数であるとは、v の標準的な十進表記において、隣り合うどの2桁についても、その差の絶対値が D 以下であることを指します。標準的な十進表記が1桁である非負整数(0 以上 9 以下)は、隣り合う2桁の組が存在しないため、常になめらかな数です。
次に、正の整数 x に対して f(x) を次のように定めます。
x の標準的な十進表記を文字列 S とみなします。S の長さを L とすると、S の連続部分文字列(S の連続する1文字以上を取り出して得られる文字列)は、切り出す開始位置と終了位置の組によって区別され、全部で L(L+1)/2 個あります。例えば x = 1023 のとき S = 1023 であり、連続部分文字列は 1, 10, 102, 1023, 0, 02, 023, 2, 23, 3 の 10 個です。
これら L(L+1)/2 個の連続部分文字列それぞれについて、次の手順でなめらかな数であるかを判定します。
- その連続部分文字列を十進法の非負整数として解釈します。先頭に
0がある場合はそれを無視して数値化します(例えば023は 23、00は 0 と解釈します)。 - 得られた非負整数がなめらかな数であるかを判定します。なめらかな数の判定は、その非負整数の標準的な十進表記に対して行います。すなわち、元の部分文字列ではなく、数値に変換した後の標準的な十進表記における隣接桁の差で判定します。
f(x) は、L(L+1)/2 個の連続部分文字列のうち、上の手順でなめらかな数と判定されるものの個数です。
補足: 異なる位置から切り出された連続部分文字列は、数値として同じ値になる場合でも、それぞれ別々に数えます。例えば x = 121 のとき S = 121 から、1文字目の 1 と3文字目の 1 はどちらも値 1(なめらかな数)ですが、これらは別々に数えて 2 個とします。
正整数 N が十進表記の文字列として与えられます。f(1) + f(2) + \cdots + f(N) を 10^9 + 7 で割った余りを求めてください。
制約
- N は先頭に不要な 0 を含まない正整数の十進表記である
- 1 \leq |N| \leq 10^5
- ここで |N| は N の十進表記の桁数を表す
- 0 \leq D \leq 9
- D は整数である
入力
N D
- 1 行目には、正整数 N が十進表記の文字列として与えられる。
- 2 行目には、なめらかな数の条件における隣接桁の差の絶対値の上限 D が整数で与えられる。
出力
f(1) + f(2) + \cdots + f(N) を 10^9 + 7 で割った余りを1行で出力せよ。
入力例 1
20 1
出力例 1
34
入力例 2
101 0
出力例 2
206
入力例 3
3141592653589793238462643383279 2
出力例 3
584892474
入力例 4
12345678901234567890123456789012345678901234567890123456789012345678901234567890 4
出力例 4
940477512
入力例 5
1 9
出力例 5
1
Score : 500 pts
Problem Statement
You are given an integer D (0 \leq D \leq 9). First, we define a "smooth number" for non-negative integers using D.
The standard decimal representation of a non-negative integer v is the decimal representation without leading zeros if v \geq 1, and 0 if v = 0. A non-negative integer v is a smooth number if, in its standard decimal representation, the absolute difference between any two adjacent digits is at most D. Non-negative integers with a 1-digit standard decimal representation (0 to 9) are always smooth numbers because there are no adjacent pairs of digits.
Next, for a positive integer x, we define f(x) as follows.
Consider the standard decimal representation of x as a string S. Let L be the length of S. There are L(L+1)/2 contiguous substrings of S (obtained by extracting one or more consecutive characters of S), distinguished by their starting and ending positions. For example, if x = 1023, then S = 1023, and the 10 contiguous substrings are 1, 10, 102, 1023, 0, 02, 023, 2, 23, and 3.
For each of these L(L+1)/2 contiguous substrings, we determine if it represents a smooth number using the following procedure:
- Interpret the contiguous substring as a non-negative integer in base 10. If there are leading zeros, ignore them to obtain the numerical value (for example,
023is interpreted as 23, and00as 0). - Determine if the resulting non-negative integer is a smooth number. Note that this determination is based on the standard decimal representation of that non-negative integer. That is, it is judged by the differences between adjacent digits in its standard decimal representation after converting to a number, not in the original substring itself.
f(x) is the number of contiguous substrings among the L(L+1)/2 ones that are determined to be smooth numbers by the above procedure.
Note: Contiguous substrings extracted from different positions are counted separately, even if they have the same numerical value. For example, if x = 121, then from S = 121, the 1 at the 1st character and the 1 at the 3rd character both have the value 1 (a smooth number), but they are counted separately as 2 occurrences.
You are given a positive integer N as a string in decimal representation. Find the sum f(1) + f(2) + \cdots + f(N) modulo 10^9 + 7.
Constraints
- N is the decimal representation of a positive integer without leading zeros.
- 1 \leq |N| \leq 10^5
- Here, |N| denotes the number of digits in the decimal representation of N.
- 0 \leq D \leq 9
- D is an integer.
Input
N D
- The first line contains a positive integer N as a string in decimal representation.
- The second line contains an integer D, the maximum allowed absolute difference between adjacent digits in the condition for smooth numbers.
Output
Print the sum f(1) + f(2) + \cdots + f(N) modulo 10^9 + 7 in a single line.
Sample Input 1
20 1
Sample Output 1
34
Sample Input 2
101 0
Sample Output 2
206
Sample Input 3
3141592653589793238462643383279 2
Sample Output 3
584892474
Sample Input 4
12345678901234567890123456789012345678901234567890123456789012345678901234567890 4
Sample Output 4
940477512
Sample Input 5
1 9
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 533 点
問題文
N 個の都市があり、初めはどの都市間にも両替所は存在しない。
これから Q 個の操作が順に与えられる。任意の時点で両替所の配置は森(閉路を持たないグラフ)を成すことが保証される。したがって、同じ連結成分に属する任意の 2 都市間の経路はちょうど 1 つである。
各両替所は 2 つの都市を結ぶ辺であり、通過する方向によって異なるアフィン変換(t \mapsto at + b)を持つ。すべての演算は \bmod{998244353} 上の整数として行う。
各両替所には、開設時に定められた 方向 がある。操作 L p q a b c d で開設された両替所の方向は「p 側 \to q 側」であり、この方向は両替所が閉鎖されるまで変わらない。
各操作は次のいずれかである。
L u v a b c d(開設):都市 u と都市 v の間に新しい両替所を開設する。この時点で u と v は異なる連結成分に属することが保証される。この両替所の方向は「u 側 \to v 側」と定められ、u 側から v 側への両替は所持金 t を at + b \pmod{998244353} に、v 側から u 側への両替は所持金 t を ct + d \pmod{998244353} に変換する。
C u v(閉鎖):都市 u と都市 v を直接結ぶ両替所を閉鎖する。この時点でその両替所が存在することが保証される。引数 u, v の順序は開設時と異なっていてもよい。閉鎖により u と v は異なる連結成分に分かれる。
U u v a b c d(更新):都市 u と都市 v を直接結ぶ両替所の為替レートを更新する。この時点でその両替所が存在することが保証される。引数 u, v の順序は開設時と異なっていてもよい。
この両替所が操作 L p q ... によって開設されたものなら、更新後は p 側から q 側への両替が t \mapsto at + b \pmod{998244353}、q 側から p 側への両替が t \mapsto ct + d \pmod{998244353} となる。
Q x y s(照会):都市 x と都市 y が同じ連結成分に属する場合、x から y への唯一の経路 x = v_0, v_1, \ldots, v_k = y に沿って、辺 v_0v_1, v_1v_2, \ldots, v_{k-1}v_k に対応する変換を初期所持金 s に順に適用した結果を \bmod{998244353} で出力せよ。
x = y の場合は k = 0 であり、変換は適用されないため s をそのまま出力する。
x と y が同じ連結成分に属さない場合は -1 を出力せよ。
制約
- 1 \leq N \leq 5 \times 10^4
- 1 \leq Q \leq 5 \times 10^4
- 操作
L u v a b c dにおいて、1 \leq u, v \leq N、u \neq v、0 \leq a, b, c, d \leq 998244352 - 操作
C u vにおいて、1 \leq u, v \leq N、u \neq v - 操作
U u v a b c dにおいて、1 \leq u, v \leq N、u \neq v、0 \leq a, b, c, d \leq 998244352 - 操作
Q x y sにおいて、1 \leq x, y \leq N、0 \leq s \leq 998244352 - 任意の時点で、両替所の配置は森を成す
- 操作はすべて正当である(上記の各操作の保証条件を満たす)
- 入力はすべて整数である
入力
N Q
\text{op}_1
\text{op}_2
:
\text{op}_Q
1 行目には、都市の数 N と操作の数 Q がスペース区切りで与えられる。続く Q 行に各操作が 1 行ずつ与えられる。各操作は以下のいずれかの形式である。
L u v a b c d C u v U u v a b c d Q x y s
出力
各 Q 操作に対して 1 行ずつ、求める値を出力せよ。x と y が同じ連結成分に属さない場合は -1 を出力せよ。
入力例 1
4 8 L 1 2 2 3 5 7 Q 1 2 10 L 2 3 4 1 6 0 Q 1 3 10 U 2 3 3 3 2 2 Q 3 1 5 C 1 2 Q 1 3 10
出力例 1
23 93 67 -1
入力例 2
5 8 Q 1 2 0 L 1 2 1 5 1 6 Q 2 1 10 L 3 4 2 0 3 0 Q 1 4 7 C 2 1 Q 1 2 123 Q 3 4 8
出力例 2
-1 16 -1 -1 16
入力例 3
8 20 L 1 2 2 1 3 4 L 2 3 5 0 7 2 L 4 5 1 10 1 20 Q 1 3 6 Q 3 1 100 L 3 4 4 4 2 2 Q 1 5 9 U 3 2 6 6 8 8 Q 1 5 9 C 3 4 Q 1 5 9 L 6 7 9 1 10 2 L 7 8 11 3 12 4 Q 6 8 5 L 5 6 13 5 14 6 Q 4 8 7 C 7 8 Q 6 8 5 L 3 8 15 7 16 8 Q 1 8 9
出力例 3
65 2110 394 494 -1 509 22388 -1 1807
入力例 4
12 35 L 1 2 2 3 4 5 L 2 3 3 4 5 6 L 3 4 4 5 6 7 L 5 6 5 6 7 8 L 6 7 6 7 8 9 L 8 9 7 8 9 10 Q 1 4 11 Q 4 1 22 L 4 5 8 9 10 11 Q 1 7 33 U 4 5 9 10 11 12 Q 7 1 44 C 2 3 Q 1 7 55 L 2 8 10 11 12 13 Q 1 9 66 L 9 10 11 12 13 14 L 10 11 12 13 14 15 Q 11 1 77 C 5 6 Q 3 7 88 L 5 12 13 14 15 16 Q 3 12 99 L 7 11 14 15 16 17 Q 6 1 111 U 1 2 15 16 17 18 U 10 11 16 17 18 19 Q 6 9 222 C 4 5 L 4 12 17 18 19 20 Q 3 5 333 C 8 9 Q 1 6 444 L 8 3 18 19 20 21 Q 1 5 555
出力例 4
321 2809 204073 3347809 -1 9535 6144873 -1 47061 742065513 4390335 341221 -1 533386148
入力例 5
1 5 Q 1 1 0 Q 1 1 1 Q 1 1 998244352 Q 1 1 123456789 Q 1 1 500000000
出力例 5
0 1 998244352 123456789 500000000
Score : 533 pts
Problem Statement
There are N cities, and initially, there are no exchange offices between any cities.
We are given Q operations in order. It is guaranteed that at any point in time, the configuration of the exchange offices forms a forest (a graph with no cycles). Therefore, there is exactly one path between any two cities belonging to the same connected component.
Each exchange office is an edge connecting two cities and has a different affine transformation (t \mapsto at + b) depending on the direction of traversal. All calculations are performed as integers modulo 998244353.
Each exchange office has a direction determined when it is opened. The direction of the exchange office opened by the operation L p q a b c d is "p-side \to q-side", and this direction does not change until the exchange office is closed.
Each operation is one of the following:
L u v a b c d(Open): Open a new exchange office between city u and city v. It is guaranteed that at this moment, u and v belong to different connected components. The direction of this exchange office is defined as "u-side \to v-side". Exchanging money from the u-side to the v-side converts the holding money t into at + b \pmod{998244353}, and exchanging from the v-side to the u-side converts t into ct + d \pmod{998244353}.
C u v(Close): Close the exchange office directly connecting city u and city v. It is guaranteed that this exchange office exists at this moment. The order of the arguments u, v may differ from when it was opened. Closing it splits u and v into different connected components.
U u v a b c d(Update): Update the exchange rates of the exchange office directly connecting city u and city v. It is guaranteed that this exchange office exists at this moment. The order of the arguments u, v may differ from when it was opened.
If this exchange office was opened by an operation L p q ..., after the update, the exchange from the p-side to the q-side becomes t \mapsto at + b \pmod{998244353}, and the exchange from the q-side to the p-side becomes t \mapsto ct + d \pmod{998244353}.
Q x y s(Query): If city x and city y belong to the same connected component, output the result of applying the transformations corresponding to the edges v_0v_1, v_1v_2, \ldots, v_{k-1}v_k in order to the initial holding money s, modulo 998244353, along the unique path x = v_0, v_1, \ldots, v_k = y from x to y.
If x = y, then k = 0, and no transformations are applied, so output s as is.
If x and y do not belong to the same connected component, output -1.
Constraints
- 1 \leq N \leq 5 \times 10^4
- 1 \leq Q \leq 5 \times 10^4
- In the operation
L u v a b c d, 1 \leq u, v \leq N, u \neq v, 0 \leq a, b, c, d \leq 998244352 - In the operation
C u v, 1 \leq u, v \leq N, u \neq v - In the operation
U u v a b c d, 1 \leq u, v \leq N, u \neq v, 0 \leq a, b, c, d \leq 998244352 - In the operation
Q x y s, 1 \leq x, y \leq N, 0 \leq s \leq 998244352 - At any point in time, the configuration of the exchange offices forms a forest.
- All operations are valid (satisfying the guaranteed conditions of each operation described above).
- All input values are integers.
Input
N Q
\text{op}_1
\text{op}_2
:
\text{op}_Q
The first line contains the number of cities N and the number of operations Q, separated by a space. The subsequent Q lines each contain an operation. Each operation is in one of the following formats:
L u v a b c d C u v U u v a b c d Q x y s
Output
For each Q operation, output the required value on a single line. If x and y do not belong to the same connected component, output -1.
Sample Input 1
4 8 L 1 2 2 3 5 7 Q 1 2 10 L 2 3 4 1 6 0 Q 1 3 10 U 2 3 3 3 2 2 Q 3 1 5 C 1 2 Q 1 3 10
Sample Output 1
23 93 67 -1
Sample Input 2
5 8 Q 1 2 0 L 1 2 1 5 1 6 Q 2 1 10 L 3 4 2 0 3 0 Q 1 4 7 C 2 1 Q 1 2 123 Q 3 4 8
Sample Output 2
-1 16 -1 -1 16
Sample Input 3
8 20 L 1 2 2 1 3 4 L 2 3 5 0 7 2 L 4 5 1 10 1 20 Q 1 3 6 Q 3 1 100 L 3 4 4 4 2 2 Q 1 5 9 U 3 2 6 6 8 8 Q 1 5 9 C 3 4 Q 1 5 9 L 6 7 9 1 10 2 L 7 8 11 3 12 4 Q 6 8 5 L 5 6 13 5 14 6 Q 4 8 7 C 7 8 Q 6 8 5 L 3 8 15 7 16 8 Q 1 8 9
Sample Output 3
65 2110 394 494 -1 509 22388 -1 1807
Sample Input 4
12 35 L 1 2 2 3 4 5 L 2 3 3 4 5 6 L 3 4 4 5 6 7 L 5 6 5 6 7 8 L 6 7 6 7 8 9 L 8 9 7 8 9 10 Q 1 4 11 Q 4 1 22 L 4 5 8 9 10 11 Q 1 7 33 U 4 5 9 10 11 12 Q 7 1 44 C 2 3 Q 1 7 55 L 2 8 10 11 12 13 Q 1 9 66 L 9 10 11 12 13 14 L 10 11 12 13 14 15 Q 11 1 77 C 5 6 Q 3 7 88 L 5 12 13 14 15 16 Q 3 12 99 L 7 11 14 15 16 17 Q 6 1 111 U 1 2 15 16 17 18 U 10 11 16 17 18 19 Q 6 9 222 C 4 5 L 4 12 17 18 19 20 Q 3 5 333 C 8 9 Q 1 6 444 L 8 3 18 19 20 21 Q 1 5 555
Sample Output 4
321 2809 204073 3347809 -1 9535 6144873 -1 47061 742065513 4390335 341221 -1 533386148
Sample Input 5
1 5 Q 1 1 0 Q 1 1 1 Q 1 1 998244352 Q 1 1 123456789 Q 1 1 500000000
Sample Output 5
0 1 998244352 123456789 500000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 533 点
問題文
高橋君は温室で N 株の植物を育てています。それぞれの植物には現在の水分量が設定されており、 i 番目の植物の水分量は T_i です。水分量が 0 以下になった植物は枯れてしまいます。
温室にはじょうろが 1 つだけあります。高橋君はこのじょうろで植物に水を与えることで、水分量を回復させることができます。具体的には、高橋君は毎秒の開始時に以下の操作を行います:
- まず、高橋君は N 株の植物の中から ちょうど 1 つ を選び、じょうろで水を与える。水を与えられた植物の水分量は P だけ増加する。
- その後、 すべての 植物の水分量が 1 減少する(水を与えられた植物も含む)。これは温室の高温による蒸発を表している。
高橋君は、すべての植物が枯れない状態(すべての植物の水分量が 1 以上である状態)を可能な限り長く維持したいと考えています。
高橋君が最適に行動したとき、すべての植物の水分量を 1 以上に保ち続けられる秒数が有限であればその最大値を、永遠に保ち続けられるならば -1 を出力してください。
ここで、秒数とは操作を行った回数を指します。つまり、 1 回も操作を行わずにいずれかの植物の水分量が 0 以下であれば答えは 0 です。操作を k 回行った直後にすべての植物の水分量が 1 以上であるような最大の k を求めてください(永遠に可能なら -1 )。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq P \leq 10^4
- 1 \leq T_i \leq 10^9
- 入力はすべて整数である。
入力
N P T_1 T_2 \ldots T_N
- 1 行目には、植物の株数を表す N と、水やりによる水分量の増加量を表す P が、スペース区切りで与えられる。
- 2 行目には、各植物の初期水分量を表す T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
出力
すべての植物の水分量を 1 以上に保ち続けられる最大の操作回数を 1 行で出力せよ。永遠に保ち続けられる場合は -1 を出力せよ。
入力例 1
2 1 2 3
出力例 1
3
入力例 2
1 1 1
出力例 2
-1
入力例 3
6 3 4 8 2 10 5 7
出力例 3
7
入力例 4
12 5 15 3 22 8 19 6 11 30 4 17 25 9
出力例 4
17
入力例 5
1 10000 1000000000
出力例 5
-1
Score : 533 pts
Problem Statement
Takahashi is growing N plants in a greenhouse. Each plant has an associated current water level, and the water level of the i-th plant is T_i. A plant with a water level of 0 or less will wither.
There is only one watering can in the greenhouse. Takahashi can restore the water levels of the plants by watering them with this can. Specifically, at the beginning of each second, Takahashi performs the following operation:
- First, Takahashi chooses exactly one of the N plants and waters it with the watering can. The water level of the watered plant increases by P.
- After that, the water level of all plants decreases by 1 (including the plant that was just watered). This represents evaporation due to the high temperature in the greenhouse.
Takahashi wants to keep all plants from withering (i.e., keep the water level of all plants at 1 or more) for as long as possible.
Assuming Takahashi acts optimally, if the number of seconds he can keep the water level of all plants at 1 or more is finite, output the maximum such number of seconds. If he can keep them alive forever, output -1.
Here, the number of seconds refers to the number of operations performed. That is, if the water level of any plant is 0 or less before performing any operations, the answer is 0. Find the maximum k such that the water level of all plants is 1 or more immediately after performing the operation k times (or -1 if it can be kept alive forever).
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq P \leq 10^4
- 1 \leq T_i \leq 10^9
- All input values are integers.
Input
N P T_1 T_2 \ldots T_N
- The first line contains N, the number of plants, and P, the amount of water level increase per watering, separated by a space.
- The second line contains the initial water levels of each plant, T_1, T_2, \ldots, T_N, separated by spaces.
Output
Print the maximum number of operations for which the water level of all plants can be kept at 1 or more. If they can be kept alive forever, print -1.
Sample Input 1
2 1 2 3
Sample Output 1
3
Sample Input 2
1 1 1
Sample Output 2
-1
Sample Input 3
6 3 4 8 2 10 5 7
Sample Output 3
7
Sample Input 4
12 5 15 3 22 8 19 6 11 30 4 17 25 9
Sample Output 4
17
Sample Input 5
1 10000 1000000000
Sample Output 5
-1