E - Ideal Sheet

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君は黒いマスと透明なマスからなるシート A,B1 枚ずつと、透明なマスのみからなる無限に広がるシート C を持っています。
また、高橋君には黒いマスと透明なマスからなる、理想とするシート X が存在します。

シート A,B,X の大きさはそれぞれ縦 H_A マス \timesW_A マス、縦 H_B マス \timesW_B マス、縦 H_X マス \timesW_X マスです。
シート A の各マスは .# からなる長さ W_A の文字列 H_AA_1,A_2,\ldots,A_{H_A} によって表され、
A_i (1\leq i\leq H_A)j 文字目 (1\leq j\leq W_A) が、 . のときシート A の上から i 行目かつ左から j 列目のマスは透明なマスであり、 # のとき黒いマスです。
シート B,X の各マスも、同様に長さ W_B の文字列 H_BB_1,B_2,\ldots,B_{H_B} および長さ W_X の文字列 H_XX_1,X_2,\ldots,X_{H_X} によって表されます。

高橋君の目標は、次の手順で、シート A,B,C から、A,B に存在する すべての黒いマスを使って シート X を作り出すことです。

  1. シート A,B をマス目に沿ってシート C に貼り付ける。この時、シート A,B はそれぞれ好きな場所に平行移動させて貼って良いが、シートを切り分けたり、回転させたりしてはいけない。
  2. シート C からマス目に沿って H_X\times W_X マスの領域を切り出す。ここで、切り出されたシートの各マスは、シート A または B の黒いマスが貼り付けられていれば黒いマスに、そうでなければ透明なマスとなる。

このとき、貼り付ける位置と切り出す領域をうまくとることで高橋君は目標を達成できるか、すなわち次の条件をともにみたすことにできるか判定してください。

  • 切り出されたシートはシート A,B黒いマスをすべて 含む。切り出されたシートの上でシート A,B の黒いマスどうしが重なって存在していても構わない。
  • 切り出されたシートは、回転させたり裏返したりすることなくシート X と一致する。

制約

  • 1\leq H_A,W_A,H_B,W_B,H_X,W_X\leq 10
  • H_A,W_A,H_B,W_B,H_X,W_X は整数
  • A_i.# のみからなる長さ W_A の文字列
  • B_i.# のみからなる長さ W_B の文字列
  • X_i.# のみからなる長さ W_X の文字列
  • シート A,B,X はそれぞれ少なくとも 1 つ以上の黒いマスを含む。

入力

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

出力

高橋君が問題文中の目標を達成できるならば Yes を、できないならば No を出力せよ。


入力例 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

出力例 1

Yes

まず、シート A をシート C に貼り付けると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

さらに、シート B をシート A と左上を合わせて貼ってみると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

ここで、上で具体的に図示されている範囲のうち、上から 1 行目かつ左から 2 列目のマスを左上として 5\times 3 マスを切り出すと下図のようになります。

...
#.#
.#.
.#.
...

これはシート A,B のすべての黒いマスを含んでおり、また、シート X と一致しているため条件を満たしています。

よって、Yes を出力します。


入力例 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

出力例 2

No

シート AB を回転させて貼ってはいけないことに注意してください。


入力例 3

1 1
#
1 2
##
1 1
#

出力例 3

No

どのように貼ったり切り出したりしても、シート B の黒いマスをすべて含むように切り出すことはできないため、1 つめの条件をみたすことができません。 よって、No を出力します。


入力例 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

出力例 4

Yes

Score : 300 points

Problem Statement

Takahashi has two sheets A and B, each composed of black squares and transparent squares, and an infinitely large sheet C composed of transparent squares.
There is also an ideal sheet X for Takahashi composed of black squares and transparent squares.

The sizes of sheets A, B, and X are H_A rows \times W_A columns, H_B rows \times W_B columns, and H_X rows \times W_X columns, respectively.
The squares of sheet A are represented by H_A strings of length W_A, A_1, A_2, \ldots, A_{H_A} consisting of . and #.
If the j-th character (1\leq j\leq W_A) of A_i (1\leq i\leq H_A) is ., the square at the i-th row from the top and j-th column from the left is transparent; if it is #, that square is black.
Similarly, the squares of sheets B and X are represented by H_B strings of length W_B, B_1, B_2, \ldots, B_{H_B}, and H_X strings of length W_X, X_1, X_2, \ldots, X_{H_X}, respectively.

