Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
文字 A
, B
, C
のみからなる文字列 t を考えます。
これに対し、以下の操作を行えます。
- 任意の部分文字列 t[l:r] と文字 (
A
,B
,C
) の任意の並べ替え (X, Y, Z) を選ぶ。ここで、t[l:r] は t の l 文字目から r 文字目までで形成される部分文字列であり、l と r を選べる。そして、t[l:r] の各文字A
,B
,C
をそれぞれ X, Y, Z で置き換える。
例えば、文字列 t = ACBAAC
に対して、部分文字列 t[3:6] と (X,Y,Z)=(C
,B
,A
) を選べます。
この操作を行うと、文字列は ACBCCA
となります。
アリーナは、すべての文字が同じであるような文字列が好きです。彼女は、文字列 t の美しさを、そのすべての文字を同じにするために必要な最小の操作回数と定義します。
長さ N の文字 A
, B
, C
のみからなる文字列 S が与えられます。
Q 個のクエリに答えてください。i 個目のクエリは以下の通りです。
- 整数 L_i と R_i が与えられるので、部分文字列 t=S[L_i:R_i] の美しさを求めよ。
制約
- 1 \le N \le 10^5
- S は文字
A
,B
,C
のみからなる文字列である。 - 1 \le Q \le 10^5
- 1 \le L_i \le R_i \le N
- 入力中のすべての数は整数である。
入力
入力は標準入力から以下の形式で与えられる。
N Q S L_1 R_1 L_2 R_2 \vdots L_Q R_Q
出力
Q 行を出力せよ。i 行目には、i 個目のクエリへの答えを出力せよ。
入力例 1
6 4 ABCCCA 3 5 2 3 1 3 1 6
出力例 1
0 1 2 2
一つ目のクエリでは、文字列は t = CCC
であり、すでにすべての文字が同じです。答えは 0 となります。
二つ目のクエリでは、文字列は t = BC
です。これは、部分文字列 t[2:2] と (X,Y,Z)=(A
,C
,B
) を選ぶことで、一回の操作で BB
に変えることができます。
三つ目のクエリでは、文字列は t = ABC
です。これは、部分文字列 t[2:3] と (X,Y,Z)=(C
,A
,B
) を選ぶことで、一回の操作で AAB
に、続いて部分文字列 t[1:2] と (X,Y,Z)=(B
,A
,C
) を選ぶことで、二回目の操作で BBB
に変えることができます。
Score : 500 points
Problem Statement
Consider a string t consisting only of characters A
, B
, and C
.
We can do the following operation with it:
- Choose any substring t[l:r] and any permutation (X, Y, Z) of characters (
A
,B
,C
). Here, t[l:r] denotes the substring formed by the l-th through the r-th characters of t, where l and r are of your choice. Then, replace each characterA
,B
, andC
in t[l:r] by X, Y, and Z, respectively.
For example, for a string t = ACBAAC
, we can choose a substring t[3:6] and (X,Y,Z)=(C
,B
,A
).
After this operation, the string will become ACBCCA
.
Alina likes strings in which all characters are the same. She defines the beauty of a string t as the minimum number of operations required to make all its characters equal.
You are given a string S of length N consisting only of characters A
, B
, and C
.
Answer Q queries. The i-th query is the following:
- Given integers L_i and R_i, find the beauty of the substring t=S[L_i:R_i].
Constraints
- 1 \le N \le 10^5
- S is a string of length N consisting only of characters
A
,B
, andC
. - 1 \le Q \le 10^5
- 1 \le L_i \le R_i \le N
- All numbers in the input are integers.
Input
Input is given from Standard Input in the following format:
N Q S L_1 R_1 L_2 R_2 \vdots L_Q R_Q
Output
Output Q lines. In the i-th line, output the answer to the i-th query.
Sample Input 1
6 4 ABCCCA 3 5 2 3 1 3 1 6
Sample Output 1
0 1 2 2
In the first query, the string is t = CCC
, in which all letters are already equal. The answer is 0.
In the second query, the string is t = BC
. We can change it to BB
in one operation, by choosing a substring t[2:2] and (X,Y,Z)=(A
,C
,B
).
In the third query, the string is t = ABC
. We can change it to AAB
in one operation, by choosing a substring t[2:3] and (X,Y,Z)=(C
,A
,B
), and then to BBB
in the next operation, by choosing a substring t[1:2] and (X,Y,Z)=(B
,A
,C
).
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 700 点
問題文
あなたは、色 C_1, C_2, \ldots, C_N の N 個のボールを持っています。 ここで、すべての色は 1 以上 N 以下の整数により表されます。 あなたは、これらのボールを円周上に並べようとしています。その後、色のペア (X, Y) であって、X < Y であり、色 X と Y の二つの隣接するボールが存在するようなペアの個数を数えます。
そのようなペアの個数を最小化する配置を求めてください。そのような配置が多数存在する場合は、そのうちのいずれかを求めてください。
例えば、色 1, 1, 2, 3 のボールについて、1, 1, 2, 3 と並べると、(1, 2), (2, 3), (1, 3) という 3 つのペアが現れます。1, 2, 1, 3 と並べると、現れるペアは (1, 2), (1, 3) の 2 つのみです。
各入力ファイルについて、T 個のテストケースを解いてください。
制約
- 1 \le T \le 5 \cdot 10^4
- 3 \le N \le 2 \cdot 10^5
- 1 \le C_i \le N
- 各入力ファイル内の N の総和は 2\cdot 10^5 を超えない。
- 入力中のすべての値は整数である。
入力
入力は標準入力から以下の形式で与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式である。
N C_1 C_2 \ldots C_N
出力
各テストケースについて、答えを以下の形式で出力せよ。
A_1 A_2 \ldots A_N
ここで、A_i は配置における円周上の(時計回りに見て)i 番目のボールの色である。
(A_1, A_2, \ldots, A_N) は (C_1, C_2, \ldots, C_N) を並べ替えたものでなければならない。また、色のペア (X, Y) であって、X < Y であり、色 X と Y の二つの隣接するボールが存在するようなペアの個数が最小化されていなければならない。
複数の解が存在する場合は、そのいずれも認められる。
入力例 1
3 3 1 2 3 4 1 2 1 3 5 2 2 5 3 3
出力例 1
1 2 3 2 1 3 1 3 3 2 5 2
Score : 700 points
Problem Statement
You have N balls of colors C_1, C_2, \ldots, C_N. Here, all colors are represented by an integer between 1 and N inclusive. You want to arrange the balls on a circle. After you do that, you will count the number of pairs of colors (X, Y) such that X < Y and there exist two adjacent balls of colors X and Y.
Find an arrangement that minimizes the number of such pairs. If there are many such arrangements, find any of them.
For example, for balls of colors 1, 1, 2, 3, if we arrange them as 1, 1, 2, 3, we get 3 pairs: (1, 2), (2, 3), (1, 3). If we arrange them as 1, 2, 1, 3, we get only 2 pairs: (1, 2), (1, 3).
Solve T test cases for each input file.
Constraints
- 1 \le T \le 5 \cdot 10^4
- 3 \le N \le 2 \cdot 10^5
- 1 \le C_i \le N
- The sum of N in one input file doesn't exceed 2\cdot 10^5.
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is in the following format:
N C_1 C_2 \ldots C_N
Output
For each test case, output your answer in the following format:
A_1 A_2 \ldots A_N
Here, A_i is the color of the i-th ball (in clockwise order) on the circle in your arrangement.
(A_1, A_2, \ldots, A_N) should be a permutation of (C_1, C_2, \ldots, C_N), and the number of pairs of colors (X, Y) such that X < Y and there exist two adjacent balls of colors X and Y, should be minimized.
If multiple solutions exist, any of them will be accepted.
Sample Input 1
3 3 1 2 3 4 1 2 1 3 5 2 2 5 3 3
Sample Output 1
1 2 3 2 1 3 1 3 3 2 5 2
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1100 点
問題文
先生が (1,2,\cdots,N) の順列 P=(P_1,P_2,\ldots,P_N) を隠し持っています。 これから、あなたはこの順列を特定します。
そのために、あなたは整数のペアの列 (A_1,B_1),(A_2,B_2),\ldots,(A_{N(N-1)/2},B_{N(N-1)/2}) を用意しました。これは、(a,b) (1 \le a < b \le N) という形のすべてのペアを並べ替えたものです。 今から、あなたはこれらのペアを先頭から検査します。ペア (A_i, B_i) に対しては、P_{A_i}<P_{B_i} であるかを尋ね、先生が答えを教えます。 ただし、この質問への答えがそれ以前の答えから特定できる場合は、この質問を省略します。
このアルゴリズムで \frac{N(N-1)}{2} 個の質問がすべてされるような順列 P の個数を 10^9+7 で割った余りを求めてください。
制約
- 2 \le N \leq 400
- 1 \le A_i < B_i \le N
- (A_i,B_i) \neq (A_j,B_j) (i \neq j)
- 入力中のすべての値は整数である。
入力
入力は標準入力から以下の形式で与えられる。
N A_1 B_1 A_2 B_2 \vdots A_{N(N-1)/2} B_{N(N-1)/2}
出力
答えを出力せよ。
入力例 1
2 1 2
出力例 1
2
明らかに、どの順列 P に対しても、質問を一つする必要があります。
入力例 2
4 1 2 1 3 2 3 2 4 3 4 1 4
出力例 2
4
例として、P=(2,3,1,4) を考えます。 この場合、二問目までで P_1 < P_2 と P_1 > P_3 を知って P_2 > P_3 と特定できるため、三問目を省略します。 従って、P=(2,3,1,4) は数えません。
入力例 3
5 1 2 2 3 3 4 4 5 1 5 1 3 2 4 3 5 1 4 2 5
出力例 3
0
Score : 1100 points
Problem Statement
A teacher has a hidden permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\cdots,N). You are going to determine it.
To do this, you prepared a sequence of pairs of integers (A_1,B_1),(A_2,B_2),\ldots,(A_{N(N-1)/2},B_{N(N-1)/2}); this is a permutation of all pairs of the form (a,b) (1 \le a < b \le N). Now, you will go over the pairs from the beginning. For a pair (A_i, B_i), you will ask if P_{A_i}<P_{B_i}, and the teacher will tell you the answer. However, you will skip this question if you can already determine the answer to it from previous answers.
Find the number of permutations P, for which with your algorithm you will have to ask all \frac{N(N-1)}{2} questions, modulo 10^9+7.
Constraints
- 2 \le N \leq 400
- 1 \le A_i < B_i \le N
- (A_i,B_i) \neq (A_j,B_j) (i \neq j)
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_{N(N-1)/2} B_{N(N-1)/2}
Output
Print the answer.
Sample Input 1
2 1 2
Sample Output 1
2
Clearly, for any permutation P, you need to ask 1 question.
Sample Input 2
4 1 2 1 3 2 3 2 4 3 4 1 4
Sample Output 2
4
Consider P=(2,3,1,4) as an example. In this case, you will skip the third question since you know P_1 < P_2 and P_1 > P_3 from previous questions and you can determine P_2 > P_3. Therefore, P=(2,3,1,4) should not be counted.
Sample Input 3
5 1 2 2 3 3 4 4 5 1 5 1 3 2 4 3 5 1 4 2 5
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1100 点
問題文
整数列 A=(A_1, A_2, \ldots, A_{N + K-1}) (1 \leq A_i \leq N+K-1) に対して、B_i を A_i,A_{i+1},\ldots,A_{i+K-1} の中の相異なる要素の個数として、列 B=(B_1, B_2, \ldots, B_N) を作ります。
B_1, B_2, \ldots, B_N が与えられます。この列 B を生成し得た列 A が存在するか判定し、存在する場合はそのような列 A を一つ構成してください。
各入力ファイルについて、T 個のテストケースを解いてください。
制約
- 1 \le T \le 5 \cdot 10^4
- 2 \le N \le 2 \cdot 10^5
- 2 \le K \le 2 \cdot 10^5
- 1 \le B_i \le K
- 各入力ファイル内の N の総和は 2\cdot 10^5 を超えない。
- 各入力ファイル内の K の総和は 2\cdot 10^5 を超えない。
- 入力中のすべての値は整数である。
入力
入力は標準入力から以下の形式で与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式である。
N K B_1 B_2 \ldots B_N
出力
各テストケースについて、題意を満たす列 A が存在しなければ、NO
と出力せよ。
そうでなければ、答えを次の形式で出力せよ。
YES A_1 A_2 \ldots A_{N+K-1}
ここで、1 \leq A_i \leq N+K-1 でなければならず、A は B を生成するものでなければならない。 複数の解が存在する場合は、そのいずれも認められる。
YES
または NO
の出力において、各文字は英大文字・小文字のいずれでもよい。
入力例 1
3 3 3 1 2 1 4 3 1 2 2 1 6 4 3 3 3 3 3 3
出力例 1
NO YES 1 1 1 2 2 2 YES 1 2 3 1 2 3 1 2 3
Score : 1100 points
Problem Statement
For an integer array A=(A_1, A_2, \ldots, A_{N + K-1}) (1 \leq A_i \leq N+K-1), let's construct an array B=(B_1, B_2, \ldots, B_N), where B_i is the number of distinct elements in A_i,A_{i+1},\ldots,A_{i+K-1}.
You are given B_1, B_2, \ldots, B_N. Determine if there exists an array A which could have produced such an array B, and if yes, construct one.
Solve T test cases for each input file.
Constraints
- 1 \le T \le 5 \cdot 10^4
- 2 \le N \le 2 \cdot 10^5
- 2 \le K \le 2 \cdot 10^5
- 1 \le B_i \le K
- The sum of N in one input file doesn't exceed 2\cdot 10^5.
- The sum of K in one input file doesn't exceed 2\cdot 10^5.
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is in the following format:
N K B_1 B_2 \ldots B_N
Output
For each test case, if such an array A doesn't exist, output NO
.
Otherwise, output the answer in the following format:
YES A_1 A_2 \ldots A_{N+K-1}
Here, 1 \leq A_i \leq N+K-1 must hold, and A should produce B. If multiple solutions exist, any of them will be accepted.
In printing YES
or NO
, you can output each letter in any case (upper or lower).
Sample Input 1
3 3 3 1 2 1 4 3 1 2 2 1 6 4 3 3 3 3 3 3
Sample Output 1
NO YES 1 1 1 2 2 2 YES 1 2 3 1 2 3 1 2 3
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1400 点
問題文
N \times N のマス盤面があります。 あなたは、隣接する(辺を共有する)どの二つのマスも同じ色にならないように、すべてのマスを 3 色で塗ろうとしています。
盤面の最も外側は、すでに塗ってしまいました。盤面の残りを意図通りに塗ることができるか判定してください。
より正確には、文字 1
, 2
, 3
からなる長さ 4N-4 の文字列 S が与えられます。この文字列は、盤面の最も外側のマスの色を、(1, 1) から始めて時計回りに表します。
厳密には、S の i 文字目は次のマスの色を表します。
- 1 \le i \le N-1 のとき、(1, i)
- N \le i \le 2N-2 のとき、(i - (N-1), N)
- 2N-1 \le i \le 3N-3 のとき、(N, 3N - 1 - i)
- 3N-2 \le i \le 4N-4 のとき、(4N-2 - i, 1)
ここで、(r,c) は r 行 c 列目のマスを指します。
盤面の最も外側では、隣接するどの二つのマスも同じ色でないことが保証されます。
各入力ファイルについて、T 個のテストケースを解いてください。
制約
- 1 \le T \le 5 \cdot 10^4
- 3 \le N \le 2 \cdot 10^5
- S は文字
1
,2
,3
からなる長さ 4N-4 の文字列である。 - 1 \le i \le 4N-5 のとき S_i \neq S_{i+1} であり、また S_{4N-4} \neq S_1 である。
- 各入力ファイル内の N の総和は 2\cdot 10^5 を超えない。
- 入力中のすべての数は整数である。
入力
入力は標準入力から以下の形式で与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式である。
N S
出力
各テストケースについて、盤面の残りを意図通りに 3 色で塗る方法があれば YES
と、なければ NO
と出力せよ。出力中の各文字は英大文字・小文字のいずれでもよい。
入力例 1
4 3 12312312 4 121212121212 7 321312312312121212121321 7 321312312312121312121321
出力例 1
NO YES NO YES
一つ目と三つ目のテストケースでは、盤面の残りを意図通りに塗る方法がないことが示せます。二つ目と四つ目のテストケースで盤面の残りを意図通りに塗る方法を以下に示します。
Score : 1400 points
Problem Statement
You have an N \times N grid board. You want to color all cells in 3 colors so that no two adjacent (edge-sharing) cells have the same color.
You have already painted the border of the board. Determine if you can color the rest of the board properly.
More precisely, you are given a string S of length 4N-4 consisting of characters 1
, 2
, and 3
. The string denotes the colors of the cells on the border, starting from the cell (1, 1), in clockwise order.
Strictly speaking, the i-th character of S denotes the color of the cell:
- (1, i) for 1 \le i \le N-1
- (i - (N-1), N) for N \le i \le 2N-2
- (N, 3N - 1 - i) for 2N-1 \le i \le 3N-3
- (4N-2 - i, 1) for 3N-2 \le i \le 4N-4.
Here, (r,c) denotes the cell in the r-th row and the c-th column.
It is guaranteed that no two adjacent cells on the border have the same color.
Solve T test cases for each input file.
Constraints
- 1 \le T \le 5 \cdot 10^4
- 3 \le N \le 2 \cdot 10^5
- S is a string of length 4N-4 consisting of characters
1
,2
, and3
. - S_i \neq S_{i+1} for 1 \le i \le 4N-5, and S_{4N-4} \neq S_1.
- The sum of N in one input file doesn't exceed 2\cdot 10^5.
- All numbers in the input are integers.
Input
Input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is in the following format:
N S
Output
For each test case, output YES
if there is a way to color the rest of the board in 3 colors properly, and NO
otherwise. You can output each letter in any case (upper or lower).
Sample Input 1
4 3 12312312 4 121212121212 7 321312312312121212121321 7 321312312312121312121321
Sample Output 1
NO YES NO YES
It can be shown that there is no way to properly color the rest of the board in the first and the third test cases. The proper colorings for the second and fourth test cases are shown below.
Time Limit: 4 sec / Memory Limit: 1024 MB
配点 : 1900 点
問題文
N, pos, val が与えられるので、(1,2,\ldots,N) の順列 P=(P_1, P_2, \ldots, P_N) であって次の条件をすべて満たすものの個数を 10^9+7 で割った余りを求めてください。
- LIS(P) + LDS(P) = N+1
- P_{pos} = val
ここで、LIS(P) は P の最長増加部分列の長さを表し、LDS(P) は P の最長減少部分列の長さを表します。
制約
- 1 \le N \le 5\cdot 10^6
- 1 \le pos, val \le N
- 入力中のすべての値は整数である。
入力
入力は標準入力から以下の形式で与えられる。
N pos val
出力
答えを出力せよ。
入力例 1
3 2 2
出力例 1
2
条件を満たす順列は (1, 2, 3), (3, 2, 1) です。
入力例 2
4 1 1
出力例 2
6
条件を満たす順列は (1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2) です。
入力例 3
5 2 5
出力例 3
11
入力例 4
2022 69 420
出力例 4
128873576
Score : 1900 points
Problem Statement
Given N, pos, val, find the number of permutations P=(P_1, P_2, \ldots, P_N) of (1,2,\ldots,N) that satisfy all of the following conditions, modulo 10^9+7:
- LIS(P) + LDS(P) = N+1
- P_{pos} = val
Here, LIS(P) denotes the length of the longest increasing subsequence of P, and LDS(P) denotes the length of the longest decreasing subsequence of P.
Constraints
- 1 \le N \le 5\cdot 10^6
- 1 \le pos, val \le N
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
N pos val
Output
Print the answer.
Sample Input 1
3 2 2
Sample Output 1
2
The following permutations satisfy the conditions: (1, 2, 3), (3, 2, 1).
Sample Input 2
4 1 1
Sample Output 2
6
The following permutations satisfy the conditions: (1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2).
Sample Input 3
5 2 5
Sample Output 3
11
Sample Input 4
2022 69 420
Sample Output 4
128873576