A - Maximal Value

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A_i < A_{i+1} > A_{i+2} を満たす 1 以上 N-2 以下の整数 i の個数を求めてください。

制約

  • 3\le N\le 100
  • 1\le A_i\le 100
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

6
3 1 4 1 5 2

出力例 1

2

i=2,42 つが条件を満たします。したがって、2 を出力してください。


入力例 2

5
1 1 1 2 1

出力例 2

1

入力例 3

10
7 3 9 8 10 3 1 5 5 4

出力例 3

2

Score : 100 points

Problem Statement

You are given an integer sequence of length N: A=(A_1,A_2,\ldots,A_N).

Find the number of integers i between 1 and N-2 (inclusive) satisfying A_i < A_{i+1} > A_{i+2}.

Constraints

  • 3\le N\le 100
  • 1\le A_i\le 100
  • 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

Output the answer.


Sample Input 1

6
3 1 4 1 5 2

Sample Output 1

2

Two values i=2,4 satisfy the condition. Thus, output 2.


Sample Input 2

5
1 1 1 2 1

Sample Output 2

1

Sample Input 3

10
7 3 9 8 10 3 1 5 5 4

Sample Output 3

2
B - Corridor Watch

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数 M,D と、 G, . からなる長さ M の文字列 S が与えられます。

M 個のマスが左右に一列に並んでおり、左から順にそれぞれ 1 から M までの番号がついています。

いくつかのマスにはガードマンが立っています。具体的には、S_i= G ならばマス i にはガードマンが立っており、S_i= . ならばマス i には誰も立っていません。

ガードマンが立っているマスからの距離が D 以下であるマスはガードマンによって監視されます。すなわち、あるマス i が存在して S_i= G かつ |x-i|\le D を満たすマス x はガードマンによって監視されます。

M 個のマスのうち、監視されていないマスの個数を求めてください。

制約

  • 0\le D < M \le 100
  • D,M は整数
  • S_iG. からなる長さ M の文字列

入力

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

M D
S

出力

答えを出力せよ。


入力例 1

7 1
.G...GG

出力例 1

1

マス 4 のみが監視されていません。


入力例 2

6 5
......

出力例 2

6

全てのマスが監視されていません。


入力例 3

21 2
....G...GG.....G.....

出力例 3

6

Score : 200 points

Problem Statement

You are given integers M,D and a string S of length M consisting of G and ..

There are M cells arranged in a row from left to right, numbered 1 through M from the left.

Some of the cells have a guardman standing on them. Specifically, a guardman stands on cell i if S_i= G, and no one stands on cell i if S_i= ..

A cell whose distance from a cell with a guardman is at most D is watched by that guardman. That is, a cell x is watched by a guardman if there exists a cell i such that S_i= G and |x-i|\le D.

Among the M cells, find the number of cells that are not watched.

Constraints

  • 0\le D < M \le 100
  • D and M are integers.
  • S_i is a string of length M consisting of G and ..

Input

The input is given from Standard Input in the following format:

M D
S

Output

Output the answer.


Sample Input 1

7 1
.G...GG

Sample Output 1

1

Only cell 4 is not watched.


Sample Input 2

6 5
......

Sample Output 2

6

All cells are not watched.


Sample Input 3

21 2
....G...GG.....G.....

Sample Output 3

6
C - Between P and Q

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

整数 N(1,2,\ldots,N) を並び替えた整数列 P=(P_1,P_2,\ldots, P_N),Q=(Q_1,Q_2,\ldots,Q_N) が与えられます。

(1,2,\ldots,N) を並び替えた整数列であって辞書順で P より大きく Q より小さいものがいくつあるか求めてください。

整数列の辞書順とは?

整数列 S = (S_1,S_2,\ldots,S_{|S|}) が整数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。

  1. |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_iT_i より(数として)小さい。

制約

  • 1\le N\le 10
  • P,Q(1,2,\ldots,N) を並び替えた整数列
  • 入力される値は全て整数

入力

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

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

出力

答えを出力せよ。


入力例 1

3
1 3 2
3 1 2

出力例 1

2

(2,1,3),(2,3,1)2 つが条件を満たします。したがって、2 を出力してください。


入力例 2

5
5 4 2 1 3
5 1 2 3 4

出力例 2

0

条件を満たす (1,2,3,4,5) を並び替えた整数列は存在しません。


入力例 3

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

出力例 3

223

Score : 300 points

Problem Statement

You are given an integer N and integer sequences P=(P_1,P_2,\ldots, P_N) and Q=(Q_1,Q_2,\ldots,Q_N), each of which is a permutation of (1,2,\ldots,N).

Find the number of integer sequences that are a permutation of (1,2,\ldots,N) and are lexicographically greater than P and lexicographically less than Q.

What is lexicographic order for integer sequences?