Takahashi's goal is to create sheet X using all black squares in sheets A and B by following the steps below with sheets A, B, and C.

  1. Paste sheets A and B onto sheet C along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
  2. Cut out an H_X\times W_X area from sheet C along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet A or B is pasted there, and transparent otherwise.

Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.

  • The cut-out sheet includes all black squares of sheets A and B. The black squares of sheets A and B may overlap on the cut-out sheet.
  • The cut-out sheet coincides sheet X without rotating or flipping.

Constraints

  • 1\leq H_A, W_A, H_B, W_B, H_X, W_X\leq 10
  • H_A, W_A, H_B, W_B, H_X, W_X are integers.
  • A_i is a string of length W_A consisting of . and #.
  • B_i is a string of length W_B consisting of . and #.
  • X_i is a string of length W_X consisting of . and #.
  • Sheets A, B, and X each contain at least one black square.

Input

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

Output

If Takahashi can achieve the goal described in the problem statement, print Yes; otherwise, print No.


Sample Input 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

Sample Output 1

Yes

First, paste sheet A onto sheet C, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

Next, paste sheet B so that its top-left corner aligns with that of sheet A, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

Now, cut out a 5\times 3 area with the square in the first row and second column of the range illustrated above as the top-left corner, as shown in the figure below.

...
#.#
.#.
.#.
...

This includes all black squares of sheets A and B and matches sheet X, satisfying the conditions.

Therefore, print Yes.


Sample Input 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

Sample Output 2

No

Note that sheets A and B may not be rotated or flipped when pasting them.


Sample Input 3

1 1
#
1 2
##
1 1
#

Sample Output 3

No

No matter how you paste or cut, you cannot cut out a sheet that includes all black squares of sheet B, so you cannot satisfy the first condition. Therefore, print No.


Sample Input 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

Sample Output 4

Yes
F - Uniqueness

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

1 から N の番号がついた N 人の人がいます。人 i は数 A_i を持っています。

「自分と同じ数を持っている人が自分以外の N-1 人の中に存在しない」という条件を満たす人のうち、持っている数が最大である人の番号を求めてください。

条件を満たす人が存在しない場合、代わりにそのことを報告してください。

制約

  • 1 \leq N \leq 3\times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力は全て整数である

入力

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

N
A_1 A_2 \ldots A_N

出力

「自分と同じ数を持っている人が自分以外の N-1 人の中に存在しない」という条件を満たす人が存在しない場合、-1 と出力せよ。

条件を満たす人が存在する場合、そのうち、持っている数が最大である人の番号を出力せよ。


入力例 1

9
2 9 9 7 9 2 4 5 8

出力例 1

9

「自分と同じ数を持っている人が自分以外の N-1 人の中に存在しない」という条件を満たすのは人 4,7,8,94 人です。
これらの人が持っている数はそれぞれ 7,4,5,8 であり、最大の数を持っているのは人 9 です。
よって答えは 9 となります。


入力例 2

4
1000000000 1000000000 998244353 998244353

出力例 2

-1

条件を満たす人が存在しない場合、-1 を出力してください。

Score : 300 points

Problem Statement

There are N people, labeled 1 to N. Person i has an integer A_i.

Among the people who satisfy the condition "None of the other N-1 people has the same integer as themselves," find the one with the greatest integer, and print that person's label.

If no person satisfies the condition, report that fact instead.

Constraints

  • 1 \leq N \leq 3\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

If no person satisfies the condition "None of the other N-1 people has the same integer as themselves," print -1.

Otherwise, among those who satisfy it, print the label of the person whose integer is the largest.


Sample Input 1

9
2 9 9 7 9 2 4 5 8

Sample Output 1

9

Those who satisfy the condition are the persons labeled 4, 7, 8, and 9.
Their integers are 7, 4, 5, and 8, respectively, and the person with the largest integer is the person labeled 9.
Thus, the answer is 9.


Sample Input 2

4
1000000000 1000000000 998244353 998244353

Sample Output 2

-1

If no person satisfies the condition, print -1.

G - Printing Machine

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

1 から N までの番号が付けられた N 個の商品がベルトコンベア上を流れています。 ベルトコンベアには印字機が取り付けられており、商品 i は今から T_i [μs] 後に印字機の範囲に入り、その D_i [μs] 後に印字機の範囲から出ます。

キーエンスの印字機は、印字機の範囲内にある商品 1 つに一瞬で印字することができます(特に、商品が印字機の範囲に入る瞬間や範囲から出る瞬間に印字することも可能です)。 ただし、1 度印字すると、次に印字するまでに 1 [μs] のチャージ時間が必要です。 印字機が印字をする商品とタイミングをうまく選んだとき、最大で何個の商品に印字することができますか?

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq T_i,D_i \leq 10^{18}
  • 入力は全て整数

入力

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

