Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は、フィギュアスケートの大会で採点結果の集計を担当することになりました。
この大会には N 人の選手が出場しており、選手には 1 から N までの番号がついています。それぞれの選手は M 人の審査員から評価を受けます。選手 i (1 \leq i \leq N) が審査員 j (1 \leq j \leq M) から受けた点数を S_{i,j} とします。各点数は 1 以上 100 以下の整数です。
大会の規定により、各選手の最終スコアは以下の手順で計算されます:
- M 人の審査員がつけた M 個の点数を、値の小さい順(昇順)に並べる。
- 並べた点数の先頭(最小値)の 1 個と末尾(最大値)の 1 個を除外する。同じ値の点数が複数あっても、除外するのはそれぞれ 1 個のみである。
- 残った M - 2 個の点数の合計を、その選手の最終スコアとする。
最終スコアが最も高い選手の番号を出力してください。最終スコアが最も高い選手が複数いる場合は、その中で番号が最も小さい選手の番号を出力してください。
制約
- 1 \leq N \leq 10^5
- 3 \leq M \leq 10^5
- N \times M \leq 10^6
- 1 \leq S_{i,j} \leq 100
- 入力はすべて整数である。
入力
N M
S_{1,1} S_{1,2} \ldots S_{1,M}
S_{2,1} S_{2,2} \ldots S_{2,M}
\vdots
S_{N,1} S_{N,2} \ldots S_{N,M}
- 1 行目には、選手の人数を表す N と審査員の人数を表す M が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各選手が受けた点数が与えられる。
- 1 + i 行目では、選手 i が M 人の審査員から受けた点数 S_{i,1}, S_{i,2}, \ldots, S_{i,M} がスペース区切りで与えられる。
出力
最終スコアが最も高い選手の番号を 1 行で出力してください。
入力例 1
3 3 5 9 3 8 6 7 4 10 1
出力例 1
2
入力例 2
5 5 50 60 70 80 90 65 75 70 85 55 90 40 80 70 60 100 100 1 1 50 72 68 75 80 71
出力例 2
5
入力例 3
4 7 80 75 90 60 85 70 95 88 92 78 85 90 83 79 95 91 89 84 87 93 86 100 70 99 88 92 91 85
出力例 3
4
Score : 266 pts
Problem Statement
Takahashi has been assigned to tally the scoring results at a figure skating competition.
There are N contestants in this competition, numbered from 1 to N. Each contestant is evaluated by M judges. Let S_{i,j} denote the score that contestant i (1 \leq i \leq N) received from judge j (1 \leq j \leq M). Each score is an integer between 1 and 100, inclusive.
According to the competition rules, each contestant's final score is calculated by the following procedure:
- Sort the M scores given by the M judges in ascending order (from smallest to largest).
- Remove the first score (the minimum) and the last score (the maximum) from the sorted list. Even if there are multiple scores with the same value, only one score is removed from each end.
- The sum of the remaining M - 2 scores becomes that contestant's final score.
Output the number of the contestant with the highest final score. If there are multiple contestants with the highest final score, output the smallest number among them.
Constraints
- 1 \leq N \leq 10^5
- 3 \leq M \leq 10^5
- N \times M \leq 10^6
- 1 \leq S_{i,j} \leq 100
- All input values are integers.
Input
N M
S_{1,1} S_{1,2} \ldots S_{1,M}
S_{2,1} S_{2,2} \ldots S_{2,M}
\vdots
S_{N,1} S_{N,2} \ldots S_{N,M}
- The first line contains N, the number of contestants, and M, the number of judges, separated by a space.
- From the 2nd line to the (N + 1)-th line, the scores received by each contestant are given.
- The (1 + i)-th line contains the scores S_{i,1}, S_{i,2}, \ldots, S_{i,M} that contestant i received from the M judges, separated by spaces.
Output
Output the number of the contestant with the highest final score on a single line.
Sample Input 1
3 3 5 9 3 8 6 7 4 10 1
Sample Output 1
2
Sample Input 2
5 5 50 60 70 80 90 65 75 70 85 55 90 40 80 70 60 100 100 1 1 50 72 68 75 80 71
Sample Output 2
5
Sample Input 3
4 7 80 75 90 60 85 70 95 88 92 78 85 90 83 79 95 91 89 84 87 93 86 100 70 99 88 92 91 85
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は、夏休みに N 日間の短期アルバイトの募集を見つけました。それぞれの日には報酬が設定されており、i 日目 (1 \leq i \leq N) の報酬は A_i 円です。報酬が負の値である日もあり、その場合は研修費などの自己負担が発生するため、働くと逆にお金を支払うことになります。
さらに、このアルバイトでは働く日ごとに交通費として 1 円を自己負担しなければなりません。したがって、i 日目に働いた場合の実質的な利益は (A_i - 1) 円です。
高橋君はシフトの都合上、連続した日程でのみ働くことができます。具体的には、1 \leq i \leq j \leq N を満たす整数の組 (i, j) を 1 つ選び、i 日目から j 日目まで毎日連続して働きます。なお、高橋君は少なくとも 1 日は働かなければなりません(すなわち i = j の場合も許されます)。
このとき、高橋君が得られる実質的な利益の合計は、各日の実質的な利益の総和であり、
(A_i - 1) + (A_{i+1} - 1) + \cdots + (A_j - 1) = \left(\sum_{k=i}^{j} A_k\right) - (j - i + 1)
円となります。ここで (j - i + 1) は働いた日数に相当し、日数分の交通費の合計を表します。
高橋君が (i, j) の選び方を最適にしたとき、実質的な利益の合計の最大値を求めてください。すべての日の報酬が小さい場合など、最大値が負になることもあり得ます。
制約
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq A_i \leq 10^9(1 \leq i \leq N)
- 入力はすべて整数
入力
N A_1 A_2 \cdots A_N
- 1 行目には、アルバイトの募集日数を表す整数 N が与えられる。
- 2 行目には、各日の報酬を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
実質的な利益の合計の最大値を 1 行で出力してください。
入力例 1
5 3 -1 4 1 2
出力例 1
4
入力例 2
8 -2 5 3 -4 8 -1 2 -3
出力例 2
8
入力例 3
15 -1000000000 1000000000 1000000000 -500000000 1000000000 -1 -1 1000000000 500000000 -1000000000 3 2 1 -5 10
出力例 3
3999999990
Score : 333 pts
Problem Statement
Takahashi found a short-term part-time job listing for N days during summer vacation. Each day has a set reward, and the reward for day i (1 \leq i \leq N) is A_i yen. Some days may have negative rewards, meaning that due to training fees or other self-paid expenses, working on such a day would require him to pay money instead.
Furthermore, for this part-time job, Takahashi must pay 1 yen out of pocket as transportation cost for each day he works. Therefore, the effective profit from working on day i is (A_i - 1) yen.
Due to scheduling constraints, Takahashi can only work on consecutive days. Specifically, he chooses a pair of integers (i, j) satisfying 1 \leq i \leq j \leq N, and works every day consecutively from day i to day j. Note that Takahashi must work at least 1 day (i.e., the case i = j is also allowed).
The total effective profit Takahashi earns is the sum of the effective profits for each day he works:
(A_i - 1) + (A_{i+1} - 1) + \cdots + (A_j - 1) = \left(\sum_{k=i}^{j} A_k\right) - (j - i + 1)
yen. Here, (j - i + 1) corresponds to the number of days worked and represents the total transportation cost for those days.
Find the maximum total effective profit when Takahashi optimally chooses (i, j). Note that the maximum value may be negative, such as when the rewards for all days are small.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All inputs are integers
Input
N A_1 A_2 \cdots A_N
- The first line contains an integer N, representing the number of days the part-time job is available.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the reward for each day.
Output
Print the maximum total effective profit in a single line.
Sample Input 1
5 3 -1 4 1 2
Sample Output 1
4
Sample Input 2
8 -2 5 3 -4 8 -1 2 -3
Sample Output 2
8
Sample Input 3
15 -1000000000 1000000000 1000000000 -500000000 1000000000 -1 -1 1000000000 500000000 -1000000000 3 2 1 -5 10
Sample Output 3
3999999990
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は N 個の地点からなる山岳トレイルに挑戦しようとしています。地点 1 がスタート地点(麓)、地点 N がゴール地点(山頂)です。
各地点には消耗度が設定されています。地点 i の消耗度は H_i です。高橋君がある地点に到達すると、その地点の消耗度の分だけスタミナが減少します。
また、このトレイルには M 個の補給食が隠されています。j 番目の補給食は地点 P_j にあり、食べるとスタミナが R_j だけ増加します。各地点には補給食は高々 1 個しかなく、各補給食は一度しか食べられません。高橋君がある地点に到達したとき、その地点に補給食がある場合は自動的に入手します。入手した補給食はその場で食べる必要はなく、個数に制限なく持ち運ぶことができます。
高橋君の初期スタミナは K です。スタミナの値に上限はなく、回復によって初期値を超えることもできます。
トレイルは以下の手順で進行します。各地点での処理は消耗 → リタイア判定 → 補給食の入手 → 補給食の使用の順に行われます。リタイア判定とは、その時点でスタミナが 0 未満であればリタイア(失敗)となることです。補給食の使用によるスタミナの増加はリタイア判定の後に行われるため、消耗直後にスタミナが 0 未満である場合はリタイアとなります。
- 初期配置: 高橋君は地点 1 からスタートします。地点 1 の消耗度 H_1 の分だけスタミナが減少します。スタミナが 0 未満になった場合はリタイアです。リタイアしなかった場合、地点 1 にある補給食があれば入手し、その後、手持ちの補給食の中から好きな個数(0 個でもよい)を選んで食べることができます。
- 移動: 高橋君は現在いる地点 i から、地点 i+1 または地点 i+2 のいずれかに移動します(ただし地点 N を超える地点には移動できません)。地点 i+2 に直接移動した場合、地点 i+1 には立ち寄らないため、地点 i+1 の消耗を受けず、地点 i+1 にある補給食も入手できません。移動先の地点の消耗度の分だけスタミナが減少します。スタミナが 0 未満になった場合はリタイアです。リタイアしなかった場合、移動先の地点にある補給食があれば入手し、その後、手持ちの補給食の中から好きな個数(0 個でもよい)を選んで食べることができます。
- 地点 N に到達するまで手順 2 を繰り返します。
補給食を食べることができるのは上記の手順中に示されたタイミング(各地点に到達し消耗を受けリタイア判定を通過した後)のみであり、地点間の移動中には食べることができません。
高橋君がリタイアせずに地点 N に到達できるかどうかを判定してください。到達できる場合は、移動経路の選び方と補給食を食べるタイミングの選び方を最適にしたとき、地点 N に到達した後に残りの手持ちの補給食をすべて食べ終えた時点でのスタミナの最大値を出力してください。到達できない場合は -1 を出力してください。
制約
- 2 \leq N \leq 10^6
- 0 \leq M \leq \min(N, 10^5)
- 0 \leq K \leq 10^9
- 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq N (1 \leq j \leq M)
- 1 \leq R_j \leq 10^9 (1 \leq j \leq M)
- P_j はすべて異なる
- 入力はすべて整数
入力
N M K H_1 H_2 \cdots H_N P_1 R_1 P_2 R_2 \vdots P_M R_M
- 1 行目には、地点の数 N 、補給食の数 M 、初期スタミナ K がスペース区切りで与えられる。
- 2 行目には、各地点の消耗度 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。
- 3 行目から M 行にわたり、各補給食の情報が与えられる。 2 + j 行目には、 j 番目の補給食がある地点 P_j と回復量 R_j がスペース区切りで与えられる。
出力
高橋君がリタイアせずに地点 N に到達できる場合は、移動経路と補給食を食べるタイミングを最適に選んだときの、地点 N に到達し残りの手持ちの補給食をすべて食べ終えた後でのスタミナの最大値を 1 行で出力せよ。到達できない場合は -1 を 1 行で出力せよ。
入力例 1
5 2 10 3 4 8 2 5 2 5 4 3
出力例 1
4
入力例 2
4 0 3 2 5 5 1
出力例 2
-1
入力例 3
12 5 15 4 7 3 10 2 8 6 1 12 4 5 3 2 10 5 4 7 9 9 20 11 5
出力例 3
21
入力例 4
30 10 25 5 12 4 20 3 15 7 2 25 6 10 4 18 1 9 14 3 22 5 8 16 2 11 7 30 4 6 13 2 10 3 12 5 8 8 5 10 20 14 6 17 15 20 10 24 25 27 7 29 30
出力例 4
75
入力例 5
2 2 0 0 0 1 1000000000 2 1000000000
出力例 5
2000000000
Score : 366 pts
Problem Statement
Takahashi is attempting a mountain trail consisting of N locations. Location 1 is the starting point (foot of the mountain), and location N is the goal (mountain summit).
Each location has an exhaustion level. The exhaustion level of location i is H_i. When Takahashi reaches a location, his stamina decreases by the exhaustion level of that location.
Additionally, M rations are hidden along this trail. The j-th ration is at location P_j, and eating it increases stamina by R_j. There is at most one ration at each location, and each ration can be eaten only once. When Takahashi reaches a location, if there is a ration there, he automatically collects it. He does not need to eat collected rations on the spot and can carry an unlimited number of them.
Takahashi's initial stamina is K. There is no upper limit on stamina, and it can exceed the initial value through recovery.
The trail proceeds according to the following steps. At each location, events occur in the order of Exhaustion \rightarrow Retirement Check \rightarrow Collecting Rations \rightarrow Using Rations. A retirement check means that if stamina is less than 0 at that moment, he retires (fails). Because the stamina increase from using rations occurs after the retirement check, if his stamina is less than 0 immediately after exhaustion, he retires.
- Initial Setup: Takahashi starts at location 1. His stamina decreases by the exhaustion level H_1 of location 1. If his stamina becomes less than 0, he retires. If he does not retire, he collects the ration at location 1 if there is one, and then can choose to eat any number (possibly 0) of the rations in his possession.
- Movement: From his current location i, Takahashi moves to either location i+1 or location i+2 (he cannot move to a location beyond location N). If he moves directly to location i+2, he does not visit location i+1, so he does not suffer the exhaustion of location i+1, nor does he obtain the ration at location i+1. His stamina decreases by the exhaustion level of the destination location. If his stamina becomes less than 0, he retires. If he does not retire, he collects the ration at the destination location if there is one, and then can choose to eat any number (possibly 0) of the rations in his possession.
- Repeat step 2 until reaching location N.
Rations can only be eaten at the timings specified in the steps above (after reaching each location, suffering exhaustion, and passing the retirement check); they cannot be eaten while traveling between locations.
Determine whether Takahashi can reach location N without retiring. If he can, output the maximum possible stamina when he reaches location N and has eaten all remaining rations in his possession, assuming optimal choices of route and timings to eat rations. If he cannot reach location N, output -1.
Constraints
- 2 \leq N \leq 10^6
- 0 \leq M \leq \min(N, 10^5)
- 0 \leq K \leq 10^9
- 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq N (1 \leq j \leq M)
- 1 \leq R_j \leq 10^9 (1 \leq j \leq M)
- All P_j are distinct.
- All input values are integers.
Input
N M K H_1 H_2 \cdots H_N P_1 R_1 P_2 R_2 \vdots P_M R_M
- The first line contains the number of locations N, the number of rations M, and the initial stamina K, separated by spaces.
- The second line contains the exhaustion levels of each location H_1, H_2, \ldots, H_N, separated by spaces.
- The next M lines provide information about each ration. The (2 + j)-th line contains the location P_j of the j-th ration and its recovery amount R_j, separated by a space.
Output
If Takahashi can reach location N without retiring, print in a single line the maximum stamina after reaching location N and eating all remaining rations in his possession, assuming optimal choices of route and timings to eat rations. If he cannot reach location N, print -1 in a single line.
Sample Input 1
5 2 10 3 4 8 2 5 2 5 4 3
Sample Output 1
4
Sample Input 2
4 0 3 2 5 5 1
Sample Output 2
-1
Sample Input 3
12 5 15 4 7 3 10 2 8 6 1 12 4 5 3 2 10 5 4 7 9 9 20 11 5
Sample Output 3
21
Sample Input 4
30 10 25 5 12 4 20 3 15 7 2 25 6 10 4 18 1 9 14 3 22 5 8 16 2 11 7 30 4 6 13 2 10 3 12 5 8 8 5 10 20 14 6 17 15 20 10 24 25 27 7 29 30
Sample Output 4
75
Sample Input 5
2 2 0 0 0 1 1000000000 2 1000000000
Sample Output 5
2000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は N 個の商品を販売するオンラインショップを運営しています。
最初、各商品 i (1 \leq i \leq N) の価格は A_i 円に設定されています。高橋君は販売開始前に、決められたルールに従って Q 回の「価格調整」を 1 番目から Q 番目まで順番に行います。
j 番目 (1 \leq j \leq Q) の価格調整では、種類 T_j、変更後の価格 X_j、取り消しフラグ R_j が与えられ、以下の手順 1〜3 をこの順に実行します。
- 対象商品の選択:直前の価格調整が完了した時点(j = 1 のときは初期状態)での各商品の価格に基づき、対象となる商品を 1 つ選ぶ。
- T_j = 1 の場合:価格が最も高い商品を選ぶ。価格が最も高い商品が複数あるときは、その中で商品番号が最も小さいものを選ぶ。
- T_j = 2 の場合:価格が最も低い商品を選ぶ。価格が最も低い商品が複数あるときは、その中で商品番号が最も小さいものを選ぶ。
- 価格の変更:手順 1 で選んだ商品の価格を X_j 円に変更する。
- 取り消しの判定:取り消しフラグ R_j の値を確認する。
- R_j = 0 の場合:手順 2 の変更をそのまま確定する。
- R_j = 1 の場合:手順 2 で行った価格変更を取り消し、選んだ商品の価格を手順 2 の実行前の値に戻す。
ここで、j 番目の価格調整の「完了」とは、手順 1〜3 をすべて実行し終えた状態を指します。R_j = 1 の場合は手順 3 で価格が元に戻されるため、結果として j 番目の価格調整の前後で各商品の価格は変化しません。ただし、R_j = 1 の場合でも手順 1 の対象商品の選択は行われることに注意してください。
すべての価格調整が終わった後の、全商品の価格の合計を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- T_j \in \{1, 2\} (1 \leq j \leq Q)
- 1 \leq X_j \leq 10^9 (1 \leq j \leq Q)
- R_j \in \{0, 1\} (1 \leq j \leq Q)
- 入力はすべて整数である。
入力
N Q A_1 A_2 \ldots A_N T_1 X_1 R_1 T_2 X_2 R_2 \vdots T_Q X_Q R_Q
- 1 行目には、商品の数 N と価格調整の回数 Q がスペース区切りで与えられる。
- 2 行目には、各商品の初期価格 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
- 3 行目から Q 行にわたって、各価格調整の情報が与えられる。
- 2 + j 行目には、j 番目の調整の種類 T_j(1:最も高い商品を選ぶ、2:最も低い商品を選ぶ)、変更後の価格 X_j、取り消しフラグ R_j(0:変更を確定、1:変更を取り消し)がスペース区切りで与えられる。
出力
すべての価格調整が終わった後の、全商品の価格の合計を 1 行で出力せよ。
入力例 1
3 4 10 20 20 1 5 0 2 30 1 2 15 0 1 8 0
出力例 1
33
入力例 2
4 5 5 5 5 5 2 1 0 1 10 1 1 7 0 2 9 0 2 3 1
出力例 2
26
入力例 3
8 12 100 50 100 75 25 75 50 25 1 60 0 1 40 1 2 90 0 2 10 0 1 30 0 2 80 1 1 110 0 2 55 0 1 65 1 2 20 0 1 45 0 2 70 0
出力例 3
480
入力例 4
15 25 1000 500 1500 500 2000 750 1250 1000 250 1750 250 1500 750 2000 1250 1 300 0 2 1800 0 1 600 1 2 400 0 1 2200 0 1 100 0 2 900 1 2 50 0 1 1600 0 2 1400 0 1 700 1 2 2100 0 1 800 0 2 1200 0 1 500 0 2 1900 1 1 1300 0 2 1100 0 1 200 0 2 1700 0 1 1000 1 2 300 0 1 2400 0 2 600 1 1 150 0
出力例 4
14250
入力例 5
1 1 1000000000 1 1 1
出力例 5
1000000000
Score : 400 pts
Problem Statement
Takahashi runs an online shop that sells N products.
Initially, the price of each product i (1 \leq i \leq N) is set to A_i yen. Before starting sales, Takahashi performs Q "price adjustments" in order from the 1-st to the Q-th, following predetermined rules.
For the j-th (1 \leq j \leq Q) price adjustment, a type T_j, a new price X_j, and a cancellation flag R_j are given, and the following steps 1–3 are executed in this order.
- Selection of target product: Based on the prices of all products at the time the previous price adjustment was completed (or the initial state when j = 1), select one target product.
- If T_j = 1: Select the product with the highest price. If there are multiple products with the highest price, select the one with the smallest product number among them.
- If T_j = 2: Select the product with the lowest price. If there are multiple products with the lowest price, select the one with the smallest product number among them.
- Price change: Change the price of the product selected in step 1 to X_j yen.
- Cancellation check: Check the value of the cancellation flag R_j.
- If R_j = 0: The change made in step 2 is finalized as is.
- If R_j = 1: The price change made in step 2 is cancelled, and the price of the selected product is reverted to the value it had before step 2 was executed.
Here, the "completion" of the j-th price adjustment refers to the state after all of steps 1–3 have been executed. If R_j = 1, the price is reverted in step 3, so as a result, the prices of all products do not change before and after the j-th price adjustment. However, note that even when R_j = 1, the selection of the target product in step 1 is still performed.
Find the total price of all products after all price adjustments have been completed.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- T_j \in \{1, 2\} (1 \leq j \leq Q)
- 1 \leq X_j \leq 10^9 (1 \leq j \leq Q)
- R_j \in \{0, 1\} (1 \leq j \leq Q)
- All input values are integers.
Input
N Q A_1 A_2 \ldots A_N T_1 X_1 R_1 T_2 X_2 R_2 \vdots T_Q X_Q R_Q
- The first line contains the number of products N and the number of price adjustments Q, separated by a space.
- The second line contains the initial prices A_1, A_2, \ldots, A_N of each product, separated by spaces.
- The following Q lines contain the information for each price adjustment.
- The (2 + j)-th line contains the type T_j of the j-th adjustment (1: select the most expensive product, 2: select the cheapest product), the new price X_j, and the cancellation flag R_j (0: finalize the change, 1: cancel the change), separated by spaces.
Output
Output in one line the total price of all products after all price adjustments have been completed.
Sample Input 1
3 4 10 20 20 1 5 0 2 30 1 2 15 0 1 8 0
Sample Output 1
33
Sample Input 2
4 5 5 5 5 5 2 1 0 1 10 1 1 7 0 2 9 0 2 3 1
Sample Output 2
26
Sample Input 3
8 12 100 50 100 75 25 75 50 25 1 60 0 1 40 1 2 90 0 2 10 0 1 30 0 2 80 1 1 110 0 2 55 0 1 65 1 2 20 0 1 45 0 2 70 0
Sample Output 3
480
Sample Input 4
15 25 1000 500 1500 500 2000 750 1250 1000 250 1750 250 1500 750 2000 1250 1 300 0 2 1800 0 1 600 1 2 400 0 1 2200 0 1 100 0 2 900 1 2 50 0 1 1600 0 2 1400 0 1 700 1 2 2100 0 1 800 0 2 1200 0 1 500 0 2 1900 1 1 1300 0 2 1100 0 1 200 0 2 1700 0 1 1000 1 2 300 0 1 2400 0 2 600 1 1 150 0
Sample Output 4
14250
Sample Input 5
1 1 1000000000 1 1 1
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は、東西に一列に並んだ N 個の山を眺めています。各山 i ( 1 \leq i \leq N )には、山の色を表す値 C_i と、標高 H_i が定められています。
高橋君は、同じ色の山のペアを「対の山」と呼んでいます。対の山同士は景観上の調和があり、写真映えするため、高橋君はそのようなペアに注目しています。
ただし、対の山 i と山 j ( i < j )の間に、両方の山よりも高い山が存在すると、間の山に遮られて一方から他方が見えなくなってしまいます。具体的には、対の山 i と山 j ( i < j )について、ある k ( i < k < j )が存在して H_k > \max(H_i, H_j) を満たすとき、この対は「見通しが悪い」と言います。
高橋君は、対の山のうち見通しが良いペア、すなわち間にどちらの山よりも高い山が存在しないペアの数を数えたいと考えています。
以下の条件をすべて満たす組 (i, j) ( i < j )の個数を求めてください。
- C_i = C_j
- すべての k ( i < k < j )について H_k \leq \max(H_i, H_j)
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq N
- 1 \leq H_i \leq 10^9
- H_i はすべて異なる(すなわち i \neq j ならば H_i \neq H_j)
- 入力はすべて整数である
入力
N C_1 H_1 C_2 H_2 : C_N H_N
- 1 行目には、山の個数を表す整数 N が与えられる。
- 2 行目から N + 1 行目では、各山の情報が与えられる。
- 1 + i 行目には、山 i の色 C_i と標高 H_i が、スペース区切りで与えられる。
出力
同じ色であり、かつ見通しが良いペアの数を 1 行で出力せよ。
入力例 1
5 1 3 2 5 1 4 1 1 2 2
出力例 1
2
入力例 2
4 1 10 2 20 3 30 4 40
出力例 2
0
入力例 3
12 1 8 2 3 1 10 3 1 2 12 1 6 3 9 2 2 1 11 3 5 2 7 3 4
出力例 3
5
入力例 4
30 1 17 2 5 3 28 1 12 4 30 2 9 5 1 3 24 1 20 4 7 5 26 2 14 6 3 1 29 3 11 6 18 4 2 5 22 2 27 7 6 1 15 7 25 3 4 6 21 4 10 5 19 2 8 7 23 6 13 1 16
出力例 4
10
入力例 5
1 1 1
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi is looking at N mountains lined up in a row from east to west. Each mountain i (1 \leq i \leq N) has a color C_i and an elevation H_i.
Takahashi calls a pair of mountains of the same color a "matching pair." Since matching pairs harmonize and look photogenic, Takahashi is interested in them.
However, if there is a mountain between mountain i and mountain j (i < j) that is taller than both of them, it blocks the view, making them invisible to each other. Specifically, for a matching pair i and j (i < j), if there exists some k (i < k < j) such that H_k > \max(H_i, H_j), this pair is said to have a "poor view."
Takahashi wants to count the number of matching pairs with a "clear view," meaning pairs where there is no mountain between them that is strictly taller than both.
Find the number of pairs (i, j) (i < j) that satisfy all of the following conditions:
- C_i = C_j
- For all k (i < k < j), H_k \leq \max(H_i, H_j)
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq N
- 1 \leq H_i \leq 10^9
- All H_i are distinct (i.e., if i \neq j, then H_i \neq H_j)
- All input values are integers.
Input
N C_1 H_1 C_2 H_2 : C_N H_N
- The first line contains an integer N, representing the number of mountains.
- The next N lines (from the 2nd line to the (N + 1)-th line) provide information about each mountain.
- The (1 + i)-th line contains the color C_i and the elevation H_i of mountain i, separated by a space.
Output
Print the number of pairs of mountains that have the same color and a clear view in a single line.
Sample Input 1
5 1 3 2 5 1 4 1 1 2 2
Sample Output 1
2
Sample Input 2
4 1 10 2 20 3 30 4 40
Sample Output 2
0
Sample Input 3
12 1 8 2 3 1 10 3 1 2 12 1 6 3 9 2 2 1 11 3 5 2 7 3 4
Sample Output 3
5
Sample Input 4
30 1 17 2 5 3 28 1 12 4 30 2 9 5 1 3 24 1 20 4 7 5 26 2 14 6 3 1 29 3 11 6 18 4 2 5 22 2 27 7 6 1 15 7 25 3 4 6 21 4 10 5 19 2 8 7 23 6 13 1 16
Sample Output 4
10
Sample Input 5
1 1 1
Sample Output 5
0