実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 800 点
問題文
A, B からなる長さ N の文字列 X,Y が与えられます.X の i 文字目を X_i と表すことにします.
以下の操作を 0 回以上 \left\lfloor \frac{N}{2} \right\rfloor 回以下行うことで, X を Y に一致させることができるか否かを判定してください.
- 1\leq l < r \leq N かつ X_l=
Aかつ X_r=Bなる整数組 (l, r) を選ぶ.X の l 番目から r 番目までの要素を反転する.ここで,「X の l 番目から r 番目までの要素を反転する」とは, X_l,X_{l+1},\ldots,X_{r-1},X_r を X_r,X_{r-1},\ldots,X_{l+1},X_l に同時に置き換えることを言う.
X を Y に一致させることが可能な場合には,そのような手順をひとつ出力してください.
T 個のテストケースが与えられるので,それぞれについて答えを求めてください.
制約
- 1 \leq T \leq 2.5\times 10^5
- 2 \leq N \leq 5\times 10^5
- 一つのテストケースに含まれる N の総和は 5\times 10^5 以下
- X,Y は
A,Bからなる長さ N の文字列 - 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる.
N X Y
出力
\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T に対する答えを順に以下の形式で出力せよ.
0 回以上 \left\lfloor \frac{N}{2} \right\rfloor 回以下の操作で X を Y に一致させることが不可能な場合には,No と出力せよ.
可能な場合には,操作手順を次の形式で出力せよ.
Yes K l_1 r_1 l_2 r_2 \vdots l_K r_K
ここで K は操作回数で,l_i,r_i は i 回目の操作で選ぶ整数 l,r を表す.これらは次を満たす必要がある.
- 0 \leq K \leq \left\lfloor \frac{N}{2} \right\rfloor
- 1 \leq l_i < r_i \leq N
条件を満たす操作手順が複数存在する場合には,そのどれを出力しても正解と見なされる.
入力例 1
3 5 ABAAB ABBAA 4 AAAA BBBB 10 BABBAABBAA BBBBAAABAA
出力例 1
Yes 1 3 5 No Yes 3 2 7 4 6 3 5
1 番目のテストケースでは,X_3= A かつ X_5= B なので,X の 3 番目から 5 番目までの要素を反転する操作を行うことができます.この操作を行うと, X は ABBAA になり,Y と一致します.
2 番目のテストケースでは,どのように操作を行っても X を Y に一致させることができません.
Score : 800 points
Problem Statement
You are given strings X and Y of length N consisting of A and B. Let X_i denote the i-th character of X.
Determine whether it is possible to make X equal to Y by performing the following operation between 0 and \left\lfloor \frac{N}{2} \right\rfloor times, inclusive.
- Choose a pair of integers (l, r) such that 1\leq l < r \leq N, X_l=
A, and X_r=B. Reverse the elements of X from the l-th to the r-th position. Here, "reversing the elements of X from the l-th to the r-th position" means simultaneously replacing X_l,X_{l+1},\ldots,X_{r-1},X_r with X_r,X_{r-1},\ldots,X_{l+1},X_l.
If it is possible to make X equal to Y, output one such sequence of operations.
T test cases are given; solve each of them.
Constraints
- 1 \leq T \leq 2.5\times 10^5
- 2 \leq N \leq 5\times 10^5
- The sum of N over all test cases is at most 5\times 10^5.
- X and Y are strings of length N consisting of
AandB. - 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 test case is given in the following format:
N X Y
Output
Output the answers for \mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T in this order in the following format.
If it is impossible to make X equal to Y using between 0 and \left\lfloor \frac{N}{2} \right\rfloor operations, inclusive, output No.
If it is possible, output a sequence of operations in the following format:
Yes K l_1 r_1 l_2 r_2 \vdots l_K r_K
Here K is the number of operations, and l_i, r_i are the integers l, r chosen in the i-th operation. These must satisfy the following:
- 0 \leq K \leq \left\lfloor \frac{N}{2} \right\rfloor
- 1 \leq l_i < r_i \leq N
If there are multiple valid sequences of operations, any of them will be accepted.
Sample Input 1
3 5 ABAAB ABBAA 4 AAAA BBBB 10 BABBAABBAA BBBBAAABAA
Sample Output 1
Yes 1 3 5 No Yes 3 2 7 4 6 3 5
In the first test case, X_3= A and X_5= B, so we can perform the operation of reversing the elements of X from the 3-rd to the 5-th position. After this operation, X is ABBAA, which equals Y.
In the second test case, it is impossible to make X equal to Y regardless of the operations performed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 900 点
問題文
正整数 N, K が与えられます.
左側に N 個の頂点 l_1, l_2, …, l_N,右側に N 個の頂点 r_1, r_2, …, r_N を持つ完全二部グラフ G があります.辺の本数は N^2 であり,各 (i, j)\ (1\leq i\leq N, 1\leq j\leq N) に対して頂点 l_i と頂点 r_j の間に無向辺が 1 つあります.
G の各辺に 1 以上 N^2 以下の整数を書き込む(ただし,書き込む N^2 個の整数は相異なるようにする)方法であって,以下の問題の答えが K になるようなものが存在するかどうか判定し,存在するならば書き込み方を一つ求めてください.
グラフ G 上のウォークのうち,通過した辺に書かれた数を通過順に並べた列が狭義単調増加となるものを良いウォークと呼びます.
良いウォークに含まれる辺の本数の最大値を求めてください.
T 個のテストケースが与えられるので,それぞれについて答えを求めてください.
ウォークとは
グラフ G 上のウォークとは, k 個(k は正整数)の頂点と k−1 個の辺を交互に並べた列 (v_1 ,e_1 ,v_2 ,\ldots,v_{k−1} ,e_{k−1} ,v_k) であって,辺 e_i が頂点 v_i と頂点 v_{i+1} を結んでいるようなものを指します.制約
- 1\leq T\leq 3000
- 1\leq N\leq 700
- 1\leq K\leq N^2
- 一つの入力に含まれる N^2 の総和は 5\times 10^5 以下
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる.
N K
出力
\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T に対する答えを順に以下の形式で出力せよ.
条件を満たす書き込み方が存在しない場合,No と出力せよ.
存在する場合,頂点 l_i と頂点 r_j を結ぶ辺に書き込む整数を A_{i,j} として,
Yes
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
と出力せよ.(A_{1,1},\ldots,A_{1,N},A_{2,1},\ldots,A_{2,N},\ldots,A_{N,1},\ldots,A_{N,N}) は (1,2,\ldots,N^2) の順列である必要がある.
解が複数存在する場合,どれを出力しても正解とみなされる.
入力例 1
3 2 1 2 3 3 5
出力例 1
No Yes 1 2 3 4 Yes 2 9 7 4 5 6 1 8 3
1 つ目のテストケースについて,条件を満たす書き込み方は存在しません.
2 つ目のテストケースについて,辺を 3 本含む良いウォークとして r_1 \xrightarrow{1} l_1 \xrightarrow{2}r_2\xrightarrow{4}l_2 が存在します.辺を 4 本以上含む良いウォークは存在しないため,この出力は条件を満たします.
3 つ目のテストケースについて,辺を 5 本含む良いウォークとして l_1 \xrightarrow{2} r_1 \xrightarrow{4}l_2\xrightarrow{6}r_3 \xrightarrow{7} l_1 \xrightarrow{9} r_2 が存在します.辺を 6 本以上含む良いウォークは存在しないため,この出力は条件を満たします.

