Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
A から偶数だけすべて取り出し、もとの順番を保って出力してください。
制約
- 1\leq N\leq 100
- 1\leq A _ i\leq 100\ (1\leq i\leq N)
- A には 1 つ以上偶数が含まれる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
A から偶数を取り出した列を、空白区切りで 1 行に出力せよ。
入力例 1
5 1 2 3 5 6
出力例 1
2 6
A=(1,2,3,5,6) です。 このうち偶数なのは A _ 2=2,A _ 5=6 の 2 つなので、2 と 6 をこの順に空白区切りで出力してください。
入力例 2
5 2 2 2 3 3
出力例 2
2 2 2
A の中には同じ要素がある場合もあります。
入力例 3
10 22 3 17 8 30 15 12 14 11 17
出力例 3
22 8 30 12 14
Score : 100 points
Problem Statement
You are given a sequence of N integers: A=(A _ 1,A _ 2,\ldots,A _ N).
Print all even numbers in A without changing the order.
Constraints
- 1\leq N\leq 100
- 1\leq A _ i\leq 100\ (1\leq i\leq N)
- A contains at least one even number.
- 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
Output
Print a line containing the sequence of all even numbers in A, with spaces in between.
Sample Input 1
5 1 2 3 5 6
Sample Output 1
2 6
We have A=(1,2,3,5,6). Among them are two even numbers, A _ 2=2 and A _ 5=6, so print 2 and 6 in this order, with a space in between.
Sample Input 2
5 2 2 2 3 3
Sample Output 2
2 2 2
A may contain equal elements.
Sample Input 3
10 22 3 17 8 30 15 12 14 11 17
Sample Output 3
22 8 30 12 14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる長さ 1 以上 25 以下の文字列 S が与えられます。
S に含まれない英小文字をひとつ出力してください。
但し、そのようなものが複数ある場合はどれを出力しても構いません。
制約
- S は英小文字からなる長さ 1 以上 25 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に含まれない英小文字をひとつ出力せよ。そのようなものが複数ある場合はどれを出力しても構わない。
入力例 1
a
出力例 1
d
S= a です。
a 以外の英小文字、 b, c, ..., z が正解となります。
入力例 2
abcdfhijklmnopqrstuvwxyz
出力例 2
e
S 中に含まれない英小文字は e と g です。
入力例 3
qazplwsxokmedcijnrfvuhbgt
出力例 3
y
Score : 100 points
Problem Statement
You are given a string S of length between 1 and 25 consisting of lowercase English letters.
Output one lowercase English letter that does not appear in S.
If there are multiple such letters, you may output any one of them.
Constraints
- S is a string of length between 1 and 25 (inclusive) consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output one lowercase English letter that does not appear in S. If there are multiple such letters, you may output any one of them.
Sample Input 1
a
Sample Output 1
d
S= a.
Any lowercase English letter other than a (that is, b, c, …, or z) is a correct answer.
Sample Input 2
abcdfhijklmnopqrstuvwxyz
Sample Output 2
e
The lowercase English letters not included in S are e and g.
Sample Input 3
qazplwsxokmedcijnrfvuhbgt
Sample Output 3
y
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 H 行、横 W 列のマス目があり、各マスには 1 つの整数が書かれています。 上から i 行目、左から j 列目のマスに書かれている整数は A_{i, j} です。
マス目が下記の条件を満たすかどうかを判定してください。
1 \leq i_1 < i_2 \leq H および 1 \leq j_1 < j_2 \leq W を満たすすべての整数の組 (i_1, i_2, j_1, j_2) について、A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} が成り立つ。
制約
- 2 \leq H, W \leq 50
- 1 \leq A_{i, j} \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
出力
マス目が問題文中の条件を満たす場合は Yes と出力し、条件を満たさない場合は No と出力せよ。
入力例 1
3 3 2 1 4 3 1 3 6 4 1
出力例 1
Yes
1 \leq i_1 < i_2 \leq H および 1 \leq j_1 < j_2 \leq W を満たす整数の組 (i_1, i_2, j_1, j_2) は 9 個存在し、それらすべてについて A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} が成り立ちます。例えば、
- (i_1, i_2, j_1, j_2) = (1, 2, 1, 2) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 3 + 1 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 2, 1, 3) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 3 \leq 3 + 4 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 2, 2, 3) について、A_{i_1, j_1} + A_{i_2, j_2} = 1 + 3 \leq 1 + 4 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 3, 1, 2) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 4 \leq 6 + 1 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 3, 1, 3) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 6 + 4 = A_{i_2, j_1} + A_{i_1, j_2}
が成り立ちます。残りの (i_1, i_2, j_1, j_2) = (1, 3, 2, 3), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 3) についても同様に確認できます。
よって、Yes を出力します。
入力例 2
2 4 4 3 2 1 5 6 7 8
出力例 2
No
問題文中の条件を満たさないので、No を出力します。
例えば、(i_1, i_2, j_1, j_2) = (1, 2, 1, 4) について、A_{i_1, j_1} + A_{i_2, j_2} = 4 + 8 > 5 + 1 = A_{i_2, j_1} + A_{i_1, j_2} です。
Score : 200 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns, where each square contains an integer. The integer written on the square at the i-th row from the top and j-th column from the left is A_{i, j}.
Determine whether the grid satisfies the condition below.
A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} holds for every quadruple of integers (i_1, i_2, j_1, j_2) such that 1 \leq i_1 < i_2 \leq H and 1 \leq j_1 < j_2 \leq W.
Constraints
- 2 \leq H, W \leq 50
- 1 \leq A_{i, j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
Output
If the grid satisfies the condition in the Problem Statement, print Yes; otherwise, print No.
Sample Input 1
3 3 2 1 4 3 1 3 6 4 1
Sample Output 1
Yes
There are nine quadruples of integers (i_1, i_2, j_1, j_2) such that 1 \leq i_1 < i_2 \leq H and 1 \leq j_1 < j_2 \leq W. For all of them, A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} holds. Some examples follow.
- For (i_1, i_2, j_1, j_2) = (1, 2, 1, 2), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 3 + 1 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 2, 1, 3), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 3 \leq 3 + 4 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 2, 2, 3), we have A_{i_1, j_1} + A_{i_2, j_2} = 1 + 3 \leq 1 + 4 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 3, 1, 2), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 4 \leq 6 + 1 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 3, 1, 3), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 6 + 4 = A_{i_2, j_1} + A_{i_1, j_2}.
We can also see that the property holds for the other quadruples: (i_1, i_2, j_1, j_2) = (1, 3, 2, 3), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 3).
Thus, we should print Yes.
Sample Input 2
2 4 4 3 2 1 5 6 7 8
Sample Output 2
No
We should print No because the condition is not satisfied.
This is because, for example, we have A_{i_1, j_1} + A_{i_2, j_2} = 4 + 8 > 5 + 1 = A_{i_2, j_1} + A_{i_1, j_2} for (i_1, i_2, j_1, j_2) = (1, 2, 1, 4).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
3 桁の正整数であって、百の位の数と十の位の数の積が一の位の数と等しいものを 326-like number と呼びます。
例えば 326,400,144 は 326-like number であり、623,777,429 は 326-like number ではありません。
整数 N が与えられるので、N 以上の最小の 326-like number を求めてください。なお、制約の条件下で答えは必ず存在します。
制約
- 100 \leq N \leq 919
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
320
出力例 1
326
320,321,322,323,324,325 は 326-like number ではなく、326 は 326-like number です。
入力例 2
144
出力例 2
144
144 は 326-like number です。
入力例 3
516
出力例 3
600
Score : 200 points
Problem Statement
A 326-like number is a three-digit positive integer where the product of the hundreds and tens digits equals the ones digit.
For example, 326,400,144 are 326-like numbers, while 623,777,429 are not.
Given an integer N, find the smallest 326-like number greater than or equal to N. It always exists under the constraints.
Constraints
- 100 \leq N \leq 919
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
320
Sample Output 1
326
320,321,322,323,324,325 are not 326-like numbers, while 326 is a 326-like number.
Sample Input 2
144
Sample Output 2
144
144 is a 326-like number.
Sample Input 3
516
Sample Output 3
600
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N),B=(B_1,B_2,\ldots,B_N) が与えられます。
Q 個のクエリが与えられるので順に処理してください。 i 番目 (1\le i\le Q) のクエリは以下で説明されます。
文字 c_i と整数 X_i,V_i が与えられる。 c_i=
Aならば A_{X_i} を、 c_i=Bならば B_{X_i} を V_i に変更する。その後、 \displaystyle \sum_{k=1}^N \min(A_k,B_k) を出力する。
制約
- 1\le N,Q \le 2 \times 10^5
- 1\le A_i,B_i \le 10^9
- c_i は
AかBのいずれか - 1\le X_i \le N
- 1\le V_i \le 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N c_1 X_1 V_1 c_2 X_2 V_2 \vdots c_Q X_Q V_Q
出力
Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。
入力例 1
4 3 3 1 4 1 2 7 1 8 A 2 3 B 3 3 A 1 7
出力例 1
7 9 9
1 番目のクエリでは A=(3,3,4,1),B=(2,7,1,8) となります。したがって、1 行目には \displaystyle \min(3,2)+\min(3,7) + \min(4 , 1)+\min(1,8) = 7 を出力してください。
2 番目のクエリでは A=(3,3,4,1),B=(2,7,3,8) となります。したがって、 2 行目には \displaystyle \min(3,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 を出力してください。
3 番目のクエリでは A=(7,3,4,1),B=(2,7,3,8) となります。したがって、 3 行目には \displaystyle \min(7,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 を出力してください。
入力例 2
1 3 1 1000000000 A 1 1 A 1 1 A 1 1
出力例 2
1 1 1
入力例 3
5 3 100 100 100 100 100 100 100 100 100 100 A 4 21 A 2 99 B 4 57
出力例 3
421 420 420
Score : 300 points
Problem Statement
You are given length-N integer sequences: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).
You are given Q queries to process in order. The i-th query (1\le i\le Q) is described as follows:
You are given a character c_i and integers X_i,V_i. If c_i=
A, change A_{X_i} to V_i; if c_i=B, change B_{X_i} to V_i. Then, output \displaystyle \sum_{k=1}^N \min(A_k,B_k).
Constraints
- 1\le N,Q \le 2 \times 10^5
- 1\le A_i,B_i \le 10^9
- c_i is either
AorB. - 1\le X_i \le N
- 1\le V_i \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N c_1 X_1 V_1 c_2 X_2 V_2 \vdots c_Q X_Q V_Q
Output
Output Q lines. The i-th line (1\le i\le Q) should contain the answer to the i-th query.
Sample Input 1
4 3 3 1 4 1 2 7 1 8 A 2 3 B 3 3 A 1 7
Sample Output 1
7 9 9
After the 1st query, A=(3,3,4,1),B=(2,7,1,8). Therefore, output \displaystyle \min(3,2)+\min(3,7) + \min(4 , 1)+\min(1,8) = 7 on the 1st line.
After the 2nd query, A=(3,3,4,1),B=(2,7,3,8). Therefore, output \displaystyle \min(3,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 on the 2nd line.
After the 3rd query, A=(7,3,4,1),B=(2,7,3,8). Therefore, output \displaystyle \min(7,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 on the 3rd line.
Sample Input 2
1 3 1 1000000000 A 1 1 A 1 1 A 1 1
Sample Output 2
1 1 1
Sample Input 3
5 3 100 100 100 100 100 100 100 100 100 100 A 4 21 A 2 99 B 4 57
Sample Output 3
421 420 420
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
1 から N までの番号が付けられた N 個のおもちゃと、1 から N-1 までの番号が付けられた N-1 個の箱があります。 おもちゃ i\ (1\leq i\leq N) の大きさは A_i、箱 i\ (1\leq i\leq N-1) の大きさは B_i です。
すべてのおもちゃを別々の箱にしまいたいと考えている高橋君は、今から以下の操作を順に行うことにしました。
- 任意の正整数 x を選んで、大きさ x の箱を 1 つ購入する。
- N 個のおもちゃそれぞれを、(元々あった箱と新しく購入した箱を合わせた)N 個の箱のいずれかに入れる。 ただし、各おもちゃはそのおもちゃの大きさ以上の大きさを持つ箱にしか入れることはできず、また 1 つの箱に 2 つ以上のおもちゃを入れることもできない。
高橋君は、操作 1 で十分な大きさの箱を購入することで操作 2 が実行できるようにしたいですが、大きな箱ほど値段が高いため、可能な限り小さな箱を購入したいです。
高橋君が操作 2 を実行できるような x の値が存在するか判定し、存在するならばその最小値を求めてください。
制約
- 2\leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_{N-1}
出力
高橋君が操作 2 を実行できるような x の値が存在するならばその最小値を、存在しないならば -1 を出力せよ。
入力例 1
4 5 2 3 7 6 2 8
出力例 1
3
x=3 とした場合(すなわち、操作 1 で大きさ 3 の箱を購入した場合)を考えます。
新しく購入した箱を箱 4 と呼ぶことにすると、おもちゃ 1,\dots,4 の大きさはそれぞれ 5,2,3,7、箱 1,\dots,4 の大きさはそれぞれ 6,2,8,3 となります。 よって、おもちゃ 1 を箱 1 に、おもちゃ 2 を箱 2 に、おもちゃ 3 を箱 4 に、おもちゃ 4 を箱 3 にそれぞれ入れることができます。
逆に、x\leq 2 のときは N 個のおもちゃすべてを別々の箱に入れることができません。 よって答えは 3 です。
入力例 2
4 3 7 2 5 8 1 6
出力例 2
-1
操作 1 でどのような大きさの箱を購入したとしても、箱 2 に入れられる大きさのおもちゃが存在しないため、操作 2 を実行することができません。
入力例 3
8 2 28 17 39 57 56 37 32 34 27 73 28 76 61 27
出力例 3
37
Score : 350 points
Problem Statement
There are N toys numbered from 1 to N, and N-1 boxes numbered from 1 to N-1. Toy i\ (1 \leq i \leq N) has a size of A_i, and box i\ (1 \leq i \leq N-1) has a size of B_i.
Takahashi wants to store all the toys in separate boxes, and he has decided to perform the following steps in order:
- Choose an arbitrary positive integer x and purchase one box of size x.
- Place each of the N toys into one of the N boxes (the N-1 existing boxes plus the newly purchased box). Here, each toy can only be placed in a box whose size is not less than the toy's size, and no box can contain two or more toys.
He wants to execute step 2 by purchasing a sufficiently large box in step 1, but larger boxes are more expensive, so he wants to purchase the smallest possible box.
Determine whether there exists a value of x such that he can execute step 2, and if it exists, find the minimum such x.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_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 \dots A_N
B_1 B_2 \dots B_{N-1}
Output
If there exists a value of x such that Takahashi can execute step 2, print the minimum such x. Otherwise, print -1.
Sample Input 1
4 5 2 3 7 6 2 8
Sample Output 1
3
Consider the case where x=3 (that is, he purchases a box of size 3 in step 1).
If the newly purchased box is called box 4, toys 1,\dots,4 have sizes of 5, 2, 3, and 7, respectively, and boxes 1,\dots,4 have sizes of 6, 2, 8, and 3, respectively. Thus, toy 1 can be placed in box 1, toy 2 in box 2, toy 3 in box 4, and toy 4 in box 3.
On the other hand, if x \leq 2, it is impossible to place all N toys into separate boxes. Therefore, the answer is 3.
Sample Input 2
4 3 7 2 5 8 1 6
Sample Output 2
-1
No matter what size of box is purchased in step 1, no toy can be placed in box 2, so it is impossible to execute step 2.
Sample Input 3
8 2 28 17 39 57 56 37 32 34 27 73 28 76 61 27
Sample Output 3
37
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
AtCoder 食堂では N 種類の主菜と M 種類の副菜が提供されており、i 種類目の主菜の価格は A_i、j 種類目の副菜の価格は B_j です。 AtCoder 食堂では新たに定食のメニューを設けることを検討しています。 定食は 1 種類の主菜と 1 種類の副菜から構成され、主菜と副菜の価格の和を s としたとき、定食の価格は \min(s,P) です。 ここで、P は入力で与えられる定数です。
定食を構成する主菜と副菜の選び方は NM 通りありますが、それらすべてに対する定食の価格の総和を求めてください。
制約
- 1\leq N,M \leq 2\times 10^5
- 1\leq A_i,B_j \leq 10^8
- 1\leq P \leq 2\times 10^8
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
答えを整数として出力せよ。 なお、本問題の制約下では、答えは 64 bit 符号付き整数型の範囲に収まることが証明できる。
入力例 1
2 2 7 3 5 6 1
出力例 1
24
- 1 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(3+6,7)=7 です。
- 1 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(3+1,7)=4 です。
- 2 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(5+6,7)=7 です。
- 2 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(5+1,7)=6 です。
よって、答えは 7+4+7+6=24 です。
入力例 2
1 3 2 1 1 1 1
出力例 2
6
入力例 3
7 12 25514963 2436426 24979445 61648772 23690081 33933447 76190629 62703497 11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857
出力例 3
2115597124
Score : 400 points
Problem Statement
AtCoder cafeteria offers N main dishes and M side dishes. The price of the i-th main dish is A_i, and that of the j-th side dish is B_j. The cafeteria is considering introducing a new set meal menu. A set meal consists of one main dish and one side dish. Let s be the sum of the prices of the main dish and the side dish, then the price of the set meal is \min(s,P). Here, P is a constant given in the input.
There are NM ways to choose a main dish and a side dish for a set meal. Find the total price of all these set meals.
Constraints
- 1\leq N,M \leq 2\times 10^5
- 1\leq A_i,B_j \leq 10^8
- 1\leq P \leq 2\times 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Print the answer as an integer. Under the constraints of this problem, it can be proved that the answer fits into a 64-bit signed integer.
Sample Input 1
2 2 7 3 5 6 1
Sample Output 1
24
- If you choose the first main dish and the first side dish, the price of the set meal is \min(3+6,7)=7.
- If you choose the first main dish and the second side dish, the price of the set meal is \min(3+1,7)=4.
- If you choose the second main dish and the first side dish, the price of the set meal is \min(5+6,7)=7.
- If you choose the second main dish and the second side dish, the price of the set meal is \min(5+1,7)=6.
Thus, the answer is 7+4+7+6=24.
Sample Input 2
1 3 2 1 1 1 1
Sample Output 2
6
Sample Input 3
7 12 25514963 2436426 24979445 61648772 23690081 33933447 76190629 62703497 11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857
Sample Output 3
2115597124
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
A, B, C の 3 種類の文字からなる長さ N の文字列 S が与えられます。
S の空でない連続する部分文字列は \dfrac{N(N+1)}2 個ありますが、そのうち A が B よりも多く含まれるものはいくつあるか求めてください。
2 つの部分文字列は、S から取り出す場所が異なれば文字列として等しくても区別して数えることに注意してください。
部分文字列とは
S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、AB は ABC の部分文字列ですが、AC は ABC の部分文字列ではありません。
制約
- 1\le N\le5\times10 ^ 5
- S は
A,B,Cからなる長さ N の文字列 - N は整数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S の連続する部分文字列のうち A が B よりも多く含まれるものの個数を出力せよ。
入力例 1
10 ACBBCABCAB
出力例 1
8
以下の 8 つの部分文字列が条件を満たします。
A:S の 1 文字目から 1 文字目AC:S の 1 文字目から 2 文字目CA:S の 5 文字目から 6 文字目CABCA:S の 5 文字目から 9 文字目A:S の 6 文字目から 6 文字目ABCA:S の 6 文字目から 9 文字目CA:S の 8 文字目から 9 文字目A:S の 9 文字目から 9 文字目
これら以外の部分文字列は条件を満たさないため、8 を出力してください。
A や CA は複数箇所から取り出すことができますが、取り出す場所が異なれば区別して数えることに注意してください。
入力例 2
4 CCBC
出力例 2
0
条件を満たす部分文字列が存在しないこともあります。
入力例 3
36 CABACBBBBBAABABACCBCABCCABAABABBCBAC
出力例 3
136
Score : 450 points
Problem Statement
You are given a string S of length N consisting of three kinds of characters: A, B, and C.
There are \dfrac{N(N+1)}2 non-empty contiguous substrings of S. Find how many of them contain more As than Bs.
Note that two substrings are counted separately if they are taken from different positions in S, even if they are equal as strings.
What is a substring?
A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, AB is a substring of ABC, but AC is not a substring of ABC.
Constraints
- 1\le N\le5\times10 ^ 5
- S is a string of length N consisting of
A,B, andC. - N is an integer.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the number of contiguous substrings of S that contain more As than Bs.
Sample Input 1
10 ACBBCABCAB
Sample Output 1
8
The following eight substrings satisfy the condition:
A: 1st to 1st character of SAC: 1st to 2nd character of SCA: 5th to 6th character of SCABCA: 5th to 9th character of SA: 6th to 6th character of SABCA: 6th to 9th character of SCA: 8th to 9th character of SA: 9th to 9th character of S
All other substrings do not satisfy the condition, so print 8.
Note that A and CA can be taken from multiple positions, but they are counted separately if taken from different positions.
Sample Input 2
4 CCBC
Sample Output 2
0
There may be no substrings that satisfy the condition.
Sample Input 3
36 CABACBBBBBAABABACCBCABCCABAABABBCBAC
Sample Output 3
136
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
長さ k の数列 a=(a_1,a_2,\dots,a_k) が 門松的 であることを、以下のようにして定めます。
- 2 \le i \le k-1 かつ a_{i-1} < a_i かつ a_i > a_{i+1} を満たす整数 i の個数を x とする。
- 2 \le i \le k-1 かつ a_{i-1} > a_i かつ a_i < a_{i+1} を満たす整数 i の個数を y とする。
- x > y であるとき、またそのときに限り、数列 a を 門松的 であると言います。
(1,2,\dots,N) の順列 P が与えられます。
P の (連続でなくともよい) 部分列であって、門松的であるものの個数を 998244353 で割った余りを求めてください。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- P は (1,2,\dots,N) の順列
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \dots P_N
出力
答えを出力せよ。
入力例 1
4 1 3 4 2
出力例 1
4
P の部分列のうち、門松的であるものは以下の 4 つです。
- (1,3,4,2)
- (1,3,2)
- (1,4,2)
- (3,4,2)
入力例 2
1 1
出力例 2
0
入力例 3
20 11 10 18 13 12 16 5 19 7 6 17 4 9 1 14 2 20 15 8 3
出力例 3
431610
例えば、部分列 a=(10,13,12,5,7,9,20,3) は門松的です。
- i=2,7 に対して a_{i-1} < a_i かつ a_i > a_{i+1} が成り立つので、 x=2 です。
- i=4 に対して a_{i-1} > a_i かつ a_i < a_{i+1} が成り立つので、 y=1 です。
- x > y であるので、部分列 a は門松的です。
Score : 525 points
Problem Statement
A sequence a=(a_1,a_2,\dots,a_k) of length k is defined to be kadomatsu-like as follows:
- Let x be the number of integers i that satisfy 2 \le i \le k-1, a_{i-1} < a_i, and a_i > a_{i+1}.
- Let y be the number of integers i that satisfy 2 \le i \le k-1, a_{i-1} > a_i, and a_i < a_{i+1}.
- The sequence a is called kadomatsu-like if and only if x > y.
You are given a permutation P of (1,2,\dots,N).
Find the number, modulo 998244353, of (not necessarily contiguous) subsequences of P that are kadomatsu-like.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- P is a permutation of (1,2,\dots,N).
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \dots P_N
Output
Output the answer.
Sample Input 1
4 1 3 4 2
Sample Output 1
4
Among the subsequences of P, the following four are kadomatsu-like:
- (1,3,4,2)
- (1,3,2)
- (1,4,2)
- (3,4,2)
Sample Input 2
1 1
Sample Output 2
0
Sample Input 3
20 11 10 18 13 12 16 5 19 7 6 17 4 9 1 14 2 20 15 8 3
Sample Output 3
431610
For example, the subsequence a=(10,13,12,5,7,9,20,3) is kadomatsu-like.
- We have a_{i-1} < a_i and a_i > a_{i+1} for i=2,7, so x=2.
- We have a_{i-1} > a_i and a_i < a_{i+1} for i=4, so y=1.
- Since x > y, the subsequence a is kadomatsu-like.