Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 a, b, c が与えられます。b がこれらの整数の中央値であるかどうか判定してください。
制約
- 1 \leq a, b, c \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
a b c
出力
b が与えられた整数の中央値であるならば Yes
、そうでないならば No
と出力せよ。
入力例 1
5 3 2
出力例 1
Yes
与えられた整数を小さい順に並べると 2, 3, 5 となり、b はこれらの整数の中央値です。
入力例 2
2 5 3
出力例 2
No
b は与えられた整数の中央値ではありません。
入力例 3
100 100 100
出力例 3
Yes
Score : 100 points
Problem Statement
Given integers a, b, and c, determine if b is the median of these integers.
Constraints
- 1 \leq a, b, c \leq 100
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
a b c
Output
If b is the median of the given integers, then print Yes
; otherwise, print No
.
Sample Input 1
5 3 2
Sample Output 1
Yes
The given integers are 2, 3, 5 when sorted in ascending order, of which b is the median.
Sample Input 2
2 5 3
Sample Output 2
No
b is not the median of the given integers.
Sample Input 3
100 100 100
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
A
を N 個、B
を N 個、…、Z
を N 個この順に繋げて得られる文字列の先頭から X 番目の文字を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq X \leq N\times 26
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X
出力
答えを出力せよ。
入力例 1
1 3
出力例 1
C
得られる文字列は ABCDEFGHIJKLMNOPQRSTUVWXYZ
です。先頭から 3 番目の文字は C
です。
入力例 2
2 12
出力例 2
F
得られる文字列は AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ
です。先頭から 12 番目の文字は F
です。
Score : 100 points
Problem Statement
Find the X-th character from the beginning of the string that is obtained by concatenating these characters: N copies of A
's, N copies of B
's, …, and N copies of Z
's, in this order.
Constraints
- 1 \leq N \leq 100
- 1 \leq X \leq N\times 26
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X
Output
Print the answer.
Sample Input 1
1 3
Sample Output 1
C
We obtain the string ABCDEFGHIJKLMNOPQRSTUVWXYZ
, whose 3-rd character from the beginning is C
.
Sample Input 2
2 12
Sample Output 2
F
We obtain the string AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ
, whose 12-th character from the beginning is F
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数からなる長さ N の数列 A=(A_1,\ldots,A_N) があります。どの隣接する 2 項の値も相異なります。
この数列に対し、次の操作によりいくつか数を挿入します。
- 数列 A のどの隣接する 2 項の差の絶対値も 1 であるとき、操作を終了する。
- 数列 A の先頭から見て、隣接する 2 項の差の絶対値が 1 でない最初の箇所を A_i,A_{i+1} とする。
- A_i < A_{i+1} ならば、A_i と A_{i+1} の間に、A_i+1,A_i+2,\ldots,A_{i+1}-1 を挿入する。
- A_i > A_{i+1} ならば、A_i と A_{i+1} の間に、A_i-1,A_i-2,\ldots,A_{i+1}+1 を挿入する。
- 手順 1 に戻る。
操作が終了したときの数列を出力してください。
制約
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- A_i \neq A_{i+1}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
操作が終了したときの数列の各要素を空白区切りで出力せよ。
入力例 1
4 2 5 1 2
出力例 1
2 3 4 5 4 3 2 1 2
最初、数列は (2,5,1,2) です。操作は以下のように行われます。
- 1 項目の 2 と 2 項目の 5 の間に 3,4 を挿入する。数列は (2,3,4,5,1,2) となる。
- 4 項目の 5 と 5 項目の 1 の間に 4,3,2 を挿入する。数列は (2,3,4,5,4,3,2,1,2) となる。
入力例 2
6 3 4 5 6 5 4
出力例 2
3 4 5 6 5 4
一度も挿入が行われないこともあります。
Score : 200 points
Problem Statement
We have a sequence of length N consisting of positive integers: A=(A_1,\ldots,A_N). Any two adjacent terms have different values.
Let us insert some numbers into this sequence by the following procedure.
- If every pair of adjacent terms in A has an absolute difference of 1, terminate the procedure.
- Let A_i, A_{i+1} be the pair of adjacent terms nearest to the beginning of A whose absolute difference is not 1.
- If A_i < A_{i+1}, insert A_i+1,A_i+2,\ldots,A_{i+1}-1 between A_i and A_{i+1}.
- If A_i > A_{i+1}, insert A_i-1,A_i-2,\ldots,A_{i+1}+1 between A_i and A_{i+1}.
- Return to step 1.
Print the sequence when the procedure ends.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- A_i \neq A_{i+1}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the terms in the sequence when the procedure ends, separated by spaces.
Sample Input 1
4 2 5 1 2
Sample Output 1
2 3 4 5 4 3 2 1 2
The initial sequence is (2,5,1,2). The procedure goes as follows.
- Insert 3,4 between the first term 2 and the second term 5, making the sequence (2,3,4,5,1,2).
- Insert 4,3,2 between the fourth term 5 and the fifth term 1, making the sequence (2,3,4,5,4,3,2,1,2).
Sample Input 2
6 3 4 5 6 5 4
Sample Output 2
3 4 5 6 5 4
No insertions may be performed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。
- X の 10^i の位以下を四捨五入する。
- 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
- 具体例を挙げる。
- 273 の 10^1 の位以下を四捨五入すれば 300 となる。
- 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
- 100 の 10^9 の位以下を四捨五入すれば 0 となる。
- 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。
制約
- X,K は整数
- 0 \le X < 10^{15}
- 1 \le K \le 15
入力
入力は以下の形式で標準入力から与えられる。
X K
出力
答えを整数として出力せよ。
入力例 1
2048 2
出力例 1
2100
操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。
入力例 2
1 15
出力例 2
0
入力例 3
999 3
出力例 3
1000
入力例 4
314159265358979 12
出力例 4
314000000000000
X は 32bit 整数型に収まらない可能性があります。
Score : 200 points
Problem Statement
Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.
- Round X off to the nearest 10^i.
- Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
- Here are some examples:
- Rounding 273 off to the nearest 10^2 yields 300.
- Rounding 999 off to the nearest 10^3 yields 1000.
- Rounding 100 off to the nearest 10^{10} yields 0.
- Rounding 1015 off to the nearest 10^1 yields 1020.
Constraints
- X and K are integers.
- 0 \le X < 10^{15}
- 1 \le K \le 15
Input
The input is given from Standard Input in the following format:
X K
Output
Print the answer as an integer.
Sample Input 1
2048 2
Sample Output 1
2100
X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.
Sample Input 2
1 15
Sample Output 2
0
Sample Input 3
999 3
Sample Output 3
1000
Sample Input 4
314159265358979 12
Sample Output 4
314000000000000
X may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は数直線上の座標 0 の位置にいます。
これから高橋君は N 回のジャンプを行います。i \, (1 \leq i \leq N) 回目のジャンプでは、正の方向に a_i または b_i 移動します。
N 回のジャンプの後に座標 X の位置にいるようにすることはできますか?
制約
- 1 \leq N \leq 100
- 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
- 1 \leq X \leq 10000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X a_1 b_1 \vdots a_N b_N
出力
N 回のジャンプの後に座標 X の位置にいるようにすることができるならば Yes
と、そうでないなら No
と出力せよ。
入力例 1
2 10 3 6 4 5
出力例 1
Yes
1 回目のジャンプでは b_1 (= 6) 移動し、2 回目のジャンプでは a_2 (= 4) 移動することで、座標 X (= 10) の位置にいるようにすることができます。
入力例 2
2 10 10 100 10 100
出力例 2
No
1 回目のジャンプの後に座標 X (= 10) の位置にいるようにすることはできますが、全てのジャンプの後に座標 X (= 10) の位置にいるようにすることはできません。
入力例 3
4 12 1 8 5 7 3 4 2 6
出力例 3
Yes
Score : 300 points
Problem Statement
Takahashi is standing at the coordinate 0 on a number line.
He will now perform N jumps. In the i-th jump (1 \leq i \leq N), he moves a_i or b_i in the positive direction.
Is it possible for him to be at the coordinate X after N jumps?
Constraints
- 1 \leq N \leq 100
- 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
- 1 \leq X \leq 10000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X a_1 b_1 \vdots a_N b_N
Output
If it is possible for Takahashi to be at the coordinate X after N jumps, print Yes
; otherwise, print No
.
Sample Input 1
2 10 3 6 4 5
Sample Output 1
Yes
By moving b_1 (= 6) in the first jump and a_2 (= 4) in the second jump, he can be at the coordinate X (= 10).
Sample Input 2
2 10 10 100 10 100
Sample Output 2
No
He can be at the coordinate X (= 10) after the first jump, but not after all jumps.
Sample Input 3
4 12 1 8 5 7 3 4 2 6
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
10^9 階建てのビルがあり、N 本のはしごがかかっています。
ビルの 1 階にいる高橋君ははしごを繰り返し使って(0 回でもよい)できるだけ高い階へ上りたいと考えています。
はしごには 1 から N までの番号がついており、はしご i は A_i 階と B_i 階を結んでいます。はしご i を利用すると A_i 階から B_i 階へ、または B_i 階から A_i 階へ双方向に移動することができますが、それ以外の階の間の移動は行うことはできません。
また、高橋君は同じ階での移動は自由に行うことができますが、はしご以外の方法で他の階へ移動することはできません。
高橋君は最高で何階へ上ることができますか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- A_i \neq B_i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \ldots A_N B_N
出力
答えを出力せよ。
入力例 1
4 1 4 4 3 4 10 8 3
出力例 1
10
はしご 1 で 4 階に進み、はしご 3 で 10 階に進むことにより、10 階にたどり着くことができます。
入力例 2
6 1 3 1 5 1 12 3 5 3 12 5 12
出力例 2
12
入力例 3
3 500000000 600000000 600000000 700000000 700000000 800000000
出力例 3
1
他の階への移動ができない場合もあります。
Score : 300 points
Problem Statement
There is a 10^9-story building with N ladders.
Takahashi, who is on the 1-st (lowest) floor, wants to reach the highest floor possible by using ladders (possibly none).
The ladders are numbered from 1 to N, and ladder i connects the A_i-th and B_i-th floors. One can use ladder i in either direction to move from the A_i-th floor to the B_i-th floor or vice versa, but not between other floors.
Takahashi can freely move within the same floor, but cannot move between floors without using ladders.
What is the highest floor Takahashi can reach?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- A_i \neq B_i
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \ldots A_N B_N
Output
Print an integer representing the answer.
Sample Input 1
4 1 4 4 3 4 10 8 3
Sample Output 1
10
He can reach the 10-th floor by using ladder 1 to get to the 4-th floor and then ladder 3 to get to the 10-th floor.
Sample Input 2
6 1 3 1 5 1 12 3 5 3 12 5 12
Sample Output 2
12
Sample Input 3
3 500000000 600000000 600000000 700000000 700000000 800000000
Sample Output 3
1
He may be unable to move between floors.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
1 から N が書かれた N 枚のカードが裏向きで積まれた山札があり、上から i 枚目のカードには整数 P_i が書かれています。
この山札を使って、以下の行動を N ターン繰り返します。
- 山札の一番上のカードを引いて、そこに書かれた整数を X とする。
- 場に見えている表向きのカードであって書かれた整数が X 以上であるもののうち、書かれた整数が最小のものの上に、引いたカードを表向きで重ねる。もし場にそのようなカードがなければ、引いたカードをどれにも重ねずに表向きで場に置く。
- その後、表向きのカードが K 枚重ねられた山が場にあればその山のカードを全て食べる。食べられたカードは全て場から消滅する。
各カードについて、何ターン目に食べられるか、あるいは最後まで食べられないかを求めてください。
制約
- 入力は全て整数
- 1 \le K \le N \le 2 \times 10^5
- P は (1,2,\dots,N) の順列 ( (1,2,\dots,N) を並べ替えて得られる列 ) である
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \dots P_N
出力
N 行出力せよ。
そのうち i (1 \le i \le N) 行目には、整数 i が書かれたカードについて、以下のように出力せよ。
- もし i が書かれたカードが食べられるなら、それが何ターン目であるかを整数として出力せよ。
- そうでないなら -1 と出力せよ。
入力例 1
5 2 3 5 2 1 4
出力例 1
4 3 3 -1 4
この入力では、 P=(3,5,2,1,4),K=2 です。
- 1 ターン目に、 3 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
- 2 ターン目に、 5 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
- 3 ターン目に、 2 が書かれたカードが 3 が書かれたカードの上に表向きで重ねられます。
- この時点で上から 2,3 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
- 4 ターン目に、 1 が書かれたカードが 5 が書かれたカードの上に表向きで重ねられます。
- この時点で上から 1,5 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
- 5 ターン目に、 4 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
- 4 が書かれたカードは、最後まで食べられませんでした。
入力例 2
5 1 1 2 3 4 5
出力例 2
1 2 3 4 5
K=1 である場合、全てのカードは場に置かれたターンに食べられます。
入力例 3
15 3 3 14 15 9 2 6 5 13 1 7 10 11 8 12 4
出力例 3
9 9 9 15 15 6 -1 -1 6 -1 -1 -1 -1 6 15
Score : 400 points
Problem Statement
There is a deck consisting of N face-down cards with an integer from 1 through N written on them. The integer on the i-th card from the top is P_i.
Using this deck, you will perform N moves, each consisting of the following steps:
- Draw the topmost card from the deck. Let X be the integer written on it.
- Stack the drawn card, face up, onto the card with the smallest integer among the face-up topmost cards on the table with an integer greater than or equal to X written on them. If there is no such card on the table, put the drawn card on the table, face up, without stacking it onto any card.
- Then, if there is a pile consisting of K face-up cards on the table, eat all those cards. The eaten cards all disappear from the table.
For each card, find which of the N moves eats it. If the card is not eaten until the end, report that fact.
Constraints
- All values in input are integers.
- 1 \le K \le N \le 2 \times 10^5
- P is a permutation of (1,2,\dots,N) (i.e. a sequence obtained by rearranging (1,2,\dots,N)).
Input
Input is given from Standard Input in the following format:
N K P_1 P_2 \dots P_N
Output
Print N lines.
The i-th line (1 \le i \le N) should describe the card with the integer i written on it. Specifically,
- if the card with i written on it is eaten in the x-th move, print x;
- if that card is not eaten in any move, print -1.
Sample Input 1
5 2 3 5 2 1 4
Sample Output 1
4 3 3 -1 4
In this input, P=(3,5,2,1,4) and K=2.
- In the 1-st move, the card with 3 written on it is put on the table, face up, without stacked onto any card.
- In the 2-nd move, the card with 5 written on it is put on the table, face up, without stacked onto any card.
- In the 3-rd move, the card with 2 written on it is stacked, face up, onto the card with 3 written on it.
- Now there is a pile consisting of K=2 face-up cards, on which 2 and 3 from the top are written, so these cards are eaten.
- In the 4-th move, the card with 1 written on it is stacked, face up, onto the card with 5 written on it.
- Now there is a pile consisting of K=2 face-up cards, on which 1 and 5 from the top are written, so these cards are eaten.
- In the 5-th move, the card with 4 written on it is put on the table, face up, without stacked onto any card.
- The card with 4 written on it was not eaten until the end.
Sample Input 2
5 1 1 2 3 4 5
Sample Output 2
1 2 3 4 5
If K=1, every card is eaten immediately after put on the table within a single move.
Sample Input 3
15 3 3 14 15 9 2 6 5 13 1 7 10 11 8 12 4
Sample Output 3
9 9 9 15 15 6 -1 -1 6 -1 -1 -1 -1 6 15
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i,j) で表します。 (i,j)\ (1\leq i\leq H,1\leq j\leq W) には 1 以上 N 以下の整数 A _ {i,j} が書かれています。
整数 h,w が与えられます。0\leq k\leq H-h,0\leq l\leq W-w を満たすすべての (k,l) の組について、次の問題を解いてください。
- k\lt i\leq k+h,l\lt j\leq l+w を満たす (i,j) を塗りつぶしたとき、塗りつぶされていないマスに書かれている数が何種類あるか求めよ。
ただし、問題を解く際に実際にマスを塗りつぶすことはない(各問題が独立である)ことに注意してください。
制約
- 1 \leq H,W,N \leq 300
- 1 \leq h \leq H
- 1 \leq w \leq W
- (h,w)\neq(H,W)
- 1 \leq A _ {i,j} \leq N\ (1\leq i\leq H,1\leq j\leq W)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N h w A _ {1,1} A _ {1,2} \dots A _ {1,W} A _ {2,1} A _ {2,2} \dots A _ {2,W} \vdots A _ {H,1} A _ {H,2} \dots A _ {H,W}
出力
(k,l) に対する答えを \operatorname{ans}_{k,l} として、以下の形式で出力せよ。
\operatorname{ans} _ {0,0} \operatorname{ans} _ {0,1} \dots \operatorname{ans} _ {0,W-w} \operatorname{ans} _ {1,0} \operatorname{ans} _ {1,1} \dots \operatorname{ans} _ {1,W-w} \vdots \operatorname{ans} _ {H-h,0} \operatorname{ans} _ {H-h,1} \dots \operatorname{ans} _ {H-h,W-w}
入力例 1
3 4 5 2 2 2 2 1 1 3 2 5 3 3 4 4 3
出力例 1
4 4 3 5 3 4
与えられた盤面は下の図のようになります。
例えば、(k,l)=(0,0) のときは塗りつぶされていないマスに書かれている数は 1,3,4,5 の 4 種類なので、4 が答えになります。
入力例 2
5 6 9 3 4 7 1 5 3 9 5 4 5 4 5 1 2 6 1 6 2 9 7 4 7 1 5 8 8 3 4 3 3 5 3
出力例 2
8 8 7 8 9 7 8 9 8
入力例 3
9 12 30 4 7 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20 20 2 2 5 9 10 9 9 23 2 29 29 29 29 29 28 28 26 26 26 15 2 29 29 29 29 29 25 25 26 26 26 15 2 29 29 29 29 29 25 25 8 25 15 15 2 18 18 18 18 1 27 27 25 25 16 16 2 19 22 1 1 1 7 3 7 7 7 7 2 19 22 22 6 6 21 21 21 7 7 7 2 19 22 22 22 22 21 21 21 24 24 24
出力例 3
21 20 19 20 18 17 20 19 18 19 17 15 21 19 20 19 18 16 21 19 19 18 19 18 20 18 18 18 19 18 18 16 17 18 19 17
Score : 500 points
Problem Statement
You have a grid with H rows from top to bottom and W columns from left to right. We denote by (i, j) the square at the i-th row from the top and j-th column from the left. (i,j)\ (1\leq i\leq H,1\leq j\leq W) has an integer A _ {i,j} between 1 and N written on it.
You are given integers h and w. For all pairs (k,l) such that 0\leq k\leq H-h and 0\leq l\leq W-w, solve the following problem:
- If you black out the squares (i,j) such that k\lt i\leq k+h and l\lt j\leq l+w, how many distinct integers are written on the squares that are not blacked out?
Note, however, that you do not actually black out the squares (that is, the problems are independent).
Constraints
- 1 \leq H,W,N \leq 300
- 1 \leq h \leq H
- 1 \leq w \leq W
- (h,w)\neq(H,W)
- 1 \leq A _ {i,j} \leq N\ (1\leq i\leq H,1\leq j\leq W)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
H W N h w A _ {1,1} A _ {1,2} \dots A _ {1,W} A _ {2,1} A _ {2,2} \dots A _ {2,W} \vdots A _ {H,1} A _ {H,2} \dots A _ {H,W}
Output
Print the answers in the following format, where \operatorname{ans}_{k,l} denotes the answer to (k, l):
\operatorname{ans} _ {0,0} \operatorname{ans} _ {0,1} \dots \operatorname{ans} _ {0,W-w} \operatorname{ans} _ {1,0} \operatorname{ans} _ {1,1} \dots \operatorname{ans} _ {1,W-w} \vdots \operatorname{ans} _ {H-h,0} \operatorname{ans} _ {H-h,1} \dots \operatorname{ans} _ {H-h,W-w}
Sample Input 1
3 4 5 2 2 2 2 1 1 3 2 5 3 3 4 4 3
Sample Output 1
4 4 3 5 3 4
The given grid is as follows:
For example, when (k,l)=(0,0), four distinct integers 1,3,4, and 5 are written on the squares that are not blacked out, so 4 is the answer.
Sample Input 2
5 6 9 3 4 7 1 5 3 9 5 4 5 4 5 1 2 6 1 6 2 9 7 4 7 1 5 8 8 3 4 3 3 5 3
Sample Output 2
8 8 7 8 9 7 8 9 8
Sample Input 3
9 12 30 4 7 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20 20 2 2 5 9 10 9 9 23 2 29 29 29 29 29 28 28 26 26 26 15 2 29 29 29 29 29 25 25 26 26 26 15 2 29 29 29 29 29 25 25 8 25 15 15 2 18 18 18 18 1 27 27 25 25 16 16 2 19 22 1 1 1 7 3 7 7 7 7 2 19 22 22 6 6 21 21 21 7 7 7 2 19 22 22 22 22 21 21 21 24 24 24
Sample Output 3
21 20 19 20 18 17 20 19 18 19 17 15 21 19 20 19 18 16 21 19 19 18 19 18 20 18 18 18 19 18 18 16 17 18 19 17
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 10^9 マス、横 10^9 マスのマス目があります。上から i 番目、左から j 番目のマスを (i,j) と表記します。
i=1,2,\ldots,N に対し (r_i,c_i) には正整数 x_i が、他の 10^{18}-N 個のマスには 0 が書かれています。
あなたはあるマス (R,C) を選び、 (R,C) と行または列が同じ 2 \times 10^9 - 1 個のマスに書かれた整数の総和 S を求めました。
S として考えられる最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq r_i,c_i,x_i \leq 10^9
- i \neq j ならば (r_i,c_i) \neq (r_j,c_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N r_1 c_1 x_1 \vdots r_N c_N x_N
出力
答えを出力せよ。
入力例 1
4 1 1 2 1 2 9 2 1 8 3 2 3
出力例 1
20
(R,C) として (2,2) を選ぶと S が 20 となります。これが最大値です。
入力例 2
1 1 1000000000 1
出力例 2
1
入力例 3
15 158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784 450968417 430302156 982631932 161735902 880895728 923078537 707723857 189330739 910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016 650287940 839296263 462224593 492601449 384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328 164163676
出力例 3
1510053068
Score : 500 points
Problem Statement
We have a grid with 10^9 rows and 10^9 columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.
For i=1,2,\ldots,N, a positive integer x_i is written on (r_i,c_i). On the other 10^{18}-N squares, 0 is written.
You choose a square (R,C) and compute the sum S of the integers written on the 2 \times 10^9 - 1 squares that share a row or column with (R,C).
Find the maximum possible value of S.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq r_i,c_i,x_i \leq 10^9
- (r_i,c_i) \neq (r_j,c_j) if i \neq j.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N r_1 c_1 x_1 \vdots r_N c_N x_N
Output
Print the answer.
Sample Input 1
4 1 1 2 1 2 9 2 1 8 3 2 3
Sample Output 1
20
If you choose (2,2) as (R,C), then S will be 20, which is the maximum possible value.
Sample Input 2
1 1 1000000000 1
Sample Output 2
1
Sample Input 3
15 158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784 450968417 430302156 982631932 161735902 880895728 923078537 707723857 189330739 910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016 650287940 839296263 462224593 492601449 384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328 164163676
Sample Output 3
1510053068