Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字と数字のみからなる文字列 S が与えられます。
S から数字である文字だけを取り出し、元の順序のまま並べた文字列を求めてください。
制約
- S は英小文字と数字のみからなる長さ 1 以上 50 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
abc462
出力例 1
462
abc462 に含まれる数字は 4, 6, 2 です。これらを元の順序のまま連結すると 462 となります。したがって、462 を出力してください。
入力例 2
codequeen
出力例 2
codequeen に数字は含まれないので空文字列を出力してください。
入力例 3
31415
出力例 3
31415
入力例 4
10plus2is12
出力例 4
10212
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters and digits.
Extract only the digit characters from S and output the string formed by arranging them in their original order.
Constraints
- S is a string of length between 1 and 50, inclusive, consisting of lowercase English letters and digits.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
abc462
Sample Output 1
462
The digits contained in abc462 are 4, 6, 2. Concatenating them in their original order gives 462. Thus, output 462.
Sample Input 2
codequeen
Sample Output 2
codequeen contains no digits, so output the empty string.
Sample Input 3
31415
Sample Output 3
31415
Sample Input 4
10plus2is12
Sample Output 4
10212
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 個の整数 A _ 1,A _ 2,\ldots,A _ N が与えられます。
これらの値がすべて等しいならば Yes 、そうでなければ No と出力してください。
制約
- 2\leq N\leq100
- 1\leq A _ i\leq100\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
与えられた A _ 1,A _ 2,\ldots,A _ N の値がすべて等しいなら Yes を、そうでなければ No を 1 行で出力せよ。
入力例 1
3 3 2 4
出力例 1
No
A _ 1\neq A _ 2 なので、No と出力してください。
入力例 2
4 3 3 3 3
出力例 2
Yes
A _ 1=A _ 2=A _ 3=A _ 4 なので、Yes と出力してください。
入力例 3
10 73 8 55 26 97 48 37 47 35 55
出力例 3
No
Score : 100 points
Problem Statement
You are given N integers A _ 1,A _ 2,\ldots,A _ N.
If their values are all equal, print Yes; otherwise, print No.
Constraints
- 2\leq N\leq100
- 1\leq A _ i\leq100\ (1\leq i\leq N)
- 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 a single line containing Yes if the values of the given A _ 1,A _ 2,\ldots,A _ N are all equal, and No otherwise.
Sample Input 1
3 3 2 4
Sample Output 1
No
We have A _ 1\neq A _ 2, so you should print No.
Sample Input 2
4 3 3 3 3
Sample Output 2
Yes
We have A _ 1=A _ 2=A _ 3=A _ 4, so you should print Yes.
Sample Input 3
10 73 8 55 26 97 48 37 47 35 55
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N の番号がついた N 人の人がいます。
人 i はキーエンス本社ビルの建築面積を S_i 平方メートルであると予想しました。
キーエンス本社ビルは下図のような形をしています。ただし、a,b はある 正の整数 です。
つまり、キーエンス本社ビルの建築面積は 4ab+3a+3b 平方メートルと表されます。
N 人のうち、この情報のみによって、予想した面積が確実に誤りであるとわかる人数を求めてください。

制約
- 1 \leq N \leq 20
- 1 \leq S_i \leq 1000
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N S_1 \ldots S_N
出力
答えを出力せよ。
入力例 1
3 10 20 39
出力例 1
1
a=1,b=1 のとき面積は 10 平方メートル、a=2,b=3 のとき面積は 39 平方メートルとなります。
しかし a,b がどのような正の整数であったとしても面積が 20 平方メートルになることはありません。
よって、人 2 の予想だけは確実に誤りであることがわかります。
入力例 2
5 666 777 888 777 666
出力例 2
3
Score : 200 points
Problem Statement
There are N people numbered 1 to N.
Person i guessed the building area of KEYENCE headquarters building to be S_i square meters.
The shape of KEYENCE headquarters building is shown below, where a and b are some positive integers.
That is, the building area of the building can be represented as 4ab+3a+3b.
Based on just this information, how many of the N people are guaranteed to be wrong in their guesses?

