Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数 N, K が与えられます.
長さ N の整数列 X=(X_1,X_2,\dots ,X_N) の累積和とは, 次のように定まる長さ N+1 の数列 Y=(Y_0,Y_1,\dots ,Y_N) のことです.
- Y_0=0
- Y_i=\displaystyle\sum_{j=1}^{i}X_j\ (i=1,2,\dots ,N)
長さ N の整数列 X=(X_1,X_2,\dots ,X_N) が良い数列であるとは, 次の条件を満たすことを言います.
- X の累積和の要素の任意の K 未満の値は, 任意の K 以上の値よりも前に現れる.
- 正確には, X の累積和を Y として, 0\le i,j\le N なる任意の整数組 (i,j) について, (Y_i\lt K かつ Y_j\ge K) ならば i\lt j が成り立つ.
長さ N の整数列 A=(A_1,A_2,\dots ,A_N) が与えられます. A の要素を自由に並べ替えることで A を良い数列にできるかどうか判定し, できる場合は並べ替えた後の A としてあり得るものをひとつ出力してください.
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq K \leq 10^9
- -10^9 \leq A_i \leq 10^9
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられる.
N K A_1 A_2 \cdots A_N
出力
A の要素を自由に並べ替えることで A を良い数列にできる場合は, 並べ替えた後の A を (A^{\prime}_1,A^{\prime}_2,\dots ,A^{\prime}_N) として, 次の形式で出力せよ.
Yes A^{\prime}_1 A^{\prime}_2 \cdots A^{\prime}_N
なお, 条件を満たす並べ替えが複数存在する場合は, どれを出力しても正答となる.
良い数列にできない場合は No
を出力せよ.
入力例 1
4 1 -1 2 -3 4
出力例 1
Yes -3 -1 2 4
A を並べ替えて (-3,-1,2,4) とすると, 問題文中の条件における Y は (0,-3,-4,-2,2) となります. この Y に現れる任意の 1 未満の値は, Y に現れる任意の 1 以上の値より前に現れています.
入力例 2
4 -1 1 -2 3 -4
出力例 2
No
入力例 3
10 1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 3
Yes -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Score : 300 points
Problem Statement
You are given integers N and K.
The cumulative sums of an integer sequence X=(X_1,X_2,\dots ,X_N) of length N is defined as a sequence Y=(Y_0,Y_1,\dots ,Y_N) of length N+1 as follows:
- Y_0=0
- Y_i=\displaystyle\sum_{j=1}^{i}X_j\ (i=1,2,\dots ,N)
An integer sequence X=(X_1,X_2,\dots ,X_N) of length N is called a good sequence if and only if it satisfies the following condition:
- Any value in the cumulative sums of X that is less than K appears before any value that is not less than K.
- Formally, for the cumulative sums Y of X, for any pair of integers (i,j) such that 0 \le i,j \le N, if (Y_i < K and Y_j \ge K), then i < j.
You are given an integer sequence A=(A_1,A_2,\dots ,A_N) of length N. Determine whether the elements of A can be rearranged to a good sequence. If so, print one such rearrangement.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq K \leq 10^9
- -10^9 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \cdots A_N
Output
If the elements of A can be rearranged to a good sequence, print the rearranged sequence (A^{\prime}_1,A^{\prime}_2,\dots ,A^{\prime}_N) in the following format:
Yes A^{\prime}_1 A^{\prime}_2 \cdots A^{\prime}_N
If there are multiple valid rearrangements, any of them is considered correct.
If a good sequence cannot be obtained, print No
.
Sample Input 1
4 1 -1 2 -3 4
Sample Output 1
Yes -3 -1 2 4
If you rearrange A to (-3,-1,2,4), the cumulative sums Y in question will be (0,-3,-4,-2,2). In this Y, any value less than 1 appears before any value not less than 1.
Sample Input 2
4 -1 1 -2 3 -4
Sample Output 2
No
Sample Input 3
10 1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
Yes -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1 以上 M 以下の整数からなる長さ M の数列 (X_1,X_2,\dots ,X_M) が与えられます.
1 以上 M 以下の整数からなる長さ N の数列 A=(A_1,A_2,\dots ,A_N) であって, 以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください.
- B=1,2,\dots ,M について, A の中で異なる位置にある 2 つの B の間(両端を含む)には X_B が存在する.
より正確には, B=1,2,\dots ,M について次の条件が成り立つことを言います.
- 1\le l\lt r\le N かつ A_l=A_r=B を満たすすべての整数組 (l,r) に対して, ある整数 m\ (l\le m\le r) が存在して, A_m=X_B が成り立つ.
制約
- 1\leq M\leq 10
- 1 \leq N \leq 10^4
- 1 \leq X_i \leq M
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられる.
M N X_1 X_2 \cdots X_M
出力
答えを出力せよ.
入力例 1
3 4 2 1 2
出力例 1
14
条件を満たす A としては例えば次のものが挙げられます.
- (1,3,2,3)
- (2,1,2,1)
- (3,2,1,3)
逆に, 次のものは条件を満たしません.
- (1,3,1,3)
- 3 の間に X_3=2 がありません
- (2,2,1,3)
- 2 の間に X_2=1 がありません
入力例 2
4 8 1 2 3 4
出力例 2
65536
1 以上 4 以下の整数からなる長さ 8 の数列はすべて条件を満たします.
X_B=B のとき, 異なる位置にある B の間には必ず B が存在することに注意してください.
入力例 3
4 9 2 3 4 1
出力例 3
628
Score : 500 points
Problem Statement
You are given a sequence (X_1, X_2, \dots, X_M) of length M consisting of integers between 1 and M, inclusive.
Find the number, modulo 998244353, of sequences A = (A_1, A_2, \dots, A_N) of length N consisting of integers between 1 and M, inclusive, that satisfy the following condition:
- For each B = 1, 2, \dots, M, the value X_B exists between any two different occurrences of B in A (including both ends).
More formally, for each B = 1, 2, \dots, M, the following condition must hold:
- For every pair of integers (l, r) such that 1 \leq l < r \leq N and A_l = A_r = B, there exists an integer m (l \leq m \leq r) such that A_m = X_B.
Constraints
- 1 \leq M \leq 10
- 1 \leq N \leq 10^4
- 1 \leq X_i \leq M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
M N X_1 X_2 \cdots X_M
Output
Print the answer.
Sample Input 1
3 4 2 1 2
Sample Output 1
14
Here are examples of sequences A that satisfy the condition:
- (1, 3, 2, 3)
- (2, 1, 2, 1)
- (3, 2, 1, 3)
Here are non-examples:
- (1, 3, 1, 3)
- There is no X_3 = 2 between the 3s.
- (2, 2, 1, 3)
- There is no X_2 = 1 between the 2s.
Sample Input 2
4 8 1 2 3 4
Sample Output 2
65536
All sequences of length 8 consisting of integers between 1 and 4 satisfy the condition.
Note that when X_B = B, there is always a B between two different occurrences of B.
Sample Input 3
4 9 2 3 4 1
Sample Output 3
628
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です.
正整数 N が与えられます.
ジャッジシステムは正整数 R および N 個の整数 A_1,A_2,\dots ,A_N を隠し持っています. ここで |A_i|\le R,\ \left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R を満たすことが保証されます.
絶対値が R 以下の整数しか書き込むことができない黒板があり, はじめは何も書き込まれていません.
ジャッジシステムは, 黒板に A_1,A_2, \dots ,A_N の値を この順で 書き込みました. あなたは, 黒板にただ 1 つの値 \displaystyle\sum_{i=1}^{N}A_i が書き込まれている状態にする必要があります.
あなたは R および A_i の値を直接知ることはできませんが, その代わりにジャッジシステムに対して次のやり取りを 25000 回まで行うことができます.
正整数 i について, i 番目に黒板に書き込まれた整数を X_i とします. 特に, i=1,2,\dots ,N について X_i=A_i です.
1 回のやり取りでは, 相異なる正整数 i,j を指定し, 次のいずれかを選んで行います.
- 足し算をしてもらう. ジャッジシステムは黒板から X_i,X_j を消し, 新たに X_i+X_j の値を黒板に書き込む.
- |X_i+X_j|\le R を満たしていなくてはならない.
- 大小比較をしてもらう. ジャッジシステムは X_i\lt X_j の真偽を答える.
ただし, 各やり取りを始める時点で i,j 番目に黒板に書き込まれた整数がすでに黒板から消されていてはなりません.
適切にやり取りを行って, 全てのやり取りを終えた後に黒板にただ 1 つの値 \displaystyle\sum_{i=1}^{N}A_i が書き込まれている状態にしてください.
R および A_i はプログラムとジャッジシステムの対話の開始前に決定されます.
制約
- 2\leq N\leq 1000
- 1\leq R\leq 10^9
- |A_i|\leq R
- \left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R
- N,R,A_i は整数.
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です.
最初に, N を標準入力から受け取ってください.
N
次に, 黒板にただ 1 つの値 \displaystyle\sum_{i=1}^{N}A_i が書かれている状態になるまで, やり取りを繰り返してください.
足し算をしてもらうときは, 以下の形式で標準出力に出力してください. 末尾に改行を入れてください. ここで i,j は相異なる正整数です.
+ i j
これに対するジャッジシステムの応答は, 次の形式で標準入力から与えられます.
P
ここで P は整数で,
- P\geq N+1 の場合は, X_i+X_j の値が黒板に書き込まれ, それが P 番目に書き込まれたことを表します.
- P=-1 の場合は, i,j が制約を満たしていないか, やり取りの回数が 25000 回を超えたことを表します.
大小比較をしてもらうときは, 以下の形式で標準出力に出力してください. 末尾に改行を入れてください. ここで i,j は相異なる正整数です.
? i j
これに対するジャッジシステムの応答は, 次の形式で標準入力から与えられます.
Q
ここで Q は整数で,
- Q=1 の場合は, X_i<X_j が真であることを表します.
- Q=0 の場合は, X_i<X_j が偽であることを表します.
- Q=-1 の場合は, i,j が制約を満たしていないか, やり取りの回数が 25000 回を超えたことを表します.
足し算をしてもらうやり取りおよび大小比較をしてもらうやり取りのいずれについても, ジャッジシステムの応答が -1 であった場合は, プログラムはすでに不正解とみなされています. この場合, ただちにプログラムを終了してください.
黒板にただ 1 つの値 \displaystyle\sum_{i=1}^{N}A_i が書かれている状態になったら, 以下の形式でそのことをジャッジシステムに報告してください. ただし, これはジャッジシステムとのやり取りの回数に計上されません. その後, ただちにプログラムを終了してください.
!
上記のいずれの形式にも当てはまらない出力を行った場合は, -1
が標準入力から与えられます.
-1
このときも, プログラムはすでに不正解とみなされています. ただちにプログラムを終了してください.
注意点
- 出力を行うたびに, 末尾に改行を入れて標準出力を flush してください. そうしなかった場合, ジャッジ結果が TLE となる可能性があります.
- 解答を出力したら(または
-1
を受け取ったら)ただちにプログラムを終了してください. そうしない場合, ジャッジ結果は不定です. - 余計な改行は不正なフォーマットの出力とみなされることに注意してください.
入出力例
N=3,R=10,A_1=-1,A_2=10,A_3=1 のときの対話の一例を示します.
入力 | 出力 | 説明 |
---|---|---|
3 |
まず整数 N が与えられます。 | |
? 1 2 |
大小比較をしてもらいます. | |
1 |
X_1\lt X_2\ (-1\lt 10) なのでジャッジシステムは 1 を返します. | |
+ 1 3 |
足し算をしてもらいます. | |
4 |
ジャッジシステムは X_1=-1,X_3=1 を黒板から消し, X_1+X_3=0 の値を黒板に書き込みました. 4 番目の書き込みでした. | |
+ 2 4 |
足し算をしてもらいます. | |
5 |
ジャッジシステムは X_2=10,X_4=0 を黒板から消し, X_2+X_4=10 の値を黒板に書き込みました. 5 番目の書き込みでした. | |
! |
黒板にはただ 1 つの値 \displaystyle\sum_{i=1}^{N}A_i が書き込まれている状態になったので, そのことをジャッジシステムに報告します. |
Score : 500 points
Problem Statement
This is an interactive problem (where your program interacts with the judge via input and output).
You are given a positive integer N.
The judge has a hidden positive integer R and N integers A_1, A_2, \dots, A_N. It is guaranteed that |A_i|\le R and \left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R.
There is a blackboard on which you can write integers with absolute values not exceeding R. Initially, the blackboard is empty.
The judge has written the values A_1, A_2, \dots, A_N on the blackboard in this order. Your task is to make the blackboard contain only one value \displaystyle\sum_{i=1}^{N}A_i.
You cannot learn the values of R and A_i directly, but you can interact with the judge up to 25000 times.
For a positive integer i, let X_i be the i-th integer written on the blackboard. Specifically, X_i = A_i for i=1,2,\dots,N.
In one interaction, you can specify two distinct positive integers i and j and choose one of the following actions:
- Perform addition. The judge will erase X_i and X_j from the blackboard and write X_i + X_j on the blackboard.
- |X_i + X_j| \leq R must hold.
- Perform comparison. The judge will tell you whether X_i < X_j is true or false.
Here, at the beginning of each interaction, the i-th and j-th integers written on the blackboard must not have been erased.
Perform the interactions appropriately so that after all interactions, the blackboard contains only one value \displaystyle\sum_{i=1}^{N}A_i.
The values of R and A_i are determined before the start of the conversation between your program and the judge.
Constraints
- 2 \leq N \leq 1000
- 1 \leq R \leq 10^9
- |A_i| \leq R
- \left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R
- N, R, and A_i are integers.
Input and Output
This is an interactive problem (where your program interacts with the judge via input and output).
First, read N from Standard Input.
N
Next, repeat the interactions until the blackboard contains only one value \displaystyle\sum_{i=1}^{N}A_i.
When performing addition, make an output in the following format to Standard Output. Append a newline at the end. Here, i and j are distinct positive integers.
+ i j
The response from the judge will be given from Standard Input in the following format:
P
Here, P is an integer:
- If P \geq N + 1, it means that the value X_i + X_j has been written on the blackboard, and it is the P-th integer written.
- If P = -1, it means that i and j do not satisfy the constraints, or the number of interactions has exceeded 25000.
When performing comparison, make an output in the following format to Standard Output. Append a newline at the end. Here, i and j are distinct positive integers.
? i j
The response from the judge will be given from Standard Input in the following format:
Q
Here, Q is an integer:
- If Q = 1, it means that X_i < X_j is true.
- If Q = 0, it means that X_i < X_j is false.
- If Q = -1, it means that i and j do not satisfy the constraints, or the number of interactions has exceeded 25000.
For both types of interactions, if the judge's response is -1, your program is already considered incorrect. In this case, terminate your program immediately.
When the blackboard contains only one value \displaystyle\sum_{i=1}^{N}A_i, report this to the judge in the following format. This does not count towards the number of interactions. Then, terminate your program immediately.
!
If you make an output in a format that does not match any of the above, -1
will be given from Standard Input.
-1
In this case, your program is already considered incorrect. Terminate your program immediately.
Notes
- For each output, append a newline at the end and flush Standard Output. Otherwise, the verdict may be TLE.
- Terminate your program immediately after outputting the result (or receiving
-1
). Otherwise, the verdict will be indeterminate. - Extra newlines will be considered as malformed output.
Sample Input and Output
Here is a possible conversation with N=3, R=10, A_1=-1, A_2=10, A_3=1.
Input | Output | Explanation |
---|---|---|
3 |
First, the integer N is given. | |
? 1 2 |
Perform a comparison. | |
1 |
The judge returns 1 because X_1\lt X_2\ (-1\lt 10). | |
+ 1 3 |
Perform an addition. | |
4 |
The judge erases X_1 = -1 and X_3 = 1 from the blackboard and writes X_1 + X_3 = 0. This is the fourth integer written. | |
+ 2 4 |
Perform an addition. | |
5 |
The judge erases X_2 = 10 and X_4 = 0 from the blackboard and writes X_2 + X_4 = 10. This is the fifth integer written. | |
! |
The blackboard contains only one value \displaystyle\sum_{i=1}^{N}A_i, so report this to the judge. |
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
頂点 1,2,\dots ,N の N 頂点からなる木が与えられます. i 番目の辺は頂点 u_i,v_i を双方向に結んでいます.
すべての頂点ははじめ白に塗られています.
この木のすべての頂点を効率よく訪れるべく, Alice は不思議なゲートを発明しました. Alice は駒とゲートを 1 個ずつ用いて次の手順で旅をします.
まず好きな頂点を選び, 駒とゲートをその頂点に置きます. その後, すべての頂点が黒に塗られるまで次の操作を何度も行います.
- 次のうち 1 つを選んで実行する.
- 駒が置かれている頂点を黒に塗る.
- 駒が置かれている頂点に隣接した頂点をひとつ選び, その頂点に駒を移動させる, コストが 1 かかる.
- ゲートが置かれている頂点に駒を移動させる.
- 駒が置かれている頂点にゲートを移動させる.
コストがかかるのは 2 番目の操作のみであることに注意してください.
有限回の操作ですべての頂点を黒に塗ることができることが証明できます. かかるコストの合計の最小値を求めてください.
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq u_i,v_i \leq N
- 与えられるグラフは木である.
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられる.
N u_1 v_1 \vdots u_{N-1} v_{N-1}
出力
答えを出力せよ.
入力例 1
4 1 2 1 3 1 4
出力例 1
3
Alice の手順の一例を示します. 駒が頂点 u にありゲートが頂点 v にある状態を (u,v) と表すことにします.
- 頂点 4 に駒とゲートを置く.
- 状態は (4,4) となる.
- 操作 1 を行う.
- 頂点 4 が黒く塗られる.
- 状態は (4,4) となる.
- 操作 2 を行い, 駒を頂点 1 に移動させる.
- コストが 1 かかる.
- 状態は (1,4) となる.
- 操作 1 を行う.
- 頂点 1 が黒く塗られる.
- 操作 4 を行う.
- 状態は (1,1) となる.
- 操作 2 を行い, 駒を頂点 2 に移動させる.
- コストが 1 かかる.
- 状態は (2,1) となる.
- 操作 1 を行う.
- 頂点 2 が黒く塗られる.
- 操作 3 を行う.
- 状態は (1,1) となる.
- 操作 2 を行い, 駒を頂点 3 に移動させる.
- コストが 1 かかる.
- 状態は (3,1) となる.
- 操作 1 を行う.
- 頂点 3 が黒く塗られる.
- すべての頂点が黒く塗られたので, 操作を終了する.
操作 2 を行った回数は 3 なので, かかるコストの合計は 3 となります. 3 より小さいコストの手順は存在しません.
入力例 2
10 1 7 7 10 10 8 8 3 8 4 10 9 9 6 9 5 7 2
出力例 2
10
Score : 700 points
Problem Statement
You are given a tree with N vertices numbered 1, 2, \dots, N. The i-th edge connects vertices u_i and v_i bidirectionally.
Initially, all vertices are painted white.
To efficiently visit all vertices of this tree, Alice has invented a magical gate. She uses one piece and one gate to travel according to the following procedure.
First, she chooses a vertex and places both the piece and the gate on that vertex. Then, she repeatedly performs the following operations until all vertices are painted black.
- Choose one of the following actions:
- Paint the vertex where the piece is placed black.
- Choose a vertex adjacent to the vertex where the piece is placed and move the piece to that vertex. The cost of this action is 1.
- Move the piece to the vertex where the gate is placed.
- Move the gate to the vertex where the piece is placed.
Note that only the second action incurs a cost.
It can be proved that it is possible to paint all vertices black in a finite number of operations. Find the minimum total cost required.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N u_1 v_1 \vdots u_{N-1} v_{N-1}
Output
Print the answer.
Sample Input 1
4 1 2 1 3 1 4
Sample Output 1
3
Here is an example of Alice's procedure. Let (u, v) denote the state where the piece is at vertex u and the gate is at vertex v.
- Place the piece and the gate at vertex 4.
- The state is now (4, 4).
- Perform action 1.
- Vertex 4 is painted black.
- The state is now (4, 4).
- Perform action 2 and move the piece to vertex 1.
- This costs 1.
- The state is now (1, 4).
- Perform action 1.
- Vertex 1 is painted black.
- Perform action 4.
- The state is now (1, 1).
- Perform action 2 and move the piece to vertex 2.
- This costs 1.
- The state is now (2, 1).
- Perform action 1.
- Vertex 2 is painted black.
- Perform action 3.
- The state is now (1, 1).
- Perform action 2 and move the piece to vertex 3.
- This costs 1.
- The state is now (3, 1).
- Perform action 1.
- Vertex 3 is painted black.
- All vertices are now painted black, so the procedure ends.
The total cost of performing action 2 is 3, and there is no procedure with a smaller cost.
Sample Input 2
10 1 7 7 10 10 8 8 3 8 4 10 9 9 6 9 5 7 2
Sample Output 2
10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
正整数 h,w に対し, 縦の辺の長さが h, 横の辺の長さが w であるような長方形を (h,w) と表すことにします. なお, 本問では長方形を回転する操作は考えず, h\neq w のとき長方形 (h,w) と長方形 (w,h) は異なるものとして扱います.
長方形の列 ((h_1,w_1),(h_2,w_2),\dots ,(h_n,w_n)) が 長方形生成列 であるとは, 次の手順が成功するような方法が存在することを言います.
- 長方形 X を (h_1,w_1) とする. 以下では, 各時点での長方形 X の縦の辺の長さと横の辺の長さをそれぞれ H,W と表す.
- i=2,3,\dots ,n の順に次のいずれか一方の操作を行う. いずれも行うことができないとき手順は失敗とし, 手順を終了する.
- X の縦の辺の長さが h_i に等しいとき, X に長方形 (h_i,w_i) を横向きに結合する. 正確には, その時点で H=h_i のとき X を長方形 (H,W+w_i) に置き換える.
- X の横の辺の長さが w_i に等しいとき, X に長方形 (h_i,w_i) を縦向きに結合する. 正確には, その時点で W=w_i のとき X を長方形 (H+h_i,W) に置き換える.
- 上記の一連の操作が失敗しなかった場合は手順は成功とし, 手順を終了する.
N 個の長方形が与えられます. i 番目の長方形は, 縦の辺の長さが H_i, 横の辺の長さが W_i の長方形です.
1\le l\le r\le N を満たす正整数の組 (l,r) であって次の条件を満たすものの個数を求めてください.
- 長方形の列 ((H_l,W_l),(H_{l+1},W_{l+1}),\dots ,(H_r,W_r)) が長方形生成列である.
制約
- 1\leq N\leq 3\times 10^5
- 1 \leq H_i,W_i \leq 10^6
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられる.
N H_1 W_1 H_2 W_2 \vdots H_N W_N
出力
答えを出力せよ.
入力例 1
4 1 2 1 3 2 3 3 1
出力例 1
7
条件を満たす (l,r) は (1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4) の 7 つです.
例えば, (l,r)=(2,4) については, 結合を縦向き \to 横向きの順に行うと手順が成功します.
入力例 2
5 2 1 2 1 1 2 3 2 1 4
出力例 2
10
入力例 3
1 1000000 1000000
出力例 3
1
入力例 4
10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 4
55
Score : 800 points
Problem Statement
For positive integers h and w, let (h,w) denote a rectangle with height h and width w. In this problem, we do not consider rotating the rectangles, and the rectangles (h,w) and (w,h) are distinguished when h \neq w.
A sequence of rectangles ((h_1,w_1),(h_2,w_2),\dots ,(h_n,w_n)) is called a rectangle generation sequence if there exists a method that successfully follows the steps below:
- Let the rectangle X be (h_1,w_1). Hereafter, let H and W respectively denote the height and width of the rectangle X at each step.
- For i=2,3,\dots ,n, perform one of the following operations. If neither can be performed, the procedure unsuccessfully terminates.
- If the height of X is equal to h_i, attach the rectangle (h_i,w_i) horizontally to X. Formally, if H=h_i at that time, replace X with the rectangle (H,W+w_i).
- If the width of X is equal to w_i, attach the rectangle (h_i,w_i) vertically to X. Formally, if W=w_i at that time, replace X with the rectangle (H+h_i,W).
- If the above series of operations does not fail, the procedure successfully terminates.
You are given N rectangles. The i-th rectangle has a height of H_i and a width of W_i.
Find the number of pairs of positive integers (l,r) that satisfy 1 \le l \le r \le N and the following condition:
- The sequence of rectangles ((H_l,W_l),(H_{l+1},W_{l+1}),\dots ,(H_r,W_r)) is a rectangle generation sequence.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq H_i, W_i \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 W_1 H_2 W_2 \vdots H_N W_N
Output
Print the answer.
Sample Input 1
4 1 2 1 3 2 3 3 1
Sample Output 1
7
The pairs (l,r) that satisfy the condition are (1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(4,4); there are seven.
For example, for (l,r)=(2,4), the procedure succeeds if the first attachment is done vertically and the second is done horizontally.
Sample Input 2
5 2 1 2 1 1 2 3 2 1 4
Sample Output 2
10
Sample Input 3
1 1000000 1000000
Sample Output 3
1
Sample Input 4
10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 4
55
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1000 点
問題文
文字 A
B
のみからなる長さ N の文字列 S が与えられます.
文字 1
2
3
のみからなる長さ N の文字列 X に対して スコア を以下の手順によって定めます.
まず変数 h_1,h_2,h_3,P をそれぞれ 0 で初期化します.
次に i=1,2,\dots ,N の順に次の操作を行います.
S の i 文字目が
A
なら操作Aを,B
なら操作Bを行う. ただし, X の i 文字目が表す数字を d とする.
操作A : h_d に 2 を加算する.
操作B : d=2 または h_d\neq h_2 のとき P を -10^{100} とする. そうでなければ h_d と h_2 にそれぞれ 1 を加算する.
h_1=h_2=h_3 のとき P に 1 を加える.
最後に最終的な P をスコアとします.
スコアを最大にする X を 1 つ出力してください.
T 個のテストケースが与えられるので, それぞれについて解いてください.
制約
- 1\le T\le 10^5
- 1\le N\le 10^6
- S は
A
B
のみからなる長さ N の文字列 - T,N は整数
- すべてのテストケースにおける N の総和は 10^6 以下
入力
入力は以下の形式で標準入力から与えられる. ここで \mathrm{test}_i は i 番目のテストケースを意味する.
T \mathrm{test}_1 \mathrm{test}_2 \vdots \mathrm{test}_T
各テストケースは以下の形式で与えられる.
N S
出力
T 行出力せよ.
i\ (1\le i\le T) 行目には i 番目のテストケースにおけるスコアを最大にする文字列 X を 1 つ出力せよ.
なお, スコアを最大にする X が複数存在する場合は, どれを出力しても正答となる.
入力例 1
5 4 ABBA 5 AAAAA 6 BBBBBB 7 ABABABA 20 AAABBBBBBBBAAABBBABA
出力例 1
1333 12321 333333 1313212 33311111133121111311
手順で i=1,2,\dots ,N と進む際の (h_1,h_2,h_3,P) の変化を述べます.
- 1 番目のテストケースについて, (0,0,0,0)\rightarrow (2,0,0,0)\rightarrow (2,1,1,0)\rightarrow (2,2,2,1)\rightarrow (2,2,4,1) となります. スコアの最大値は 1 です.
- 2 番目のテストケースについて, (0,0,0,0)\rightarrow (2,0,0,0)\rightarrow (2,2,0,0)\rightarrow (2,2,2,1)\rightarrow (2,4,2,1)\rightarrow (4,4,2,1) となります. スコアの最大値は 1 です.
3,4,5 番目のテストケースでは, スコアの最大値はそれぞれ 0,0,2 です. スコアが最大となる X は複数存在する可能性がありますが, そのうちの 1 つを出力してください.
Score : 1000 points
Problem Statement
You are given a string S of length N consisting of the characters A
and B
.
For a string X of length N consisting of the characters 1
, 2
, and 3
, the score is determined by the following procedure:
First, initialize the variables h_1, h_2, h_3, P to 0.
Then, for i = 1, 2, \dots, N in this order, perform the following operations:
If the i-th character of S is
A
, perform operation A; if it isB
, perform operation B. Let d be the number represented by the i-th character of X.
Operation A: Add 2 to h_d.
Operation B: If d = 2 or h_d \neq h_2, set P to -10^{100}. Otherwise, add 1 to both h_d and h_2.
If h_1 = h_2 = h_3, add 1 to P.
The final value of P is the score.
Print one string X that maximizes the score.
You have T test cases to solve.
Constraints
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^6
- S is a string of length N consisting of
A
andB
. - T and N are integers.
- The sum of N across all test cases is at most 10^6.
Input
The input is given from Standard Input in the following format. Here, \mathrm{test}_i denotes the i-th test case.
T \mathrm{test}_1 \mathrm{test}_2 \vdots \mathrm{test}_T
Each test case is given in the following format:
N S
Output
Print T lines.
The i-th line (1 \leq i \leq T) should contain a string X that maximizes the score for the i-th test case.
If multiple strings X maximize the score, any of them is considered correct.
Sample Input 1
5 4 ABBA 5 AAAAA 6 BBBBBB 7 ABABABA 20 AAABBBBBBBBAAABBBABA
Sample Output 1
1333 12321 333333 1313212 33311111133121111311
Let us describe the changes in (h_1, h_2, h_3, P) as we proceed with i = 1, 2, \dots, N.
- For the first test case, (0,0,0,0) \rightarrow (2,0,0,0) \rightarrow (2,1,1,0) \rightarrow (2,2,2,1) \rightarrow (2,2,4,1). The maximum score is 1.
- For the second test case, (0,0,0,0) \rightarrow (2,0,0,0) \rightarrow (2,2,0,0) \rightarrow (2,2,2,1) \rightarrow (2,4,2,1) \rightarrow (4,4,2,1). The maximum score is 1.
For the third, fourth, and fifth test cases, the maximum scores are 0, 0, and 2, respectively. There may be multiple strings X that maximize the score, but you only need to print one of them.