Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。
N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X 個 (X>0) 存在するならば、X を文字列として扱って S_i+
(
+X+)
を出力する。
制約
- 1 \leq N \leq 2\times 10^5
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
問題文中の指示にしたがって、N 行出力せよ。
入力例 1
5 newfile newfile newfolder newfile newfolder
出力例 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
入力例 2
11 a a a a a a a a a a a
出力例 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Score : 300 points
Problem Statement
For two strings A and B, let A+B denote the concatenation of A and B in this order.
You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:
- if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
- if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+
(
+X+)
, treating X as a string.
Constraints
- 1 \leq N \leq 2\times 10^5
- S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines as specified in the Problem Statement.
Sample Input 1
5 newfile newfile newfolder newfile newfolder
Sample Output 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
Sample Input 2
11 a a a a a a a a a a a
Sample Output 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Time Limit: 2 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A = (A_1, \dots, A_N) が与えられます。
以下の条件を全て満たす整数の組 (i, j, k) の総数を求めてください。
- 1 \leq i, j, k \leq N
- \frac{A_i}{A_j} = A_k
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 6 2 3
出力例 1
2
(i, j, k) = (1, 2, 3), (1, 3, 2) が条件を満たします。
入力例 2
1 2
出力例 2
0
入力例 3
10 1 3 2 4 6 8 2 2 3 7
出力例 3
62
Score : 400 points
Problem Statement
You are given an integer sequence A = (A_1, \dots, A_N) of length N.
Find the number of triplets of integers (i, j, k) satisfying all of the conditions below.
- 1 \leq i, j, k \leq N
- \frac{A_i}{A_j} = A_k
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 6 2 3
Sample Output 1
2
(i, j, k) = (1, 2, 3), (1, 3, 2) satisfy the conditions.
Sample Input 2
1 2
Sample Output 2
0
Sample Input 3
10 1 3 2 4 6 8 2 2 3 7
Sample Output 3
62
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
ある国には N 個の都市と M 個の発電所があります。これらを総称して地点と呼びます。
地点には 1,2,\dots,N+M の番号がつけられており、そのうち都市は地点 1,2,\dots,N で発電所は地点 N+1,N+2,\dots,N+M です。
この国には電線が E 本あり、電線 i ( 1 \le i \le E ) は地点 U_i と地点 V_i を双方向に結びます。
また、ある都市に 電気が通っている とは、ある都市から電線をいくつか辿って少なくともひとつの発電所に辿り着くことができる状態を言います。
今、 Q 個のイベントが起こります。そのうち i ( 1 \le i \le Q ) 番目のイベントでは電線 X_i が切れ、その電線を辿ることができなくなります。一度切れた電線は、その後のイベントにおいても切れたままです。
全てのイベントについて、そのイベントが終わった直後に電気が通っている都市の数を求めてください。
制約
- 入力は全て整数
- 1 \le N,M
- N+M \le 2 \times 10^5
- 1 \le Q \le E \le 5 \times 10^5
- 1 \le U_i < V_i \le N+M
- i \neq j ならば、 U_i \neq U_j または V_i \neq V_j
- 1 \le X_i \le E
- X_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M E U_1 V_1 U_2 V_2 \vdots U_E V_E Q X_1 X_2 \vdots X_Q
出力
Q 行出力せよ。
そのうち i ( 1 \le i \le Q ) 行目には i 番目のイベントが終わった直後に電気が通っている都市の数を出力せよ。
入力例 1
5 5 10 2 3 4 10 5 10 6 9 2 9 4 8 1 7 3 6 8 10 1 8 6 3 5 8 10 2 7
出力例 1
4 4 2 2 2 1
はじめ、全ての都市に電気が通っています。
- 1 番目のイベントによって地点 5 と地点 10 を結ぶ電線 3 が切れます。
- これにより、都市 5 に電気が通らなくなり、電気が通っている都市の数は 4 となります。
- 2 番目のイベントによって地点 2 と地点 9 を結ぶ電線 5 が切れます。
- 3 番目のイベントによって地点 3 と地点 6 を結ぶ電線 8 が切れます。
- これにより、都市 2,3 に電気が通らなくなり、電気が通っている都市の数は 2 となります。
- 4 番目のイベントによって地点 1 と地点 8 を結ぶ電線 10 が切れます。
- 5 番目のイベントによって地点 4 と地点 10 を結ぶ電線 2 が切れます。
- 6 番目のイベントによって地点 1 と地点 7 を結ぶ電線 7 が切れます。
- これにより、都市 1 に電気が通らなくなり、電気が通っている都市の数は 1 となります。
Score : 500 points
Problem Statement
A country has N cities and M power plants, which we collectively call places.
The places are numbered 1,2,\dots,N+M, among which Places 1,2,\dots,N are the cities and Places N+1,N+2,\dots,N+M are the power plants.
This country has E power lines. Power Line i (1 \le i \le E) connects Place U_i and Place V_i bidirectionally.
A city is said to be electrified if one can reach at least one of the power plants from the city using some power lines.
Now, Q events will happen. In the i-th (1 \le i \le Q) event, Power Line X_i breaks, making it unusable. Once a power line breaks, it remains broken in the succeeding events.
Find the number of electrified cities right after each events.
Constraints
- All values in input are integers.
- 1 \le N,M
- N+M \le 2 \times 10^5
- 1 \le Q \le E \le 5 \times 10^5
- 1 \le U_i < V_i \le N+M
- If i \neq j, then U_i \neq U_j or V_i \neq V_j.
- 1 \le X_i \le E
- X_i are distinct.
Input
Input is given from Standard Input in the following format:
N M E U_1 V_1 U_2 V_2 \vdots U_E V_E Q X_1 X_2 \vdots X_Q
Output
Print Q lines.
The i-th line should contain the number of electrified cities right after the i-th event.
Sample Input 1
5 5 10 2 3 4 10 5 10 6 9 2 9 4 8 1 7 3 6 8 10 1 8 6 3 5 8 10 2 7
Sample Output 1
4 4 2 2 2 1
Initially, all cities are electrified.
- The 1-st event breaks Power Line 3 that connects Point 5 and Point 10.
- Now City 5 is no longer electrified, while 4 cities remain electrified.
- The 2-nd event breaks Power Line 5 that connects Point 2 and Point 9.
- The 3-rd event breaks Power Line 8 that connects Point 3 and Point 6.
- Now Cities 2 and 3 are no longer electrified, while 2 cities remain electrified.
- The 4-th event breaks Power Line 10 that connects Point 1 and Point 8.
- The 5-th event breaks Power Line 2 that connects Point 4 and Point 10.
- The 6-th event breaks Power Line 7 that connects Point 1 and Point 7.
- Now City 1 is no longer electrified, while 1 city remains electrified.
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
H 行 W 列のマス目があり、最初、1 以上 (H\times W) 以下の整数がちょうど 1 つずつ書き込まれています。
具体的には、1\leq i\leq H, 1\leq j\leq W について、上から i 行目かつ左から j 列目のマスには S_{i,j} が書き込まれています。
以下、上から i 行目かつ左から j 列目のマスをマス (i,j) と呼びます。
次の操作を 高々 20 回 (0 回でも良い)繰り返すことで、
任意の整数の組 (i,j) (1\leq i\leq H, 1\leq j\leq W) について、
マス (i,j) に ((i-1)\times W+j) が書き込まれている状態にできるか判定し、
できる場合は必要な操作回数の最小値を出力してください。
20 回以下でできない場合(何回操作を繰り返してもできない場合を含む)は -1 を出力してください。
操作:マス目から (H-1) \times (W-1) の長方形を選んで 180 度回転させる。
より厳密には、整数 x,y (0 \leq x, y \leq 1) を選び、
1 \leq i \leq H-1, 1 \leq j \leq W-1 をみたすすべての整数の組 (i,j) について同時に、
マス (i+x,j+y) に書かれた整数をマス (H-i+x,W-j+y) に書かれた数に書き換える。
マス目に書き込まれている整数が条件をみたしていれば良く、書き込まれている向き等は考える必要がないことに注意してください。
制約
- 3\leq H,W\leq 8
- 1\leq S_{i,j}\leq H\times W
- (i,j)\neq (i',j') ならば S_{i,j}\neq S_{i',j'}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W S_{1,1} S_{1,2} \ldots S_{1,W} S_{2,1} S_{2,2} \ldots S_{2,W} \vdots S_{H,1} S_{H,2} \ldots S_{H,W}
出力
問題文の条件をみたすために必要な操作回数の最小値を出力せよ。
ただし、20 回以下の操作で条件をみたすようにできないときは -1 を出力せよ。
入力例 1
3 3 9 4 3 2 1 8 7 6 5
出力例 1
2
次の順で操作を行うことで 2 回の操作で問題文の条件をみたすようにすることができます。
- 左上の長方形を選び、操作を行う。すなわち x=0, y=0 を選んで操作を行う。
- 右下の長方形を選び、操作を行う。すなわち x=1, y=1 を選んで操作を行う。
一方で 1 回以下の操作でみたすようにすることは不可能であるため、2 を出力します。
入力例 2
4 6 15 18 1 14 3 4 23 24 19 8 9 12 13 2 17 6 5 16 21 22 7 20 11 10
出力例 2
-1
20 回以下の操作で条件をみたすようにすることができないため、-1 を出力します。
入力例 3
4 6 1 4 13 16 15 18 21 20 9 12 23 10 17 14 5 6 3 2 11 22 7 24 19 8
出力例 3
20
入力例 4
4 3 1 2 3 4 5 6 7 8 9 10 11 12
出力例 4
0
Score: 550 points
Problem Statement
There is a grid with H rows and W columns. Initially, each integer from 1 to (H \times W) is written exactly once in the grid.
Specifically, for 1 \leq i \leq H and 1 \leq j \leq W, the cell at the i-th row from the top and the j-th column from the left contains S_{i,j}.
Below, let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.
Determine if it is possible to reach a state where, for all pairs of integers (i,j) (1 \leq i \leq H, 1 \leq j \leq W),
the cell (i,j) contains the integer ((i-1) \times W + j) by repeating the following operation at most 20 times (possibly zero).
If it is possible, print the minimum number of operations required.
If it is impossible in 20 or fewer operations (including the case where it is impossible no matter how many times the operation is repeated), print -1.
Operation: Choose a rectangle of size (H-1) \times (W-1) from the grid and rotate it 180 degrees.
More precisely, choose integers x and y (0 \leq x, y \leq 1),
and for all pairs of integers (i,j) satisfying 1 \leq i \leq H-1 and 1 \leq j \leq W-1,
simultaneously replace the integer written in cell (i+x,j+y) with the number written in cell (H-i+x,W-j+y).
Note that it is only necessary for the integers written in the cells to satisfy the condition; the orientation in which they are written does not matter.
Constraints
- 3 \leq H,W \leq 8
- 1 \leq S_{i,j} \leq H \times W
- If (i,j) \neq (i',j'), then S_{i,j} \neq S_{i',j'}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W S_{1,1} S_{1,2} \ldots S_{1,W} S_{2,1} S_{2,2} \ldots S_{2,W} \vdots S_{H,1} S_{H,2} \ldots S_{H,W}
Output
Print the minimum number of operations required to meet the conditions of the problem statement.
If it is impossible to satisfy the conditions with 20 or fewer operations, output -1.
Sample Input 1
3 3 9 4 3 2 1 8 7 6 5
Sample Output 1
2
Operating in the following order will satisfy the conditions of the problem statement in 2 operations.
- Operate by choosing the rectangle in the top-left corner. That is, by choosing x=0 and y=0.
- Operate by choosing the rectangle in the bottom-right corner. That is, by choosing x=1 and y=1.
On the other hand, it is impossible to satisfy the conditions with one or fewer operations, so print 2.
Sample Input 2
4 6 15 18 1 14 3 4 23 24 19 8 9 12 13 2 17 6 5 16 21 22 7 20 11 10
Sample Output 2
-1
It is impossible to satisfy the conditions with 20 or fewer operations, so print -1.
Sample Input 3
4 6 1 4 13 16 15 18 21 20 9 12 23 10 17 14 5 6 3 2 11 22 7 24 19 8
Sample Output 3
20
Sample Input 4
4 3 1 2 3 4 5 6 7 8 9 10 11 12
Sample Output 4
0