A - ADjacent

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

縦 2 行、横 2 列のグリッドがあります。

グリッドの各マスには A, B, C, D のいずれかがちょうど 1 つ書かれており、4 マスに書かれている文字は相異なります。 上から i 行目、左から j 列目のマスには S_{i,j} が書かれています。

A が書かれたマスと D が書かれたマスが隣接している、すなわち辺を共有しているならば Yes を、そうでないならば No を出力してください。

制約

  • S_{i,j} は A, B, C, D のいずれか
  • (i_1,j_1)\neq (i_2,j_2) ならば S_{i_1,j_1} と S_{i_2,j_2} は相異なる

入力

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

S_{1,1}S_{1,2}  
S_{2,1}S_{2,2}   

出力

答えを 1 行で出力せよ。


入力例 1

AB
DC

出力例 1

Yes

A は上から 1 行目、左から 1 列目のマスに書かれています。
D は上から 2 行目、左から 1 列目のマスに書かれています。
この 2 マスは辺を共有しているため、隣接しています。


入力例 2

AB
CD

出力例 2

No

入力例 3

BD
CA

出力例 3

Yes

Score : 100 points

Problem Statement

There is a grid with 2 rows and 2 columns.

Each cell of the grid contains exactly one of A, B, C, and D, and the characters written in the 4 cells are distinct. The cell at the i-th row from the top and the j-th column from the left contains S_{i,j}.

If the cell containing A and the cell containing D are adjacent, that is, they share an edge, print Yes; otherwise, print No.

Constraints

  • S_{i,j} is one of A, B, C, and D.
  • If (i_1,j_1)\neq (i_2,j_2), then S_{i_1,j_1} and S_{i_2,j_2} are distinct.

Input

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

S_{1,1}S_{1,2}  
S_{2,1}S_{2,2}   

Output

Print the answer in a single line.


Sample Input 1

AB
DC

Sample Output 1

Yes

A is written in the cell at the 1-st row from the top and the 1-st column from the left.
D is written in the cell at the 2-nd row from the top and the 1-st column from the left.
These 2 cells share an edge, so they are adjacent.


Sample Input 2

AB
CD

Sample Output 2

No

Sample Input 3

BD
CA

Sample Output 3

Yes
B - Invalid Vote

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

選挙に N 人の候補者 1,2,\dots,N が立候補しています。 各候補者の名前は英小文字からなり、互いに異なります。 候補者 i の名前は S_i です。

M 枚の投票用紙 1,2,\dots,M が投じられ、投票用紙 j には英小文字からなる文字列 T_j が書かれていました。

T_j が名前に完全に一致している候補者がいる場合、その候補者に 1 票が入ります。 そうでなく、T_j を 1 文字書き換えて名前が一致するような候補者がちょうど 1 人いる場合、その候補者に 1 票が入ります。 そうでない場合、それは無効票となります。

i=1,2,\dots,N について、候補者 i に何票入るかを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq |S_i| \leq 10
  • i \neq j ならば S_i \neq S_j
  • 1 \leq |T_j| \leq 10
  • S_i,T_j は英小文字からなる文字列
  • N と M は整数

入力

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

N M  
S_1  
S_2  
\vdots  
S_N  
T_1  
T_2  
\vdots  
T_M  

出力

答えを合計 N 行で出力せよ。
i 行目には、候補者 i に何票入るかを出力せよ。


入力例 1

5 8
takahashi
aoki
sato
kato
kano
aoki
takanashi
sano
atcoder
chokudai
kato
aoki
yato

出力例 1

1
2
0
1
0

takahashi,aoki,sato,kato,kano の 5 人の候補者がいます。
投票用紙 1 には aoki と書かれています。
候補者 2 に 1 票が入ります。

投票用紙 2 には takanashi と書かれています。
5 文字目を n から h に書き換えることで候補者 1 の名前と一致し、またそのような候補者は他にいないため、候補者 1 に 1 票が入ります。

投票用紙 3 には sano と書かれています。
3 文字目を n から t に書き換えることで候補者 3 の名前と一致し、1 文字目を s から k に書き換えることで候補者 5 の名前と一致するため、これは無効票となります。


