Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
非負整数 K に対して、以下のようにレベル K のカーペットを定義します。
- レベル 0 のカーペットは黒いマス 1 個のみからなる 1\times 1 のグリッドである。
- K>0 のとき、レベル K のカーペットは 3^K\times 3^K のグリッドである。
このグリッドを 3^{K-1}\times 3^{K-1} のブロック 9 個に分割したとき、
- 中央のブロックはすべて白いマスからなる。
- 他の 8 個のブロックは、レベル (K-1) のカーペットである。
非負整数 N が与えられます。
レベル N のカーペットを出力の形式に従って出力してください。
制約
- 0\leq N \leq 6
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
3^N 行出力せよ。
i 行目 (1\leq i\leq 3^N) には、.
と #
からなる長さ 3^N の文字列 S_i を出力せよ。
S_i の j 文字目 (1\leq j\leq 3^N) は、レベル N のカーペットの上から i 行目かつ左から j 列目のマスが黒いとき #
とし、白いとき .
とせよ。
入力例 1
1
出力例 1
### #.# ###
レベル 1 のカーペットは次のような 3\times 3 のグリッドです。
これを出力形式にしたがって出力すると出力例のようになります。
入力例 2
2
出力例 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
レベル 2 のカーペットは 9\times 9 のグリッドとなります。
Score : 250 points
Problem Statement
For a non-negative integer K, we define a level-K carpet as follows:
- A level-0 carpet is a 1 \times 1 grid consisting of a single black cell.
- For K > 0, a level-K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks:
- The central block consists entirely of white cells.
- The other eight blocks are level-(K-1) carpets.
You are given a non-negative integer N.
Print a level-N carpet according to the specified format.
Constraints
- 0 \leq N \leq 6
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print 3^N lines.
The i-th line (1 \leq i \leq 3^N) should contain a string S_i of length 3^N consisting of .
and #
.
The j-th character of S_i (1 \leq j \leq 3^N) should be #
if the cell at the i-th row from the top and j-th column from the left of a level-N carpet is black, and .
if it is white.
Sample Input 1
1
Sample Output 1
### #.# ###
A level-1 carpet is a 3 \times 3 grid as follows:
When output according to the specified format, it looks like the sample output.
Sample Input 2
2
Sample Output 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
A level-2 carpet is a 9 \times 9 grid.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
長さ M の A の部分列(連続でなくてもよい) B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。
注記
数列の部分列とは、数列から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。
例えば、(10,30) は (10,20,30) の部分列ですが、(20,10) は (10,20,30) の部分列ではありません。
制約
- 1 \le M \le N \le 2000
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 2 5 4 -1 8
出力例 1
21
B=(A_1,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times 5 + 2 \times 8 = 21 となります。22 以上の値を達成することはできないため、解は 21 です。
入力例 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
出力例 2
54
Score : 400 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a (not necessarily contiguous) subsequence B=(B_1,B_2,\dots,B_M) of length M of A.
Notes
A subsequence of a number sequence is a sequence that is obtained by removing 0 or more elements from the original number sequence and concatenating the remaining elements without changing the order.
For example, (10,30) is a subsequence of (10,20,30), but (20,10) is not a subsequence of (10,20,30).
Constraints
- 1 \le M \le N \le 2000
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 2 5 4 -1 8
Sample Output 1
21
When B=(A_1,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times 5 + 2 \times 8 = 21. Since it is impossible to achieve 22 or a larger value, the solution is 21.
Sample Input 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
Sample Output 2
54
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N までの、辺に 1 から M までの番号がついた N 頂点 M 辺の単純無向グラフがあります。 辺 i は頂点 u_i と頂点 v_i を結んでいます。
また、全ての頂点は赤か青のいずれか一方で塗られています。頂点 i の色は C_i で表されて、C_i が 0 ならば頂点 i は赤く、1 ならば頂点 i は青く塗られています。
今、高橋君が頂点 1 に、青木君が頂点 N にいます。
2 人は次の行動を 0 回以上好きな回数繰り返します。
- 2 人が同時に、今いる頂点に隣接している頂点のいずれか 1 個に移動する。
ただし、高橋君の移動先の頂点の色と、青木君の移動先の頂点の色は異なる必要がある。
上記の行動を繰り返すことで、高橋君が頂点 N に、青木君が頂点 1 にいる状態にできますか?
可能である場合は必要な行動回数の最小値を答えてください。不可能である場合は -1
を出力してください。
入力のはじめに T が与えられるので、T 個のテストケースについて問題を解いてください。
制約
- 1 \leq T \leq 1000
- 2 \leq N \leq 2000
- 1 \leq M \leq \min(\frac{N(N-1)}{2}, 2000)
- C_i \in \lbrace 0, 1 \rbrace
- 1 \leq u_i, v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値は全て整数
- 全てのテストケースに対する N の総和は 2000 を超えない。
- 全てのテストケースに対する M の総和は 2000 を超えない。
入力
入力は以下の形式で標準入力から与えられる。ここで \text{test}_i は i 番目のテストケースを意味する。
T \text{test}_1 \text{test}_2 \vdots \text{test}_T
各テストケースは以下の形式で与えられる。
N M C_1 C_2 \dots C_N u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
各テストケースでは、高橋君が頂点 N に、青木君が頂点 1 にいる状態にできる場合は必要な行動回数の最小値を、できない場合は -1
を出力せよ。
入力例 1
3 4 4 0 1 0 1 1 2 2 3 1 3 2 4 3 3 0 1 0 1 2 2 3 1 3 6 6 0 0 1 1 0 1 1 2 2 6 3 6 4 6 4 5 2 4
出力例 1
3 -1 3
1 番目のテストケースでは、高橋君と青木君は以下のように行動することで、 3 回の行動で目的の状態を達成することができて、これが最小です。
- 高橋君が頂点 3 に、青木君が頂点 2 に移動する。
- 高橋君が頂点 2 に、青木君が頂点 3 に移動する。
- 高橋君が頂点 4 に、青木君が頂点 1 に移動する。
ここで、1 回目の移動で高橋君と青木君がともに頂点 2 に移動することはできないのに注意してください。(なぜならば、高橋君の移動先の頂点の色と青木君の移動先の頂点の色は異なる必要があるからです。)
2 番目のテストケースでは、2 人はどのように行動しても目的の状態を達成することはできません。
Score : 500 points
Problem Statement
There is a simple undirected graph with N vertices numbered 1 through N and M edges numbered 1 through M. Edge i connects vertex u_i and vertex v_i.
Every vertex is painted either red or blue. The color of vertex i is represented by C_i; vertex i is painted red if C_i is 0 and blue if C_i is 1.
Now, Takahashi is on vertex 1 and Aoki is on vertex N.
They may repeat the following move zero or more times.
- Each of the two simultaneously moves to a vertex adjacent to the current vertex.
Here, the vertices that Takahashi and Aoki move to must have different colors.
By repeating the move above, can Takahashi and Aoki simultaneously end up on vertices N and 1, respectively?
If it is possible, find the minimum number of moves required. If it is impossible, print -1
.
You are given T at the beginning of the input. Solve the problem for T test cases.
Constraints
- 1 \leq T \leq 1000
- 2 \leq N \leq 2000
- 1 \leq M \leq \min(\frac{N(N-1)}{2}, 2000)
- C_i \in \lbrace 0, 1 \rbrace
- 1 \leq u_i, v_i \leq N
- The graph given in the input is simple.
- All values in the input are integers.
- The sum of N over all test cases does not exceed 2000.
- The sum of M over all test cases does not exceed 2000.
Input
The input is given from Standard Input in the following format, where \text{test}_i denotes the i-th test case:
T \text{test}_1 \text{test}_2 \vdots \text{test}_T
Each test case is given in the following format:
N M C_1 C_2 \dots C_N u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print T lines. The i-th line should contain the answer to the i-th test case.
For each test case, print the minimum number of moves required for Takahashi and Aoki to simultaneously end up in vertices N and 1, respectively, if it is possible, and -1
otherwise.
Sample Input 1
3 4 4 0 1 0 1 1 2 2 3 1 3 2 4 3 3 0 1 0 1 2 2 3 1 3 6 6 0 0 1 1 0 1 1 2 2 6 3 6 4 6 4 5 2 4
Sample Output 1
3 -1 3
For the 1-st test case, Takahashi and Aoki can achieve the objective by making the following 3 moves, which is the minimum number:
- Takahashi moves to vertex 3, and Aoki moves to vertex 2.
- Takahashi moves to vertex 2, and Aoki moves to vertex 3.
- Takahashi moves to vertex 4, and Aoki moves to vertex 1.
Note that in the 1-st move, it is disallowed that both Takahashi and Aoki move to vertex 2 (because the colors of vertices that Takahashi and Aoki move to must be different.)
For the 2-nd case, no matter how they move, they cannot achieve the objective.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。
高橋君は次の操作を好きなだけ (0 回でも良い) 繰り返す事ができます。
1 以上 N 以下の、どの 2 つも互いに相異なる 3 つの整数 i,j,k を選ぶ。
A の i 番目の要素と j 番目の要素を交換し、B の i 番目の要素と k 番目の要素を交換する。
高橋君がうまく操作を繰り返すことによって、
A と B を一致させる事が可能ならば Yes
を、不可能ならば No
を出力してください。
ただし、A と B が一致しているとは、任意の 1\leq i\leq N について A の i 番目の要素と B の i 番目の要素が等しいことを言います。
制約
- 3 \leq N \leq 2\times 10^5
- 1\leq A_i,B_i\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
操作を繰り返すことによって、高橋君が A と B を一致させる事が可能ならば Yes
を、不可能ならば No
を出力せよ。
入力例 1
3 1 2 1 1 1 2
出力例 1
Yes
(i,j,k)=(1,2,3) として 1 回操作を行うことで、A_1 と A_2、B_1 と B_3 がそれぞれ交換され、
A,B はともに (2,1,1) となって一致します。よって、Yes
を出力します。
入力例 2
3 1 2 2 1 1 2
出力例 2
No
どのように操作を行っても A と B を一致させることはできません。よって、No
を出力します。
入力例 3
5 1 2 3 2 1 3 2 2 1 1
出力例 3
Yes
入力例 4
8 1 2 3 4 5 6 7 8 7 8 5 6 4 3 1 2
出力例 4
No
Score : 500 points
Problem Statement
You are given two sequences of N numbers: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).
Takahashi can repeat the following operation any number of times (possibly zero).
Choose three pairwise distinct integers i, j, and k between 1 and N.
Swap the i-th and j-th elements of A, and swap the i-th and k-th elements of B.
If there is a way for Takahashi to repeat the operation to make A and B equal, print Yes
; otherwise, print No
.
Here, A and B are said to be equal when, for every 1\leq i\leq N, the i-th element of A and that of B are equal.
Constraints
- 3 \leq N \leq 2\times 10^5
- 1\leq A_i,B_i\leq N
- 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 B_1 B_2 \ldots B_N
Output
Print Yes
if there is a way for Takahashi to repeat the operation to make A and B equal, and print No
otherwise.
Sample Input 1
3 1 2 1 1 1 2
Sample Output 1
Yes
Performing the operation once with (i,j,k)=(1,2,3) swaps A_1 and A_2, and swaps B_1 and B_3,
making both A and B equal to (2,1,1). Thus, you should print Yes
.
Sample Input 2
3 1 2 2 1 1 2
Sample Output 2
No
There is no way to perform the operation to make A and B equal, so you should print No
.
Sample Input 3
5 1 2 3 2 1 3 2 2 1 1
Sample Output 3
Yes
Sample Input 4
8 1 2 3 4 5 6 7 8 7 8 5 6 4 3 1 2
Sample Output 4
No