E - NewFolder(1)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。

N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。

  • S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
  • S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X(X>0) 存在するならば、X を文字列として扱って S_i+ ( +X+ ) を出力する。

制約

  • 1 \leq N \leq 2\times 10^5
  • S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

問題文中の指示にしたがって、N 行出力せよ。


入力例 1

5
newfile
newfile
newfolder
newfile
newfolder

出力例 1

newfile
newfile(1)
newfolder
newfile(2)
newfolder(1)

入力例 2

11
a
a
a
a
a
a
a
a
a
a
a

出力例 2

a
a(1)
a(2)
a(3)
a(4)
a(5)
a(6)
a(7)
a(8)
a(9)
a(10)

Score : 300 points

Problem Statement

For two strings A and B, let A+B denote the concatenation of A and B in this order.

You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:

  • if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
  • if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+ ( +X+ ), treating X as a string.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print N lines as specified in the Problem Statement.


Sample Input 1

5
newfile
newfile
newfolder
newfile
newfolder

Sample Output 1

newfile
newfile(1)
newfolder
newfile(2)
newfolder(1)

Sample Input 2

11
a
a
a
a
a
a
a
a
a
a
a

Sample Output 2

a
a(1)
a(2)
a(3)
a(4)
a(5)
a(6)
a(7)
a(8)
a(9)
a(10)
F - ABC conjecture

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正の整数 N が与えられます。

A\leq B\leq C かつ ABC\leq N であるような正の整数の組 (A,B,C) の個数を求めてください。

なお、制約の条件下で答えは 2^{63} 未満であることが保証されます。

制約

  • 1 \leq N \leq 10^{11}
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

5

条件を満たす組は (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2)5 つです。


入力例 2

100

出力例 2

323

入力例 3

100000000000

出力例 3

5745290566750

Score : 300 points

Problem Statement

You are given a positive integer N.

Find the number of triples of positive integers (A, B, C) such that A\leq B\leq C and ABC\leq N.

The Constraints guarantee that the answer is less than 2^{63}.

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

5

There are five such triples: (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2).


Sample Input 2

100

Sample Output 2

323

Sample Input 3

100000000000

Sample Output 3

5745290566750
G - Range Count Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

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

以下の形式で与えられる Q 個のクエリに答えてください。

  • 整数 L,R,X が与えられる。 A_L, \ldots,A_R のうち、値が X に等しいものの個数を求めよ。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq Q \leq 2\times 10^5
  • 各クエリについて、 1\le L \leq R \leq N, 1 \leq X \leq N
  • 入力は全て整数

入力

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

N 
A_1 A_2 \ldots A_N
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

ただし、\mathrm{Query}_ii 個目のクエリを表す。

各クエリは以下の形式で与えられる。

L R X

出力

Q 行出力せよ。i 行目には、i 個目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

2
0
0
1

1 個目のクエリでは、 (A_1,A_2,A_3,A_4,A_5) =(3,1,4,1,5) のうち値が 1 に等しいものの個数は 2 です。

2 個目のクエリでは、 (A_2,A_3,A_4) =(1,4,1) のうち値が 3 に等しいものの個数は 0 です。

Score : 400 points

Problem Statement

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

Answer Q queries given in the following format.

  • You are given integers L, R, and X. Find the number of elements among A_L, \ldots, A_R whose values are equal to X.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq Q \leq 2\times 10^5
  • 1\le L \leq R \leq N, 1 \leq X \leq N, for each query.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N 
A_1 A_2 \ldots A_N
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

Here, \mathrm{Query}_i represents the i-th query.

Each query is in the following format:

L R X

Output

Print Q lines, the i-th of which contains the answer to the i-th query.


Sample Input 1

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

Sample Output 1

2
0
0
1

In the first query, two of (A_1,A_2,A_3,A_4,A_5) =(3,1,4,1,5) have values equal to 1.

In the second query, zero of (A_2,A_3,A_4) =(1,4,1) have values equal to 3.

H - Chinese Restaurant (Three-Star Version)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

回転テーブルの周りに人 0、人 1\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。

操作を完了させた後において、人 i の不満度は料理 i が人 (i-k) \bmod N、人 (i+k) \bmod N のいずれかの目の前に置かれているような最小の非負整数 k です。
N 人の不満度の総和の最小値を求めてください。

a \bmod m とは 整数 a と正整数 m に対し、a \bmod ma-xm の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • i \neq j ならば p_i \neq p_j
  • 入力はすべて整数

入力

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

N
p_0 \ldots p_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 0 3

出力例 1

2

操作を 1 回行うと下の画像のようになります。

この時、不満度の総和が 2 になることを以下のように確かめられます。

  • 0 の不満度は、料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので 1 です。
  • 1 の不満度は、料理 1 が人 1\ (=(1+0) \bmod 4) の目の前に置かれているので 0 です。
  • 2 の不満度は、料理 2 が人 2\ (=(2+0) \bmod 4) の目の前に置かれているので 0 です。
  • 3 の不満度は、料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので 1 です。

不満度の総和を 2 より小さくすることは出来ないため、答えは 2 です。


入力例 2

3
0 1 2

出力例 2

0

入力例 3

10
3 9 6 1 7 2 8 0 5 4

出力例 3

20

Score : 500 points

Problem Statement

Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:

  • Rotate the turntable by one N-th of a counterclockwise turn. The dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.

When you are finished, Person i gains frustration of k, where k is the minimum integer such that Dish i is in front of either Person (i-k) \bmod N or Person (i+k) \bmod N.
Find the minimum possible sum of frustration of the N people.

