実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さは |i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。
残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。
制約
- 1\leq K\leq N \leq 2\times 10^5
- 1\leq A_1 < A_2 < \dots < A_K \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_K
出力
奇妙さの総和の最小値を整数として出力せよ。
入力例 1
4 2 1 3
出力例 1
2
以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。
色 1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4) の 3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。
入力例 2
5 1 2
出力例 2
0
(1,1),(3,3),(4,4),(5,5) の 4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。
入力例 3
8 5 1 2 4 7 8
出力例 3
2
Score : 350 points
Problem Statement
Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.
Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.
Constraints
- 1\leq K\leq N \leq 2\times 10^5
- 1\leq A_1 < A_2 < \dots < A_K \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_K
Output
Print the minimum total weirdness as an integer.
Sample Input 1
4 2 1 3
Sample Output 1
2
Below, let (i,j) denote a pair of a sock of color i and a sock of color j.
There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.
Sample Input 2
5 1 2
Sample Output 2
0
The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).
Sample Input 3
8 5 1 2 4 7 8
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
いくつかの正方形を辺でつなげてできる、連結な多角形の形をしたパズルのピースのことを ポリオミノ と呼びます。
縦 4 マス、横 4 マスのグリッドと、グリッドに収まる大きさの 3 個のポリオミノがあります。
i 番目のポリオミノの形は 16 個の文字 P_{i,j,k} (1 \leq j, k \leq 4) によって表されます。P_{i, j, k} は何も置かれていないグリッドに i 番目のポリオミノを置いたときの状態を意味して、P_{i, j, k} が # の場合は上から j 行目、左から k 列目のマスにポリオミノが置かれていることを、. の場合は置かれていないことを意味します。(入出力例 1 の図も参考にしてください。)
あなたは次の条件を全て満たすように 3 個のポリオミノ全てをグリッドに敷き詰めることにしました。
- グリッドの全てのマスはポリオミノで覆われている。
- ポリオミノ同士が重なるように置くことはできない。
- ポリオミノがグリッドからはみ出るように置くことはできない。
- ポリオミノの平行移動と回転は自由に行うことができるが、裏返すことはできない。
条件を満たすようにグリッドにポリオミノを敷き詰めることは可能ですか?
制約
- P_{i, j, k} は
#または. - 与えられるポリオミノは連結である。つまり、ポリオミノを構成する正方形同士は、正方形のみを上下左右に辿って互いに行き来できる
- 与えられるポリオミノは空でない
入力
入力は以下の形式で標準入力から与えられる。
P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}
出力
問題文の条件を満たすようにポリオミノを敷き詰めることが可能である場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
.... ###. .#.. .... .... .### .##. .... ..#. .##. .##. .##.
出力例 1
Yes
入力例 1 に対応するポリオミノの形は次の図のようになります。

この場合、次の図のようにポリオミノを配置することで、問題文の条件を満たすようにグリッドにポリオミノを敷き詰めることができます。

よって答えは Yes になります。
入力例 2
###. #.#. ##.. .... .... ..#. .... .... #### ##.. #... #...
出力例 2
Yes
入力例 2 の 1 番目のポリオミノのように、ポリオミノは穴の空いた多角形の形をしている場合があります。
入力例 3
##.. #..# #### .... .... ##.. .##. .... .#.. .#.. .#.. .#..
出力例 3
No
ポリオミノを敷き詰めるときに、ポリオミノを裏返してはならないのに注意してください。
入力例 4
.... ..#. .... .... .... ..#. .... .... .... ..#. .... ....
出力例 4
No
入力例 5
.... #### #... #... .... #### ...# ..## .... ..## ..#. ..##
出力例 5
No
入力例 6
###. .##. ..#. .### .... ...# ..## ...# .... #... #... #...
出力例 6
Yes
Score : 400 points
Problem Statement
A polyomino is a puzzle piece in the shape of a connected polygon made by connecting several squares by their edges.
There is a grid with four rows and four columns, and three polyominoes that fit within the grid.
The shape of the i-th polyomino is represented by 16 characters P_{i,j,k} (1 \leq j, k \leq 4). They describe the state of the grid when the i-th polyomino is placed on it. If P_{i, j, k} is #, the square at the j-th row from the top and k-th column from the left is occupied by the polyomino; if it is ., the square is not occupied. (Refer to the figures at Sample Input/Output 1.)
You want to fill the grid with all three polyominoes so that all of the following conditions are satisfied.
- All squares of the grid are covered by the polyominoes.
- The polyominoes must not overlap each other.
- The polyominoes must not stick out of the grid.
- The polyominoes may be freely translated and rotated but may not be flipped over.
Can the grid be filled with the polyominoes to satisfy these conditions?
Constraints
- P_{i, j, k} is
#or.. - The given polyominoes are connected. In other words, the squares that make up a polyomino can be reached from each other by following only the squares up, down, left, and right.
- The given polyominoes are not empty.
Input
The input is given from Standard Input in the following format:
P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}
Output
If it is possible to fill the grid with the polyominoes to satisfy the conditions in the problem statement, print Yes; otherwise, print No.
Sample Input 1
.... ###. .#.. .... .... .### .##. .... ..#. .##. .##. .##.
Sample Output 1
Yes
The figure below shows the shapes of the polyominoes corresponding to Sample Input 1.