For integer sequences S = (S_1,S_2,\ldots,S_{|S|}) and T = (T_1,T_2,\ldots,T_{|T|}), we say that S is lexicographically smaller than T if 1. or 2. below holds. Here, |S|, |T| denote the lengths of S, T, respectively.

  1. |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
  2. There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following two conditions hold.
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_i is (numerically) smaller than T_i.

Constraints

  • 1\le N\le 10
  • P and Q are integer sequences that are permutations of (1,2,\ldots,N).
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

Output

Output the answer.


Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

2

Two sequences (2,1,3),(2,3,1) satisfy the condition. Thus, output 2.


Sample Input 2

5
5 4 2 1 3
5 1 2 3 4

Sample Output 2

0

There is no permutation of (1,2,3,4,5) satisfying the condition.


Sample Input 3

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

Sample Output 3

223
D - Pre-Palindrome

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

以下の条件を満たす英小文字からなる文字列を良い文字列とします。

  • 1 文字以下を書き換えることで回文にすることができる。

例えば aiwaiabcdcza などは良い文字列ですが、abcdatcoder などは良い文字列ではありません。特に、回文も良い文字列であることに注意してください。

英小文字からなる文字列 S が与えられます。

S の空でない部分文字列(連続な部分列)であって良い文字列であるものの個数を求めてください。

2 つの部分文字列は、S から取り出す場所が異なれば文字列として等しくても区別して数えることに注意してください。

部分文字列とは

S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • S は英小文字からなる長さ 1 以上 10^4 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

ababa

出力例 1

13

S2 文字目から 3 文字目までの ba1 文字目から 5 文字目までの ababa などが良い文字列です。

良い文字列は部分文字列 15 通りのうち 1 文字目から 4 文字目までの abab2 文字目から 5 文字目までの baba を除く 13 通りです。


入力例 2

atcoder

出力例 2

18

入力例 3

abccbacbacb

出力例 3

40

Score : 400 points

Problem Statement

A string consisting of lowercase English letters is called a good string if it satisfies the following condition.

  • It can be turned into a palindrome by rewriting at most one character.

For example, a, iwai, and abcdcza are good strings, but abcd and atcoder are not good strings. Note that, in particular, a palindrome is also a good string.

You are given a string S consisting of lowercase English letters.

Find the number of non-empty substrings (contiguous subsequences) of S that are good strings.

Two substrings taken from different positions of S are counted separately even if they are equal as strings.

What is a substring?

A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • S is a string of length between 1 and 10^4, inclusive, consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

Output the answer.


Sample Input 1

ababa

Sample Output 1

13

The substring ba from the 2nd through 3rd characters of S and the substring ababa from the 1st through 5th characters are some good strings.

Among the 15 substrings, the good strings are the 13 substrings other than abab from the 1st through 4th characters and baba from the 2nd through 5th characters.


Sample Input 2

atcoder

Sample Output 2

18

Sample Input 3

abccbacbacb

Sample Output 3

40
E - Sum of Average

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

正整数 N と長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

f(l,r)A_l,A_{l+1},\ldots,A_r の(算術)平均として定義します。

\displaystyle \sum_{1\le l\le r\le N} f(l,r)\text{mod }{998244353} で求めてください。

有理数 \text{mod }{998244353} の定義

この問題の制約のもとでは、求める有理数を既約分数 \frac{P}{Q} で表した時、Q {{}\not\equiv{}} 0 \pmod{998244353} となることが証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 1\le N\le 5\times 10^5
  • 0\le A_i < 998244353
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

2
2 3

出力例 1

499122184

\displaystyle f(1,1)=2,f(1,2)=\frac{2+3}2=\frac52,f(2,2)=3 です。したがって、\displaystyle \sum_{1\le l\le r\le N} f(l,r)=2+\frac52+3=\frac{15}2 です。

\displaystyle \frac{15}2\text{mod }{998244353}499122184 なので、499122184 を出力してください。


入力例 2

6
1 2 3 4 5 6

出力例 2

499122250

入力例 3

9
3 1 4 1 5 9 2 6 5

出力例 3

855638200

Score : 450 points

Problem Statement

You are given a positive integer N and a length-N integer sequence A=(A_1,A_2,\ldots,A_N).

Define f(l,r) as the (arithmetic) mean of A_l,A_{l+1},\ldots,A_r.

Find \displaystyle \sum_{1\le l\le r\le N} f(l,r), modulo 998244353.

Definition of a rational number modulo 998244353

Under the constraints of this problem, it can be proved that the rational number to be found is always an irreducible fraction \frac{P}{Q} such that Q {{}\not\equiv{}} 0 \pmod{998244353}. Thus, there is a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Output this R.

Constraints

  • 1\le N\le 5\times 10^5
  • 0\le A_i < 998244353
  • 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

Output the answer.


Sample Input 1

2
2 3

Sample Output 1

499122184

We have \displaystyle f(1,1)=2,f(1,2)=\frac{2+3}2=\frac52,f(2,2)=3. Thus, \displaystyle \sum_{1\le l\le r\le N} f(l,r)=2+\frac52+3=\frac{15}2.