Score : 900 points
Problem Statement
You are given positive integers N and K.
There is a complete bipartite graph G with N vertices l_1, l_2, \ldots, l_N on the left and N vertices r_1, r_2, \ldots, r_N on the right. It has N^2 edges; for each (i, j)\ (1\leq i\leq N, 1\leq j\leq N), there is one undirected edge between vertices l_i and r_j.
Determine whether there exists a way to write an integer between 1 and N^2, inclusive, on each edge of G (where the N^2 integers written are all distinct) such that the answer to the following problem is K, and if such a way exists, find one.
Among all walks on graph G, we call a walk good if the sequence of numbers written on the edges traversed, in the order they are traversed, is strictly increasing.
Find the maximum number of edges contained in a good walk.
T test cases are given; solve each of them.
What is a walk?
A walk on graph G is a sequence of k vertices and k-1 edges alternately arranged, (v_1 ,e_1 ,v_2 ,\ldots,v_{k−1} ,e_{k−1} ,v_k) (where k is a positive integer), such that edge e_i connects vertices v_i and v_{i+1}.Constraints
- 1\leq T\leq 3000
- 1\leq N\leq 700
- 1\leq K\leq N^2
- The sum of N^2 in each input is at most 5\times 10^5.
- 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 test case is given in the following format:
N K
Output
Output the answers for \mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T in this order in the following format.
If no valid way of writing exists, output No.
If a valid way exists, letting A_{i,j} be the integer written on the edge connecting vertices l_i and r_j,
Yes
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
output as above. (A_{1,1},\ldots,A_{1,N},A_{2,1},\ldots,A_{2,N},\ldots,A_{N,1},\ldots,A_{N,N}) must be a permutation of (1,2,\ldots,N^2).
If multiple solutions exist, any of them will be accepted.
Sample Input 1
3 2 1 2 3 3 5
Sample Output 1
No Yes 1 2 3 4 Yes 2 9 7 4 5 6 1 8 3
For the first test case, no valid way of writing exists.
For the second test case, a good walk containing three edges is r_1 \xrightarrow{1} l_1 \xrightarrow{2}r_2\xrightarrow{4}l_2. No good walk contains four or more edges, so this output satisfies the condition.
For the third test case, a good walk containing five edges is l_1 \xrightarrow{2} r_1 \xrightarrow{4}l_2\xrightarrow{6}r_3 \xrightarrow{7} l_1 \xrightarrow{9} r_2. No good walk contains six or more edges, so this output satisfies the condition.