In this case, you can fill the grid with them to satisfy the conditions in the problem statement by placing them as shown in the figure below.

Thus, the answer is Yes.
Sample Input 2
###. #.#. ##.. .... .... ..#. .... .... #### ##.. #... #...
Sample Output 2
Yes
As in the first polyomino in Sample Input 2, a polyomino may be in the shape of a polygon with a hole.
Sample Input 3
##.. #..# #### .... .... ##.. .##. .... .#.. .#.. .#.. .#..
Sample Output 3
No
Note that the polyominoes may not be flipped over when filling the grid.
Sample Input 4
.... ..#. .... .... .... ..#. .... .... .... ..#. .... ....
Sample Output 4
No
Sample Input 5
.... #### #... #... .... #### ...# ..## .... ..## ..#. ..##
Sample Output 5
No
Sample Input 6
###. .##. ..#. .### .... ...# ..## ...# .... #... #... #...
Sample Output 6
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A = (A_1, \dots, A_N) と整数 M, K が与えられます。
i = 1, \dots, N - M + 1 に対して、次の独立な問題を解いてください。
M 個の整数 A_i, A_{i + 1}, \dots, A_{i + M - 1} を昇順に並べ替えたときの先頭 K 個の値の総和を求めよ。
制約
- 1 \leq K \leq M \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 A_2 \ldots A_N
出力
i = k のときの問題の答えを \mathrm{answer}_k として、次の形式で出力せよ。
\mathrm{answer}_1 \mathrm{answer}_2 \ldots \mathrm{answer}_{N-M+1}
入力例 1
6 4 3 3 1 4 1 5 9
出力例 1
5 6 10
- i = 1 のとき、A_i, A_{i+1}, A_{i+2}, A_{i+3} を小さい順に並べると 1, 1, 3, 4 となり、小さい方から 3 個の値の総和は 5 です。
- i = 2 のとき、A_i, A_{i+1}, A_{i+2}, A_{i+3} を小さい順に並べると 1, 1, 4, 5 となり、小さい方から 3 個の値の総和は 6 です。
- i = 3 のとき、A_i, A_{i+1}, A_{i+2}, A_{i+3} を小さい順に並べると 1, 4, 5, 9 となり、小さい方から 3 個の値の総和は 10 です。
入力例 2
10 6 3 12 2 17 11 19 8 4 3 6 20
出力例 2
21 14 15 13 13
Score : 500 points
Problem Statement
You are given an integer sequence A = (A_1, \dots, A_N) of length N, and integers M and K.
For each i = 1, \dots, N - M + 1, solve the following independent problem.
Find the sum of the first K values in the sorted list of the M integers A_i, A_{i + 1}, \dots, A_{i + M - 1} in ascending order.
Constraints
- 1 \leq K \leq M \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 A_2 \ldots A_N
Output
Let \mathrm{answer}_k be the answer to the problem for i = k, and print them in the following format:
\mathrm{answer}_1 \mathrm{answer}_2 \ldots \mathrm{answer}_{N-M+1}
Sample Input 1
6 4 3 3 1 4 1 5 9
Sample Output 1
5 6 10
- For i = 1, sorting A_i, A_{i+1}, A_{i+2}, A_{i+3} in ascending order yields 1, 1, 3, 4, where the sum of the first three values is 5.
- For i = 2, sorting A_i, A_{i+1}, A_{i+2}, A_{i+3} in ascending order yields 1, 1, 4, 5, where the sum of the first three values is 6.
- For i = 3, sorting A_i, A_{i+1}, A_{i+2}, A_{i+3} in ascending order yields 1, 4, 5, 9, where the sum of the first three values is 10.
Sample Input 2
10 6 3 12 2 17 11 19 8 4 3 6 20
Sample Output 2
21 14 15 13 13
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
A \cap B = \emptyset を満たす 2 つの整数の集合 A, B に対して、f(A,B) を以下のように定義します。
- A \cup B に含まれる要素を昇順に並べた数列を C=(C_1,C_2,\dots,C_{|A|+|B|}) とする。
- A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace となるような k_1,k_2,\dots,k_{|A|} をとる。 このとき、\displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i とする。
例えば、A=\lbrace 1,3\rbrace,B=\lbrace 2,8\rbrace のとき、C=(1,2,3,8) より A=\lbrace C_1,C_3\rbrace なので、f(A,B)=1+3=4 です。
それぞれが M 個の要素からなる N 個の整数の集合 S_1,S_2\dots,S_N があり、各 i\ (1 \leq i \leq N) について S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace です。 ただし、S_i \cap S_j = \emptyset\ (i \neq j) が保証されます。
\displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j) を求めてください。
制約
- 1\leq N \leq 10^4
- 1\leq M \leq 10^2
- 1\leq A_{i,j} \leq 10^9
- i_1 \neq i_2 または j_1 \neq j_2 ならば A_{i_1,j_1} \neq A_{i_2,j_2}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A_{1,1} A_{1,2} \dots A_{1,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}
出力
答えを整数として出力せよ。
入力例 1
3 2 1 3 2 8 4 6
出力例 1
12
S_1,S_2 はそれぞれ問題文中で例示した A,B と一致し、f(S_1,S_2)=1+3=4 です。 f(S_1,S_3)=1+2=3,f(S_2,S_3)=1+4=5 であるため、4+3+5=12 が答えです。
入力例 2
1 1 306
出力例 2
0
入力例 3
4 4 155374934 164163676 576823355 954291757 797829355 404011431 353195922 138996221 191890310 782177068 818008580 384836991 160449218 545531545 840594328 501899080
出力例 3
102
Score : 525 points
Problem Statement
For two sets of integers, A and B, such that A \cap B = \emptyset, we define f(A,B) as follows.
- Let C=(C_1,C_2,\dots,C_{|A|+|B|}) be a sequence consisting of the elements of A \cup B, sorted in ascending order.
- Take k_1,k_2,\dots,k_{|A|} such that A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace. Then, let \displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i.
For example, if A=\lbrace 1,3\rbrace and B=\lbrace 2,8\rbrace, then C=(1,2,3,8), so A=\lbrace C_1,C_3\rbrace; thus, f(A,B)=1+3=4.
We have N sets of integers, S_1,S_2\dots,S_N, each of which has M elements. For each i\ (1 \leq i \leq N), S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace. Here, it is guaranteed that S_i \cap S_j = \emptyset\ (i \neq j).
Find \displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j).
Constraints
- 1\leq N \leq 10^4
- 1\leq M \leq 10^2
- 1\leq A_{i,j} \leq 10^9
- If i_1 \neq i_2 or j_1 \neq j_2, then A_{i_1,j_1} \neq A_{i_2,j_2}.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_{1,1} A_{1,2} \dots A_{1,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}
Output
Print the answer as an integer.
Sample Input 1
3 2 1 3 2 8 4 6
Sample Output 1
12
S_1 and S_2 respectively coincide with A and B exemplified in the problem statement, and f(S_1,S_2)=1+3=4. Since f(S_1,S_3)=1+2=3 and f(S_2,S_3)=1+4=5, the answer is 4+3+5=12.
Sample Input 2
1 1 306
Sample Output 2
0
Sample Input 3
4 4 155374934 164163676 576823355 954291757 797829355 404011431 353195922 138996221 191890310 782177068 818008580 384836991 160449218 545531545 840594328 501899080
Sample Output 3
102