N
T_1 D_1
T_2 D_2
\vdots
T_N D_N

出力

印字機が印字することのできる商品の数の最大値を出力せよ。


入力例 1

5
1 1
1 1
2 1
1 2
1 4

出力例 1

4

以下、今から t [μs] 後のことを単に時刻 t とよびます。

例えば、次のようにして 4 個の商品に印字することができます。

  • 時刻 1 : 商品 1,2,4,5 が印字機の範囲に入る。商品 4 に印字する。
  • 時刻 2 : 商品 3 が印字機の範囲に入り、商品 1,2 が印字機の範囲から出る。商品 1 に印字する。
  • 時刻 3 : 商品 3,4 が印字機の範囲から出る。商品 3 に印字する。
  • 時刻 4.5 : 商品 5 に印字する。
  • 時刻 5 : 商品 5 が印字機の範囲から出る。

5 個の商品すべてに印字することはできないため、答えは 4 です。


入力例 2

2
1 1
1000000000000000000 1000000000000000000

出力例 2

2

入力例 3

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

出力例 3

6

Score : 450 points

Problem Statement

There are N products labeled 1 to N flowing on a conveyor belt. A Keyence printer is attached to the conveyor belt, and product i enters the range of the printer T_i microseconds from now and leaves it D_i microseconds later.

The Keyence printer can instantly print on one product within the range of the printer (in particular, it is possible to print at the moment the product enters or leaves the range of the printer). However, after printing once, it requires a charge time of 1 microseconds before it can print again. What is the maximum number of products the printer can print on when the product and timing for the printer to print are chosen optimally?

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq T_i,D_i \leq 10^{18}
  • All input values are integers.

Input

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

N
T_1 D_1
T_2 D_2
\vdots
T_N D_N

Output

Print the maximum number of products the printer can print on.


Sample Input 1

5
1 1
1 1
2 1
1 2
1 4

Sample Output 1

4

Below, we will simply call the moment t microseconds from now time t.

For example, you can print on four products as follows:

  • Time 1 : Products 1,2,4,5 enter the range of the printer. Print on product 4.
  • Time 2 : Product 3 enters the range of the printer, and products 1,2 leave the range of the printer. Print on product 1.
  • Time 3 : Products 3,4 leave the range of the printer. Print on product 3.
  • Time 4.5 : Print on product 5.
  • Time 5 : Product 5 leaves the range of the printer.

It is impossible to print on all five products, so the answer is 4.


Sample Input 2

2
1 1
1000000000000000000 1000000000000000000

Sample Output 2

2

Sample Input 3

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

Sample Output 3

6
H - Defect-free Squares

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 475

問題文

H 行, 横 W 列のグリッドがあります。グリッドの上から i 行目, 左から j 列目のマスを (i, j) と呼びます。
グリッドの各マスは穴の空いたマスとそうでないマスのどちらかです。穴が空いたマスは (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N) のちょうど N マスです。

正整数の組 (i, j, n) が次の条件を満たすとき、(i, j) を左上隅, (i + n - 1, j + n - 1) を右下隅とする正方形領域を 穴のない正方形 と呼びます。

  • i + n - 1 \leq H
  • j + n - 1 \leq W
  • 0 \leq k \leq n - 1, 0 \leq l \leq n - 1 を満たす全ての非負整数の組 (k, l) に対して、(i + k, j + l) は穴が空いていないマスである。

グリッド内に穴のない正方形は何個ありますか?

制約

  • 1 \leq H, W \leq 3000
  • 0 \leq N \leq \min(H \times W, 10^5)
  • 1 \leq a_i \leq H
  • 1 \leq b_i \leq W
  • (a_i, b_i) は互いに異なる
  • 入力される値は全て整数

入力

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

H W N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

出力

穴のない正方形の個数を出力せよ。


入力例 1

2 3 1
2 3

出力例 1

6

穴のない正方形は全部で 6 個あります。
それらを列挙すると次の通りです。このうちはじめの 5 個は n = 1 の場合であり、領域の左上隅のマスと右下隅のマスが一致します。

  • (1, 1) を左上隅かつ右下隅とする正方形領域
  • (1, 2) を左上隅かつ右下隅とする正方形領域
  • (1, 3) を左上隅かつ右下隅とする正方形領域
  • (2, 1) を左上隅かつ右下隅とする正方形領域
  • (2, 2) を左上隅かつ右下隅とする正方形領域
  • (1, 1) を左上隅, (2, 2) を右下隅とする正方形領域

入力例 2

3 2 6
1 1
1 2
2 1
2 2
3 1
3 2

出力例 2

0

