実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
空の列と、N 個のボールがあります。i 個目 (1\leq i\leq N) のボールの大きさは 2^{A_i} です。
これから N 回の操作を行います。
i 回目の操作では、i 個目のボールを列の一番右に付け加えた後、次の手順を繰り返します。
- 列にあるボールが 1 つ以下ならば操作を終了する。
- 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 異なる ならば操作を終了する。
- 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 等しい ならば、2 つのボールを取り除き、「取り除かれた 2 つのボールの大きさの和」の大きさのボール 1 つを列の一番右に付け加える。その後、1. に戻り、手順を繰り返す。
N 回の操作の後で、列にあるボールの数を求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 0\leq A_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
N 回の操作の後で、列にあるボールの数を出力せよ。
入力例 1
7 2 1 1 3 5 3 3
出力例 1
3
操作は次のように行われます。
- 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^2 です。
- 2 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^2, 2^1 です。
- 3 回目の操作の後、列にあるボールは 1 つで、大きさは 2^3 です。これは次のようにして得ることができます。
- 3 回目の操作において 3 個目のボールを付け加えたとき、列にあるボールの大きさは順に 2^2,2^1,2^1 となります。
- 右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^1+2^1=2^2 のボールが追加されます。このとき、列にあるボールの大きさは 2^2, 2^2 となります。
- さらに、再び右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^2+2^2=2^3 のボールが追加され、列にあるボールの大きさは 2^3 となります。
- 4 回目の操作の後、列にあるボールは 1 つで、大きさは 2^4 です。
- 5 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^4, 2^5 です。
- 6 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^3 です。
- 7 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^4 です。
よって、最後に列にあるボールの数である 3 を出力します。
入力例 2
5 0 0 0 1 2
出力例 2
4
操作は次のように行われます。
- 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^0 です。
- 2 回目の操作の後、列にあるボールは 1 つで、大きさは 2^1 です。
- 3 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^1, 2^0 です。
- 4 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^1, 2^0, 2^1 です。
- 5 回目の操作の後、列にあるボールは 4 つで、大きさは順に 2^1, 2^0, 2^1, 2^2 です。
よって、最後に列にあるボールの数である 4 を出力します。
Score: 250 points
Problem Statement
You have an empty sequence and N balls. The size of the i-th ball (1 \leq i \leq N) is 2^{A_i}.
You will perform N operations.
In the i-th operation, you add the i-th ball to the right end of the sequence, and repeat the following steps:
- If the sequence has one or fewer balls, end the operation.
- If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation.
- If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process.
Determine the number of balls remaining in the sequence after the N operations.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the number of balls in the sequence after the N operations.
Sample Input 1
7 2 1 1 3 5 3 3
Sample Output 1
3
The operations proceed as follows:
- After the first operation, the sequence has one ball, of size 2^2.
- After the second operation, the sequence has two balls, of sizes 2^2 and 2^1 in order.
- After the third operation, the sequence has one ball, of size 2^3. This is obtained as follows:
- When the third ball is added during the third operation, the sequence has balls of sizes 2^2, 2^1, 2^1 in order.
- The first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^1 + 2^1 = 2^2 is added. Now, the sequence has balls of sizes 2^2, 2^2.
- Again, the first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^2 + 2^2 = 2^3 is added, leaving the sequence with a ball of size 2^3.
- After the fourth operation, the sequence has one ball, of size 2^4.
- After the fifth operation, the sequence has two balls, of sizes 2^4 and 2^5 in order.
- After the sixth operation, the sequence has three balls, of sizes 2^4, 2^5, 2^3 in order.
- After the seventh operation, the sequence has three balls, of sizes 2^4, 2^5, 2^4 in order.
Therefore, you should print 3, the final number of balls in the sequence.
Sample Input 2
5 0 0 0 1 2
Sample Output 2
4
The operations proceed as follows:
- After the first operation, the sequence has one ball, of size 2^0.
- After the second operation, the sequence has one ball, of size 2^1.
- After the third operation, the sequence has two balls, of sizes 2^1 and 2^0 in order.
- After the fourth operation, the sequence has three balls, of sizes 2^1, 2^0, 2^1 in order.
- After the fifth operation, the sequence has four balls, of sizes 2^1, 2^0, 2^1, 2^2 in order.
Therefore, you should print 4, the final number of balls in the sequence.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
整数 N が与えられます。N の各桁の数字を取り出して並べ(並べる順序は好きに変えてよい)、2 つの正整数に分離することを考えましょう。
例えば、123 という整数に対しては以下の 6 通りの分離の仕方が考えられます。
- 12 と 3
- 21 と 3
- 13 と 2
- 31 と 2
- 23 と 1
- 32 と 1
なお、ここで分離されたあとの 2 整数に leading zero が含まれていてはなりません。例えば、101 という整数を 1 と 01 の 2 つに分離することはできません。また上述の「正整数に分離する」という条件より、101 を 11 と 0 の 2 つに分離することもできません。
適切に N を分離したとき、分離後の 2 数の積の最大値はいくらになりますか?
制約
- N は 1 以上 10^9 以下の整数
- N には 0 でない桁が 2 つ以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
N
出力
分離後の 2 数の積の最大値を出力せよ。
入力例 1
123
出力例 1
63
問題文中にある通り、以下の 6 通りの分離の仕方が考えられます。
- 12 と 3
- 21 と 3
- 13 と 2
- 31 と 2
- 23 と 1
- 32 と 1
積はそれぞれ 36, 63, 26, 62, 23, 32 であり、この中の最大値は 63 です。
入力例 2
1010
出力例 2
100
考えられる分離の仕方は以下の 2 通りです。
- 100 と 1
- 10 と 10
いずれの場合にも積は 100 となります。
入力例 3
998244353
出力例 3
939337176
Score : 300 points
Problem Statement
You are given an integer N. Consider permuting the digits in N and separate them into two positive integers.
For example, for the integer 123, there are six ways to separate it, as follows:
- 12 and 3,
- 21 and 3,
- 13 and 2,
- 31 and 2,
- 23 and 1,
- 32 and 1.
Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer 101 into 1 and 01. Additionally, since the resulting integers must be positive, it is not allowed to separate 101 into 11 and 0, either.
What is the maximum possible product of the two resulting integers, obtained by the optimal separation?
Constraints
- N is an integer between 1 and 10^9 (inclusive).
- N contains two or more digits that are not 0.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible product of the two integers after separation.
Sample Input 1
123
Sample Output 1
63
As described in Problem Statement, there are six ways to separate it:
- 12 and 3,
- 21 and 3,
- 13 and 2,
- 31 and 2,
- 23 and 1,
- 32 and 1.
The products of these pairs, in this order, are 36, 63, 26, 62, 23, 32, with 63 being the maximum.
Sample Input 2
1010
Sample Output 2
100
There are two ways to separate it:
- 100 and 1,
- 10 and 10.
In either case, the product is 100.
Sample Input 3
998244353
Sample Output 3
939337176
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
空の数列 A があります。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。
-
1 x: A に x を追加する。 -
2 x k: A の x 以下の要素のうち、大きい方から k 番目の値を出力する。(k は \bf{5} 以下)
ただし、A に x 以下の要素が k 個以上存在しないときは-1と出力する。 -
3 x k: A の x 以上の要素のうち、小さい方から k 番目の値を出力する。(k は \bf{5} 以下)
ただし、A に x 以上の要素が k 個以上存在しないときは-1と出力する。
制約
- 1\leq Q \leq 2\times 10^5
- 1\leq x\leq 10^{18}
- 1\leq k\leq 5
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
i 番目のクエリ \text{query}_i では、まずクエリの種類 c_i (1,2,3 のいずれか) が与えられる。
c_i=1 の場合は x が追加で与えられ、c_i=2,3 の場合は x,k が追加で与えられる。
すなわち、各クエリは以下に示す 3 つの形式のいずれかである。
1 x
2 x k
3 x k
出力
c_i=2,3 を満たすクエリの個数を q として q 行出力せよ。
j(1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。
入力例 1
11 1 20 1 10 1 30 1 20 3 15 1 3 15 2 3 15 3 3 15 4 2 100 5 1 1 2 100 5
出力例 1
20 20 30 -1 -1 1
\text{query}_{1,2,3,4} が終了した段階で、A=(20,10,30,20) となっています。
\text{query}_{5,6,7} について、A の 15 以上の要素は (20,30,20) です。
このうち小さい方から 1 番目の値は 20 、2 番目の値は 20 、3 番目の値は 30 です。
Score : 400 points
Problem Statement
We have an empty sequence A.
Given Q queries, process them in order.
Each query is of one of the following three types.
-
1 x: Insert x to A. -
2 x k: Among the elements of A that are less than or equal to x, print the k-th largest value. (k is no more than \bf{5})
If there are less than k elements of A that are less than or equal to x, then print-1. -
3 x k: Among the elements of A that are greater than or equal to x, print the k-th smallest value. (k is no more than \bf{5})
If there are less than k elements of A that are greater than or equal to x, then print-1.
Constraints
- 1\leq Q \leq 2\times 10^5
- 1\leq x\leq 10^{18}
- 1\leq k\leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
In the i-th query \text{query}_i, the type of query c_i (which is either 1, 2, or 3) is given first.
If c_i=1, then x is additionally given; if c_i=2, 3, then x and k are additionally given.
In other words, each query is given in one of the following three formats:
1 x
2 x k
3 x k
Output
Print q lines, where q is the number of queries such that c_i=2,3.
The j-th line (1\leq j\leq q) should contain the answer for the j-th such query.
Sample Input 1
11 1 20 1 10 1 30 1 20 3 15 1 3 15 2 3 15 3 3 15 4 2 100 5 1 1 2 100 5
Sample Output 1
20 20 30 -1 -1 1
After \text{query}_{1,2,3,4} have been processed, we have A=(20,10,30,20).
For \text{query}_{5,6,7}, the elements of A greater than or equal to 15 are (20,30,20).
The 1-st smallest value of them is 20; the 2-nd is 20; the 3-rd is 30.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
N 頂点 M 辺の連結な無向グラフがあり、 i 番目の辺は頂点 U_i と頂点 V_i を双方向に結びます。
また、全ての頂点に整数が書いてあり、頂点 v には整数 A_v が書かれています。
頂点 1 から頂点 N への単純なパス ( 同じ頂点を複数回通らないパス ) に対して、以下のように得点を定めます。
- パス上の頂点に書かれた整数を通った順に並べた数列 を S とする。
- S が広義単調増加になっていない場合、そのパスの得点は 0 である。
- そうでない場合、 S に含まれる整数の種類数が得点となる。
頂点 1 から頂点 N への全ての単純なパスのうち、最も得点が高いものを求めてその得点を出力してください。
S が広義単調増加であるとは?
長さ l の数列 S=(S_1,S_2,\dots,S_l) が広義単調増加であるとは、 全ての整数 1 \le i < l について S_i \le S_{i+1} を満たすことを言います。制約
- 入力は全て整数
- 2 \le N \le 2 \times 10^5
- N-1 \le M \le 2 \times 10^5
- 1 \le A_i \le 2 \times 10^5
- グラフは連結である
- 1 \le U_i < V_i \le N
- i \neq j なら (U_i,V_i) \neq (U_j,V_j)
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
答えを整数として出力せよ。
入力例 1
5 6 10 20 30 40 50 1 2 1 3 2 5 3 4 3 5 4 5
出力例 1
4
1 \rightarrow 3 \rightarrow 4 \rightarrow 5 というパスについて S=(10,30,40,50) となり、このパスの得点は 4 で、これが最大です。
入力例 2
4 5 1 10 11 4 1 2 1 3 2 3 2 4 3 4
出力例 2
0
頂点 1 から頂点 N への単純パスであって、 S が広義単調増加となるものはありません。この場合、最大の得点は 0 です。
入力例 3
10 12 1 2 3 3 4 4 4 6 5 7 1 3 2 9 3 4 5 6 1 2 8 9 4 5 8 10 7 10 4 6 2 8 6 7
出力例 3
5
Score : 525 points
Problem Statement
There is a connected undirected graph with N vertices and M edges, where the i-th edge connects vertex U_i and vertex V_i bidirectionally.
Each vertex has an integer written on it, with integer A_v written on vertex v.
For a simple path from vertex 1 to vertex N (a path that does not pass through the same vertex multiple times), the score is determined as follows:
- Let S be the sequence of integers written on the vertices along the path, listed in the order they are visited.
- If S is not non-decreasing, the score of that path is 0.
- Otherwise, the score is the number of distinct integers in S.
Find the path from vertex 1 to vertex N with the highest score among all simple paths and print that score.
What does it mean for S to be non-decreasing?
A sequence S=(S_1,S_2,\dots,S_l) of length l is said to be non-decreasing if and only if S_i \le S_{i+1} for all integers 1 \le i < l.Constraints
- All input values are integers.
- 2 \le N \le 2 \times 10^5
- N-1 \le M \le 2 \times 10^5
- 1 \le A_i \le 2 \times 10^5
- The graph is connected.
- 1 \le U_i < V_i \le N
- (U_i,V_i) \neq (U_j,V_j) if i \neq j.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
Print the answer as an integer.
Sample Input 1
5 6 10 20 30 40 50 1 2 1 3 2 5 3 4 3 5 4 5
Sample Output 1
4
The path 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 has S=(10,30,40,50) for a score of 4, which is the maximum.
Sample Input 2
4 5 1 10 11 4 1 2 1 3 2 3 2 4 3 4
Sample Output 2
0
There is no simple path from vertex 1 to vertex N such that S is non-decreasing. In this case, the maximum score is 0.
Sample Input 3
10 12 1 2 3 3 4 4 4 6 5 7 1 3 2 9 3 4 5 6 1 2 8 9 4 5 8 10 7 10 4 6 2 8 6 7
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
正整数 N,M および、各要素が 1 以上 N 以下の整数である長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。
各要素が 1 以上 M 以下の整数である長さ N の数列 x=(x_1,x_2,\dots,x_N) のうち、以下の条件を満たすものの数を 998244353 で割った余りを求めてください。
- 全ての i\ (1\leq i\leq N) について、x_i \leq x_{A_i}
制約
- 1\leq N,M \leq 2025
- 1\leq A_i\leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 3 2 1 1
出力例 1
6
x=(1,1,1),(2,2,1),(2,2,2),(3,3,1),(3,3,2),(3,3,3) が条件を満たします。
入力例 2
4 9 1 1 1 1
出力例 2
2025
入力例 3
10 5 9 4 5 5 4 2 1 5 7 2
出力例 3
10010
Score : 550 points
Problem Statement
You are given positive integers N, M, and a sequence A = (A_1, A_2, \dots, A_N) of length N, each element being an integer between 1 and N, inclusive.
Find the number, modulo 998244353, of sequences x = (x_1, x_2, \dots, x_N) of length N, each element being an integer between 1 and M, inclusive, that satisfy the following condition:
- x_i \leq x_{A_i} for every i (1 \leq i \leq N).
Constraints
- 1 \leq N, M \leq 2025
- 1 \leq A_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
3 3 2 1 1
Sample Output 1
6
The sequences x=(1,1,1),(2,2,1),(2,2,2),(3,3,1),(3,3,2),(3,3,3) satisfy the condition.
Sample Input 2
4 9 1 1 1 1
Sample Output 2
2025
Sample Input 3
10 5 9 4 5 5 4 2 1 5 7 2
Sample Output 3
10010