Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
N 棟のビルが等間隔に一列に並んでいます。手前から i 番目のビルの高さは H_i です。
あなたは次の条件をともに満たすようにいくつかのビルを選んで電飾で飾ろうとしています。
- 選んだビルたちは高さが等しい
- 選んだビルたちは等間隔に並んでいる
最大でいくつのビルを選ぶことができますか? なお、ちょうど 1 つのビルを選んだときは条件を満たすとみなします。
制約
- 1 \leq N \leq 3000
- 1 \leq H_i \leq 3000
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N H_1 \ldots H_N
出力
答えを出力せよ。
入力例 1
8 5 7 5 7 7 5 7 7
出力例 1
3
手前から 2,5,8 番目のビルを選ぶと条件を満たします。
入力例 2
10 100 200 300 400 500 600 700 800 900 1000
出力例 2
1
1つのビルを選んだときは条件を満たすとみなします。
入力例 3
32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
出力例 3
3
Score : 350 points
Problem Statement
There are N buildings arranged in a line at equal intervals. The height of the i-th building from the front is H_i.
You want to decorate some of these buildings with illuminations so that both of the following conditions are satisfied:
- The chosen buildings all have the same height.
- The chosen buildings are arranged at equal intervals.
What is the maximum number of buildings you can choose? If you choose exactly one building, it is considered to satisfy the conditions.
Constraints
- 1 \leq N \leq 3000
- 1 \leq H_i \leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 \ldots H_N
Output
Print the answer.
Sample Input 1
8 5 7 5 7 7 5 7 7
Sample Output 1
3
Choosing the 2nd, 5th, and 8th buildings from the front satisfies the conditions.
Sample Input 2
10 100 200 300 400 500 600 700 800 900 1000
Sample Output 2
1
Choosing just one building is considered to satisfy the conditions.
Sample Input 3
32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から N の番号がついた N 人の人がいます。
人 i は数 Q_i が書かれたゼッケンを着けており、人 P_i を見つめています。
i が書かれたゼッケンを着けている人が見つめている人の着けているゼッケンにかかれている数を、i=1,2,\ldots,N のそれぞれについて求めてください。
制約
- 2 \leq N \leq 3\times 10^5
- 1 \leq P_i \leq N
- P_i は相異なる
- 1 \leq Q_i \leq N
- Q_i は相異なる
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \dots P_N Q_1 Q_2 \dots Q_N
出力
i が書かれたゼッケンを着けている人が見つめている人の着けているゼッケンにかかれている数を S_i とする。
S_1,S_2,\ldots,S_N をこの順に空白区切りで出力せよ。
入力例 1
4 4 3 2 1 2 3 1 4
出力例 1
3 4 1 2
人 3 は 1 が書かれたゼッケンを着けており、人 3 が見つめている人 2 は 3 が書かれたゼッケンを着けています。 よって i=1 に対する答えは 3 になります。
入力例 2
10 2 6 4 3 7 8 9 10 1 5 1 4 8 2 10 5 7 3 9 6
出力例 2
4 8 6 5 3 10 9 2 1 7
Score : 300 points
Problem Statement
There are N people numbered from 1 to N.
Person i is wearing a bib with the number Q_i and is staring at person P_i.
For each i = 1,2,\ldots,N, find the number written on the bib of the person that the person wearing the bib with number i is staring at.
Constraints
- 2 \leq N \leq 3\times 10^5
- 1 \leq P_i \leq N
- The values of P_i are distinct.
- 1 \leq Q_i \leq N
- The values of Q_i are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \dots P_N Q_1 Q_2 \dots Q_N
Output
Let S_i be the number written on the bib of the person that the person wearing the bib with number i is staring at.
Print S_1, S_2, \ldots, S_N in this order, separated by a single space.
Sample Input 1
4 4 3 2 1 2 3 1 4
Sample Output 1
3 4 1 2
Person 3 is wearing the bib with the number 1, and the person that person 3 is staring at, person 2, is wearing the bib with the number 3. Thus, the answer for i = 1 is 3.
Sample Input 2
10 2 6 4 3 7 8 9 10 1 5 1 4 8 2 10 5 7 3 9 6
Sample Output 2
4 8 6 5 3 10 9 2 1 7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君はすぬけ君たちを捕まえようとしています。
数直線上の座標 0,1,2,3,4 の 5 箇所に穴があり、すぬけ君たちの巣につながっています。
これから N 匹のすぬけ君が穴から出てきます。i 番目のすぬけ君は時刻 T_i に座標 X_i の穴から出てきて、大きさは A_i であることがわかっています。
高橋君は時刻 0 に座標 0 におり、数直線上を単位時間あたり 1 以下の速さで移動することができます。
すぬけ君が穴から出てきたのと同じ時刻に同じ座標に高橋君がいるとき、かつ、そのときに限り、高橋君はすぬけ君を捕まえることができます。
すぬけ君を捕まえるのにかかる時間は無視できます。
高橋君が適切に行動したとき、捕まえることができるすぬけ君の大きさの合計の最大値を求めてください。
制約
- 1 \leq N \leq 10^5
- 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
- 0 \leq X_i \leq 4
- 1 \leq A_i \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N T_1 X_1 A_1 T_2 X_2 A_2 \vdots T_N X_N A_N
出力
答えを整数として出力せよ。
入力例 1
3 1 0 100 3 3 10 5 4 1
出力例 1
101
次のように行動するのが最適です。
- 座標 0 で待機し、時刻 1 に 1 番目のすぬけ君を捕まえる
- 座標 4 へ移動し、時刻 5 に 3 番目のすぬけ君を捕まえる
1 番目と 2 番目のすぬけ君を両方とも捕まえることはできないので、これが最大です。
入力例 2
3 1 4 1 2 4 1 3 4 1
出力例 2
0
高橋君はすぬけ君を 1 匹も捕まえることができません。
入力例 3
10 1 4 602436426 2 1 623690081 3 3 262703497 4 4 628894325 5 3 450968417 6 1 161735902 7 1 707723857 8 2 802329211 9 0 317063340 10 2 125660016
出力例 3
2978279323
Score : 400 points
Problem Statement
Takahashi is trying to catch many Snuke.
There are five pits at coordinates 0, 1, 2, 3, and 4 on a number line, connected to Snuke's nest.
Now, N Snuke will appear from the pits. It is known that the i-th Snuke will appear from the pit at coordinate X_i at time T_i, and its size is A_i.
Takahashi is at coordinate 0 at time 0 and can move on the line at a speed of at most 1.
He can catch a Snuke appearing from a pit if and only if he is at the coordinate of that pit exactly when it appears.
The time it takes to catch a Snuke is negligible.
Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.
Constraints
- 1 \leq N \leq 10^5
- 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
- 0 \leq X_i \leq 4
- 1 \leq A_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N T_1 X_1 A_1 T_2 X_2 A_2 \vdots T_N X_N A_N
Output
Print the answer as an integer.
Sample Input 1
3 1 0 100 3 3 10 5 4 1
Sample Output 1
101
The optimal strategy is as follows.
- Wait at coordinate 0 to catch the first Snuke at time 1.
- Go to coordinate 4 to catch the third Snuke at time 5.
It is impossible to catch both the first and second Snuke, so this is the best he can.
Sample Input 2
3 1 4 1 2 4 1 3 4 1
Sample Output 2
0
Takahashi cannot catch any Snuke.
Sample Input 3
10 1 4 602436426 2 1 623690081 3 3 262703497 4 4 628894325 5 3 450968417 6 1 161735902 7 1 707723857 8 2 802329211 9 0 317063340 10 2 125660016
Sample Output 3
2978279323
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
1
から 9
までの数字からなる文字列 S に対して、 f(S) を次の手順によって得られる文字列 T とします。(S_i は S の i 番目の文字を意味します)
- 文字列 T がある。はじめ、T は空文字列である。
- i=1, 2, \dots, |S| - 1 の順に次の操作を行う。
- S_{i+1} を整数として解釈したときの値を n とする。T の末尾に S_i を n 個追加する。
例えば S = 313
のとき、以下の手順によって f(S) = 3111
に決まります。
- はじめ T は空文字列である。
- i=1 のとき n = 1 である。T に
3
を 1 個追加する。T は3
になる。 - i=2 のとき n = 3 である。T に
1
を 3 個追加する。T は3111
になる。 - 操作を終了する。T として
3111
を得る。
1
から 9
までの数字からなる長さ N の文字列 S が与えられます。
あなたは「S を f(S) に置き換える」という操作を S の長さが 1 になるまで繰り返します。
操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを求めてください。ただし、操作が無限に続く場合は -1
を出力してください。
制約
- 2 \leq N \leq 10^6
- S は
1
,2
,3
,4
,5
,6
,7
,8
,9
からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを出力せよ。ただし、操作が無限に続く場合は -1
を出力せよ。
入力例 1
3 313
出力例 1
4
S = 313
の場合、操作を 4 回行うと S の長さが 1 になります。
- f(S) =
3111
である。S を3111
に置き換える。 - f(S) =
311
である。S を311
に置き換える。 - f(S) =
31
である。S を31
に置き換える。 - f(S) =
3
である。S を3
に置き換える。 - S の長さが 1 になったので操作を終了する。
入力例 2
9 123456789
出力例 2
-1
S = 123456789
の場合、操作が無限に続きます。この場合は -1
を出力してください。
入力例 3
2 11
出力例 3
1
Score : 600 points
Problem Statement
For a string S consisting of digits from 1
through 9
, let f(S) be the string T obtained by the following procedure. (S_i denotes the i-th character of S.)
- Let T be an initially empty string.
- For i=1, 2, \dots, |S| - 1, perform the following operation:
- Append n copies of S_i to the tail of T, where n is the value when S_{i+1} is interpreted as an integer.
For example, S = 313
yields f(S) = 3111
by the following steps.
- T is initially empty.
- For i=1, we have n = 1. Append one copy of
3
to T, which becomes3
. - For i=2, we have n = 3. Append three copies of
1
to T, which becomes3111
. - Terminate the procedure. We obtain T =
3111
.
You are given a length-N string S consisting of digits from 1
through 9
.
You repeat the following operation until the length of S becomes 1: replace S with f(S).
Find how many times, modulo 998244353, you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1
instead.
Constraints
- 2 \leq N \leq 10^6
- S is a length-N string consisting of
1
,2
,3
,4
,5
,6
,7
,8
, and9
.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the number of times, modulo 998244353, that you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1
instead.
Sample Input 1
3 313
Sample Output 1
4
If S = 313
, the length of S be comes 1 after four operations.
- We have f(S) =
3111
. Replace S with3111
. - We have f(S) =
311
. Replace S with311
. - We have f(S) =
31
. Replace S with31
. - We have f(S) =
3
. Replace S with3
. - Now that the length of S is 1, terminate the repetition.
Sample Input 2
9 123456789
Sample Output 2
-1
If S = 123456789
, you indefinitely repeat the operation. In this case, -1
should be printed.
Sample Input 3
2 11
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
英大小文字と (
、 )
からなる文字列 S=S_1 S_2 S_3 \dots S_{|S|} が与えられます。
文字列 S 中の括弧は、対応が取れています。
次の操作を、操作ができなくなるまで繰り返します。
- まず、以下の条件を全て満たす整数組 (l,r) をひとつ選択する。
- l < r
- S_l =
(
- S_r =
)
- S_{l+1},S_{l+2},\dots,S_{r-1} は全て英大文字または英小文字である
- T=\overline{S_{r-1}S_{r-2} \dots S_{l+1}} とする。
- 但し、 \overline{x} は x の大文字と小文字を反転させた文字列を指す。
- その後、 S の l 文字目から r 文字目までを削除し、削除した位置に T を挿入する。
詳細は入出力例を参照してください。
上記の操作を使って全ての (
と )
を除去することができ、最終的な文字列は操作の方法や順序によらないことが証明できます。
このとき、最終的な文字列を求めてください。
「 S 中の括弧の対応が取れている」とは?
まず、正しい括弧列を次の通り定義します。- 正しい括弧列とは、以下のいずれかの条件を満たす文字列です。
- 空文字列
- ある正しい括弧列 A が存在して、
(
, A,)
をこの順に連結した文字列 - ある空でない正しい括弧列 A,B が存在して、 A,B をこの順に連結した文字列
(
と )
を順序を保って抜き出した時、それが正しい括弧列となることを指す。
制約
- 1 \le |S| \le 5 \times 10^5
- S は英大小文字と
(
、)
からなる - S 中の括弧は対応が取れている
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
((A)y)x
出力例 1
YAx
S= ((A)y)x
に対して操作を行います。
- l=2,r=4 を選択します。このとき削除される文字列は
(A)
で、代わりにa
が挿入されます。- この操作の結果、 S=
(ay)x
となります。
- この操作の結果、 S=
- l=1,r=4 を選択します。このとき削除される文字列は
(ay)
で、代わりにYA
が挿入されます。- この操作の結果、 S=
YAx
となります。
- この操作の結果、 S=
括弧を除去した結果、文字列は YAx
となったので、これを出力してください。
入力例 2
((XYZ)n(X(y)Z))
出力例 2
XYZNXYZ
S= ((XYZ)n(X(y)Z))
に対して操作を行います。
- l=10,r=12 を選択します。このとき削除される文字列は
(y)
で、代わりにY
が挿入されます。- この操作の結果、 S=
((XYZ)n(XYZ))
となります。
- この操作の結果、 S=
- l=2,r=6 を選択します。このとき削除される文字列は
(XYZ)
で、代わりにzyx
が挿入されます。- この操作の結果、 S=
(zyxn(XYZ))
となります。
- この操作の結果、 S=
- l=6,r=10 を選択します。このとき削除される文字列は
(XYZ)
で、代わりにzyx
が挿入されます。- この操作の結果、 S=
(zyxnzyx)
となります。
- この操作の結果、 S=
- l=1,r=9 を選択します。このとき削除される文字列は
(zyxnzyx)
で、代わりにXYZNXYZ
が挿入されます。- この操作の結果、 S=
XYZNXYZ
となります。
- この操作の結果、 S=
括弧を除去した結果、文字列は XYZNXYZ
となったので、これを出力してください。
入力例 3
(((()))(()))(())
出力例 3
操作結果が空文字列になる場合もあります。
入力例 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
出力例 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve
Score: 550 points
Problem Statement
You are given a string S = S_1 S_2 S_3 \dots S_{|S|} consisting of uppercase and lowercase English letters, (
, and )
.
The parentheses in the string S are properly matched.
Repeat the following operation until no more operations can be performed:
- First, select one pair of integers (l, r) that satisfies all of the following conditions:
- l < r
- S_l =
(
- S_r =
)
- Each of the characters S_{l+1}, S_{l+2}, \dots, S_{r-1} is an uppercase or lowercase English letter.
- Let T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}}.
- Here, \overline{x} denotes the string obtained by toggling the case of each character in x (uppercase to lowercase and vice versa).
- Then, delete the l-th through r-th characters of S and insert T at the position where the deletion occurred.
Refer to the sample inputs and outputs for clarification.
It can be proved that it is possible to remove all (
s and )
s from the string using the above operations, and the final string is independent of how and in what order the operations are performed.
Determine the final string.
What does it mean that the parentheses in S are properly matched?
First, a correct parenthesis sequence is defined as follows:- A correct parenthesis sequence is a string that satisfies one of the following conditions:
- It is an empty string.
- There exists a correct parenthesis sequence A, and the string is formed by concatenating
(
, A,)
in this order. - There exist non-empty correct parenthesis sequences A and B, and the string is formed by concatenating A and B in this order.
(
s and )
s extracted from S without changing the order form a correct parenthesis sequence.
Constraints
- 1 \le |S| \le 5 \times 10^5
- S consists of uppercase and lowercase English letters,
(
, and)
. - The parentheses in S are properly matched.
Input
The input is given from Standard Input in the following format:
S
Output
Print the final string.
Sample Input 1
((A)y)x
Sample Output 1
YAx
Let us perform the operations for S = ((A)y)x
.
- Choose l=2 and r=4. The substring
(A)
is removed and replaced witha
.- After this operation, S =
(ay)x
.
- After this operation, S =
- Choose l=1 and r=4. The substring
(ay)
is removed and replaced withYA
.- After this operation, S =
YAx
.
- After this operation, S =
After removing the parentheses, the string becomes YAx
, which should be printed.
Sample Input 2
((XYZ)n(X(y)Z))
Sample Output 2
XYZNXYZ
Let us perform the operations for S = ((XYZ)n(X(y)Z))
.
- Choose l=10 and r=12. The substring
(y)
is removed and replaced withY
.- After this operation, S =
((XYZ)n(XYZ))
.
- After this operation, S =
- Choose l=2 and r=6. The substring
(XYZ)
is removed and replaced withzyx
.- After this operation, S =
(zyxn(XYZ))
.
- After this operation, S =
- Choose l=6 and r=10. The substring
(XYZ)
is removed and replaced withzyx
.- After this operation, S =
(zyxnzyx)
.
- After this operation, S =
- Choose l=1 and r=9. The substring
(zyxnzyx)
is removed and replaced withXYZNXYZ
.- After this operation, S =
XYZNXYZ
.
- After this operation, S =
After removing the parentheses, the string becomes XYZNXYZ
, which should be printed.
Sample Input 3
(((()))(()))(())
Sample Output 3
The final outcome may be an empty string.
Sample Input 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
Sample Output 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve