A - Dividing a String

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

英小文字からなる文字列 S が与えられます。以下の条件をみたす最大の正整数 K を求めてください。

  • S の空でない K 個の文字列への分割 S=S_1S_2...S_K であって S_i \neq S_{i+1} (1 ≦ i ≦ K-1) を満たすものが存在する。

ただし、S_1,S_2,...,S_K をこの順に連結して得られる文字列のことを S_1S_2...S_K によって表しています。

制約

  • 1 ≦ |S| ≦ 2 \times 10^5
  • S は英小文字からなる

入力

入力は以下の形式で標準入力から与えられる。

S

出力

条件をみたす最大の正整数 K を出力せよ。


入力例 1

aabbaa

出力例 1

4

例えば aa,b,ba,aS4 つの文字列に分割することができます。


入力例 2

aaaccacabaababc

出力例 2

12

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:

  • There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1).

Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.

Constraints

  • 1 \leq |S| \leq 2 \times 10^5
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum positive integer K that satisfies the condition.


Sample Input 1

aabbaa

Sample Output 1

4

We can, for example, divide S into four strings aa, b, ba, and a.


Sample Input 2

aaaccacabaababc

Sample Output 2

12
B - RGB Balls

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

色のついたボールが 3N 個あり、それぞれには 1 から 3N の番号がついています。 各ボールの色は長さ 3N の文字列 S によって表されており、ボール i の色は S_iR のとき赤色、G のとき緑色、B のとき青色です。 赤色のボール、緑色のボール、青色のボールはそれぞれ N 個ずつあります。

高橋君はこの 3N 個のボールを、各人が赤、青、緑のボールを 1 つずつ割り当てられるよう、N 人の人に分配することにしました。 ただし、ボールをもらう人たちはできるだけ近い番号のボールが欲しいので、高橋君はさらに以下の条件をみたすように分配することにしました。

  • j 番目の人が受け取ったボールの番号を小さい順に a_j < b_j < c_j とする。
  • このとき \sum_j (c_j-a_j) ができるだけ小さくなるように分配する。

高橋君がボールを分配する方法は何通りあるか求めてください。 答えは非常に大きくなることがあるので、998244353 で割ったあまりを求めてください。 ただし、2 つのボールの分配方法が異なるとは、ある人が存在して、その人が受け取ったボールの集合が異なることを指します。

制約

  • 1 ≦ N ≦ 10^5
  • |S|=3N
  • SR, G, B のみからなり、それぞれ N 回ずつ S に登場する

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

高橋君のボールの分配方法が何通りあるかを 998244353 で割ったあまりを出力せよ。


入力例 1

3
RRRGGGBBB

出力例 1

216

例えば以下のようにボールを分配したとき、\sum_j (c_j-a_j) の値が 18 となり最小となります。

  • 1 番目の人にボール 1,5,9 を渡す。
  • 2 番目の人にボール 2,4,8 を渡す。
  • 3 番目の人にボール 3,6,7 を渡す。

入力例 2

5
BBRGRRGRGGRBBGB

出力例 2

960

Score : 800 points

Problem Statement

We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is R, green if S_i is G, and blue if S_i is B. There are N red balls, N green balls, and N blue balls.

Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition:

  • Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order.
  • Then, \sum_j (c_j-a_j) should be as small as possible.

Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls.

Constraints

  • 1 \leq N \leq 10^5
  • |S|=3N
  • S consists of R, G, and B, and each of these characters occurs N times in S.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways in which Takahashi can distribute the balls, modulo 998244353.


Sample Input 1

3
RRRGGGBBB

Sample Output 1

216

The minimum value of \sum_j (c_j-a_j) is 18 when the balls are, for example, distributed as follows:

  • The first person gets Ball 1, 5, and 9.
  • The second person gets Ball 2, 4, and 8.
  • The third person gets Ball 3, 6, and 7.

Sample Input 2

5
BBRGRRGRGGRBBGB

Sample Output 2

960
C - Numbers on a Circle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

円環状に N 個の正整数が並んでおり、それらには円環に沿った順に 1 から N の番号がついています。

i 番目の数は A_i です。高橋君は i 番目の正整数が B_i となるようにしたいです。 そこで、高橋君は以下の操作を繰り返し行うことにしました。

  • 1 \leqq i \leqq N なる整数 i を一つ選ぶ。
  • i-1,i,i+1 番目の数をそれぞれ a,b,c としたとき、i 番目の数を a+b+c に置き換える。

ただし、0 番目の数は N 番目の数を指し、N+1 番目の数は 1 番目の数を指すことに注意してください。

高橋君が条件をみたすように操作を行うことができるかどうか判定してください。 また可能である場合は、高橋君が行う必要のある操作回数として考えられる最小の値を求めてください。

