実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
# と . からなる H 行 W 列の図形 S,T が与えられます。
図形 S は H 個の文字列として与えられ、 S_i の j 文字目は S の i 行 j 列にある要素を表します。 T についても同様です。
S の列を並べ替えて T と等しくできるか判定してください。
但し、図形 X の列を並べ替えるとは、以下の操作を言います。
- (1,2,\dots,W) の順列 P=(P_1,P_2,\dots,P_W) をひとつ選択する。
- その後、全ての 1 \le i \le H を満たす整数 i について、以下の操作を同時に行う。
- 1 \le j \le W を満たす全ての整数 j について同時に、 X の i 行 j 列にある要素を i 行 P_j 列にある要素に置き換える。
制約
- H,W は整数
- 1 \le H,W
- 1 \le H \times W \le 4 \times 10^5
- S_i,T_i は
#と.からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
出力
S を T と等しくできるなら Yes 、 そうでないなら No と出力せよ。
入力例 1
3 4 ##.# ##.. #... .### ..## ...#
出力例 1
Yes
例えば S の 3,4,2,1 列目をこの順に左から並べ替えた場合、 S を T と等しくできます。
入力例 2
3 3 #.# .#. #.# ##. ##. .#.
出力例 2
No
この入力では、 S を T と等しくすることができません。
入力例 3
2 1 # . # .
出力例 3
Yes
S=T である場合もあります。
入力例 4
8 7 #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. ....### ####... ....### ####... ....### ####... ....### ####...
出力例 4
Yes
Score : 300 points
Problem Statement
You are given patterns S and T consisting of # and ., each with H rows and W columns.
The pattern S is given as H strings, and the j-th character of S_i represents the element at the i-th row and j-th column. The same goes for T.
Determine whether S can be made equal to T by rearranging the columns of S.
Here, rearranging the columns of a pattern X is done as follows.
- Choose a permutation P=(P_1,P_2,\dots,P_W) of (1,2,\dots,W).
- Then, for every integer i such that 1 \le i \le H, simultaneously do the following.
- For every integer j such that 1 \le j \le W, simultaneously replace the element at the i-th row and j-th column of X with the element at the i-th row and P_j-th column.
Constraints
- H and W are integers.
- 1 \le H,W
- 1 \le H \times W \le 4 \times 10^5
- S_i and T_i are strings of length W consisting of
#and..
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
Output
If S can be made equal to T, print Yes; otherwise, print No.
Sample Input 1
3 4 ##.# ##.. #... .### ..## ...#
Sample Output 1
Yes
If you, for instance, arrange the 3-rd, 4-th, 2-nd, and 1-st columns of S in this order from left to right, S will be equal to T.
Sample Input 2
3 3 #.# .#. #.# ##. ##. .#.
Sample Output 2
No
In this input, S cannot be made equal to T.
Sample Input 3
2 1 # . # .
Sample Output 3
Yes
It is possible that S=T.
Sample Input 4
8 7 #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. ....### ####... ....### ####... ....### ####... ....### ####...
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
整数の多重集合 S があります。はじめ S は空です。
Q 個のクエリが与えられるので順に処理してください。 クエリは次の 3 種類のいずれかです。
-
1 x: S に x を 1 個追加する。 -
2 x c: S から x を \mathrm{min}(c, (S に含まれる x の個数 )) 個削除する。 -
3: (S の最大値 )- (S の最小値 ) を出力する。このクエリを処理するとき、 S が空でないことが保証される。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
3のクエリを処理するとき、S は空でない。- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
i 番目のクエリを表す \mathrm{query}_i は以下の 3 種類のいずれかである。
1 x
2 x c
3
出力
3 のクエリに対する答えを順に改行区切りで出力せよ。
入力例 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
出力例 1
1 5 4
多重集合 S は以下のように変化します。
- 1 番目のクエリ : S に 3 を追加する。S は \lbrace3 \rbrace となる。
- 2 番目のクエリ : S に 2 を追加する。S は \lbrace2, 3\rbrace となる。
- 3 番目のクエリ : S = \lbrace 2, 3\rbrace の最大値は 3 、最小値は 2 なので、 3-2=1 を出力する。
- 4 番目のクエリ : S に 2 を追加する。S は \lbrace2,2,3 \rbrace となる。
- 5 番目のクエリ : S に 7 を追加する。S は \lbrace2, 2,3, 7\rbrace となる。
- 6 番目のクエリ : S = \lbrace2,2,3, 7\rbrace の最大値は 7 、最小値は 2 なので、 7-2=5 を出力する。
- 7 番目のクエリ : S に含まれる 2 の個数は 2 個なので、 \mathrm{min(2,3)} = 2 個の 2 を S から削除する。S は \lbrace3, 7\rbrace となる。
- 8 番目のクエリ : S = \lbrace3, 7\rbrace の最大値は 7 、最小値は 3 なので、 7-3=4 を出力する。
入力例 2
4 1 10000 1 1000 2 100 3 1 10
出力例 2
クエリ 3 が含まれない場合、何も出力してはいけません。
Score : 300 points
Problem Statement
We have a multiset of integers S, which is initially empty.
Given Q queries, process them in order. Each query is of one of the following types.
-
1 x: Insert an x into S. -
2 x c: Remove an x from S m times, where m = \mathrm{min}(c,( the number of x's contained in S)). -
3: Print ( maximum value of S)-( minimum value of S). It is guaranteed that S is not empty when this query is given.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
- When a query of type
3is given, S is not empty. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
\mathrm{query}_i, which denotes the i-th query, is in one of the following formats:
1 x
2 x c
3
Output
Print the answers for the queries of type 3 in the given order, separated by newlines.
Sample Input 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
Sample Output 1
1 5 4
The multiset S transitions as follows.
- 1-st query: insert 3 into S. S is now \lbrace 3 \rbrace.
- 2-nd query: insert 2 into S. S is now \lbrace 2, 3 \rbrace.
- 3-rd query: the maximum value of S = \lbrace 2, 3\rbrace is 3 and its minimum value is 2, so print 3-2=1.
- 4-th query: insert 2 into S. S is now \lbrace 2,2,3 \rbrace.
- 5-th query: insert 7 into S. S is now \lbrace 2, 2,3, 7\rbrace.
- 6-th query: the maximum value of S = \lbrace 2,2,3, 7\rbrace is 7 and its minimum value is 2, so print 7-2=5.
- 7-th query: since there are two 2's in S and \mathrm{min(2,3)} = 2, remove 2 from S twice. S is now \lbrace 3, 7\rbrace.
- 8-th query: the maximum value of S = \lbrace 3, 7\rbrace is 7 and its minimum value is 3, so print 7-3=4.
Sample Input 2
4 1 10000 1 1000 2 100 3 1 10
Sample Output 2
If the given queries do not contain that of type 3, nothing should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
以下のような、無限に広い六角形のグリッドがあります。最初、全てのマスは白です。

ある六角形のマスは 2 つの整数 i,j を用いて (i,j) と表されます。
マス (i,j) は以下の 6 つのマスと隣接します。
- (i-1,j-1)
- (i-1,j)
- (i,j-1)
- (i,j+1)
- (i+1,j)
- (i+1,j+1)
高橋くんは、 N 個のマス (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) を黒く塗りました。
黒いマスがいくつの連結成分をなすか求めてください。
ただし、ある 2 つの黒いマスが同じ連結成分に属するとは、この 2 つの黒いマスの間をいくつかの隣接する黒いマスを辿って移動できることを指します。
制約
- 入力は全て整数
- 1 \le N \le 1000
- |X_i|,|Y_i| \le 1000
- (X_i,Y_i) は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
6 -1 -1 0 1 0 2 1 0 1 2 2 0
出力例 1
3
高橋くんがマスを黒く塗った後、グリッドは下図の状態になります。

黒いマスがなす連結成分は以下の 3 つです。
- (-1,-1)
- (1,0),(2,0)
- (0,1),(0,2),(1,2)
入力例 2
4 5 0 4 1 -3 -4 -2 -5
出力例 2
4
入力例 3
5 2 1 2 -1 1 0 3 1 1 -1
出力例 3
1
Score : 400 points
Problem Statement
We have an infinite hexagonal grid shown below. Initially, all squares are white.

A hexagonal cell is represented as (i,j) with two integers i and j.
Cell (i,j) is adjacent to the following six cells:
- (i-1,j-1)
- (i-1,j)
- (i,j-1)
- (i,j+1)
- (i+1,j)
- (i+1,j+1)
Takahashi has painted N cells (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) black.
Find the number of connected components formed by the black cells.
Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell.
Constraints
- All values in the input are integers.
- 1 \le N \le 1000
- |X_i|,|Y_i| \le 1000
- The pairs (X_i,Y_i) are distinct.
Input
The 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 answer as an integer.
Sample Input 1
6 -1 -1 0 1 0 2 1 0 1 2 2 0
Sample Output 1
3
After Takahashi paints cells black, the grid looks as shown below.

The black squares form the following three connected components:
- (-1,-1)
- (1,0),(2,0)
- (0,1),(0,2),(1,2)
Sample Input 2
4 5 0 4 1 -3 -4 -2 -5
Sample Output 2
4
Sample Input 3
5 2 1 2 -1 1 0 3 1 1 -1
Sample Output 3
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
N 頂点からなる木が与えられます。頂点は 1 から N までの番号がついており、 i 番目の辺は頂点 A_i, B_i を結んでいます。
長さ N の正整数列 C = (C_1, C_2, \ldots ,C_N) が与えられます。d(a, b) を頂点 a, b の間にある辺の数とし、 x = 1, 2, \ldots ,N に対して \displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i)) とします。\displaystyle \min_{1 \leq v \leq N} f(v) を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i, B_i \leq N
- 与えられるグラフは木である。
- 1 \leq C_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
C_1 C_2 \cdots C_N
出力
答えを一行に出力せよ。
入力例 1
4 1 2 1 3 2 4 1 1 1 2
出力例 1
5
例として、 f(1) を求めることを考えます。d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2 です。
よって、 f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6 となります。
同様に、 f(2) = 5, f(3) = 9, f(4) = 6 です。f(2) が最小なので 5 を出力します。
入力例 2
2 2 1 1 1000000000
出力例 2
1
f(2) = 1 で、これが最小です。
入力例 3
7 7 3 2 5 2 4 3 1 3 6 2 1 2 7 6 9 3 4 6
出力例 3
56
Score: 475 points
Problem Statement
You are given a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects vertices A_i and B_i.
You are also given a sequence of positive integers C = (C_1, C_2, \ldots ,C_N) of length N. Let d(a, b) be the number of edges between vertices a and b, and for x = 1, 2, \ldots, N, let \displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i)). Find \displaystyle \min_{1 \leq v \leq N} f(v).
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i, B_i \leq N
- The given graph is a tree.
- 1 \leq C_i \leq 10^9
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
C_1 C_2 \cdots C_N
Output
Print the answer in one line.
Sample Input 1
4 1 2 1 3 2 4 1 1 1 2
Sample Output 1
5
For example, consider calculating f(1). We have d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2.
Thus, f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6.
Similarly, f(2) = 5, f(3) = 9, f(4) = 6. Since f(2) is the minimum, print 5.
Sample Input 2
2 2 1 1 1000000000
Sample Output 2
1
f(2) = 1, which is the minimum.
Sample Input 3
7 7 3 2 5 2 4 3 1 3 6 2 1 2 7 6 9 3 4 6
Sample Output 3
56
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
座標平面上に高橋君と荷物があります。
高橋君は現在 (X_A,Y_A) におり、荷物は (X_B,Y_B) にあります。 高橋君は荷物を (X_C,Y_C) まで運びたいです。
高橋君は (x,y) にいるとき、1 回の行動で次のいずれかの動きをすることができます。
- (x+1,y) に移動する。移動前の時点で荷物が (x+1,y) にあった時、荷物を (x+2,y) に移動させる。
- (x-1,y) に移動する。移動前の時点で荷物が (x-1,y) にあった時、荷物を (x-2,y) に移動させる。
- (x,y+1) に移動する。移動前の時点で荷物が (x,y+1) にあった時、荷物を (x,y+2) に移動させる。
- (x,y-1) に移動する。移動前の時点で荷物が (x,y-1) にあった時、荷物を (x,y-2) に移動させる。
荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を求めてください。
制約
- -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
- (X_A,Y_A)\neq (X_B,Y_B)
- (X_B,Y_B)\neq (X_C,Y_C)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
X_A Y_A X_B Y_B X_C Y_C
出力
荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を出力せよ。
入力例 1
1 2 3 3 0 5
出力例 1
9
高橋君は次のように行動することで 9 回で荷物を (0,5) に運ぶことができます。
- (2,2) へ移動する。
- (3,2) へ移動する。
- (3,3) へ移動する。荷物は (3,4) に移動する。
- (3,4) へ移動する。荷物は (3,5) に移動する。
- (4,4) へ移動する。
- (4,5) へ移動する。
- (3,5) へ移動する。荷物は (2,5) に移動する。
- (2,5) へ移動する。荷物は (1,5) に移動する。
- (1,5) へ移動する。荷物は (0,5) に移動する。
8 回以下で荷物を (0,5) に運ぶことができないので、9 を出力します。
入力例 2
0 0 1 0 -1 0
出力例 2
6
入力例 3
-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000
出力例 3
800000000000000003
Score : 525 points
Problem Statement
Takahashi and a cargo are on a coordinate plane.
Takahashi is currently at (X_A,Y_A), and the cargo is at (X_B,Y_B). He wants to move the cargo to (X_C,Y_C).
When he is at (x,y), he can make one of the following moves in a single action.
- Move to (x+1,y). If the cargo is at (x+1,y) before the move, move it to (x+2,y).
- Move to (x-1,y). If the cargo is at (x-1,y) before the move, move it to (x-2,y).
- Move to (x,y+1). If the cargo is at (x,y+1) before the move, move it to (x,y+2).
- Move to (x,y-1). If the cargo is at (x,y-1) before the move, move it to (x,y-2).
Find the minimum number of actions required to move the cargo to (X_C,Y_C).
Constraints
- -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
- (X_A,Y_A)\neq (X_B,Y_B)
- (X_B,Y_B)\neq (X_C,Y_C)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X_A Y_A X_B Y_B X_C Y_C
Output
Print the minimum number of actions required to move the cargo to (X_C,Y_C).
Sample Input 1
1 2 3 3 0 5
Sample Output 1
9
Takahashi can move the cargo to (0,5) in nine actions as follows.
- Move to (2,2).
- Move to (3,2).
- Move to (3,3). The cargo moves to (3,4).
- Move to (3,4). The cargo moves to (3,5).
- Move to (4,4).
- Move to (4,5).
- Move to (3,5). The cargo moves to (2,5).
- Move to (2,5). The cargo moves to (1,5).
- Move to (1,5). The cargo moves to (0,5).
It is impossible to move the cargo to (0,5) in eight or fewer actions, so you should print 9.
Sample Input 2
0 0 1 0 -1 0
Sample Output 2
6
Sample Input 3
-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000
Sample Output 3
800000000000000003