What is a \bmod m? For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • p_i \neq p_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_0 \ldots p_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

2

The figure below shows the table after one operation.

Here, the sum of their frustration is 2 because:

  • Person 0 gains a frustration of 1 since Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
  • Person 1 gains a frustration of 0 since Dish 1 is in front of Person 1\ (=(1+0) \bmod 4);
  • Person 2 gains a frustration of 0 since Dish 2 is in front of Person 2\ (=(2+0) \bmod 4);
  • Person 3 gains a frustration of 1 since Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).

We cannot make the sum of their frustration less than 2, so the answer is 2.


Sample Input 2

3
0 1 2

Sample Output 2

0

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

20
I - Monochromatic Path

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

各マスが白または黒で塗られた HW 列のグリッドがあります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) について、 グリッドの上から i 行目、左から j 行目のマス(以下、単にマス (i, j) と呼ぶ)の色は A_{i, j} で表されます。 A_{i, j} = 0 のときマス (i, j) は白色、A_{i, j} = 1 のときマス (i, j) は黒色です。

「下記の 2 つの操作のどちらかを行うこと」を好きなだけ( 0 回でも良い)繰り返すことができます。

  • 1 \leq i \leq H を満たす整数を選び、R_i 円払った後、グリッドの上から i 行目にあるそれぞれのマスの色を反転させる(白色のマスは黒色に、黒色のマスは白色に塗り替える)。
  • 1 \leq j \leq W を満たす整数を選び、C_j 円払った後、グリッドの左から j 列目にあるそれぞれのマスの色を反転させる。

下記の条件を満たすようにするためにかかる合計費用の最小値を出力して下さい。

  • マス (1, 1) から「現在いるマスの右隣もしくは下隣のマスに移動する」 ことを繰り返してマス (H, W) まで移動する経路であって、通るマス(マス (1, 1) とマス (H, W) も含む)の色がすべて同じであるようなものが存在する。

本問題の制約下において、有限回の操作によって上記の条件を満たすようにすることが可能であることが証明できます。

制約

  • 2 \leq H, W \leq 2000
  • 1 \leq R_i \leq 10^9
  • 1 \leq C_j \leq 10^9
  • A_{i, j} \in \lbrace 0, 1\rbrace
  • 入力はすべて整数

入力

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

H W
R_1 R_2 \ldots R_H
C_1 C_2 \ldots C_W
A_{1, 1}A_{1, 2}\ldots A_{1, W}
A_{2, 1}A_{2, 2}\ldots A_{2, W}
\vdots
A_{H, 1}A_{H, 2}\ldots A_{H, W}

出力

答えを出力せよ。


入力例 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

出力例 1

9

白色のマスを 0 、黒色のマスを 1 で表します。 初期状態のグリッドに対し、R_2 = 3 円払って上から 2 行目にあるそれぞれのマスを反転させると、

0100
0100
1010

となります。さらに、C_2 = 6 円払って左から 2 列目にあるそれぞれのマスを反転させると、

0000
0000
1110

となり、マス (1, 1) からマス (3, 4) への移動経路であって通るマスの色がすべて同じであるようなものが存在する状態になります(例えば (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4) という経路)。 かかった合計費用は 3+6 = 9 円であり、このときが考えられる中で最小です。


入力例 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

出力例 2

125

Score : 500 points

Problem Statement

We have a grid with H rows and W columns. Each square is painted either white or black. For each integer pair (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, the color of the square at the i-th row from the top and j-th column from the left (we simply denote this square by Square (i, j)) is represented by A_{i, j}. Square (i, j) is white if A_{i, j} = 0, and black if A_{i, j} = 1.

You may perform the following operations any number of (possibly 0) times in any order:

  • Choose an integer i such that 1 \leq i \leq H, pay R_i yen (the currency in Japan), and invert the color of each square in the i-th row from the top in the grid. (White squares are painted black, and black squares are painted white.)
  • Choose an integer j such that 1 \leq j \leq W, pay C_j yen, and invert the color of each square in the j-th column from the left in the grid.

Print the minimum total cost to satisfy the following condition:

  • There exists a path from Square (1, 1) to Square (H, W) that can be obtained by repeatedly moving down or to the right, such that all squares in the path (including Square (1, 1) and Square (H, W)) have the same color.

We can prove that it is always possible to satisfy the condition in a finite number of operations under the Constraints of this problem.

Constraints

  • 2 \leq H, W \leq 2000
  • 1 \leq R_i \leq 10^9
  • 1 \leq C_j \leq 10^9
  • A_{i, j} \in \lbrace 0, 1\rbrace
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
R_1 R_2 \ldots R_H
C_1 C_2 \ldots C_W
A_{1, 1}A_{1, 2}\ldots A_{1, W}
A_{2, 1}A_{2, 2}\ldots A_{2, W}
\vdots
A_{H, 1}A_{H, 2}\ldots A_{H, W}

Output

Print the answer.


Sample Input 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

Sample Output 1

9

We denote a white square by 0 and a black square by 1. On the initial grid, you can pay R_2 = 3 yen to invert the color of each square in the 2-nd row from the top to make the grid:

0100
0100
1010

Then, you can pay C_2 = 6 yen to invert the color of each square in the 2-nd row from the left to make the grid:

0000
0000
1110

Now, there exists a path from Square (1, 1) to Square (3, 4) such that all squares in the path have the same color (such as the path (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4)). The total cost paid is 3+6 = 9 yen, which is the minimum possible.


Sample Input 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

Sample Output 2

125