Constraints
- 1 \leq N \leq 20
- 1 \leq S_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N S_1 \ldots S_N
Output
Print the answer.
Sample Input 1
3 10 20 39
Sample Output 1
1
The area would be 10 square meters if a=1,b=1, and 39 square meters if a=2,b=3.
However, no pair of positive integers a and b would make the area 20 square meters.
Thus, we can only be sure that Person 2 guessed wrong.
Sample Input 2
5 666 777 888 777 666
Sample Output 2
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
チェス盤のどこにコマが置かれているか答えてください。
縦 8 マス、横 8 マスのグリッドがあります。グリッドの各マスには、次のルールで定められる長さ 2 の文字列の名前がついています。
- 左から 1 列目にあるマスの名前の 1 文字目は
aである。同様に、左から 2,3,\ldots,8 列目にあるマスの名前の 1 文字目はb,c,d,e,f,g,hである。 - 下から 1 行目にあるマスの名前の 2 文字目は
1である。同様に、下から 2,3,\ldots,8 行目にあるマスの名前の 2 文字目は2,3,4,5,6,7,8である。
例えば、グリッドの左下のマスの名前は a1、右下のマスの名前は h1、右上のマスの名前は h8 です。
グリッドの状態を表す長さ 8 の 8 つの文字列 S_1,\ldots,S_8 が与えられます。
S_i の j 文字目は、グリッドの上から i 行目 左から j 列目のマスにコマが置かれているとき *、置かれていないとき . であり、S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在します。
コマが置かれているマスの名前を求めてください。
制約
- S_i は
.および*のみからなる長さ 8 の文字列である - S_1,\ldots,S_8 の中に文字
*はちょうど 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
出力
答えを出力せよ。
入力例 1
........ ........ ........ ........ ........ ........ ........ *.......
出力例 1
a1
問題文中で説明したとおり、グリッドの左下のマスの名前は a1 です。
入力例 2
........ ........ ........ ........ ........ .*...... ........ ........
出力例 2
b3
Score : 200 points
Problem Statement
Locate a piece on a chessboard.
We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2-character name determined as follows.
- The first character of the name of a square in the 1-st column from the left is
a. Similarly, the first character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th column from the left isb,c,d,e,f,g,h, respectively. - The second character of the name of a square in the 1-st row from the bottom is
1. Similarly, the second character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th row from the bottom is2,3,4,5,6,7,8, respectively.
For instance, the bottom-left square is named a1, the bottom-right square is named h1, and the top-right square is named h8.
You are given 8 strings S_1,\ldots,S_8, each of length 8, representing the state of the grid.
The j-th character of S_i is * if the square at the i-th row from the top and j-th column from the left has a piece on it, and . otherwise.
The character * occurs exactly once among S_1,\ldots,S_8.
Find the name of the square that has a piece on it.
Constraints
- S_i is a string of length 8 consisting of
.and*. - The character
*occurs exactly once among S_1,\ldots,S_8.
Input
The input is given from Standard Input in the following format:
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
Output
Print the answer.
Sample Input 1
........ ........ ........ ........ ........ ........ ........ *.......
Sample Output 1
a1
As explained in the problem statement, the bottom-left square is named a1.
Sample Input 2
........ ........ ........ ........ ........ .*...... ........ ........
Sample Output 2
b3
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
文字列 T が次の条件を満たすとき、T を良い括弧列と呼びます。
-
次の操作を 0 回以上繰り返すことで T を空文字列にすることができる。
- T に(連続な)部分文字列として含まれる
()を選び、これを取り除く。
- T に(連続な)部分文字列として含まれる
例えば () や (()()), および空文字列は良い括弧列ですが、)()( や ))) は良い括弧列ではありません。
文字列 S があります。S ははじめ空文字列です。
以下で説明されるクエリを与えられる順に Q 個処理してください。そして、各クエリの直後に S が良い括弧列であるかを判定してください。
クエリは次の 2 種類です。
1 c: 文字 c が与えられる。c は(または)である。c を S の末尾に追加する。2: S の末尾の文字を削除する。この時、S は空文字列でないことが保証される。
制約
- 1 \leq Q \leq 8 \times 10^5
- 1 種類目のクエリの c は
(または) - 2 種類目のクエリを与えられた時点で S は空でないことが保証される
- Q は整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_i は i 番目のクエリを意味する。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の 2 種類のいずれかの形式で与えられる。
1 c
2
出力
Q 行出力せよ。i 行目には、i 番目のクエリを処理した直後の文字列 S が良い括弧列である場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
8 1 ( 2 1 ( 1 ) 2 1 ( 1 ) 1 )
出力例 1
No Yes No Yes No No No Yes
1 番目のクエリを処理した直後の S は ( で、これは良い括弧列ではありません。
2 番目のクエリを処理した直後の S は空文字列で、これは良い括弧列です。
3 番目のクエリを処理した直後の S は ( で、これは良い括弧列ではありません。
4 番目のクエリを処理した直後の S は () で、これは良い括弧列です。
5 番目のクエリを処理した直後の S は ( で、これは良い括弧列ではありません。
6 番目のクエリを処理した直後の S は (( で、これは良い括弧列ではありません。
7 番目のクエリを処理した直後の S は (() で、これは良い括弧列ではありません。
8 番目のクエリを処理した直後の S は (()) で、これは良い括弧列です。
Score : 300 points
Problem Statement
A string T is called a good bracket sequence if and only if it satisfies the following condition:
-
T can be made into an empty string by repeating the following operation zero or more times:
- Choose
()contained in T as a (contiguous) substring and remove it.
- Choose
For example, (), (()()), and the empty string are good bracket sequences, but )()( and ))) are not good bracket sequences.
There is a string S. Initially, S is an empty string.
Process Q queries in the order they are given. After each query, determine whether S is a good bracket sequence.
There are two types of queries:
1 c: A character c is given. c is either(or). Append c to the end of S.2: Remove the last character of S. It is guaranteed that S is not an empty string at this time.
Constraints
- 1 \leq Q \leq 8 \times 10^5
- c in queries of the first type is
(or). - It is guaranteed that S is not empty when a query of the second type is given.
- Q is an integer.
Input
The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i-th query.
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in one of the following two formats:
1 c
2
Output
Output Q lines. The i-th line should contain Yes if the string S immediately after processing the i-th query is a good bracket sequence, and No otherwise.
Sample Input 1
8 1 ( 2 1 ( 1 ) 2 1 ( 1 ) 1 )
Sample Output 1
No Yes No Yes No No No Yes
S immediately after processing the 1st query is (, which is not a good bracket sequence.
S immediately after processing the 2nd query is an empty string, which is a good bracket sequence.
S immediately after processing the 3rd query is (, which is not a good bracket sequence.
S immediately after processing the 4th query is (), which is a good bracket sequence.
S immediately after processing the 5th query is (, which is not a good bracket sequence.
S immediately after processing the 6th query is ((, which is not a good bracket sequence.
S immediately after processing the 7th query is ((), which is not a good bracket sequence.
S immediately after processing the 8th query is (()), which is a good bracket sequence.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
人 1, 人 2,\ldots, 人 N の N 人が一列に並んでいます。
並び方の情報が長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) として与えられます。
A _ i\ (1\leq i\leq N) は次のような情報を表しています。
- A _ i=-1 のとき、人 i は先頭に並んでいる。
- A _ i\neq -1 のとき、人 i は人 A _ i のすぐ後ろに並んでいる。
列に並んでいる人の番号を先頭から順番に出力してください。
制約
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 もしくは 1\leq A _ i\leq N\ (1\leq i\leq N)
- 情報と矛盾しないような N 人の並び方がただ 1 通り存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
人 s _ 1, 人 s _ 2,\ldots, 人 s _ N がこの順に列に並んでいるとき、s _ 1,s _ 2,\ldots,s _ N をこの順に空白を区切りとして出力せよ。
入力例 1
6 4 1 -1 5 3 2
出力例 1
3 5 4 1 2 6
先頭から、人 3, 人 5, 人 4, 人 1, 人 2, 人 6 がこの順に列に並んでいるとき、与えられた情報と一致しています。
実際、
- 人 1 は人 4 のすぐ後ろに並んでいます。
- 人 2 は人 1 のすぐ後ろに並んでいます。
- 人 3 は先頭に並んでいます。
- 人 4 は人 5 のすぐ後ろに並んでいます。
- 人 5 は人 3 のすぐ後ろに並んでいます。
- 人 6 は人 2 のすぐ後ろに並んでいます。
となり、与えられた情報と一致していることが確認できます。
よって、3,5,4,1,2,6 をこの順に空白区切りで出力してください。
入力例 2
10 -1 1 2 3 4 5 6 7 8 9
出力例 2
1 2 3 4 5 6 7 8 9 10
入力例 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
出力例 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Score: 300 points
Problem Statement
There are N people standing in a line: person 1, person 2, \ldots, person N.
You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
A _ i\ (1\leq i\leq N) represents the following information:
- if A _ i=-1, person i is at the front of the line;
- if A _ i\neq -1, person i is right behind person A _ i.
Print the people's numbers in the line from front to back.
Constraints
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
- There is exactly one way to arrange the N people consistent with the information given.
- 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
If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.
Sample Input 1
6 4 1 -1 5 3 2
Sample Output 1
3 5 4 1 2 6
If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.
Indeed, it can be seen that:
- person 1 is standing right behind person 4,
- person 2 is standing right behind person 1,
- person 3 is at the front of the line,
- person 4 is standing right behind person 5,
- person 5 is standing right behind person 3, and
- person 6 is standing right behind person 2.
Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.
Sample Input 2
10 -1 1 2 3 4 5 6 7 8 9
Sample Output 2
1 2 3 4 5 6 7 8 9 10
Sample Input 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
Sample Output 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
ある星には N 人の宇宙人がおり、全員未成年です。
i 人目の宇宙人は現在 A_i 個の石を所持しており、ちょうど i 年後に成人します。
この星では誰かが成人するとき、石を 1 個以上所持している成人全員が、成人する宇宙人に成人祝いとして石を 1 個渡します。
N 年後に各宇宙人が所持している石の個数を求めてください。
ただし、今後新たな宇宙人は産まれないものとします。
制約
- 1 \leq N \leq 5 \times 10^5
- 0 \leq A_i \leq 5 \times 10^5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
N 年後に i 人目の宇宙人が所持している石の個数を B_i として B_1, B_2, \ldots, B_N をこの順に空白区切りで出力せよ。
入力例 1
4 5 0 9 3
出力例 1
2 0 10 5
i 人目の宇宙人が持っている石の個数を C_i で表します。
はじめ (C_1, C_2, C_3, C_4) = (5, 0, 9, 3) です。
1 年後には (C_1, C_2, C_3, C_4) = (5, 0, 9, 3) となります。
2 年後には (C_1, C_2, C_3, C_4) = (4, 1, 9, 3) となります。
3 年後には (C_1, C_2, C_3, C_4) = (3, 0, 11, 3) となります。
4 年後には (C_1, C_2, C_3, C_4) = (2, 0, 10, 5) となります。
入力例 2
5 4 6 7 2 5
出力例 2
0 4 7 4 9
入力例 3
10 2 9 1 2 0 4 6 7 1 5
出力例 3
0 2 0 0 0 4 7 10 4 10
Score : 400 points
Problem Statement
On a certain planet, there are N aliens, all of whom are minors.
The i-th alien currently has A_i stones, and will become an adult exactly i years later.
When someone becomes an adult on this planet, every adult who has at least one stone gives exactly one stone as a congratulatory gift to the alien who has just become an adult.
Find how many stones each alien will have after N years.
Assume that no new aliens will be born in the future.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 0 \leq A_i \leq 5 \times 10^5
- 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
Let B_i be the number of stones owned by the i-th alien after N years. Print B_1, B_2, \ldots, B_N in this order, separated by spaces.
Sample Input 1
4 5 0 9 3
Sample Output 1
2 0 10 5
Let C_i be the number of stones that the i-th alien has at a given time.
Initially, (C_1, C_2, C_3, C_4) = (5, 0, 9, 3).
After 1 year, (C_1, C_2, C_3, C_4) = (5, 0, 9, 3).
After 2 years, (C_1, C_2, C_3, C_4) = (4, 1, 9, 3).
After 3 years, (C_1, C_2, C_3, C_4) = (3, 0, 11, 3).
After 4 years, (C_1, C_2, C_3, C_4) = (2, 0, 10, 5).
Sample Input 2
5 4 6 7 2 5
Sample Output 2
0 4 7 4 9
Sample Input 3
10 2 9 1 2 0 4 6 7 1 5
Sample Output 3
0 2 0 0 0 4 7 10 4 10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
H 行 W 列のグリッドがあります。このグリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。
高橋君はこのグリッドの 1 つのマスの上におり、グリッド上のいくつかのマスにはゴミが落ちています。
各マスの状態は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_i の j 文字目を S_{i, j} として、以下のような状態を表します。
- S_{i, j} =
Tのときマス (i, j) は高橋君がいるマスである。 - S_{i, j} =
#のときマス (i, j) はゴミが落ちているマスである。 - S_{i, j} =
.のときマス (i, j) はゴミが落ちていない空きマスである。
また、高橋君がいるマスにはゴミは落ちていません。
高橋君は、以下の操作を繰り返し行うことができます。
- 上下左右のいずれかの方向を 1 つ選び、すべてのゴミを同時にその方向へ 1 マス移動させる。ただし、ゴミがグリッドの外に出た場合にはゴミは消滅し、高橋君のいるマスにゴミが移動した場合は高橋君は汚れる。
高橋君が汚れることなくすべてのゴミを消滅させることができるか判定し、できる場合は操作を行う回数の最小値を求めてください。
制約
- 2 \leq H, W \leq 12
- S_i は長さ W の
T,#,.からなる文字列 - S_{i, j} =
Tなる (i, j) がちょうど 1 つ存在する - S_{i, j} =
#なる (i, j) が1 つ以上存在する
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
高橋君が汚れることなくすべてのゴミを消滅させることができる場合、操作を行う回数として考えられる最小値を出力せよ。そうでないとき、-1 と出力せよ。
入力例 1
3 4 ###. #.T. ...#
出力例 1
3
高橋君は以下のように操作を行うことで 3 回の操作ですべてのゴミを消滅させることができます。
-
上を選ぶ。この操作後、ゴミはマス (1, 1) とマス (2, 4) にある。
-
右を選ぶ。この操作後、ゴミはマス (1, 2) にある。
-
上を選ぶ。この操作後、すべてのゴミは消滅している。
入力例 2
3 3 ### #T# ###
出力例 2
-1
入力例 3
5 5 ###.. #T... ..##. ##..# #..#.
出力例 3
5
Score : 500 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Takahashi is on one of the cells in this grid, and there is trash on some cells of the grid.
The states of the cells are given by H strings S_1, S_2, \ldots, S_H of length W, where S_{i, j} denotes the j-th character of S_i and represents the following state:
- If S_{i, j} =
T, cell (i, j) is the cell where Takahashi is. - If S_{i, j} =
#, cell (i, j) is a cell with trash. - If S_{i, j} =
., cell (i, j) is an empty cell without trash.
There is no trash on the cell where Takahashi is.
He can repeatedly perform the following operation:
- Choose one of the four directions (up, down, left, or right), and move all trash simultaneously one cell in that direction. Here, if trash goes outside the grid, the trash disappears, and if trash moves to the cell where he is, he gets dirty.
Determine whether it is possible for him to make all trash disappear without getting dirty, and if possible, find the minimum possible number of operations.
Constraints
- 2 \leq H, W \leq 12
- S_i is a string of length W consisting of
T,#, and.. - There is exactly one (i, j) such that S_{i, j} =
T. - There is at least one (i, j) such that S_{i, j} =
#.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
If it is possible for Takahashi to make all trash disappear without getting dirty, print the minimum possible number of operations. Otherwise, print -1.
Sample Input 1
3 4 ###. #.T. ...#
Sample Output 1
3
Takahashi can make all trash disappear in three operations as follows:
-
Choose up. After this operation, trash is on cells (1, 1) and (2, 4).
-
Choose right. After this operation, trash is on cell (1, 2).
-
Choose up. After this operation, all trash has disappeared.
Sample Input 2
3 3 ### #T# ###
Sample Output 2
-1
Sample Input 3
5 5 ###.. #T... ..##. ##..# #..#.
Sample Output 3
5
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
正整数からなる(空でも良い)数列 X=(X_1,X_2,\ldots) が以下の 3 つの条件をすべてみたすとき、かつそのときに限り、X を 1122 数列 と呼びます。
(1122 数列の定義はD問題と共通です。)
- \lvert X \rvert は偶数である。ここで、\lvert X \rvert は X の長さを表す。
- 1\leq i\leq \frac{\lvert X \rvert}{2} をみたす整数 i について、X_{2i-1} と X_{2i} は等しい。
- 各正整数は X に現れないか、ちょうど 2 回現れるかのどちらかである。すなわち、X に含まれる正整数は X にちょうど 2 回ずつ登場する。
正整数からなる長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられるので、A の (連続でなくても良い)部分列 であって、1122 数列であるようなもののうち最長のものの長さを出力してください。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 20
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
A の(連続でなくても良い)部分列であって、1122 数列であるようなもののうち最長のものの長さを出力せよ。
入力例 1
7 1 3 3 1 2 2 1
出力例 1
4
例えば A の部分列として、1,4,5,6 項目をとると (1,1,2,2) となりますが、これは長さが 4 の 1122 数列となっています。
これより長い部分列であって、1122 数列の条件をみたすようなものは存在しないため、4 を出力します。
入力例 2
1 20
出力例 2
0
Score : 525 points
Problem Statement
A sequence X = (X_1, X_2, \ldots) of positive integers (possibly empty) is called a 1122 sequence if and only if it satisfies all of the following three conditions: (The definition of a 1122 sequence is the same as in Problem D.)
- \lvert X \rvert is even. Here, \lvert X \rvert denotes the length of X.
- For each integer i satisfying 1\leq i\leq \frac{|X|}{2}, X_{2i-1} and X_{2i} are equal.
- Each positive integer appears in X either not at all or exactly twice. That is, every positive integer contained in X appears exactly twice in X.
Given a sequence A = (A_1, A_2, \ldots, A_N) of length N consisting of positive integers, print the maximum length of a subsequence (not necessarily contiguous) of A that is a 1122 sequence.
Constraints
- 1\leq N \leq 2 \times 10^5
- 1\leq A_i \leq 20
- 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 maximum length of a (not necessarily contiguous) subsequence of A that is a 1122 sequence.
Sample Input 1
7 1 3 3 1 2 2 1
Sample Output 1
4
For example, choosing the 1-st, 4-th, 5-th, and 6-th elements of A, we get (1, 1, 2, 2), which is a 1122 sequence of length 4.
There is no longer subsequence that satisfies the conditions for a 1122 sequence, so the answer is 4.
Sample Input 2
1 20
Sample Output 2
0