入力例 2

2 5
a
b
a
b
c
a
aa

出力例 2

2
1

Score : 200 points

Problem Statement

N candidates 1,2,\dots,N are running in an election. The name of each candidate consists of lowercase English letters, and the names are distinct. The name of candidate i is S_i.

M ballots 1,2,\dots,M were cast, and ballot j has a string T_j consisting of lowercase English letters written on it.

If there is a candidate whose name exactly matches T_j, that candidate receives 1 vote. Otherwise, if there is exactly one candidate whose name can be obtained by changing 1 character of T_j, that candidate receives 1 vote. Otherwise, the ballot is invalid.

For each i=1,2,\dots,N, find how many votes candidate i receives.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq |S_i| \leq 10
  • S_i \neq S_j if i \neq j.
  • 1 \leq |T_j| \leq 10
  • S_i and T_j are strings consisting of lowercase English letters.
  • N and M are integers.

Input

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

N M  
S_1  
S_2  
\vdots  
S_N  
T_1  
T_2  
\vdots  
T_M  

Output

Print N lines in total.
The i-th line should contain the number of votes candidate i receives.


Sample Input 1

5 8
takahashi
aoki
sato
kato
kano
aoki
takanashi
sano
atcoder
chokudai
kato
aoki
yato

Sample Output 1

1
2
0
1
0

There are 5 candidates: takahashi, aoki, sato, kato, and kano.
Ballot 1 has aoki written on it.
Candidate 2 receives 1 vote.

Ballot 2 has takanashi written on it.
Changing the 5-th character from n to h makes it match the name of candidate 1, and there is no other such candidate, so candidate 1 receives 1 vote.

Ballot 3 has sano written on it.
Changing the 3-rd character from n to t makes it match the name of candidate 3, and changing the 1-st character from s to k makes it match the name of candidate 5, so this ballot is invalid.


Sample Input 2

2 5
a
b
a
b
c
a
aa

Sample Output 2

2
1
C - Self-Stopping Shower

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は銭湯のシャワーを使っています。 このシャワーにはボタンがあります。 時刻 t にボタンを押すと時刻 t から時刻 t+K までの K 秒間お湯が出ます。 お湯が出ているときに再びボタンを押した場合も、最後にボタンを押した時刻から K 秒間お湯が出ます。

N 人の青木君がいて、ボタンを 1 回ずつ押してくれます。 i 人目の青木君は時刻 A_i にボタンを押します。

時刻 0 から時刻 T までの間に常にお湯が出ているようにするために高橋君は最小で何回ボタンを押す必要があるかを求めてください。 ただし、ボタンを押したタイミングと最後にボタンを押してからちょうど K 秒経ったタイミングはいずれもお湯が出ているものとします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq T \leq 10^9
  • 0 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N K T  
A_1 A_2 \ldots A_N  

出力

答えを 1 行で出力せよ。


入力例 1

3 4 20
6 7 12

出力例 1

4

高橋君は時刻 0,2,10,16 にボタンを押すと、時刻 0 から時刻 20 までの間に常にお湯が出ているようにできます。
ボタンを押す回数が 3 回以下のとき、常にお湯が出ているようにはできないため、答えは 4 です。


入力例 2

9 37 480
12 123 132 213 231 312 321 500 1000

出力例 2

11

Score : 300 points

Problem Statement

Takahashi is using a shower at a public bath. This shower has a button. If the button is pressed at time t, hot water comes out for K seconds, from time t to time t+K. Even if the button is pressed again while hot water is coming out, hot water comes out for K seconds from the time the button was last pressed.

There are N Aokis, each of whom presses the button once. The i-th Aoki presses the button at time A_i.

Find the minimum number of times Takahashi needs to press the button so that hot water is always coming out from time 0 to time T. Here, hot water is considered to be coming out both at the moment the button is pressed and at the moment exactly K seconds after the last press.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq T \leq 10^9
  • 0 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N K T  
A_1 A_2 \ldots A_N  

Output

Print the answer in a single line.


Sample Input 1

3 4 20
6 7 12

Sample Output 1

4