制約

  • 3 ≦ N ≦ 2 \times 10^5
  • 1 ≦ A_i, B_i ≦ 10^9
  • 入力中のすべての値は整数である

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

出力

高橋君が行う必要のある操作回数として考えられる最小の値を出力せよ。 ただし、高橋君が条件をみたすように操作を行うことができない場合は -1 を出力せよ。


入力例 1

3
1 1 1
13 5 7

出力例 1

4

例えば高橋君は以下のように操作を行うことができます。

  • 2 番目の数を 3 に置き換える。
  • 2 番目の数を 5 に置き換える。
  • 3 番目の数を 7 に置き換える。
  • 1 番目の数を 13 に置き換える。

入力例 2

4
1 2 3 4
2 3 4 5

出力例 2

-1

入力例 3

5
5 6 5 2 1
9817 1108 6890 4343 8704

出力例 3

25

Score : 800 points

Problem Statement

There are N positive integers arranged in a circle.

Now, the i-th number is A_i. Takahashi wants the i-th number to be B_i. For this objective, he will repeatedly perform the following operation:

  • Choose an integer i such that 1 \leq i \leq N.
  • Let a, b, c be the (i-1)-th, i-th, and (i+1)-th numbers, respectively. Replace the i-th number with a+b+c.

Here the 0-th number is the N-th number, and the (N+1)-th number is the 1-st number.

Determine if Takahashi can achieve his objective. If the answer is yes, find the minimum number of operations required.

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

Print the minimum number of operations required, or -1 if the objective cannot be achieved.


Sample Input 1

3
1 1 1
13 5 7

Sample Output 1

4

Takahashi can achieve his objective by, for example, performing the following operations:

  • Replace the second number with 3.
  • Replace the second number with 5.
  • Replace the third number with 7.
  • Replace the first number with 13.

Sample Input 2

4
1 2 3 4
2 3 4 5

Sample Output 2

-1

Sample Input 3

5
5 6 5 2 1
9817 1108 6890 4343 8704

Sample Output 3

25
D - Sorting a Grid

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

NM 列のマス目があります。 このマス目には 1 から NM までの整数がそれぞれ 1 つずつ書かれています。 上から i 行目、左から j 列目にあるマスに書かれている数は A_{ij} です。

あなたはこのマス目を以下の手順に従って並べ替える必要があります。

  1. まず N 個の行それぞれに対して、その行に書かれている数を好きに並べ替える。
  2. 次に M 個の列それぞれに対して、その列に書かれている数を好きに並べ替える。
  3. 最後に N 個の行それぞれに対して、その行に書かれている数を好きに並べ替える。

最終的に上から i 行目、左から j 行目にあるマスに書かれている数が M\times (i-1)+j となるようにしたいです。 そのような並べ替え方を一つ構成してください。与えられた制約の下で、常に条件をみたすように並べ替えられることができることは保証されています。

制約

  • 1 ≦ N,M ≦ 100
  • 1 ≦ A_{ij} ≦ NM
  • A_{ij} は相異なる

入力

入力は以下の形式で標準入力から与えられる。

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

出力

以下の形式で並べ替え方を出力せよ。

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

ただし、B_{ij} は手順 1 を行った後に上から i 行目、左から j 行目にあるマスに書かれている数であり、 C_{ij} は手順 2 を行った後に上から i 行目、左から j 行目にあるマスに書かれている数である。


入力例 1

3 2
2 6
4 3
1 5

出力例 1

2 6 
4 3 
5 1 
2 1 
4 3 
5 6 

入力例 2

3 4
1 4 7 10
2 5 8 11
3 6 9 12

出力例 2

1 4 7 10 
5 8 11 2 
9 12 3 6 
1 4 3 2 
5 8 7 6 
9 12 11 10 

Score : 1100 points

Problem Statement

We have a grid with N rows and M columns of squares. Each integer from 1 to NM is written in this grid once. The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}.

You need to rearrange these numbers as follows:

  1. First, for each of the N rows, rearrange the numbers written in it as you like.
  2. Second, for each of the M columns, rearrange the numbers written in it as you like.
  3. Finally, for each of the N rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the i-th row from the top and the j-th column from the left to be M\times (i-1)+j. Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective.

Constraints

  • 1 \leq N,M \leq 100
  • 1 \leq A_{ij} \leq NM
  • A_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print one way to rearrange the numbers in the following format:

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

Here B_{ij} is the number written in the square at the i-th row from the top and the j-th column from the left after Step 1, and C_{ij} is the number written in that square after Step 2.


Sample Input 1

3 2
2 6
4 3
1 5

Sample Output 1

2 6 
4 3 
5 1 
2 1 
4 3 
5 6 

Sample Input 2

3 4
1 4 7 10
2 5 8 11
3 6 9 12

