Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
頂点に 1 から N までの番号がついた N 頂点の単純有向非巡回グラフ G が与えられます。 G には M 本の辺があり、i 番目の辺は頂点 x_i から頂点 y_i へ張られています。
(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) であって以下の条件を満たすものを特別な順列と呼ぶことにします。
- すべての i=1,2,\cdots,M について、P_{x_i} \lt P_{y_i}
特別な順列 P が与えられます。 これを使って高橋君と青木君がゲームをします。 高橋君 は P の各項の値を知っていますが、青木君は P が特別な順列であるということしか知りません。 なお、二人とも G の形は知っています。 高橋君は P のいくつかの項を選び、その位置と値を青木君に伝えます。 青木君が P のすべての項の値を特定するために高橋君が伝える必要がある項の個数の最小値を求めてください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 10^4
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq x_i,y_i \leq N
- 与えられるグラフ G は単純有向非巡回グラフ
- 1 \leq P_i \leq N
- P_1,P_2,\ldots,P_N は互いに相異なる
- P_{x_i} \lt P_{y_i}
- 全てのテストケースにおける N の総和は 2 \times 10^5 以下
- 全てのテストケースにおける M の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式で与えられる。
N M x_1 y_1 x_2 y_2 \vdots x_M y_M P_1 P_2 \ldots P_N
出力
答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。
入力例 1
3 5 4 3 4 4 1 3 1 5 2 5 4 1 2 3 4 0 4 2 1 3 10 15 6 2 8 5 4 9 7 10 3 7 5 6 8 9 3 5 5 2 8 2 3 9 5 9 10 2 3 2 7 4 8 9 2 6 4 5 3 1 10 7
出力例 1
2 3 6
1 つ目のケースでは、高橋君が P_1=5, \; P_4=2 であることを伝えると、青木君は P のすべての項を特定することができます。また、高橋君が P のいずれの項を 1 つだけ伝えたとしても、青木君は P のすべての項を特定することはできません。
Score : 700 points
Problem Statement
You are given a simple directed acyclic graph G with N vertices numbered 1 to N. G has M edges, and the i-th edge is directed from vertex x_i to vertex y_i.
A permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) that satisfies the following condition is called a special permutation:
- P_{x_i} \lt P_{y_i} for all i=1,2,\cdots,M.
You are given a special permutation P. Takahashi and Aoki will play a game using it. Takahashi knows the value of each term of P, but Aoki only knows that P is a special permutation. Both of them know G. Takahashi selects some terms of P and tells Aoki their positions and values. Find the minimum number of terms that Takahashi needs to tell Aoki so that Aoki can determine the values of all terms of P.
Solve T test cases per input.
Constraints
- 1 \leq T \leq 10^4
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq x_i,y_i \leq N
- The given graph G is a simple directed acyclic graph.
- 1 \leq P_i \leq N
- P_1,P_2,\ldots,P_N are pairwise distinct.
- P_{x_i} \lt P_{y_i}
- The sum of N over all test cases is at most 2 \times 10^5.
- The sum of M over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is given in the following format:
N M x_1 y_1 x_2 y_2 \vdots x_M y_M P_1 P_2 \ldots P_N
Output
Output T lines in total. The t-th line should contain the answer for the t-th test case.
Sample Input 1
3 5 4 3 4 4 1 3 1 5 2 5 4 1 2 3 4 0 4 2 1 3 10 15 6 2 8 5 4 9 7 10 3 7 5 6 8 9 3 5 5 2 8 2 3 9 5 9 10 2 3 2 7 4 8 9 2 6 4 5 3 1 10 7
Sample Output 1
2 3 6
In the first case, if Takahashi tells that P_1=5, \; P_4=2, Aoki can determine all terms of P. Also, if Takahashi tells only one term of P, Aoki cannot determine all terms of P.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
0 と 1 からなる長さ N の整数列 A=(A_1,A_2,\dots,A_N),\; B=(B_1,B_2,\dots,B_N) が与えられます。 数列 A の i 番目から j 番目の連続部分列を A[i,j] と表すことにします。ただし、i>j のとき、A[i,j] は長さ 0 の数列とします。 A に対して、以下の操作を \lfloor\frac{N}{2}\rfloor 回まで行えます。
-  以下の 3 条件を満たす 4 整数 a,b,c,d を選ぶ。 - 1 \leq a \leq b < c \leq d \leq N
- b-a=d-c
- \sum_{i=a}^{b}{A_i}=\sum_{i=c}^{d}{A_i}
 