穴のない正方形が存在しない場合もあります。


入力例 3

1 1 0

出力例 3

1

穴のない正方形がグリッド全体と一致する場合もあります。


入力例 4

3000 3000 0

出力例 4

9004500500

Score : 475 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left of the grid.
Each square of the grid is holed or not. There are exactly N holed squares: (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N).

When the triple of positive integers (i, j, n) satisfies the following condition, the square region whose top-left corner is (i, j) and whose bottom-right corner is (i + n - 1, j + n - 1) is called a holeless square.

  • i + n - 1 \leq H.
  • j + n - 1 \leq W.
  • For every pair of non-negative integers (k, l) such that 0 \leq k \leq n - 1, 0 \leq l \leq n - 1, square (i + k, j + l) is not holed.

How many holeless squares are in the grid?

Constraints

  • 1 \leq H, W \leq 3000
  • 0 \leq N \leq \min(H \times W, 10^5)
  • 1 \leq a_i \leq H
  • 1 \leq b_i \leq W
  • All (a_i, b_i) are pairwise different.
  • All input values are integers.

Input

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

H W N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

Output

Print the number of holeless squares.


Sample Input 1

2 3 1
2 3

Sample Output 1

6

There are six holeless squares, listed below. For the first five, n = 1, and the top-left and bottom-right corners are the same square.

  • The square region whose top-left and bottom-right corners are (1, 1).
  • The square region whose top-left and bottom-right corners are (1, 2).
  • The square region whose top-left and bottom-right corners are (1, 3).
  • The square region whose top-left and bottom-right corners are (2, 1).
  • The square region whose top-left and bottom-right corners are (2, 2).
  • The square region whose top-left corner is (1, 1) and whose bottom-right corner is (2, 2).

Sample Input 2

3 2 6
1 1
1 2
2 1
2 2
3 1
3 2

Sample Output 2

0

There may be no holeless square.


Sample Input 3

1 1 0

Sample Output 3

1

The whole grid may be a holeless square.


Sample Input 4

3000 3000 0

Sample Output 4

9004500500
I - Divide the Cake

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

AtCoder Land で高橋君と青木君はイチゴの乗ったケーキを食べることになりました。

AtCoder Landのケーキは円形で、N 個の半径方向の切れ込みによって N 個の扇形ピースに区切られています。

切れ込みを時計回りの順に切れ込み 1, 切れ込み 2, \ldots, 切れ込み N と番号をふったとき、時計回りに切れ込み i (1 \leq i \leq N-1) から切れ込み i+1 までの間の部分をピース i と呼びます。また、時計回りに切れ込み N から切れ込み 1 までの間の部分をピース N と呼びます。

ピース i (1 \leq i \leq N) の上には A_i 個のイチゴが乗っています。

高橋君と青木君はこれから以下のゲームをします。

  • まず、高橋君が N 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み i とする。
  • 次に、青木君が高橋君の選んだ切れ込み以外の N-1 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み j とする。
  • 高橋君は切れ込み i から時計回りに見ていって、切れ込み j までの範囲のピースをすべてもらう。
  • 青木君は、残りのピースをすべてもらう。
  • 高橋君がもらったピースの 1 ピースあたりのイチゴの個数の平均値が青木君がもらったピースの 1 ピースあたりのイチゴの個数の平均値以上であるとき、高橋君の勝ちです。そうでないとき、青木君の勝ちです。

青木君が勝つために最適な切れ込みを選ぶとき、高橋君は必ず勝つためにはどの切れ込みを選べばよいか求めてください。そのような切れ込みが、存在しないときは -1 を、複数存在する場合はそのうち番号が最小のものを答えてください。

制約

  • 2 \leq N \leq 5 \times 10^{5}
  • 0 \leq A_i \leq 5 \times 10^{5}
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えとなる切れ込みの番号を出力せよ。答えとなる切れ込みが存在しない場合は -1 を出力せよ。


入力例 1

3
3 8 5

出力例 1

2

高橋君が切れ込み 1 を選んだ時、青木君は切れ込み 2 を選ぶと高橋君はピース 1 のみを、青木君はピース 2,3 をもらいます。高橋君のもらったピースの上に乗ってるイチゴの個数の平均値は 3、青木君のもらったピースの上に乗ってるイチゴの個数の平均値は 6.5 です。よって青木君が勝ちます。

高橋君が切れ込み 2 を選んだ時、青木君はどの切れ込みを選んでも高橋君が勝ちます。よって、答えは 2 です。


入力例 2

15
4096 64 512 1 256 16384 8 1024 2048 2 128 32 4 16 8192

出力例 2

15