実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
H 行 W 列のマス目があり、そのうち二つの異なるマスに駒が置かれています。
マス目の状態は H 個の長さ W の文字列 S_1, \dots, S_H で表されます。S_{i, j} = o
ならば i 行目 j 列目のマスに駒が置かれていることを、S_{i, j} = -
ならばそのマスには駒が置かれていないことを表します。なお、S_{i, j} は文字列 S_i の j 文字目を指します。
一方の駒をマス目の外側に出ないように上下左右の隣接するマスに動かすことを繰り返すとき、もう一方の駒と同じマスに移動させるためには最小で何回動かす必要がありますか?
制約
- 2 \leq H, W \leq 100
- H, W は整数
- S_i \, (1 \leq i \leq H) は
o
および-
のみからなる長さ W の文字列 - S_{i, j} =
o
となる整数 1 \leq i \leq H, 1 \leq j \leq W の組がちょうど二つ存在する
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 \vdots S_H
出力
答えを出力せよ。
入力例 1
2 3 --o o--
出力例 1
3
1 行目 3 列目に置かれている駒を 下 \rightarrow 左 \rightarrow 左 と移動すると 3 回でもう一方の駒と同じマスに移動させることができます。2 回以下で移動させることはできないので、3 を出力します。
入力例 2
5 4 -o-- ---- ---- ---- -o--
出力例 2
4
Score : 200 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns, in which two distinct squares have a piece.
The state of the squares is represented by H strings S_1, \dots, S_H of length W. S_{i, j} = o
means that there is a piece in the square at the i-th row from the top and j-th column from the left; S_{i, j} = -
means that the square does not have a piece. Here, S_{i, j} denotes the j-th character of the string S_i.
Consider repeatedly moving one of the pieces to one of the four adjacent squares. It is not allowed to move the piece outside the grid. How many moves are required at minimum for the piece to reach the square with the other piece?
Constraints
- 2 \leq H, W \leq 100
- H and W are integers.
- S_i \, (1 \leq i \leq H) is a string of length W consisting of
o
and-
. - There exist exactly two pairs of integers 1 \leq i \leq H, 1 \leq j \leq W such that S_{i, j} =
o
.
Input
Input is given from Standard Input in the following format:
H W S_1 \vdots S_H
Output
Print the answer.
Sample Input 1
2 3 --o o--
Sample Output 1
3
The piece at the 1-st row from the top and 3-rd column from the left can reach the square with the other piece in 3 moves: down, left, left. Since it is impossible to do so in two or less moves, 3 should be printed.
Sample Input 2
5 4 -o-- ---- ---- ---- -o--
Sample Output 2
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 頂点 N-1 辺の木が与えられます。
頂点には 1,2,\ldots,N の番号がついており、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。
この木がスターであるか判定してください。
ただしスターとは、1 つの頂点から、他の全ての頂点に 1 本ずつ辺が出ている木のことです。
注記
「木」については、Wikipedia「木(数学)」 を参照してください。
制約
- 3 \leq N \leq 10^5
- 1 \leq a_i \lt b_i \leq N
- 与えられるグラフは木である
入力
入力は以下の形式で標準入力から与えられる。
N a_1 b_1 \vdots a_{N-1} b_{N-1}
出力
与えられたグラフがスターであるなら Yes
と、スターでないなら No
と出力せよ。
入力例 1
5 1 4 2 4 3 4 4 5
出力例 1
Yes
与えられたグラフはスターです。
入力例 2
4 2 4 1 4 2 3
出力例 2
No
与えられたグラフはスターではありません。
入力例 3
10 9 10 3 10 4 10 8 10 1 10 2 10 7 10 6 10 5 10
出力例 3
Yes
Score : 200 points
Problem Statement
You are given a tree with N vertices and N-1 edges.
The vertices are numbered 1,2,\ldots,N. The i-th edge connects Vertex a_i and Vertex b_i.
Determine whether this tree is a star.
Here, a star is a tree where there is a vertex directly connected to all other vertices.
Notes
For the definition of a tree, see Tree (graph theory) - Wikipedia.
Constraints
- 3 \leq N \leq 10^5
- 1 \leq a_i \lt b_i \leq N
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N a_1 b_1 \vdots a_{N-1} b_{N-1}
Output
If the given graph is a star, print Yes
; otherwise, print No
.
Sample Input 1
5 1 4 2 4 3 4 4 5
Sample Output 1
Yes
The given graph is a star.
Sample Input 2
4 2 4 1 4 2 3
Sample Output 2
No
The given graph is not a star.
Sample Input 3
10 9 10 3 10 4 10 8 10 1 10 2 10 7 10 6 10 5 10
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 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.
実行時間制限: 2 sec / メモリ制限: 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
0
and1
. - 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.
実行時間制限: 2 sec / メモリ制限: 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.