- A[a,b] と A[c,d] を入れ替える。より厳密には、A を、A[1,a-1], A[c,d], A[b+1,c-1], A[a,b], A[d+1,N] の順に結合した数列に置き換える。
A を B に一致させることが可能かどうかを判定して、可能ならばその操作手順を 1 つ構成してください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T \leq 25000
- 2 \leq N \leq 2 \times 10^5
- 0 \leq A_i \leq 1
- 0 \leq B_i \leq 1
- 全てのテストケースにおける N の総和は 2\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式で与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
答えを以下の形式で出力せよ。
output_1 output_2 \vdots output_T
ここで、output_t は t 番目のテストケースへの出力を表す。
各ケースでは、A を B に一致させられるならば、必要な操作回数を K 回、k 回目の操作で選ぶ (a,b,c,d) を (a_k,b_k,c_k,d_k) として、以下の形式で出力せよ。
Yes K a_1 b_1 c_1 d_1 a_2 b_2 c_2 d_2 \vdots a_K b_K c_K d_K
A を B に一致させることが不可能ならば No を出力せよ。
入力例 1
3 8 1 0 0 1 0 1 0 1 0 0 1 1 1 0 1 0 2 0 0 1 0 3 1 1 1 1 1 1
出力例 1
Yes 2 3 4 6 7 1 4 5 8 No Yes 0
1 つ目のテストケースでは、A は最初、(1,0,0,1,0,1,0,1) です。
(a,b,c,d)=(3,4,6,7) を選んで操作を行うと、A は (1,0,1,0,0,0,1,1) になります。
次に、(a,b,c,d)=(1,4,5,8) を選んで操作を行うと、A は (0,0,1,1,1,0,1,0) になり、B に一致します。
2 つ目のテストケースでは、A を B に一致させることはできません。
3 つ目のテストケースでは、A は最初から B に一致しています。
Score : 800 points
Problem Statement
You are given integer sequences A=(A_1,A_2,\dots,A_N),\; B=(B_1,B_2,\dots,B_N) of length N consisting of 0 and 1. Let A[i,j] denote the contiguous subsequence from the i-th through j-th elements of sequence A. If i>j, A[i,j] is a sequence of length 0. You can perform the following operation on A at most \lfloor\frac{N}{2}\rfloor times:
-  Choose four integers a,b,c,d that satisfy the following three conditions: - 1 \leq a \leq b < c \leq d \leq N
- b-a=d-c
- \sum_{i=a}^{b}{A_i}=\sum_{i=c}^{d}{A_i}
 
- Swap A[a,b] and A[c,d]. More precisely, replace A with the concatenation of A[1,a-1], A[c,d], A[b+1,c-1], A[a,b], A[d+1,N] in this order.
Determine whether it is possible to make A match B, and if possible, construct one sequence of operations.
Solve T test cases per input.
Constraints
- 1 \leq T \leq 25000
- 2 \leq N \leq 2 \times 10^5
- 0 \leq A_i \leq 1
- 0 \leq B_i \leq 1
- The sum of N over all test cases is at most 2\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is given in the following format:
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Output the answer in the following format:
output_1 output_2 \vdots output_T
Here, output_t represents the output for the t-th test case.
For each case, if you can make A match B, let K be the number of operations required and (a_k,b_k,c_k,d_k) be the values chosen in the k-th operation, then output in the following format:
Yes K a_1 b_1 c_1 d_1 a_2 b_2 c_2 d_2 \vdots a_K b_K c_K d_K
If it is impossible to make A match B, output No.
Sample Input 1
3 8 1 0 0 1 0 1 0 1 0 0 1 1 1 0 1 0 2 0 0 1 0 3 1 1 1 1 1 1
Sample Output 1
Yes 2 3 4 6 7 1 4 5 8 No Yes 0
In the first test case, A is initially (1,0,0,1,0,1,0,1).
Performing the operation with (a,b,c,d)=(3,4,6,7) changes A to (1,0,1,0,0,0,1,1).
Then, performing the operation with (a,b,c,d)=(1,4,5,8) changes A to (0,0,1,1,1,0,1,0), which matches B.
In the second test case, it is impossible to make A match B.
In the third test case, A already matches B from the beginning.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 900 点
問題文
正整数 N が与えられます。
以下の条件をすべて満たすような非負整数列 P=(P_1, P_2, \dots,P_N), A=(A_1, A_2, \dots,A_N) を 1 つ出力してください。
- 0 \leq P_i < 2^{30}~(1 \leq i \leq N)
- 0 \leq A_i < 2^{30}~(1 \leq i \leq N)
- (P_1~\mathrm{OR}~A_i, P_2~\mathrm{OR}~A_i, \dots, P_N~\mathrm{OR}~A_i) の最長狭義単調増加部分列の長さは i である。(1 \leq i \leq N)- \mathrm{OR} はビット単位 \mathrm{OR} 演算を表します。
 