If Takahashi presses the button at times 0,2,10,16, hot water is always coming out from time 0 to time 20.
If he presses the button 3 times or fewer, it is impossible to keep hot water always coming out, so the answer is 4.


Sample Input 2

9 37 480
12 123 132 213 231 312 321 500 1000

Sample Output 2

11
D - Concatenation of Prefixes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

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

以下の手順で長さ \frac{N(N+1)}{2} の数列 B を作ります。

  • 最初、 B を空列とする。
  • i=1,2,\dots,N の順に、B の末尾に (A_1,\dots,A_i) を連結する。

以下の形式の質問が Q 個与えられるのでそれらに答えてください。

質問: L,R が与えられる。B_L+B_{L+1}+\dots+B_R を求めよ。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • -10^8 \leq A_i \leq 10^8
  • 各質問において、1\leq L\leq R \leq \frac{N(N+1)}{2}
  • 入力は全て整数

入力

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

N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ここで \mathrm{query}_i は i 番目の質問を表し、以下の形式で与えられる。

L R

出力

Q 行出力せよ。
i 行目には i 番目の質問に対する答えを出力せよ。


入力例 1

4 3
1 10 100 1000
3 6
1 10
8 8

出力例 1

121
1234
10

B=(1,1,10,1,10,100,1,10,100,1000) です。

  • 1 番目の質問に対する答えは 10+1+10+100=121 です。
  • 2 番目の質問に対する答えは 1+1+10+1+10+100+1+10+100+1000=1234 です。
  • 3 番目の質問に対する答えは 10 です。

入力例 2

7 1
100000000 100000000 100000000 100000000 100000000 100000000 100000000
1 28

出力例 2

2800000000

Score : 400 points

Problem Statement

You are given an integer sequence (A_1,\dots,A_N) of length N.

Construct a sequence B of length \frac{N(N+1)}{2} by the following procedure.

  • Initially, let B be an empty sequence.
  • For i=1,2,\dots,N in this order, append (A_1,\dots,A_i) to the end of B.

You are given Q queries of the following form; answer them.

Query: You are given L and R. Find B_L+B_{L+1}+\dots+B_R.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • -10^8 \leq A_i \leq 10^8
  • For each query, 1\leq L\leq R \leq \frac{N(N+1)}{2}.
  • All input values are integers.

Input

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

N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_i represents the i-th query and is given in the following format:

L R

Output

Print Q lines.
The i-th line should contain the answer to the i-th query.


Sample Input 1

4 3
1 10 100 1000
3 6
1 10
8 8

Sample Output 1

121
1234
10

B=(1,1,10,1,10,100,1,10,100,1000).

  • The answer to the 1-st query is 10+1+10+100=121.
  • The answer to the 2-nd query is 1+1+10+1+10+100+1+10+100+1000=1234.
  • The answer to the 3-rd query is 10.

Sample Input 2

7 1
100000000 100000000 100000000 100000000 100000000 100000000 100000000
1 28

Sample Output 2

2800000000
E - Product-Bounded Arithmetic Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

以下の条件を全て満たす正整数列 A=(A_1,\dots,A_{|A|}) を良い数列と定めます。

  • |A|\geq 3
  • A は等差数列である。即ち、全ての 1 \leq i \leq |A|-1 について A_{i+1}-A_{i}=A_2-A_1
  • A_2-A_1 > 0

良い数列のうち、要素の総積が N 以下であるものの個数を求めてください。

なお、制約の条件下で答えが 10^{18} 以下となることが証明されます。

制約

  • 1 \leq N \leq 10^{18}
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

120

出力例 1

18

総積が 24 以下の良い数列は (1,2,3),(1,2,3,4),(1,3,5),(2,3,4) の 4 個です。


入力例 2

1000000000000000000

出力例 2

1051148114007

Score : 450 points

Problem Statement

A sequence of positive integers A=(A_1,\dots,A_{|A|}) is called a good sequence if it satisfies all of the following conditions.

  • |A|\geq 3
  • A is an arithmetic progression; that is, A_{i+1}-A_{i}=A_2-A_1 for all 1 \leq i \leq |A|-1.
  • A_2-A_1 > 0

