実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。
- 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i を 1 減らし、A_j を 1 増やす。
A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 4 7 3 7
出力例 1
3
以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。
- i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
- i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
- i=4,j=3 として操作を行う。A=(5,6,5,5) になる。
3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。
入力例 2
1 313
出力例 2
0
入力例 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
出力例 3
2499999974
Score : 400 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).
- Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.
Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 4 7 3 7
Sample Output 1
3
By the following three operations, the difference between the minimum and maximum values of A becomes at most one.
- Choose i=2 and j=3 to make A=(4,6,4,7).
- Choose i=4 and j=1 to make A=(5,6,4,6).
- Choose i=4 and j=3 to make A=(5,6,5,5).
You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.
Sample Input 2
1 313
Sample Output 2
0
Sample Input 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
Sample Output 3
2499999974
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
2 次元グリッド上に 2 つの図形 S と T があります。グリッドは正方形のマスからなります。
S は N 行 N 列のグリッド内にあり、S_{i,j} が #
であるようなマス全体からなります。
T も N 行 N 列のグリッド内にあり、T_{i,j} が #
であるようなマス全体からなります。
S と T を 90 度回転及び平行移動の繰り返しによって一致させることができるか判定してください。
制約
- 1 \leq N \leq 200
- S,T は
#
と.
のみからなる - S,T は 1 つ以上
#
を含む
入力
入力は以下の形式で標準入力から与えられる。
N S_{1,1}S_{1,2}\ldots S_{1,N} \vdots S_{N,1}S_{N,2}\ldots S_{N,N} T_{1,1}T_{1,2}\ldots T_{1,N} \vdots T_{N,1}T_{N,2}\ldots T_{N,N}
出力
S と T を90 度回転及び平行移動の繰り返しによって一致させることができるとき Yes
を、そうでないとき No
を出力せよ。
入力例 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
出力例 1
Yes
S を左回りに 90 度回転させ、平行移動することで T に一致させることができます。
入力例 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
出力例 2
No
90 度回転と平行移動の繰り返しによって一致させることはできません。
入力例 3
4 #... ..#. ..#. .... #... #... ..#. ....
出力例 3
Yes
S 及び T は連結とは限りません。
入力例 4
4 #... .##. ..#. .... ##.. #... ..#. ....
出力例 4
No
回転や移動の操作は連結成分ごとにできるわけではなく、S,T 全体に対して行うことに注意してください。
Score : 300 points
Problem Statement
We have two figures S and T on a two-dimensional grid with square cells.
S lies within a grid with N rows and N columns, and consists of the cells where S_{i,j} is #
.
T lies within the same grid with N rows and N columns, and consists of the cells where T_{i,j} is #
.
Determine whether it is possible to exactly match S and T by 90-degree rotations and translations.
Constraints
- 1 \leq N \leq 200
- Each of S and T consists of
#
and.
. - Each of S and T contains at least one
#
.
Input
Input is given from Standard Input in the following format:
N S_{1,1}S_{1,2}\ldots S_{1,N} \vdots S_{N,1}S_{N,2}\ldots S_{N,N} T_{1,1}T_{1,2}\ldots T_{1,N} \vdots T_{N,1}T_{N,2}\ldots T_{N,N}
Output
Print Yes
if it is possible to exactly match S and T by 90-degree rotations and translations, and No
otherwise.
Sample Input 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
Sample Output 1
Yes
We can match S to T by rotating it 90-degrees counter-clockwise and translating it.
Sample Input 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
Sample Output 2
No
It is impossible to match them by 90-degree rotations and translations.
Sample Input 3
4 #... ..#. ..#. .... #... #... ..#. ....
Sample Output 3
Yes
Each of S and T may not be connected.
Sample Input 4
4 #... .##. ..#. .... ##.. #... ..#. ....
Sample Output 4
No
Note that it is not allowed to rotate or translate just a part of a figure; it is only allowed to rotate or translate a whole figure.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
非負整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
A の(添え字が相異なる) K 個の項の和として考えられる非負整数の集合を S とします。
S に含まれる D の倍数の最大値を求めてください。ただし、S に D の倍数が含まれない場合、代わりに -1
と出力してください。
制約
- 1 \leq K \leq N \leq 100
- 1 \leq D \leq 100
- 0 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K D a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
4 2 2 1 2 3 4
出力例 1
6
A から 2 個の項を選ぶ方法を列挙すると
- a_1 と a_2 を選ぶ。選ばれた項の和は 1+2=3 となる。
- a_1 と a_3 を選ぶ。選ばれた項の和は 1+3=4 となる。
- a_1 と a_4 を選ぶ。選ばれた項の和は 1+4=5 となる。
- a_2 と a_3 を選ぶ。選ばれた項の和は 2+3=5 となる。
- a_2 と a_4 を選ぶ。選ばれた項の和は 2+4=6 となる。
- a_3 と a_4 を選ぶ。選ばれた項の和は 3+4=7 となる。
となり、S=\{3,4,5,6,7\} となります。S に含まれる 2 の倍数のうち最大のものは 6 なので、6 と出力します。
入力例 2
3 1 2 1 3 5
出力例 2
-1
この例では S=\{1,3,5\} です。S に含まれる非負整数はいずれも 2 の倍数でないため、-1
と出力します。
Score : 400 points
Problem Statement
You are given a sequence of non-negative integers A=(a_1,a_2,\ldots,a_N).
Let S be the set of non-negative integers that can be the sum of K terms in A (with distinct indices).
Find the greatest multiple of D in S. If there is no multiple of D in S, print -1
instead.
Constraints
- 1 \leq K \leq N \leq 100
- 1 \leq D \leq 100
- 0 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K D a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
4 2 2 1 2 3 4
Sample Output 1
6
Here are all the ways to choose two terms in A.
- Choose a_1 and a_2, whose sum is 1+2=3.
- Choose a_1 and a_3, whose sum is 1+3=4.
- Choose a_1 and a_4, whose sum is 1+4=5.
- Choose a_2 and a_3, whose sum is 2+3=5.
- Choose a_2 and a_4, whose sum is 2+4=6.
- Choose a_3 and a_4, whose sum is 3+4=7.
Thus, we have S=\{3,4,5,6,7\}. The greatest multiple of 2 in S is 6, so you should print 6.
Sample Input 2
3 1 2 1 3 5
Sample Output 2
-1
In this example, we have S=\{1,3,5\}. Nothing in S is a multiple of 2, so you should print -1
.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点の根付き木があります。頂点には 1 から N の番号がついており、根は頂点 1 です。
i 番目の辺は頂点 A_i と B_i を結んでいます。
頂点 i には整数 X_i が書かれています。
Q 個のクエリが与えられます。i 番目のクエリでは整数の組 (V_i,K_i) が与えられるので、次の問題に答えてください。
- 問題:頂点 V_i の部分木に含まれる頂点に書かれた整数のうち、大きい方から K_i 番目の値を求めよ
制約
- 2 \leq N \leq 10^5
- 0\leq X_i\leq 10^9
- 1\leq A_i,B_i\leq N
- 1\leq Q \leq 10^5
- 1\leq V_i\leq N
- 1\leq K_i\leq 20
- 与えられるグラフは木である
- 頂点 V_i の部分木は頂点を K_i 個以上持つ
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 \ldots X_N A_1 B_1 \vdots A_{N-1} B_{N-1} V_1 K_1 \vdots V_Q K_Q
出力
Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
5 2 1 2 3 4 5 1 4 2 1 2 5 3 2 1 2 2 1
出力例 1
4 5
この入力において与えられる木は下図のようなものです。
1 番目のクエリでは、頂点 1 の部分木に含まれる頂点 1,2,3,4,5 に書かれた数のうち大きい方から 2 番目である 4 を出力します。
2 番目のクエリでは、頂点 2 の部分木に含まれる頂点 2,3,5 に書かれた数のうち大きい方から 1 番目である 5 を出力します。
入力例 2
6 2 10 10 10 9 8 8 1 4 2 1 2 5 3 2 6 4 1 4 2 2
出力例 2
9 10
入力例 3
4 4 1 10 100 1000 1 2 2 3 3 4 1 4 2 3 3 2 4 1
出力例 3
1 10 100 1000
Score : 500 points
Problem Statement
We have a rooted tree with N vertices. The vertices are numbered 1 through N, and the root is Vertex 1.
The i-th edge connects Vertices A_i and B_i.
Vertex i has an integer X_i written on it.
You are given Q queries. For the i-th query, given a pair of integers (V_i,K_i), answer the following question.
- Question: among the integers written on the vertices in the subtree rooted at Vertex V_i, find the K_i-th largest value.
Constraints
- 2 \leq N \leq 10^5
- 0\leq X_i\leq 10^9
- 1\leq A_i,B_i\leq N
- 1\leq Q \leq 10^5
- 1\leq V_i\leq N
- 1\leq K_i\leq 20
- The given graph is a tree.
- The subtree rooted at Vertex V_i has K_i or more vertices.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q X_1 \ldots X_N A_1 B_1 \vdots A_{N-1} B_{N-1} V_1 K_1 \vdots V_Q K_Q
Output
Print Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
5 2 1 2 3 4 5 1 4 2 1 2 5 3 2 1 2 2 1
Sample Output 1
4 5
The tree given in this input is shown below.
For the 1-st query, the vertices in the subtree rooted at Vertex 1 are Vertices 1, 2, 3, 4, and 5, so print the 2-nd largest value of the numbers written on these vertices, 4.
For the 2-nd query, the vertices in the subtree rooted at Vertex 2 are Vertices 2, 3, and 5, so print the 1-st largest value of the numbers written on these vertices, 5.
Sample Input 2
6 2 10 10 10 9 8 8 1 4 2 1 2 5 3 2 6 4 1 4 2 2
Sample Output 2
9 10
Sample Input 3
4 4 1 10 100 1000 1 2 2 3 3 4 1 4 2 3 3 2 4 1
Sample Output 3
1 10 100 1000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
2 次元平面上の N 個の相異なる点が与えられます。点 i\, (1 \leq i \leq N) の座標は (x_i,y_i) です。
2 つの点 i,j\, (1 \leq i,j \leq N) の距離を \mathrm{min} (|x_i-x_j|,|y_i-y_j|) 、すなわち x 座標の差と y 座標の差の小さい方と定義します。
異なる 2 つの点の距離の最大値を求めてください。
制約
- 2 \leq N \leq 200000
- 0 \leq x_i,y_i \leq 10^9
- (x_i,y_i) \neq (x_j,y_j) (i \neq j)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
異なる 2 つの点の距離の最大値を出力せよ。
入力例 1
3 0 3 3 1 4 10
出力例 1
4
点 1 と点 2 の距離は 2 、点 1 と点 3 の距離は 4 、点 2 と点 3 の距離は 1 です。よって 4 を出力してください。
入力例 2
4 0 1 0 4 0 10 0 6
出力例 2
0
入力例 3
8 897 729 802 969 765 184 992 887 1 104 521 641 220 909 380 378
出力例 3
801
Score : 500 points
Problem Statement
Given are N distinct points in a two-dimensional plane. Point i (1 \leq i \leq N) has the coordinates (x_i,y_i).
Let us define the distance between two points i and j be \mathrm{min} (|x_i-x_j|,|y_i-y_j|): the smaller of the difference in the x-coordinates and the difference in the y-coordinates.
Find the maximum distance between two different points.
Constraints
- 2 \leq N \leq 200000
- 0 \leq x_i,y_i \leq 10^9
- (x_i,y_i) \neq (x_j,y_j) (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 x_2 y_2 \vdots x_N y_N
Output
Print the maximum distance between two different points.
Sample Input 1
3 0 3 3 1 4 10
Sample Output 1
4
The distances between Points 1 and 2, between Points 1 and 3, and between Points 2 and 3 are 2, 4, and 1, respectively, so your output should be 4.
Sample Input 2
4 0 1 0 4 0 10 0 6
Sample Output 2
0
Sample Input 3
8 897 729 802 969 765 184 992 887 1 104 521 641 220 909 380 378
Sample Output 3
801