\displaystyle \frac{15}2 in modulo-998244353 expression is 499122184, so output 499122184.


Sample Input 2

6
1 2 3 4 5 6

Sample Output 2

499122250

Sample Input 3

9
3 1 4 1 5 9 2 6 5

Sample Output 3

855638200
F - Chmax

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

正整数 N(1,2,\ldots,N) の並び替え P=(P_1,P_2,\ldots,P_N) が与えられます。

変数 x,y,c があります。はじめ x=y=c=0 です。

あなたは k=1,2,\ldots,N の順に以下の操作のいずれかを行います:

  • 操作 1x < P_k ならば c1 増やす。その後、x\max(x,P_k) に置き換える。
  • 操作 2y < P_k ならば c1 増やす。その後、y\max(y,P_k) に置き換える。

最終的な c の値の最大値を求めてください。

制約

  • 1\le N\le 5\times 10^5
  • P(1,2,\ldots,N) の並び替え
  • 入力される値は全て整数

入力

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

N
P_1 P_2 \ldots P_N

出力

答えを出力せよ。


入力例 1

5
4 3 1 2 5

出力例 1

4

以下のように操作することで c=4 を達成することができます:

  • k=1 のとき:操作 1 を行う。(x,y,c)=(4,0,1) となる。
  • k=2 のとき:操作 1 を行う。(x,y,c)=(4,0,1) となる。
  • k=3 のとき:操作 2 を行う。(x,y,c)=(4,1,2) となる。
  • k=4 のとき:操作 2 を行う。(x,y,c)=(4,2,3) となる。
  • k=5 のとき:操作 2 を行う。(x,y,c)=(4,5,4) となる。

どのように操作しても c4 より大きくすることはできないので、4 を出力してください。


入力例 2

6
6 5 4 3 2 1

出力例 2

2

入力例 3

9
3 6 5 2 7 8 9 1 4

出力例 3

7

Score : 500 points

Problem Statement

You are given a positive integer N and a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N).

There are variables x,y,c. Initially, x=y=c=0.

For k=1,2,\ldots,N in this order, you perform one of the following operations:

  • Operation 1: Increase c by 1 if x < P_k. Then, replace x with \max(x,P_k).
  • Operation 2: Increase c by 1 if y < P_k. Then, replace y with \max(y,P_k).

Find the maximum possible final value of c.

Constraints

  • 1\le N\le 5\times 10^5
  • P is a permutation of (1,2,\ldots,N).
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Output the answer.


Sample Input 1

5
4 3 1 2 5

Sample Output 1

4

By performing the operations as follows, you can achieve c=4:

  • For k=1: Perform operation 1. Then, (x,y,c)=(4,0,1).
  • For k=2: Perform operation 1. Then, (x,y,c)=(4,0,1).
  • For k=3: Perform operation 2. Then, (x,y,c)=(4,1,2).
  • For k=4: Perform operation 2. Then, (x,y,c)=(4,2,3).
  • For k=5: Perform operation 2. Then, (x,y,c)=(4,5,4).

c cannot be made greater than 4 no matter how you perform the operations, so output 4.


Sample Input 2

6
6 5 4 3 2 1

Sample Output 2

2

Sample Input 3

9
3 6 5 2 7 8 9 1 4

Sample Output 3

7
G - Restricted Permutation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

整数 Nox からなる長さ N の文字列 S が与えられます。

以下の条件を満たす (1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) の個数を 998244353 で割ったあまりを求めてください。

  • k=1,2,\ldots,N に対し、以下の 2 つは同値となる。
    • S_k= o
    • P(1,2,\ldots,k) の順列を連続部分列として含む

制約

  • 1\le N\le 2000
  • S_iox からなる長さ N の文字列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

3
oxo

出力例 1

2

P=(1,3,2),(2,3,1) が条件を満たします。


入力例 2

7
xxxxxxx

出力例 2

0

条件を満たす P は存在しません。


入力例 3

15
oxxxoxxxxxooxxo

出力例 3

1627648

Score : 550 points

Problem Statement

You are given an integer N and a string S of length N consisting of o and x.

Find the number, modulo 998244353, of permutations P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) satisfying the following condition.

  • For k=1,2,\ldots,N, the following two are equivalent.
    • S_k= o
    • P contains a permutation of (1,2,\ldots,k) as a contiguous subsequence.

Constraints

  • 1\le N\le 2000
  • S_i is a string of length N consisting of o and x.

Input

The input is given from Standard Input in the following format:

N
S

Output

Output the answer.


Sample Input 1

3
oxo

Sample Output 1

2

P=(1,3,2),(2,3,1) satisfy the condition.


Sample Input 2

7
xxxxxxx

Sample Output 2

0

There is no P satisfying the condition.


Sample Input 3

15
oxxxoxxxxxooxxo

Sample Output 3

1627648