Find the number of good sequences such that the product of all elements is at most N.

It can be proved that the answer is at most 10^{18} under the constraints.

Constraints

  • 1 \leq N \leq 10^{18}
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

120

Sample Output 1

18

There are 4 good sequences whose product is at most 24: (1,2,3),(1,2,3,4),(1,3,5),(2,3,4).


Sample Input 2

1000000000000000000

Sample Output 2

1051148114007
F - Cake Cutting

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

N 個のケーキがあり、 i 個目のケーキの大きさは A_i です。これらのケーキを M 人に配ろうとしています。

ケーキの大きさの総和 A_1+\dots+A_N を S とします。 ここで、 S は M の倍数であることが保証されます。

あなたは、以下の操作を好きな順序で好きな回数( 0 回でもよい)行うことができます。

  • ケーキを 1 つ選び、それを自由に 2 つに切り分ける(大きさが整数である必要はない)。

操作後、ケーキを M 人に配ります。どの人がもらうケーキの大きさの合計も \frac{S}{M} となるようにするための、操作回数の最小値を求めてください。

より厳密な問題文は以下の通りです。

  • 列 A=(A_1,\dots,A_N) と正整数 M が与えられる。S=\sum_i A_i と定める。
  • 以下の操作を好きな回数行う。
    • 1\leq i \leq |A| を満たす整数 i と 0 < x < A_i を満たす実数 x を自由に選ぶ。A の末尾に x,A_i-x を追加し、 i 番目の要素を削除した長さ |A|+1 の列で A を置き換える。
  • 以下の条件を全て満たす \mathcal{P} が存在するための最小の操作回数を求めよ。
    • \mathcal{P} は \{1,2,\dots,|A|\} の分割である。即ち、以下の 3 条件を満たす集合である。
      • \mathcal{P} の各要素は \{1,2,\dots,|A|\} の部分集合である。
      • X,Y \in \mathcal{P} かつ X\neq Y ならば、X \cap Y = \emptyset である。
      • \bigcup_{X \in \mathcal{P}}X=\{1,2,\dots,|A|\} が成り立つ。
    • すべての X\in \mathcal{P} について、\sum_{i\in X}A_i=\frac{S}{M} が成り立つ。

制約

  • 1 \leq N \leq 20
  • 1 \leq M \leq 100
  • 1 \leq A_i \leq 10^9
  • A_1+\dots+A_N は M の倍数
  • 与えられる数値は全て整数

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

2 3
7 23

出力例 1

2

次のように 2 回の操作を行うのが最小です。

  • 大きさ 23 のケーキを大きさ 10,13 に切り分ける。ケーキの大きさはそれぞれ (7,10,13) になる。
  • 大きさ 13 のケーキを大きさ 10,3 に切り分ける。ケーキの大きさはそれぞれ (7,10,10,3) になる。
  • 「大きさ 7 のケーキと大きさ 3 のケーキ」「大きさ 10 のケーキ」「大きさ 10 のケーキ」をそれぞれの人に与える。

入力例 2

4 2
1234 123 12 1

出力例 2

1

次のように 1 回の操作を行うのが最小です。

  • 大きさ 1234 のケーキを大きさ 685,549 に切り分ける。ケーキの大きさはそれぞれ (123,12,1,685,549) になる。
  • 「大きさ 123,12,1,549 の 4 個のケーキ」「大きさ 685 のケーキ」をそれぞれの人に与える。

Score : 525 points

Problem Statement

There are N cakes, and the size of the i-th cake is A_i. You are going to distribute these cakes to M people.

Let S be the total size of the cakes, A_1+\dots+A_N. Here, it is guaranteed that S is a multiple of M.

You can perform the following operation any number of times (possibly zero) in any order.

  • Choose one cake and cut it into 2 pieces in any way you like (the sizes need not be integers).

After the operations, you distribute the cakes to the M people. Find the minimum number of operations required so that the total size of the cakes each person receives is \frac{S}{M}.