実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 1000 点
問題文
N 頂点 M 辺の単純有向グラフ G があります.頂点には 1 から N の番号が付いており,i 番目の辺は頂点 a_i から頂点 b_i へ張られています.i\neq j のとき (a_i,b_i)\neq (b_j,a_j) であることが保証されます.また 2 以上 N-2 以下の整数 K が与えられます.
以下の操作を 0 回以上好きな回数行うことで,G を有向閉路を含まないグラフ(DAG)にできるか否かを判定してください.
- \lbrace 1,\ldots,N\rbrace のサイズ K の部分集合 S を選ぶ.始点と終点の番号がともに S に含まれるような有向辺すべてについて,その向きを反転させる.
- すなわち,頂点 u から頂点 v\ (u,v\in S) に辺が張られているとき,その辺を削除し,頂点 v から頂点 u への有向辺を追加する.この操作はすべての対象の辺に対して同時に行われる.
T 個のテストケースが与えられるので,それぞれについて答えを求めてください.
制約
- 1 \leq T \leq 50000
- 4 \leq N \leq 2\times 10^5
- 0 \leq M \leq \min({N(N-1), 2\times 10^5})
- 2 \leq K \leq N-2
- 1 \leq a_i, b_i \leq N
- a_i \neq b_i
- i\neq j ならば (a_i, b_i) \neq (a_j, b_j) かつ (a_i,b_i) \neq (b_j,a_j)
- 一つの入力に含まれる N の総和は 2\times 10^5 以下
- 一つの入力に含まれる M の総和は 2\times 10^5 以下
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる.
N M K a_1 b_1 a_2 b_2 \vdots a_M b_M
出力
\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T に対する答えを順に以下の形式で出力せよ.
操作を 0 回以上好きな回数行うことで G を有向閉路を含まないグラフ(DAG)にできるならば Yes ,できないならば No と出力せよ.
入力例 1
3 7 10 3 1 2 2 3 3 1 4 5 5 6 6 4 1 4 2 5 3 6 7 1 7 19 5 1 4 2 3 2 4 2 5 3 5 3 6 3 7 4 3 4 6 4 7 5 1 5 4 5 6 6 1 6 2 6 7 7 1 7 2 7 5 9 15 5 1 6 2 7 2 9 3 1 3 4 4 5 4 6 4 7 4 8 5 7 5 9 6 5 6 9 7 9 8 9
出力例 1
Yes No Yes
1 番目のテストケースでは,以下のように 2 回の操作を行うことで G を有向閉路を含まないグラフにできます.
-
1 回目の操作では S = \lbrace 1, 2,7 \rbrace として操作を行う. 始点と終点の番号がともに S に含まれるような有向辺は頂点 1 から頂点 2 へ張られた辺と,頂点 7 から頂点 1 へ張られた辺である.これらの辺の向きが反転する.
-
2 回目の操作では S = \lbrace 4, 5,7 \rbrace として操作を行う. 始点と終点の番号がともに S に含まれるような有向辺は頂点 4 から頂点 5 へ張られた辺である.この辺の向きが反転し,G は有向閉路を含まないグラフになる.
2 番目のテストケースでは,どのように操作を行っても G を有向閉路を含まないグラフにできません.
Score : 1000 points
Problem Statement
There is a simple directed graph G with N vertices and M edges. The vertices are numbered 1 through N, and the i-th edge goes from vertex a_i to vertex b_i. It is guaranteed that (a_i,b_i)\neq (b_j,a_j) for i\neq j. Also, an integer K with 2 \leq K \leq N-2 is given.
Determine whether it is possible to make G a graph with no directed cycles (DAG) by performing the following operation zero or more times.
- Choose a size-K subset S of \lbrace 1,\ldots,N\rbrace. For every directed edge whose both endpoints' numbers are in S, reverse its direction.
- That is, if there is an edge from vertex u to vertex v\ (u,v\in S), delete that edge and add a directed edge from vertex v to vertex u. This operation is performed simultaneously for all applicable edges.
T test cases are given; solve each of them.
Constraints
- 1 \leq T \leq 50000
- 4 \leq N \leq 2\times 10^5
- 0 \leq M \leq \min({N(N-1), 2\times 10^5})
- 2 \leq K \leq N-2
- 1 \leq a_i, b_i \leq N
- a_i \neq b_i
- (a_i, b_i) \neq (a_j, b_j) and (a_i,b_i) \neq (b_j,a_j) for i\neq j.
- The sum of N in each input is at most 2\times 10^5.
- The sum of M in each input 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
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N M K a_1 b_1 a_2 b_2 \vdots a_M b_M
Output
Output the answers for \mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T in this order in the following format.
Output Yes if it is possible to make G a graph with no directed cycles (DAG) by performing the operation zero or more times, and No otherwise.
Sample Input 1
3 7 10 3 1 2 2 3 3 1 4 5 5 6 6 4 1 4 2 5 3 6 7 1 7 19 5 1 4 2 3 2 4 2 5 3 5 3 6 3 7 4 3 4 6 4 7 5 1 5 4 5 6 6 1 6 2 6 7 7 1 7 2 7 5 9 15 5 1 6 2 7 2 9 3 1 3 4 4 5 4 6 4 7 4 8 5 7 5 9 6 5 6 9 7 9 8 9
Sample Output 1
Yes No Yes
In the first test case, G can be made into a graph with no directed cycles by performing the following two operations.
-
In the first operation, perform the operation with S = \lbrace 1, 2,7 \rbrace. The directed edges whose both endpoints' numbers are in S are the edge from vertex 1 to vertex 2 and the edge from vertex 7 to vertex 1. The directions of these edges are reversed.
-
In the second operation, perform the operation with S = \lbrace 4, 5,7 \rbrace. The directed edge whose both endpoints' numbers are in S is the edge from vertex 4 to vertex 5. The direction of this edge is reversed, and G becomes a graph with no directed cycles.
In the second test case, it is impossible to make G a graph with no directed cycles regardless of the operations performed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 1000 点
問題文
正整数 N と素数 P が与えられます.
すべての要素が 0 である長さ N の数列 A=(A_1,A_2,\ldots,A_N) があります.
あなたは以下の操作を 0 回以上好きな回数繰り返すことができます.
- 整数の組 (l, r)\ (1\leq l\leq r\leq N) を選ぶ.A_l,A_{l+1},\ldots,A_r を全て \dfrac{r(r-1)}{2}+l で置き換える.
操作を繰り返すことで最終的に得られる A としてあり得るものの個数を P で割った余りを求めてください.
制約
- 1\leq N\leq 5000
- P は 10^8\lt P\lt 10^9 を満たす素数
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる.
N P
出力
答えを出力せよ.
入力例 1
1 998244353
出力例 1
2
得られる A は (0),(1) の 2 通りです.
入力例 2
2 998244353
出力例 2
7
得られる A は (0,0),(0,3),(1,0),(1,2),(1,3),(2,2),(2,3) の 7 通りです.
入力例 3
3 998244353
出力例 3
38
入力例 4
4 998244353
出力例 4
282
入力例 5
77 777777773
出力例 5
112061248
Score : 1000 points
Problem Statement
You are given a positive integer N and a prime P.
There is a sequence A=(A_1,A_2,\ldots,A_N) of length N where all elements are 0.
You can repeat the following operation zero or more times.
- Choose a pair of integers (l, r)\ (1\leq l\leq r\leq N). Replace each of A_l,A_{l+1},\ldots,A_r with \dfrac{r(r-1)}{2}+l.
Find the number, modulo P, of sequences that can be obtained as A after performing operations.
Constraints
- 1\leq N\leq 5000
- P is a prime satisfying 10^8\lt P\lt 10^9.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P
Output
Output the answer.
Sample Input 1
1 998244353
Sample Output 1
2
The two possible sequences A are (0) and (1).
Sample Input 2
2 998244353
Sample Output 2
7
The seven possible sequences A are (0,0),(0,3),(1,0),(1,2),(1,3),(2,2),(2,3).
Sample Input 3
3 998244353
Sample Output 3
38
Sample Input 4
4 998244353
Sample Output 4
282
Sample Input 5
77 777777773
Sample Output 5
112061248
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 1000 点
問題文
4\times 500 のグリッドグラフの各頂点に 0 または 1 を書き込むことを考えます.N=0,1,\ldots,999899,999900 に対して以下の条件を満たすことができるように上手に書き込んでください.
- グリッドグラフのハミルトンパスであって,そのパスの頂点に書かれた数を並べて得られる列の転倒数が N であるようなものが存在する.
![]()
3\times 4 の例.この書き込み方では N = 10, 11, \ldots, 20 が達成可能である.
この問題は インタラクティブ な問題です.問題文中のパラメータは H=4,W=500,Q=200 で固定されています.
あなたとジャッジは下記の手順を行います.手順はフェイズ 1 とフェイズ 2 からなり,まずフェイズ 1 を行った直後,続けてフェイズ 2 を行います.
(フェイズ 1)
各要素が 0 または 1 である H\times W 行列 A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W) を出力してください.
(フェイズ 2)
以下の問題を Q 回解いてください.
ジャッジから整数 N が与えられる.
H 行 W 列のグリッドを考える.上から i 行目,左から j 列目のマス (i,j) には整数 A_{i,j} が書かれている.
好きなマスから開始し,上下左右に隣接するマスへの移動を繰り返して HW 個すべてのマスをちょうど一度ずつ通り,好きなマスで終わる長さ HW の経路 P であって,以下を満たすものを一つ求めよ.
- P で通るマスに書かれた整数を順に並べて得られる長さ HW の数列の転倒数は N である.
制約
- H=4,W=500,Q=200
- 0\leq N\leq 999900
- 入力される数値は全て整数
入出力
この問題はインタラクティブな問題である.
最初に H,W,Q が標準入力から与えられる.
H W Q
(フェイズ 1)
各要素が 0 または 1 である H\times W 行列 A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W) を H 行に渡って出力せよ.
i 行目には,A_{i,1}A_{i,2}\ldots A_{i,W} を長さ W の 0 と 1 からなる文字列として出力せよ.
A_{1,1}A_{1,2}\ldots A_{1,W}
A_{2,1}A_{2,2}\ldots A_{2,W}
\vdots
A_{H,1}A_{H,2}\ldots A_{H,W}
(フェイズ 2)
Q 回問題を解く.各問題では,まず整数 N が標準入力から与えられる.
N
(なお,直前の問題が不正解だったり,A や経路の出力形式が誤っていたりした場合,ここでは N の代わりに -1 が標準入力から与えられる.この場合はただちにプログラムを正常終了すること.ただし,Q 個目の問題で初めて誤った出力を行った場合,その後の入力は何も与えられない.)
その後,条件を満たす経路 P を,i 番目に訪れるマスを (x_i, y_i) として以下の形式で出力せよ.
x_1 y_1
x_2 y_2
\vdots
x_{HW} y_{HW}
x_i,y_i は以下を満たす必要がある.
- 1\leq x_i\leq H, 1\leq y_i\leq W
- i\neq j ならば (x_i,y_i)\neq (x_j,y_j)
- i=1,2,\ldots,HW-1 に対し |x_i-x_{i+1}|+|y_i-y_{i+1}|=1
- 数列 (A_{x_1,y_1}, A_{x_2,y_2},\ldots,A_{x_{HW},y_{HW}}) の転倒数は N
注意点
- ジャッジから
-1という入力を受け取った場合はただちにプログラムを正常終了してください.終了した場合のジャッジ結果は WA となりますが,終了しなかった場合のジャッジ結果は不定です. - 出力を行うたびに,末尾に改行を入れて標準出力を flush してください.そうしない場合,ジャッジ結果が TLE となる可能性があります.
- 余分な改行は不正な形式の出力とみなされるため,行わないでください.
- フェイズ 2 を終了したらただちにプログラムを終了してください.そうしない場合,ジャッジ結果は不定です.
入出力例
以下は,H=3,W=4,Q=2 の場合の入出力例です.
この例は制約を満たさないので,ジャッジには含まれません.
| 入力 | 出力 | 説明 |
|---|---|---|
3 4 2 | H,W,Q が与えられます. | |
1101 0110 1010 | A=\begin{pmatrix} 1&1&0&1 \\ 0&1&1&0 \\ 1&0&1&0 \end{pmatrix} を出力します. | |
10 | 1 つ目の問題の N が与えられます. | |
3 2 3 3 3 4 2 4 1 4 1 3 2 3 2 2 1 2 1 1 2 1 3 1 | (3,2)\to(3,3)\to(3,4)\to(2,4)\to(1,4)\to(1,3)\to(2,3)\to(2,2)\to(1,2)\to(1,1)\to(2,1)\to(3,1) という経路を出力します.この経路で通るマスに書かれた整数を順に並べて得られる数列は (0,1,0,0,1,0,1,1,1,1,0,1) であり,この数列の転倒数は 10 であるため条件を満たします. | |
15 | 2 つ目の問題の N が与えられます. | |
1 1 2 1 3 1 3 2 3 3 3 4 2 4 2 3 2 2 1 2 1 3 1 4 | (1,1)\to(2,1)\to(3,1)\to(3,2)\to(3,3)\to(3,4)\to(2,4)\to(2,3)\to(2,2)\to(1,2)\to(1,3)\to(1,4) という経路を出力します.この経路で通るマスに書かれた整数を順に並べて得られる数列は (1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1) であり,この数列の転倒数は 15 であるため条件を満たします. |
Score : 1000 points
Problem Statement
Consider writing 0 or 1 on each vertex of a 4\times 500 grid graph. Write the values cleverly so that for each N=0,1,\ldots,999899,999900, the following condition is satisfied.
- There exists a Hamiltonian path on the grid graph such that the sequence of numbers written on the vertices along the path has an inversion count of N.
![]()
An example with 3\times 4. With this assignment, N = 10, 11, \ldots, 20 are achievable.
This is an interactive problem. The parameters in the problem statement are fixed at H=4,W=500,Q=200.
You and the judge perform the following steps. The steps consist of Phase 1 and Phase 2; Phase 1 is performed first, immediately followed by Phase 2.
(Phase 1)
Output an H\times W matrix A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W) where each element is 0 or 1.
(Phase 2)
Solve the following problem Q times.
The judge gives you an integer N.
Consider an H-row W-column grid. The cell (i,j), located at the i-th row from the top and the j-th column from the left, has the integer A_{i,j} written on it.
Find a path P of length HW that starts at any cell, moves to an adjacent cell (up, down, left, or right) repeatedly, visits all HW cells exactly once, and ends at any cell, such that the following is satisfied.
- The inversion count of the sequence of length HW obtained by listing the integers written on the cells visited by P in order is N.
Constraints
- H=4,W=500,Q=200
- 0\leq N\leq 999900
- All input values are integers.
Interaction
This is an interactive problem.
First, H,W,Q are given from Standard Input.
H W Q
(Phase 1)
Output an H\times W matrix A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W) where each element is 0 or 1, over H lines.
The i-th line should contain A_{i,1}A_{i,2}\ldots A_{i,W} as a string of length W consisting of 0 and 1.
A_{1,1}A_{1,2}\ldots A_{1,W}
A_{2,1}A_{2,2}\ldots A_{2,W}
\vdots
A_{H,1}A_{H,2}\ldots A_{H,W}
(Phase 2)
Solve the problem Q times. For each problem, first an integer N is given from Standard Input.
N
(Note: if your answer to the previous problem was incorrect, or if A or the path was output in an incorrect format, then instead of N, -1 is given from Standard Input here. In that case, immediately terminate the program normally. However, if an incorrect output is given for the first time in the Q-th problem, no further input will be given.)
Then, output a path P satisfying the condition, where the i-th visited cell is (x_i, y_i), in the following format:
x_1 y_1
x_2 y_2
\vdots
x_{HW} y_{HW}
x_i and y_i must satisfy the following.
- 1\leq x_i\leq H, 1\leq y_i\leq W.
- (x_i,y_i)\neq (x_j,y_j) if i\neq j.
- |x_i-x_{i+1}|+|y_i-y_{i+1}|=1 for i=1,2,\ldots,HW-1.
- The inversion count of the sequence (A_{x_1,y_1}, A_{x_2,y_2},\ldots,A_{x_{HW},y_{HW}}) is N.
Notes
- If you receive
-1as input from the judge, immediately terminate the program normally. If you terminate, the verdict will be WA; otherwise, the verdict is indeterminate. - In each output, include a newline at the end and flush Standard Output. Otherwise, the verdict may be TLE.
- Extra newlines in output are considered as malformed; do not include them.
- Terminate your program immediately after finishing Phase 2. Otherwise, the verdict is indeterminate.
Sample Interaction
Below is a sample interaction for the case H=3,W=4,Q=2.
This example does not satisfy the constraints and is not included in the judge.
| Input | Output | Explanation |
|---|---|---|
3 4 2 | H,W,Q are given. | |
1101 0110 1010 | Output A=\begin{pmatrix} 1&1&0&1 \\ 0&1&1&0 \\ 1&0&1&0 \end{pmatrix}. | |
10 | N for the first problem is given. | |
3 2 3 3 3 4 2 4 1 4 1 3 2 3 2 2 1 2 1 1 2 1 3 1 | Output the path (3,2)\to(3,3)\to(3,4)\to(2,4)\to(1,4)\to(1,3)\to(2,3)\to(2,2)\to(1,2)\to(1,1)\to(2,1)\to(3,1). The sequence of integers written on the cells visited along this path is (0,1,0,0,1,0,1,1,1,1,0,1), and the inversion count of this sequence is 10, so the condition is satisfied. | |
15 | N for the second problem is given. | |
1 1 2 1 3 1 3 2 3 3 3 4 2 4 2 3 2 2 1 2 1 3 1 4 | Output the path (1,1)\to(2,1)\to(3,1)\to(3,2)\to(3,3)\to(3,4)\to(2,4)\to(2,3)\to(2,2)\to(1,2)\to(1,3)\to(1,4). The sequence of integers written on the cells visited along this path is (1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1), and the inversion count of this sequence is 15, so the condition is satisfied. |
実行時間制限: 5 sec / メモリ制限: 1024 MiB
配点 : 1800 点
問題文
1 から N までの番号がついた N 個の仕事があり,これらを N 日間で終わらせたいです. 1 日にちょうど 1 つの仕事を行います.
仕事 i は,L_i 日目から R_i 日目までのいずれかの日に行うことができます. ここで,L_i,R_i に関して,以下の条件が満たされることが保証されます.
- 1 \leq L_i \leq i \leq R_i \leq N
- 1 \leq L_1 \leq L_2 \leq \cdots \leq L_N \leq N
- 1 \leq R_1 \leq R_2 \leq \cdots \leq R_N \leq N
特に最初の条件より,N 個の仕事を N 日間で終わらせることが可能だとわかります.
また,それぞれの仕事には L または R のタイプが定まっています.
最初,すべての仕事はタイプ R です.
仕事を行うコストを以下のように定義します.
- 仕事 i を x_i 日目に行うとする.仕事 i がタイプ
Lならコストを x_i-L_i と定め,タイプRならコストを R_i-x_i と定める.
今から,仕事のタイプを L に変更するクエリが N 回来ます.
i 番目のクエリでは,仕事 P_i のタイプを L に変更します.
各 k=0,1,\ldots,N に対して,次の問題を解いて下さい.
- 最初の k 回のクエリを処理した状態を考える. この時,N 個の仕事のコストの総和としてあり得る最小値を求めよ.
制約
- 1 \leq N \leq 10^6
- 1 \leq L_i \leq i \leq R_i \leq N
- 1 \leq L_1 \leq L_2 \leq \cdots \leq L_N \leq N
- 1 \leq R_1 \leq R_2 \leq \cdots \leq R_N \leq N
- (P_1,P_2,\ldots,P_N) は (1,2,\ldots,N) の順列
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる.
N L_1 R_1 L_2 R_2 \vdots L_N R_N P_1 P_2 \ldots P_N
出力
各 k=0,1,2,\ldots,N に対する答えをこの順に空白区切りで出力せよ.
入力例 1
3 1 3 1 3 2 3 2 1 3
出力例 1
3 1 1 2
各 k に対する最適な仕事のやり方は以下のとおりです.
- k=0: 仕事 1,2,3 をそれぞれ 1,2,3 日目に行う.コストの総和は (3-1)+(3-2)+(3-3)=3 になる.
- k=1: 仕事 1,2,3 をそれぞれ 2,1,3 日目に行う.コストの総和は (3-2)+(1-1)+(3-3)=1 になる.
- k=2: 仕事 1,2,3 をそれぞれ 1,2,3 日目に行う.コストの総和は (1-1)+(2-1)+(3-3)=1 になる.
- k=3: 仕事 1,2,3 をそれぞれ 1,2,3 日目に行う.コストの総和は (1-1)+(2-1)+(3-2)=2 になる.
入力例 2
4 1 4 2 4 3 4 4 4 1 2 3 4
出力例 2
6 3 1 0 0
入力例 3
8 1 5 1 5 2 6 2 6 3 7 3 8 5 8 5 8 5 8 7 3 2 6 4 1
出力例 3
17 13 10 11 7 3 6 10 14
入力例 4
15 1 4 1 4 1 8 2 8 2 11 2 11 3 11 3 11 4 12 5 12 5 13 8 14 11 15 11 15 12 15 15 13 9 3 8 11 6 5 12 2 4 7 14 1 10
出力例 4
44 41 37 29 22 16 12 11 16 20 21 27 35 39 42 49
Score : 1800 points
Problem Statement
There are N jobs numbered 1 through N, and you want to complete them over N days. You perform exactly one job per day.
Job i can be performed on any day from day L_i through day R_i. Here, the following conditions are guaranteed to hold for L_i and R_i:
- 1 \leq L_i \leq i \leq R_i \leq N
- 1 \leq L_1 \leq L_2 \leq \cdots \leq L_N \leq N
- 1 \leq R_1 \leq R_2 \leq \cdots \leq R_N \leq N
Particularly, from the first condition, it follows that it is possible to complete all N jobs over N days.
Furthermore, each job has a type of L or R. Initially, all jobs are of type R.
The cost of performing a job is defined as follows:
- Suppose job i is performed on day x_i. If job i is of type
L, the cost is defined as x_i-L_i; if it is of typeR, the cost is defined as R_i-x_i.
From now on, there will be N queries that change the type of a job to L. In the i-th query, the type of job P_i is changed to L.
For each k=0,1,\ldots,N, solve the following problem:
- Consider the state after processing the first k queries. Find the minimum possible total cost of all N jobs.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq L_i \leq i \leq R_i \leq N
- 1 \leq L_1 \leq L_2 \leq \cdots \leq L_N \leq N
- 1 \leq R_1 \leq R_2 \leq \cdots \leq R_N \leq N
- (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 R_1 L_2 R_2 \vdots L_N R_N P_1 P_2 \ldots P_N
Output
For each k=0,1,2,\ldots,N, output the answer in this order, separated by spaces.
Sample Input 1
3 1 3 1 3 2 3 2 1 3
Sample Output 1
3 1 1 2
An optimal schedule for each k is as follows:
- k=0: Perform jobs 1,2,3 on days 1,2,3 respectively. The total cost is (3-1)+(3-2)+(3-3)=3.
- k=1: Perform jobs 1,2,3 on days 2,1,3 respectively. The total cost is (3-2)+(1-1)+(3-3)=1.
- k=2: Perform jobs 1,2,3 on days 1,2,3 respectively. The total cost is (1-1)+(2-1)+(3-3)=1.
- k=3: Perform jobs 1,2,3 on days 1,2,3 respectively. The total cost is (1-1)+(2-1)+(3-2)=2.
Sample Input 2
4 1 4 2 4 3 4 4 4 1 2 3 4
Sample Output 2
6 3 1 0 0
Sample Input 3
8 1 5 1 5 2 6 2 6 3 7 3 8 5 8 5 8 5 8 7 3 2 6 4 1
Sample Output 3
17 13 10 11 7 3 6 10 14
Sample Input 4
15 1 4 1 4 1 8 2 8 2 11 2 11 3 11 3 11 4 12 5 12 5 13 8 14 11 15 11 15 12 15 15 13 9 3 8 11 6 5 12 2 4 7 14 1 10
Sample Output 4
44 41 37 29 22 16 12 11 16 20 21 27 35 39 42 49