Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。
- 魔法 A :箱の中にボールを 1 つ増やす
- 魔法 B :箱の中のボールの数を 2 倍にする
合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。  
魔法以外の方法でボールの数を変化させることはできません。
制約
- 1 \leq N \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
A , B のみからなる文字列 S を出力せよ。
S の i 文字目が A ならば、髙橋君が i 回目に使う魔法が魔法 A であることを表し、B ならば魔法 B であることを表す。
S の長さは \mathbf{120} 以下でなければならない。
入力例 1
5
出力例 1
AABA
ボールの数は、0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5 と変化します。
AAAAA などの答えも正解になります。  
入力例 2
14
出力例 2
BBABBAAAB
ボールの数は、0 \xrightarrow{B} 0 \xrightarrow{B} 0  \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4  \xrightarrow{A}5  \xrightarrow{A}6 \xrightarrow{A} 7  \xrightarrow{B}14 と変化します。
S の長さを最小化する必要はありません。
Score : 300 points
Problem Statement
We have an empty box.
Takahashi can cast the following two spells any number of times in any order.
- Spell A: puts one new ball into the box.
- Spell B: doubles the number of balls in the box.
Tell us a way to have exactly N balls in the box with at most \mathbf{120} casts of spells.
It can be proved that there always exists such a way under the Constraints given.
There is no way other than spells to alter the number of balls in the box.
Constraints
- 1 \leq N \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print a string S consisting of A and B.
The i-th character of S should represent the spell for the i-th cast.
S must have at most \mathbf{120} characters.
Sample Input 1
5
Sample Output 1
AABA
This changes the number of balls as follows: 0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5.
There are also other acceptable outputs, such as AAAAA.
Sample Input 2
14
Sample Output 2
BBABBAAAB
This changes the number of balls as follows: 0 \xrightarrow{B} 0 \xrightarrow{B} 0  \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4  \xrightarrow{A}5  \xrightarrow{A}6 \xrightarrow{A} 7  \xrightarrow{B}14.
It is not required to minimize the length of S.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、0 と 1 からなる長さ N の文字列 S によって表され、
S の i 文字目が 0 であるとき i 番目の人が子供であることを、1 であるとき i 番目の人が大人であることをさします。
ロボットである高橋君に対して実数 X を設定すると、
高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。
X が実数全体を動くとき、f(X) の最大値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- S は 0と1からなる長さ N の文字列
- 1\leq W_i\leq 10^9
- N,W_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S W_1 W_2 \ldots W_N
出力
f(X) の最大値を整数で一行に出力せよ。
入力例 1
5 10101 60 45 30 40 80
出力例 1
4
X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。
よって、f(50)=4 です。
5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。
入力例 2
3 000 1 2 3
出力例 2
3
例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。
入力例 3
5 10101 60 50 50 50 60
出力例 3
4
例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。
Score : 300 points
Problem Statement
There are N people, each of whom is either a child or an adult.  The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1.
If the i-th character of S is 0, then the i-th person is a child; if it is 1, then the i-th person is an adult.  
When Takahashi the robot is given a real number X,
Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.
Find the maximum value of f(X) for all real values of X.
Constraints
- 1\leq N\leq 2\times 10^5
- S is a string of length N consisting of 0and1.
- 1\leq W_i\leq 10^9
- N and W_i are integers.
Input
Input is given from Standard Input in the following format:
N S W_1 W_2 \ldots W_N
Output
Print the maximum value of f(X) as an integer in a single line.
Sample Input 1
5 10101 60 45 30 40 80
Sample Output 1
4
When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged.
Thus, f(50)=4.
This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.
Sample Input 2
3 000 1 2 3
Sample Output 2
3
For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.
Sample Input 3
5 10101 60 50 50 50 60
Sample Output 3
4
For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ n の数列 S = (s_1, s_2, \dots, s_n) がすべての i (1 \leq i \leq n - 1) に対して s_i \leq s_{i+1} を満たすとき、かつそのときに限り「数列 S は広義単調増加である」と呼びます。
広義単調増加な長さ N の整数列 A = (a_1, a_2, \dots, a_N), B = (b_1, b_2, \dots, b_N) が与えられます。
このとき、次の条件を満たす広義単調増加な長さ N の整数列 C = (c_1, c_2, \dots, c_N) を考えます。
- すべての i (1 \leq i \leq N) に対して a_i \leq c_i \leq b_i が成り立つ。
整数列 C としてあり得る数列の個数を 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N \leq 3000
- 0 \leq a_i \leq b_i \leq 3000 (1 \leq i \leq N)
- 整数列 A,B は広義単調増加である。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N a_1 a_2 \dots a_N b_1 b_2 \dots b_N
出力
C としてあり得る数列の個数を 998244353 で割ったあまりを出力せよ。
入力例 1
2 1 1 2 3
出力例 1
5
C としてあり得る数列は次の 5 個です。
- (1, 1)
- (1, 2)
- (1, 3)
- (2, 2)
- (2, 3)
数列 (2, 1) は広義単調増加でないため条件を満たさないことに注意してください。
入力例 2
3 2 2 2 2 2 2
出力例 2
1
C としてあり得る数列は次の 1 個です。
- (2, 2, 2)
入力例 3
10 1 2 3 4 5 6 7 8 9 10 1 4 9 16 25 36 49 64 81 100
出力例 3
978222082
個数を 998244353 で割ったあまりを求めることに注意してください。
Score : 400 points
Problem Statement
A sequence of n numbers, S = (s_1, s_2, \dots, s_n), is said to be non-decreasing if and only if s_i \leq s_{i+1} holds for every i (1 \leq i \leq n - 1).
Given are non-decreasing sequences of N integers each: A = (a_1, a_2, \dots, a_N) and B = (b_1, b_2, \dots, b_N).
Consider a non-decreasing sequence of N integers, C = (c_1, c_2, \dots, c_N), that satisfies the following condition:
- a_i \leq c_i \leq b_i for every i (1 \leq i \leq N).
Find the number, modulo 998244353, of sequences that can be C.
Constraints
- 1 \leq N \leq 3000
- 0 \leq a_i \leq b_i \leq 3000 (1 \leq i \leq N)
- The sequences A and B are non-decreasing.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N a_1 a_2 \dots a_N b_1 b_2 \dots b_N
Output
Print the number, modulo 998244353, of sequences that can be C.
Sample Input 1
2 1 1 2 3
Sample Output 1
5
There are five sequences that can be C, as follows.
- (1, 1)
- (1, 2)
- (1, 3)
- (2, 2)
- (2, 3)
Note that (2, 1) does not satisfy the condition since it is not non-decreasing.
Sample Input 2
3 2 2 2 2 2 2
Sample Output 2
1
There is one sequence that can be C, as follows.
- (2, 2, 2)
Sample Input 3
10 1 2 3 4 5 6 7 8 9 10 1 4 9 16 25 36 49 64 81 100
Sample Output 3
978222082
Be sure to find the count modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の根付き木が与えられます。頂点 1 が根です。
i = 1, 2, \ldots, N-1 について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。
i = 1, 2, \ldots, N について、頂点 i を根とする部分木に含まれる頂点全体からなる集合を S_i で表します。(各頂点は自身を根とする部分木に含まれます。すなわち、i \in S_i です。)
また、整数 l, r について、l 以上 r 以下の整数全体からなる集合を [l, r] で表します。 すなわち、[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace です。
整数の 2 つ組を N 個並べた列 \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) であって以下の条件を満たすものを考えます。
- 1 \leq i \leq N を満たすすべての整数 i について、1 \leq L_i \leq R_i
- 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について次が成り立つ- S_i \subseteq S_j ならば、[L_i, R_i] \subseteq [L_j, R_j]
- S_i \cap S_j = \emptyset ならば、[L_i, R_i] \cap [L_j, R_j] = \emptyset
 