A more formal statement is as follows.

  • You are given a sequence A=(A_1,\dots,A_N) and a positive integer M. Let S=\sum_i A_i.
  • You can perform the following operation any number of times.
    • Choose an integer i with 1\leq i \leq |A| and a real number x with 0 < x < A_i. Replace A with the sequence of length |A|+1 obtained by appending x,A_i-x to the end of A and removing the i-th element.
  • Find the minimum number of operations required for there to exist a \mathcal{P} satisfying all of the following conditions.
    • \mathcal{P} is a partition of \{1,2,\dots,|A|\}; that is, it is a set satisfying the following 3 conditions.
      • Each element of \mathcal{P} is a subset of \{1,2,\dots,|A|\}.
      • If X,Y \in \mathcal{P} and X\neq Y, then X \cap Y = \emptyset.
      • \bigcup_{X \in \mathcal{P}}X=\{1,2,\dots,|A|\} holds.
    • \sum_{i\in X}A_i=\frac{S}{M} holds for every X\in \mathcal{P}.

Constraints

  • 1 \leq N \leq 20
  • 1 \leq M \leq 100
  • 1 \leq A_i \leq 10^9
  • A_1+\dots+A_N is a multiple of M.
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

2 3
7 23

Sample Output 1

2

The minimum is achieved by performing 2 operations as follows.

  • Cut the cake of size 23 into pieces of sizes 10 and 13. The cake sizes become (7,10,13).
  • Cut the cake of size 13 into pieces of sizes 10 and 3. The cake sizes become (7,10,10,3).
  • Give the cakes of sizes 7 and 3 to one person, a cake of size 10 to another person, and the remaining cake of size 10 to the last person.

Sample Input 2

4 2
1234 123 12 1

Sample Output 2

1

The minimum is achieved by performing 1 operation as follows.

  • Cut the cake of size 1234 into pieces of sizes 685 and 549. The cake sizes become (123,12,1,685,549).
  • Give the 4 cakes of sizes 123,12,1,549 to one person and the cake of size 685 to the other.
G - Zonal Score Minimization

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600 点

問題文

長さ 2 以上の正整数列のスコアをその数列の最大値と最小値の差で定義します。
長さ N の正整数列 A が与えられるので、A を長さ 2 以上であるような 1 つ以上の連続部分列に分割したときに各連続部分列のスコアの総和としてあり得る値の最小値を求めてください。
より形式的には、ある正整数 K に対して長さが 2 以上であるような K 個の正整数列 B_1,B_2,\dots,B_K をこの順に連結させると A と同じ数列になるときに \sum_{k=1}^{K}\left(\max(B_k)-\min(B_k)\right) としてあり得る値の最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N  
A_1 A_2 \ldots A_N

出力

答えを 1 行で出力せよ。


入力例 1

5
1 3 6 8 9

出力例 1

5

A を (1,3) と (6,8,9) に分割すると、スコアはそれぞれ 2,3 となり、総和は 5 です。
各連続部分列のスコアの総和を 4 以下にすることはできないため、答えは 5 です。


入力例 2

6
1 4 2 8 7 9

出力例 2

5

入力例 3

7
10 480 10 480 480 10 480

出力例 3

470

Score : 600 points

Problem Statement

The score of a sequence of positive integers of length at least 2 is defined as the difference between the maximum and minimum values of the sequence.
You are given a sequence A of N positive integers. Find the minimum possible total score of the contiguous subsequences when A is divided into one or more contiguous subsequences, each of length at least 2.
More formally, find the minimum possible value of \sum_{k=1}^{K}\left(\max(B_k)-\min(B_k)\right) over all positive integers K and sequences of positive integers B_1,B_2,\dots,B_K, each of length at least 2, such that concatenating B_1,B_2,\dots,B_K in this order yields A.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 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 answer in a single line.


Sample Input 1

5
1 3 6 8 9

Sample Output 1

5

If A is divided into (1,3) and (6,8,9), their scores are 2 and 3, respectively, for a total of 5.
It is impossible to make the total score of the contiguous subsequences 4 or less, so the answer is 5.


Sample Input 2

6
1 4 2 8 7 9

Sample Output 2

5

Sample Input 3

7
10 480 10 480 480 10 480

Sample Output 3

470