この問題の制約下で、条件を満たす P, A が 1 つ以上存在することが証明できます。
1 つの入力につき、T 個のテストケースを解いてください。
ビット単位 \mathrm{OR} 演算とは
非負整数 A, B のビット単位 \mathrm{OR}、A\ \mathrm{OR}\ B は以下のように定義されます。
- A\ \mathrm{OR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも片方が 1 であれば 1、そうでなければ 0 である。
制約
- 1 \leq T
- 1 \leq N \leq 1024
- 1 つの入力に含まれるテストケースについて、N の総和は 200000 以下
- 1 つの入力に含まれるテストケースについて、N^2 の総和は 1024^2 以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各ケースは以下の形式で与えられる。
N
出力
t 番目に \mathrm{case}_t に対する答えを出力せよ。
各ケースについて、以下の形式に従って答えを出力せよ。
P_1 P_2 \ldots P_N A_1 A_2 \ldots A_N
答えが複数存在する場合、どれを出力しても正解となる。
入力例 1
3 3 8 1
出力例 1
3 14 5 13 2 10 6 902 909 186 684 219 471 248 1023 249 64 0 264 768 778 794 2025 1026
1 番目のテストケースについて、出力例は P = (3, 14, 5), A = (13, 2, 10) です。
- (3~\mathrm{OR}~13, 14~\mathrm{OR}~13, 5~\mathrm{OR}~13) = (15, 15, 13) の最長狭義単調増加部分列の 1 つは (13) であり、その長さは 1 です。
- (3~\mathrm{OR}~2, 14~\mathrm{OR}~2, 5~\mathrm{OR}~2) = (3, 14, 7) の最長狭義単調増加部分列の 1 つは (3, 7) であり、その長さは 2 です。
- (3~\mathrm{OR}~10, 14~\mathrm{OR}~10, 5~\mathrm{OR}~10) = (11, 14, 15) の最長狭義単調増加部分列の 1 つは (11, 14, 15) であり、その長さは 3 です。
上記より、P = (3, 14, 5), A = (13, 2, 10) は条件を満たすことが確認できます。
Score : 900 points
Problem Statement
You are given a positive integer N.
Output one pair of non-negative integer sequences P=(P_1, P_2, \dots,P_N), A=(A_1, A_2, \dots,A_N) that satisfies all of the following conditions:
- 0 \leq P_i < 2^{30}~(1 \leq i \leq N)
- 0 \leq A_i < 2^{30}~(1 \leq i \leq N)
- The length of a longest strictly increasing subsequence of (P_1~\mathrm{OR}~A_i, P_2~\mathrm{OR}~A_i, \dots, P_N~\mathrm{OR}~A_i) is i. (1 \leq i \leq N)- \mathrm{OR} denotes the bitwise \mathrm{OR} operation.
 
It can be proved that under the constraints of this problem, there exists at least one pair P, A that satisfies the conditions.
Solve T test cases per input.
What is bitwise \mathrm{OR}?
The bitwise \mathrm{OR} of non-negative integers A, B, denoted A\ \mathrm{OR}\ B, is defined as follows:
- When A\ \mathrm{OR}\ B is written in binary, the digit at position 2^k (k \geq 0) is 1 if at least one of the digits at position 2^k in the binary representations of A and B is 1, and 0 otherwise.
Constraints
- 1 \leq T
- 1 \leq N \leq 1024
- For test cases in a single input, the sum of N is at most 200000.
- For test cases in a single input, the sum of N^2 is at most 1024^2.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each case is given in the following format:
N
Output
Output a solution for \mathrm{case}_1, \mathrm{case}_2, \dots, \mathrm{case}_T in this order.
For each case, output the answer in the following format:
P_1 P_2 \ldots P_N A_1 A_2 \ldots A_N
If there are multiple solutions, any of them will be considered correct.
Sample Input 1
3 3 8 1
Sample Output 1
3 14 5 13 2 10 6 902 909 186 684 219 471 248 1023 249 64 0 264 768 778 794 2025 1026
For the first test case, the sample output is P = (3, 14, 5), A = (13, 2, 10).
- One longest strictly increasing subsequence of (3~\mathrm{OR}~13, 14~\mathrm{OR}~13, 5~\mathrm{OR}~13) = (15, 15, 13) is (13), whose length is 1.
- One longest strictly increasing subsequence of (3~\mathrm{OR}~2, 14~\mathrm{OR}~2, 5~\mathrm{OR}~2) = (3, 14, 7) is (3, 7), whose length is 2.
- One longest strictly increasing subsequence of (3~\mathrm{OR}~10, 14~\mathrm{OR}~10, 5~\mathrm{OR}~10) = (11, 14, 15) is (11, 14, 15), whose length is 3.
From the above, it can be confirmed that P = (3, 14, 5), A = (13, 2, 10) satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1100 点
問題文
以下の操作を行った結果 A として得られる可能性がある数列を特別な数列と呼ぶことにします。
- N 頂点 0 辺からなる無向グラフと長さ 0 の数列 A を用意する。
- 1 \leq i < j \leq N を満たす整数 i,j と q \in \{1,2\} に対し、以下のクエリを用意する。
- q=1 のとき、頂点 i と頂点 j を辺で結ぶ。
- q=2 のとき、頂点 i と頂点 j が連結であるならば 1 を、連結でないならば 0 を、A の末尾に追加する。
- 考えられるクエリは N(N-1) 通りあるが、これらを好きな順に並べ替え、その順に処理する。
0 と 1 からなる長さ \frac{N(N-1)}{2} の数列 B が与えられます。 B に対して以下の操作を何回でも行うことができます。
- 1 \leq i \leq \frac{N(N-1)}{2}-1 を満たす整数 i を選び、B_i と B_{i+1} を入れ替える。
B を特別な数列にすることが可能かを判定し、可能ならばそのために必要な操作回数の最小値を求めてください。
1 つの入力につき、T 個のテストケースを解いてください。
制約
- 1 \leq T
- 2 \leq N \leq 500
- |B| =\frac{N(N-1)}{2}
- 0 \leq B_i \leq 1
- 全てのテストケースにおける |B| の総和は 3 \times 10^5 以下
- 全てのテストケースにおける N^3 の総和は 500^3 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式で与えられる。
N  
B_1 B_2 \ldots B_{\frac{N(N-1)}{2}}
出力
答えを合計 T 行で出力せよ。
t 行目には、t 番目のテストケースについてB を特別な数列にすることが可能ならばそのために必要な操作回数の最小値を、不可能ならば -1 を出力せよ。
入力例 1
4 4 1 1 0 1 1 0 2 1 3 0 0 0 5 1 1 1 1 1 1 1 1 1 0
出力例 1
1 0 0 3
1 つ目のテストケースでは、(q,i,j)=(1,1,4), (2,1,4), (1,2,4), (2,1,2), (1,1,2), (2,2,3), (2,2,4), (2,3,4), (1,3,4), (1,1,3), (2,1,3), (1,2,3) の順にクエリを処理すると、得られる数列は (1,1,0,1,0,1) となり、B に 1 回の操作を行うことでこの数列に変えることができます。また、(1,1,0,1,1,0) は特別な数列ではないため、答えは 1 になります。
Score : 1100 points
Problem Statement
A sequence that can be obtained as A by performing the following operations is called a special sequence:
- Prepare an undirected graph consisting of N vertices and 0 edges, and a sequence A of length 0.
- Prepare queries for integers i,j satisfying 1 \leq i < j \leq N and q \in \{1,2\}:
- When q=1, connect vertices i and j with an edge.
- When q=2, append 1 to the end of A if vertices i and j are connected, and append 0 otherwise.
- There are N(N-1) possible queries; rearrange them in any order and process them in that order.
You are given a sequence B of length \frac{N(N-1)}{2} consisting of 0 and 1. You can perform the following operation on B any number of times:
- Choose an integer i satisfying 1 \leq i \leq \frac{N(N-1)}{2}-1, and swap B_i and B_{i+1}.
Determine whether it is possible to make B a special sequence, and if possible, find the minimum number of operations required.
Solve T test cases per input.
Constraints
- 1 \leq T
- 2 \leq N \leq 500
- |B| =\frac{N(N-1)}{2}
- 0 \leq B_i \leq 1
- The sum of |B| over all test cases is at most 3 \times 10^5.
- The sum of N^3 over all test cases is at most 500^3.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is given in the following format:
N  
B_1 B_2 \ldots B_{\frac{N(N-1)}{2}}
Output
Output T lines in total.
The t-th line should contain the minimum number of operations required to make B a special sequence for the t-th test case if it is possible to do so, and -1 otherwise.
Sample Input 1
4 4 1 1 0 1 1 0 2 1 3 0 0 0 5 1 1 1 1 1 1 1 1 1 0
Sample Output 1
1 0 0 3
In the first test case, if the queries are processed in the order (q,i,j)=(1,1,4), (2,1,4), (1,2,4), (2,1,2), (1,1,2), (2,2,3), (2,2,4), (2,3,4), (1,3,4), (1,1,3), (2,1,3), (1,2,3), the resulting sequence is (1,1,0,1,0,1), and B can be changed to this sequence by performing the operation once. Also, (1,1,0,1,1,0) is not a special sequence, so the answer is 1.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 1200 点
問題文
文字列 X = X_1X_2\dots X_{|X|} と正整数 l,r~(1 \leq l \leq r \leq |X|) に対して、X_{l,r} = X_l X_{l+1}\dots X_r とします。
A, B からなる文字列 S=S_1S_2\dots S_{|S|} が与えられます。
Q 個のクエリが与えられるので、それぞれについて以下の問いに答えてください。
- T = S_{l,r} とした状態から、「T に連続部分文字列として含まれる ABを 1 つ選び削除する」という操作を 0 回以上行うことによって得られる回文としてあり得る長さの最小値を求めてください。
 どのように操作を行っても回文が得られない場合は-1を出力してください。
制約
- 1 \leq |S| \leq 4 \times 10^5
- 1 \leq Q \leq 5 \times 10^4
- 1 \leq l \leq r \leq |S|
- S は A,Bからなる文字列
- Q,l,r は整数
入力
入力は以下の形式で標準入力から与えられる。
S
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q
\mathrm{Query}_q は q 個目のクエリを表し、以下の形式で与えられる。
l r
出力
Q 行出力せよ。q 行目に \mathrm{Query}_q に対する答えを出力せよ。
入力例 1
ABBABB 5 1 6 4 5 3 5 2 3 3 4
出力例 1
2 0 1 2 -1
1 個目、2 個目、3 個目のクエリについて、以下のように操作を行うことで、操作によって得られる回文のうち長さが最小のものが得られます。
- ABBABB\rightarrow- ABBB\rightarrow- BB
- AB\rightarrow (空文字列)
- BAB\rightarrow- B
Score : 1200 points
Problem Statement
For a string X = X_1X_2\dots X_{|X|} and positive integers l,r~(1 \leq l \leq r \leq |X|), let X_{l,r} = X_l X_{l+1}\dots X_r.
You are given a string S=S_1S_2\dots S_{|S|} consisting of A and B.
You are given Q queries. For each query, answer the following question:
- Starting from the state T = S_{l,r}, find the minimum possible length of a palindrome that can be obtained by performing the operation "choose one occurrence of ABthat appears as a contiguous substring in T and delete it" zero or more times.
 If no palindrome can be obtained regardless of how the operation is performed, output-1.
Constraints
- 1 \leq |S| \leq 4 \times 10^5
- 1 \leq Q \leq 5 \times 10^4
- 1 \leq l \leq r \leq |S|
- S is a string consisting of AandB.
- Q,l,r are integers.
Input
The input is given from Standard Input in the following format:
S
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q
\mathrm{Query}_q represents the q-th query and is given in the following format:
l r
Output
Output Q lines. The q-th line should contain the answer for \mathrm{Query}_q.
Sample Input 1
ABBABB 5 1 6 4 5 3 5 2 3 3 4
Sample Output 1
2 0 1 2 -1
For the first, second, and third queries, by performing the operation as follows, you can obtain a palindrome with the minimum length among those obtained by the operation.
- ABBABB\rightarrow- ABBB\rightarrow- BB
- AB\rightarrow (empty string)
- BAB\rightarrow- B