そのような \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) が少なくとも 1 つ存在することが示せます。 それらのうち、登場する整数の最大値 \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace が最小のものを 1 つ出力してください。(複数ある場合はどれを出力しても正解となります。)
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- 入力はすべて整数
- 与えられるグラフは木である
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
下記の形式で N 行出力せよ。すなわち、i = 1, 2, \ldots, N について、i 行目に L_i と R_i を空白区切りで出力せよ。
L_1 R_1 L_2 R_2 \vdots L_N R_N
入力例 1
3 2 1 3 1
出力例 1
1 2 2 2 1 1
(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) が問題文中の条件を満たします。
実際、[L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset が成り立ちます。
また、\max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 であり、これが最小です。
入力例 2
5 3 4 5 4 1 2 1 4
出力例 2
1 3 3 3 2 2 1 2 1 1
入力例 3
5 4 5 3 2 5 2 3 1
出力例 3
1 1 1 1 1 1 1 1 1 1
Score : 500 points
Problem Statement
You are given a rooted tree with N vertices. The root is Vertex 1.
For each i = 1, 2, \ldots, N-1, the i-th edge connects Vertex u_i and Vertex v_i.
For each i = 1, 2, \ldots, N, let S_i denote the set of all vertices in the subtree rooted at Vertex i. (Each vertex is in the subtree rooted at itself, that is, i \in S_i.)
Additionally, for integers l and r, let [l, r] denote the set of all integers between l and r, that is, [l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace.
Consider a sequence of N pairs of integers \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) that satisfies the conditions below.
- 1 \leq L_i \leq R_i for every integer i such that 1 \leq i \leq N.
- The following holds for every pair of integers (i, j) such that 1 \leq i, j \leq N.- [L_i, R_i] \subseteq [L_j, R_j] if S_i \subseteq S_j
- [L_i, R_i] \cap [L_j, R_j] = \emptyset if S_i \cap S_j = \emptyset
 
It can be shown that there is at least one sequence \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big). Among those sequences, print one that minimizes \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace, the maximum integer used. (If there are multiple such sequences, you may print any of them.)
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- All values in input are integers.
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Print N lines in the format below. That is, for each i = 1, 2, \ldots, N, the i-th line should contain L_i and R_i separated by a space.
L_1 R_1 L_2 R_2 \vdots L_N R_N
Sample Input 1
3 2 1 3 1
Sample Output 1
1 2 2 2 1 1
(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) satisfies the conditions.
Indeed, we have [L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset.
Additionally, \max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 is the minimum possible value.
Sample Input 2
5 3 4 5 4 1 2 1 4
Sample Output 2
1 3 3 3 2 2 1 2 1 1
Sample Input 3
5 4 5 3 2 5 2 3 1
Sample Output 3
1 1 1 1 1 1 1 1 1 1
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