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
配点 : 200 点
問題文
長さ N の整数列 A=(A_1,\ldots,A_N) が与えられます。ここで A_1,A_2,\ldots,A_N は相異なります。
A の中で二番目に大きい要素は A の何番目の要素でしょうか。
制約
- 2\leq N\leq 100
- 1\leq A_i \leq 10^9
- A_1,A_2,\ldots,A_N は相異なる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{N}
出力
A の中で二番目に大きい要素が A の X 番目であるとき、X を整数として出力せよ。
入力例 1
4 8 2 5 1
出力例 1
3
A の中で二番目に大きい要素は A_3 なので 3 を出力してください。
入力例 2
8 1 2 3 4 5 10 9 11
出力例 2
6
Score : 200 points
Problem Statement
You are given an integer sequence A=(A_1,\ldots,A_N) of length N. Here, A_1, A_2, \ldots, A_N are all distinct.
Which element in A is the second largest?
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 10^9
- A_1, A_2, \ldots, A_N are all distinct.
- 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 integer X such that the X-th element in A is the second largest.
Sample Input 1
4 8 2 5 1
Sample Output 1
3
The second largest element in A is A_3, so print 3.
Sample Input 2
8 1 2 3 4 5 10 9 11
Sample Output 2
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
文字列 S の各文字を並べ替えて作ることが可能な文字列を辞書順にすべて列挙したとき、前から K 番目にくる文字列を求めてください。
「各文字を並べ替えて作ることが可能な文字列」とは?
「文字列 A が文字列 B の各文字を並べ替えて作ることが可能な文字列である」とは、任意の文字が文字列 A と文字列 B に同数含まれるということを指します。制約
- 1 \le |S| \le 8
- S は英小文字のみからなる
- S の各文字を並べ替えてできる文字列は K 種類以上存在する
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
答えを出力せよ。
入力例 1
aab 2
出力例 1
aba
文字列 aab の各文字を並べ替えて作ることが可能な文字列は \{ aab, aba, baa \} の 3 つですが、このうち辞書順で前から 2 番目にくるものは aba です。
入力例 2
baba 4
出力例 2
baab
入力例 3
ydxwacbz 40320
出力例 3
zyxwdcba
Score : 300 points
Problem Statement
Find the K-th lexicographically smallest string among the strings that are permutations of a string S.
What is a permutation of a string?
A string A is said to be a permutation of a string B when any character occurs the same number of times in A and B.Constraints
- 1 \le |S| \le 8
- S consists of lowercase English letters.
- There are at least K distinct strings that are permutations of S.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the answer.
Sample Input 1
aab 2
Sample Output 1
aba
There are three permutations of a string aab: \{ aab, aba, baa \}. The 2-nd lexicographically smallest of them is aba.
Sample Input 2
baba 4
Sample Output 2
baab
Sample Input 3
ydxwacbz 40320
Sample Output 3
zyxwdcba
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字からなる文字列 S, T が与えられます。ここで、S と T の長さは等しいです。
X を空の配列とし、以下の操作を S と T が等しくなるまで繰り返します。
- S の文字を 1 文字書き換え、X の末尾に S を追加する。
こうして得られる文字列の配列 X のうち要素数最小のものを求めてください。要素数最小のものが複数考えられる場合は、そのうち辞書順最小のものを求めてください。
文字列の配列の辞書順とは
長さ N の文字列 S = S_1 S_2 \ldots S_N が長さ N の文字列 T = T_1 T_2 \ldots T_N より辞書順で小さいとは、ある整数 1 \leq i \leq N が存在して下記の 2 つがともに成り立つことをいいます。
- S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
- S_i が T_i よりアルファベット順で早い。
要素数 M の文字列の配列 X = (X_1,X_2,\ldots,X_M) が要素数 M の文字列の配列 Y = (Y_1,Y_2,\ldots,Y_M) より辞書順で小さいとは、ある整数 1 \leq j \leq M が存在して下記の 2 つがともに成り立つことをいいます。
- (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
- X_j が Y_j より辞書順で小さい。
制約
- S, T は英小文字からなる長さ 1 以上 100 以下の文字列
- S と T の長さは等しい
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
答えの要素数を M として M + 1 行出力せよ。
1 行目には M の値を出力せよ。
i + 1 (1 \leq i \leq M) 行目には答えの i 番目の要素を出力せよ。
入力例 1
adbe bcbc
出力例 1
3 acbe acbc bcbc
はじめ、S = adbe です。
以下のように操作することで、X = ( acbe , acbc , bcbc ) とすることができます。
-
S を
acbeに書き換え、X の末尾にacbeを追加する。 -
S を
acbcに書き換え、X の末尾にacbcを追加する。 -
S を
bcbcに書き換え、X の末尾にbcbcを追加する。
入力例 2
abcde abcde
出力例 2
0
入力例 3
afwgebrw oarbrenq
出力例 3
8 aawgebrw aargebrw aarbebrw aarbebnw aarbebnq aarbeenq aarbrenq oarbrenq
Score : 300 points
Problem Statement
You are given two strings S and T consisting of lowercase English letters. Here, S and T have equal lengths.
Let X be an empty array, and repeat the following operation until S equals T:
- Change one character in S, and append S to the end of X.
Find the array of strings X with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.
What is lexicographical order on arrays of strings?
A string S = S_1 S_2 \ldots S_N of length N is lexicographically smaller than a string T = T_1 T_2 \ldots T_N of length N if there exists an integer 1 \leq i \leq N such that both of the following are satisfied:
- S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
- S_i comes earlier than T_i in alphabetical order.
An array of strings X = (X_1,X_2,\ldots,X_M) with M elements is lexicographically smaller than an array of strings Y = (Y_1,Y_2,\ldots,Y_M) with M elements if there exists an integer 1 \leq j \leq M such that both of the following are satisfied:
- (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
- X_j is lexicographically smaller than Y_j.
Constraints
- S and T are strings consisting of lowercase English letters with length between 1 and 100, inclusive.
- The lengths of S and T are equal.
Input
The input is given from Standard Input in the following format:
S T
Output
Let M be the number of elements in the desired array. Print M + 1 lines.
The first line should contain the value of M.
The i + 1-th line (1 \leq i \leq M) should contain the i-th element of the array.
Sample Input 1
adbe bcbc
Sample Output 1
3 acbe acbc bcbc
Initially, S = adbe.
We can obtain X = ( acbe , acbc , bcbc ) by performing the following operations:
-
Change S to
acbeand appendacbeto the end of X. -
Change S to
acbcand appendacbcto the end of X. -
Change S to
bcbcand appendbcbcto the end of X.
Sample Input 2
abcde abcde
Sample Output 2
0
Sample Input 3
afwgebrw oarbrenq
Sample Output 3
8 aawgebrw aargebrw aarbebrw aarbebnw aarbebnq aarbeenq aarbrenq oarbrenq
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
atcoder の並べ替えである文字列 S が与えられます。
この文字列 S に対して以下の操作を 0 回以上行います。
- S 中の隣接する 2 文字を選び、入れ替える。
S を atcoder にするために必要な最小の操作回数を求めてください。
制約
- S は
atcoderの並べ替えである文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
catredo
出力例 1
8
catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
という流れで操作を行うと、 8 回で S を atcoder にすることができ、これが達成可能な最小の操作回数です。
入力例 2
atcoder
出力例 2
0
この場合、文字列 S は元から atcoder です。
入力例 3
redocta
出力例 3
21
Score : 400 points
Problem Statement
You are given a string S that is a permutation of atcoder.
On this string S, you will perform the following operation 0 or more times:
- Choose two adjacent characters of S and swap them.
Find the minimum number of operations required to make S equal atcoder.
Constraints
- S is a string that is a permutation of
atcoder
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
catredo
Sample Output 1
8
You can make S equal atcoder in 8 operations as follows:
catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
This is the minimum number of operations achievable.
Sample Input 2
atcoder
Sample Output 2
0
In this case, the string S is already atcoder.
Sample Input 3
redocta
Sample Output 3
21