Sample Output 2

1 4 7 10 
5 8 11 2 
9 12 3 6 
1 4 3 2 
5 8 7 6 
9 12 11 10 
E - Reversing and Concatenating

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1300

問題文

高橋君は英小文字からなる長さ N の文字列 S を持っています。 高橋君は S に対して以下の操作を K 回行うことにしました。

  • S を反転した文字列を T として、ST をこの順に連結して得られる文字列を U とする。
  • ある U の連続する長さ N の部分文字列を S' として、SS' で置き換える。

最終的な S として考えられる文字列の内、辞書順で最小のものを求めてください。

制約

  • 1 ≦ N ≦ 5000
  • 1 ≦ K ≦ 10^9
  • |S|=N
  • S は英小文字からなる

入力

入力は以下の形式で標準入力から与えられる。

N K
S

出力

最終的な S として考えられる文字列の内、辞書順で最小のものを出力せよ。


入力例 1

5 1
bacba

出力例 1

aabca

S=bacbaのとき、T=abcab, U=bacbaabcabであるので S'=aabcaとするのが最適です。


入力例 2

10 2
bbaabbbaab

出力例 2

aaaabbaabb

Score : 1300 points

Problem Statement

Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times:

  • Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order.
  • Let S' be some contiguous substring of U with length N, and replace S with S'.

Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq K \leq 10^9
  • |S|=N
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the lexicographically smallest possible string that can be the string S after the K operations.


Sample Input 1

5 1
bacba

Sample Output 1

aabca

When S=bacba, T=abcab, U=bacbaabcab, and the optimal choice of S' is S'=aabca.


Sample Input 2

10 2
bbaabbbaab

Sample Output 2

aaaabbaabb
F - Counting of Subarrays

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1800

問題文

正整数列 S 及び正整数 k,l が以下のいずれかの条件をみたすとき、 S レベル (k,l) に属すると定義することにします。

  • S の要素数が 1 であり、その要素の値が k である。
  • あるレベル(k-1,l) に属する数列 T_1,T_2,...,T_m (m ≧ l) が存在して、 T_1,T_2,...,T_m をこの順に連結して得られる数列と S が一致する。

ただし、k=1 のとき二番目の条件は意味を持たない、つまりレベル(1,l)の正整数列は一つ目の条件をみたすもののみであることに注意して下さい。

正整数列 A_1,A_2,...,A_N と正整数 L が与えられます。 以下の条件をみたす部分列 A_i,A_{i+1},...,A_j (1 ≦ i ≦ j ≦ N) の個数を求めてください。

  • ある正整数 K が存在して、数列 A_i,A_{i+1},...,A_j がレベル(K,L)に属する。

制約

  • 1 ≦ N ≦ 2 \times 10^5
  • 2 ≦ L ≦ N
  • 1 ≦ A_i ≦ 10^9

入力

入力は以下の形式で標準入力から与えられる。

N L
A_1 A_2 ... A_N

出力

条件をみたす部分列の個数を出力せよ。


入力例 1

9 3
2 1 1 1 1 1 1 2 3

出力例 1

22

例えば (1,1,1)(2) という数列はともにレベル (2,3) に属するので、(2,1,1,1,1,1,1) という数列はレベル (3,3) に属します。


入力例 2

9 2
2 1 1 1 1 1 1 2 3

出力例 2

41

入力例 3

15 3
4 3 2 1 1 1 2 3 2 2 1 1 1 2 2

出力例 3

31

Score : 1800 points

Problem Statement

For a sequence S of positive integers and positive integers k and l, S is said to belong to level (k,l) when one of the following conditions is satisfied:

  • The length of S is 1, and its only element is k.
  • There exist sequences T_1,T_2,...,T_m (m \geq l) belonging to level (k-1,l) such that the concatenation of T_1,T_2,...,T_m in this order coincides with S.

Note that the second condition has no effect when k=1, that is, a sequence belongs to level (1,l) only if the first condition is satisfied.

Given are a sequence of positive integers A_1,A_2,...,A_N and a positive integer L. Find the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the following condition:

  • There exists a positive integer K such that the sequence A_i,A_{i+1},...,A_j belongs to level (K,L).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq L \leq N
  • 1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N L
A_1 A_2 ... A_N

Output

Print the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the condition.


Sample Input 1

9 3
2 1 1 1 1 1 1 2 3

Sample Output 1

22

For example, both of the sequences (1,1,1) and (2) belong to level (2,3), so the sequence (2,1,1,1,1,1,1) belong to level (3,3).


Sample Input 2

9 2
2 1 1 1 1 1 1 2 3

Sample Output 2

41

Sample Input 3

15 3
4 3 2 1 1 1 2 3 2 2 1 1 1 2 2

Sample Output 3

31