Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
2N 人の人が横一列に並んでおり、左から i 番目の人は色 A_i の服を着ています。ここで、服の色は 1 から N の N 色であり、それぞれの色についてちょうど 2 人の人がその色の服を着ています。
i=1,2,\ldots,N のうち、以下の条件を満たすものは何通りあるか求めてください。
- 色 i の服を着た二人の人の間にはちょうど一人いる。
制約
- 2\leq N\leq 100
- 1\leq A_i \leq N
- A には 1 以上 N 以下の整数全てがそれぞれ 2 個ずつ含まれる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{2N}
出力
答えを出力せよ。
入力例 1
3 1 2 1 3 2 3
出力例 1
2
条件を満たす i は 1 と 3 の 2 個です。
実際、色 1 の服を着ているのは左から 1 番目の人と左から 3 番目の人で、間にちょうど一人います。
入力例 2
2 1 1 2 2
出力例 2
0
条件を満たす i が存在しない場合もあります。
入力例 3
4 4 3 2 3 2 1 4 1
出力例 3
3
Score : 150 points
Problem Statement
There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color A_i. Here, the clothes have N colors from 1 to N, and exactly two people are wearing clothes of each color.
Find how many of the integers i=1,2,\ldots,N satisfy the following condition:
- There is exactly one person between the two people wearing clothes of color i.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq N
- Each integer from 1 through N appears exactly twice in A.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{2N}
Output
Print the answer.
Sample Input 1
3 1 2 1 3 2 3
Sample Output 1
2
There are two values of i that satisfy the condition: 1 and 3.
In fact, the people wearing clothes of color 1 are at the 1st and 3rd positions from the left, with exactly one person in between.
Sample Input 2
2 1 1 2 2
Sample Output 2
0
There may be no i that satisfies the condition.
Sample Input 3
4 4 3 2 3 2 1 4 1
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
高橋君は改札機の利用履歴を集計しました。 しかし、高橋君はうっかりいくつかの入退場記録を消してしまいました。 高橋君は消してしまった記録の復元を試みようとしています。
i, o のみからなる文字列 S が与えられます。
S の任意の位置に文字を 0 文字以上挿入することで、変更後の文字列が以下の条件を満たすようにしたいです。
- 長さが偶数であり、奇数文字目が
iで偶数文字目がoである。
挿入する必要のある文字数の最小値を求めて下さい。なお、問題の制約下で、有限個の文字を適切に挿入することで、S が条件をみたすようにできることが証明できます。
制約
- S は
i,oからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ioi
出力例 1
1
3 文字目のあとに o を挿入して ioio とすることで、条件を満たすことができます。0 文字以下の挿入で条件を満たすことはできません。
入力例 2
iioo
出力例 2
2
1 文字目のあとに o を、3 文字目のあとに i を挿入することで、条件を満たすことができます。1 文字以下の挿入で条件を満たすことはできません。
入力例 3
io
出力例 3
0
S がすでに条件を満たします。
Score : 250 points
Problem Statement
Takahashi aggregated usage records from ticket gates. However, he accidentally erased some records of entering and exiting stations. He is trying to restore the erased records.
You are given a string S consisting of i and o. We want to insert zero or more characters at arbitrary positions in S so that the resulting string satisfies the following conditions:
- Its length is even, and every odd-numbered (1st, 3rd, ...) character is
iwhile every even-numbered (2nd, 4th, ...) character iso.
Find the minimum number of characters that need to be inserted. It can be proved under the constraints of this problem that by inserting an appropriate finite number of characters, S can be made to satisfy the conditions.
Constraints
- S is a string of length between 1 and 100, consisting of
iando.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ioi
Sample Output 1
1
We can insert o after the 3rd character to form ioio to satisfy the conditions. The conditions cannot be satisfied by inserting zero or fewer characters.
Sample Input 2
iioo
Sample Output 2
2
We can insert o after the 1st character and i after the 3rd character to satisfy the conditions. The conditions cannot be satisfied by inserting one or fewer characters.
Sample Input 3
io
Sample Output 3
0
S already satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
非負整数 K に対して、以下のようにレベル K のカーペットを定義します。
- レベル 0 のカーペットは黒いマス 1 個のみからなる 1\times 1 のグリッドである。
- K>0 のとき、レベル K のカーペットは 3^K\times 3^K のグリッドである。
このグリッドを 3^{K-1}\times 3^{K-1} のブロック 9 個に分割したとき、
- 中央のブロックはすべて白いマスからなる。
- 他の 8 個のブロックは、レベル (K-1) のカーペットである。
非負整数 N が与えられます。
レベル N のカーペットを出力の形式に従って出力してください。
制約
- 0\leq N \leq 6
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
3^N 行出力せよ。
i 行目 (1\leq i\leq 3^N) には、. と # からなる長さ 3^N の文字列 S_i を出力せよ。
S_i の j 文字目 (1\leq j\leq 3^N) は、レベル N のカーペットの上から i 行目かつ左から j 列目のマスが黒いとき # とし、白いとき . とせよ。
入力例 1
1
出力例 1
### #.# ###
レベル 1 のカーペットは次のような 3\times 3 のグリッドです。

これを出力形式にしたがって出力すると出力例のようになります。
入力例 2
2
出力例 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
レベル 2 のカーペットは 9\times 9 のグリッドとなります。
Score : 250 points
Problem Statement
For a non-negative integer K, we define a level-K carpet as follows:
- A level-0 carpet is a 1 \times 1 grid consisting of a single black cell.
- For K > 0, a level-K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks:
- The central block consists entirely of white cells.
- The other eight blocks are level-(K-1) carpets.
You are given a non-negative integer N.
Print a level-N carpet according to the specified format.
Constraints
- 0 \leq N \leq 6
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print 3^N lines.
The i-th line (1 \leq i \leq 3^N) should contain a string S_i of length 3^N consisting of . and #.
The j-th character of S_i (1 \leq j \leq 3^N) should be # if the cell at the i-th row from the top and j-th column from the left of a level-N carpet is black, and . if it is white.
Sample Input 1
1
Sample Output 1
### #.# ###
A level-1 carpet is a 3 \times 3 grid as follows:

When output according to the specified format, it looks like the sample output.
Sample Input 2
2
Sample Output 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
A level-2 carpet is a 9 \times 9 grid.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
長さ N の整数列 A,B が与えられます。1 以上 N 以下の整数 i,j を選んで、 A_i + B_j の値を最大化してください。
制約
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9\,(i=1,2,\dots,N)
- |B_j| \leq 10^9\,(j=1,2,\dots,N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
A_i + B_j の最大値を出力せよ。
入力例 1
2 -1 5 3 -7
出力例 1
8
(i,j)=(1,1),(1,2),(2,1),(2,2) に対する A_i+B_j の値はそれぞれ 2,-8,8,-2 であり、(i,j)=(2,1) が最大値 8 を達成します。
入力例 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
出力例 2
33
Score : 250 points
Problem Statement
You are given two integer sequences A and B, each of length N. Choose integers i, j (1 \leq i, j \leq N) to maximize the value of A_i + B_j.
Constraints
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9 (i=1,2,\dots,N)
- |B_j| \leq 10^9 (j=1,2,\dots,N)
- 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
Output
Print the maximum possible value of A_i + B_j.
Sample Input 1
2 -1 5 3 -7
Sample Output 1
8
For (i,j) = (1,1), (1,2), (2,1), (2,2), the values of A_i + B_j are 2, -8, 8, -2 respectively, and (i,j) = (2,1) achieves the maximum value 8.
Sample Input 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
Sample Output 2
33
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
0 および 1 からなる長さ N の文字列 S が与えられます。
あなたは S に対して以下の操作を好きな回数(0 回でもよい)行うことができます。
- 先頭または末尾の 1 文字を削除し、その文字を反転して(
0ならば1に、1ならば0に変えて)、好きな位置に挿入し直す。 より厳密には、r(0)=1,r(1)=0として、以下のいずれかを行う。(ここで、S_i は S の i 文字目を表す。)- 好きな i\ (1\leq i\leq N) を選んで、S を S_2\dots S_i\,r(S_1)\,S_{i+1}\dots S_N に変更する。
- 好きな i\ (0\leq i\leq N-1) を選んで、S を S_1\dots S_i\,r(S_N)\,S_{i+1}\dots S_{N-1} に変更する。
S の全ての文字を同じ文字にするために必要な操作回数の最小値を求めてください。 なお、そのような操作列が必ず存在することは証明可能です。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 2\times 10^5
- 2\leq N \leq 5\times 10^5
- T,N は整数
- S は
0および1からなる長さ N の文字列 - 全てのテストケースにおける N の総和は 5\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
N S
出力
T 行出力せよ。i 行目 (1\leq i\leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
3 5 01001 3 000 15 110010111100101
出力例 1
4 0 16
1 番目のテストケースについて、例えば以下のように 4 回の操作をすることで S の全ての文字を 0 にすることができます。
3 回以下の操作で S の全ての文字を同じ文字にすることはできないので、答えは 4 です。
- 先頭の文字
0を削除し、1を(削除した後の S における)1 文字目と 2 文字目の間に挿入する。S=11001になる。 - 先頭の文字
1を削除し、0を(削除した後の S における)2 文字目と 3 文字目の間に挿入する。S=10001になる。 - 末尾の文字
1を削除し、0を(削除した後の S における)末尾に挿入する。S=10000になる。 - 先頭の文字
1を削除し、0を(削除した後の S における)先頭に挿入する。S=00000になる。
2 番目のテストケースについて、はじめから S の全ての文字が同じ文字であるため、操作は 1 回も必要ありません。
Score : 400 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
You can perform the following operation on S any number of times (possibly zero):
- Delete the first or last character, flip it (change
0to1or1to0), and insert it back at any position. More formally, let r(0)=1and r(1)=0, and perform one of the following: (Here, S_i denotes the i-th character of S.)- Choose any i\ (1\leq i\leq N) and change S to S_2\dots S_i\,r(S_1)\,S_{i+1}\dots S_N.
- Choose any i\ (0\leq i\leq N-1) and change S to S_1\dots S_i\,r(S_N)\,S_{i+1}\dots S_{N-1}.
Find the minimum number of operations required to make all characters of S the same. It can be proved that such a sequence of operations always exists.
You are given T test cases, so solve each of them.
Constraints
- 1\leq T\leq 2\times 10^5
- 2\leq N \leq 5\times 10^5
- T and N are integers.
- S is a string of length N consisting of
0and1. - The sum of N over all test cases is at most 5\times 10^5.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i represents the i-th test case. Each test case is given in the following format:
N S
Output
Output T lines. The i-th line (1\leq i\leq T) should contain the answer for the i-th test case.
Sample Input 1
3 5 01001 3 000 15 110010111100101
Sample Output 1
4 0 16
For the first test case, for example, you can make all characters of S into 0 with four operations as follows.
It is impossible to make all characters of S the same with three or fewer operations, so the answer is 4.
- Delete the first character
0, and insert1between the 1st and 2nd characters (in S after deletion). S becomes11001. - Delete the first character
1, and insert0between the 2nd and 3rd characters (in S after deletion). S becomes10001. - Delete the last character
1, and insert0at the end (in S after deletion). S becomes10000. - Delete the first character
1, and insert0at the beginning (in S after deletion). S becomes00000.
For the second test case, all characters of S are the same from the beginning, so no operation is needed.