E - Number Place

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

9\times 9 のマス目 A があり、各マスには 1 以上 9 以下の整数が書き込まれています。
具体的には、 A の上から i 行目、左から j 列目のマスには A_{i,j} が書き込まれています。

A が次の条件をすべてみたしているならば Yes を、そうでないならば No を出力してください。

  • A の各行について、その行に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
  • A の各列について、その列に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
  • A の行を上から 3 行ずつ 3 つに分け、同様に列も左から 3 列ずつ 3 つに分ける。 これによって A9 つの 3\times 3 のマス目に分けたとき、それぞれの 3\times 3 のマス目には 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。

制約

  • 1\leq A_{i,j}\leq 9
  • 入力はすべて整数

入力

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

A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}

出力

マス目 A が問題文の条件をすべてみたすならば Yes を、 そうでないならば No を出力せよ。


入力例 1

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8

出力例 1

Yes

マス目 A は次のようになっています。

マス目 A3 つの条件をすべてみたしているため、Yes を出力します。


入力例 2

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8

出力例 2

No

マス目 A は次のようになっています。

例えば左上の 3\times 3 のマス目に注目すると 3 つめの条件をみたしていないことが分かるため、No を出力します。


入力例 3

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6

出力例 3

No

マス目 A は次のようになっています。

例えば一番左の列に注目すると 2 つめの条件をみたしていないことが分かるため、No を出力します。

Score : 250 points

Problem Statement

There is a 9\times 9 grid A, where each cell contains an integer between 1 and 9, inclusive.
Specifically, the cell at the i-th row from the top and j-th column from the left contains A_{i,j}.

If A satisfies all of the following conditions, print Yes. Otherwise, print No.

  • For each row of A, the nine cells in that row contain each integer from 1 to 9 exactly once.
  • For each column of A, the nine cells in that column contain each integer from 1 to 9 exactly once.
  • Divide the rows of A into three groups, each of three rows, from top to bottom, and similarly divide the columns into three groups, each of three columns, from left to right. Each 3\times 3 grid obtained from A in this way contains each integer from 1 to 9 exactly once.

Constraints

  • 1\leq A_{i,j}\leq 9
  • All input values are integers.

Input

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

A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}

Output

If the grid A satisfies all the conditions in the problem statement, print Yes; otherwise, print No.


Sample Input 1

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8

Sample Output 1

Yes

The grid A is shown below.

The grid A satisfies all three conditions, so print Yes.


Sample Input 2

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8

Sample Output 2

No

The grid A is shown below.

For example, if you look at the top left 3\times 3 grid, you can see that the third condition is unsatisfied, so print No.


Sample Input 3

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6

Sample Output 3

No

The grid A is shown below.

For example, if you look at the leftmost column, you can see that the second condition is unsatisfied, so print No.

F - Convex Quadrilateral

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

2 次元座標平面があります。x 軸正方向を右向き、y 軸正方向を上向きとします。

この平面上に自己交差のない四角形があります。
4 つの頂点の座標は反時計回りに (A_x,A_y),(B_x,B_y),(C_x,C_y),(D_x,D_y) です。

この四角形が凸であるか判定してください。

なお、四角形の 4 つの内角が全て 180 度未満であるとき、かつ、その時に限り、その四角形は凸であるといいます。

制約

  • -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
  • 入力に含まれる値は全て整数である
  • 与えられる 4 点は四角形の 4 頂点を反時計回りに並べたものである
  • 与えられる 4 点のなす四角形は自己交差がなく退化していない。すなわち
    • どの 2 頂点も同じ座標にない
    • どの 3 頂点も同一直線上にない
    • 隣接しない 2 辺は共有点を持たない

入力

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

A_x A_y
B_x B_y
C_x C_y
D_x D_y

出力

与えられる四角形が凸なら Yes、凸でないなら No を出力せよ。


入力例 1

0 0
1 0
1 1
0 1

出力例 1

Yes

与えられた四角形は正方形であり、4 つの内角は全て 90 度です。したがって、この四角形は凸です。

図


入力例 2

0 0
1 1
-1 0
1 -1

出力例 2

No

A270 度です。したがって、この四角形は凸ではありません。

図

Score : 300 points

Problem Statement

Consider a two-dimensional coordinate plane, where the x-axis is oriented to the right, and the y-axis is oriented upward.

In this plane, there is a quadrilateral without self-intersection.
The coordinates of the four vertices are (A_x,A_y), (B_x,B_y), (C_x,C_y), and (D_x,D_y), in counter-clockwise order.

Determine whether this quadrilateral is convex.

Here, a quadrilateral is convex if and only if all four interior angles are less than 180 degrees.

Constraints

  • -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
  • All values in input are integers.
  • The given four points are the four vertices of a quadrilateral in counter-clockwise order.
  • The quadrilateral formed by the given four points has no self-intersection and is non-degenerate. That is,
    • no two vertices are at the same coordinates;
    • no three vertices are colinear; and
    • no two edges that are not adjacent have a common point.

Input

Input is given from Standard Input in the following format:

A_x A_y
B_x B_y
C_x C_y
D_x D_y

Output

If the given quadrilateral is convex, print Yes; otherwise, print No.


Sample Input 1

0 0
1 0
1 1
0 1

Sample Output 1

Yes

The given quadrilateral is a square, whose four interior angles are all 90 degrees. Thus, this quadrilateral is convex.

Figure


Sample Input 2

0 0
1 1
-1 0
1 -1

Sample Output 2

No

The angle A is 270 degrees. Thus, this quadrilateral is not convex.

Figure

G - Takahashi's Solitaire

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は N 枚のカードを手札として持っています。 i = 1, 2, \ldots, N について、i 番目のカードには非負整数 A_i が書かれています。

高橋君は、まず手札から好きなカードを 1 枚選んで、テーブルの上に置きます。 その後、高橋君は好きな回数( 0 回でも良い)だけ、下記の操作を繰り返します。

  • 最後にテーブルの上に置いたカードに書かれた整数を X とする。手札に整数 X または整数 (X+1)\bmod M が書かれたカードがあれば、そのうち好きなものを 1 枚選んで、テーブルの上に置く。ここで、(X+1)\bmod M(X+1)M で割ったあまりを表す。

最終的に手札に残ったカードに書かれている整数の総和としてあり得る最小値を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • 0 \leq A_i \lt M
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

9 7
3 0 2 5 5 3 0 6 3

出力例 1

11

高橋君が、まず 4 番目のカード(書かれた整数は 5 )をテーブルの上に置き、その後、以下の手順で操作を行う場合を考えます。

  • 5 番目のカード(書かれた整数は 5 )をテーブルの上に置く。
  • 8 番目のカード(書かれた整数は 6 )をテーブルの上に置く。
  • 2 番目のカード(書かれた整数は 0 )をテーブルの上に置く。
  • 7 番目のカード(書かれた整数は 0 )をテーブルの上に置く。

このとき、最終的に手札に残ったカードは、1, 3, 6, 9 枚目のカードであり、それらに書かれた整数の総和は 3 + 2 + 3 +3 = 11 です。 これが、最終的に手札に残ったカードに書かれている整数の総和としてあり得る最小値です。


入力例 2

1 10
4

出力例 2

0

入力例 3

20 20
18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12

出力例 3

99

Score : 400 points

Problem Statement

Takahashi has N cards in his hand. For i = 1, 2, \ldots, N, the i-th card has an non-negative integer A_i written on it.

First, Takahashi will freely choose a card from his hand and put it on a table. Then, he will repeat the following operation as many times as he wants (possibly zero).

  • Let X be the integer written on the last card put on the table. If his hand contains cards with the integer X or the integer (X+1)\bmod M written on them, freely choose one of those cards and put it on the table. Here, (X+1)\bmod M denotes the remainder when (X+1) is divided by M.

Print the smallest possible sum of the integers written on the cards that end up remaining in his hand.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • 0 \leq A_i \lt M
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

9 7
3 0 2 5 5 3 0 6 3

Sample Output 1

11

Assume that he first puts the fourth card (5 is written) on the table and then performs the following.

  • Put the fifth card (5 is written) on the table.
  • Put the eighth card (6 is written) on the table.
  • Put the second card (0 is written) on the table.
  • Put the seventh card (0 is written) on the table.

Then, the first, third, sixth, and ninth cards will end up remaining in his hand, and the sum of the integers on those cards is 3 + 2 + 3 +3 = 11. This is the minimum possible sum of the integers written on the cards that end up remaining in his hand.


Sample Input 2

1 10
4

Sample Output 2

0

Sample Input 3

20 20
18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12

Sample Output 3

99
H - Jump Distance Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

座標平面上に N 個の点 P_1,P_2,\ldots,P_N があり、点 P_i の座標は (X_i,Y_i) です。
2 つの点 A,B の距離 \text{dist}(A,B) を次のように定義します。

最初、ウサギが点 A にいる。
(x,y) にいるウサギは (x+1,y+1), (x+1,y-1), (x-1,y+1), (x-1,y-1) のいずれかに 1 回のジャンプで移動することができる。
A から点 B まで移動するために必要なジャンプの回数の最小値を \text{dist}(A,B) として定義する。
ただし、何度ジャンプを繰り返しても点 A から点 B まで移動できないとき、\text{dist}(A,B)=0 とする。

\displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i,P_j) を求めてください。

制約

  • 2\leq N\leq 2\times 10^5
  • 0\leq X_i,Y_i\leq 10^8
  • i\neq j ならば (X_i,Y_i)\neq (X_j,Y_j)
  • 入力はすべて整数

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

\displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i,P_j) の値を整数で出力せよ。


入力例 1

3
0 0
1 3
5 6

出力例 1

3

P_1,P_2,P_3 の座標はそれぞれ (0,0), (1,3), (5,6) です。
P_1 から P_2 へはウサギは (0,0)\to (1,1)\to (0,2)\to (1,3)3 回で移動でき、2 回以下では P_1 から P_2 まで移動できないため、
\text{dist}(P_1,P_2)=3 です。
P_1 から P_3 および P_2 から P_3 へはウサギは移動できないため、\text{dist}(P_1,P_3)=\text{dist}(P_2,P_3)=0 となります。

よって、答えは \displaystyle\sum_{i=1}^{2}\displaystyle\sum_{j=i+1}^3\text{dist}(P_i,P_j)=\text{dist}(P_1,P_2)+\text{dist}(P_1,P_3)+\text{dist}(P_2,P_3)=3+0+0=3 となります。


入力例 2

5
0 5
1 7
2 9
3 8
4 6

出力例 2

11

Score: 500 points

Problem Statement

On a coordinate plane, there are N points P_1, P_2, \ldots, P_N, where point P_i has coordinates (X_i, Y_i).
The distance \text{dist}(A, B) between two points A and B is defined as follows:

A rabbit is initially at point A.
A rabbit at position (x, y) can jump to (x+1, y+1), (x+1, y-1), (x-1, y+1), or (x-1, y-1) in one jump.
\text{dist}(A, B) is defined as the minimum number of jumps required to get from point A to point B.
If it is impossible to get from point A to point B after any number of jumps, let \text{dist}(A, B) = 0.

Calculate the sum \displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i, P_j).

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i, Y_i \leq 10^8
  • For i \neq j, (X_i, Y_i) \neq (X_j, Y_j)
  • All input values are integers.

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the value of \displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i, P_j) as an integer.


Sample Input 1

3
0 0
1 3
5 6

Sample Output 1

3

P_1, P_2, and P_3 have coordinates (0,0), (1,3), and (5,6), respectively.
The rabbit can get from P_1 to P_2 in three jumps via (0,0) \to (1,1) \to (0,2) \to (1,3), but not in two or fewer jumps,
so \text{dist}(P_1, P_2) = 3.
The rabbit cannot get from P_1 to P_3 or from P_2 to P_3, so \text{dist}(P_1, P_3) = \text{dist}(P_2, P_3) = 0.

Therefore, the answer is \displaystyle\sum_{i=1}^{2}\displaystyle\sum_{j=i+1}^3\text{dist}(P_i, P_j)=\text{dist}(P_1, P_2)+\text{dist}(P_1, P_3)+\text{dist}(P_2, P_3)=3+0+0=3.


Sample Input 2

5
0 5
1 7
2 9
3 8
4 6

Sample Output 2

11
I - Useless for LIS

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ N の整数列 A が与えられます。

t = 1, 2, \dots ,N について、 A_tA の最長増加部分列に含まれることがあるか判定してください。

A_tA の最長増加部分列に含まれることがあるとは、以下のことをいいます。

  • 最長増加部分列の長さを L とする。各要素が 1 以上 N 以下の単調増加な整数列 i = (i_1, i_2, \dots ,i_L) \ (i_1 < i_2 < \dots < i_L) であって以下をすべて満たすものが存在する。

    • A_{i_1} < A_{i_2} < \dots < A_{i_L}
    • ある k \ (1 \leq k \leq L) が存在して i_k = t である

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

最長増加部分列とは?

A の部分列とは A の要素をいくつか抜き出して元の順に並べてできる列を指します。

A の最長増加部分列とは、 A の狭義単調増加な部分列のうち列の長さが最大のものを指します。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

ここで \mathrm{case_i}i 番目のケースの入力を意味する。各ケースは以下の形式で与えられる。

N
A_1 A_2 \cdots A_N

出力

以下の形式で出力せよ。

\mathrm{answer}_1
\mathrm{answer}_2
\vdots
\mathrm{answer}_T

ここで \mathrm{answer}_ii 番目のケースの出力を意味する。各ケースについては、次の通りである。

A_tA の最長増加部分列に含まれることがある tm 個存在し、昇順に i_1, i_2, \dots ,i_m であったとする。このとき、以下の形式で出力せよ。

m
i_1 i_2 \cdots i_m

入力例 1

1
5
2 1 4 5 3

出力例 1

4
1 2 3 4

最長増加部分列の 1 つは (2, 4, 5) で、長さは 3 です。(1, 4, 5) も最長増加部分列の 1 つです。しかし、 A_5 を含む最長増加部分列はありません。

よって、 1, 2, 3, 4 を出力します。


入力例 2

2
6
2 5 3 4 3 4
5
10000 1000 100 1 10

出力例 2

5
1 3 4 5 6
2
4 5

Score : 525 points

Problem Statement

You are given an integer sequence A of length N.

For each t = 1, 2, \dots, N, determine whether A_t is included in a longest increasing subsequence of A.

Here, A_t is included in a longest increasing subsequence of A if and only if the following holds:

  • Let L be the length of a longest increasing subsequence of A. There exists a strictly increasing integer sequence i = (i_1, i_2, \dots, i_L) \ (i_1 < i_2 < \dots < i_L), where each element is between 1 and N, inclusive, that satisfies all of the following conditions:

    • A_{i_1} < A_{i_2} < \dots < A_{i_L}.
    • i_k = t for some k \ (1 \leq k \leq L).

You are given T test cases; solve each of them.

What is a longest increasing subsequence?

A subsequence of a sequence A is a sequence that can be derived by extracting some elements from A without changing the order.

A longest increasing subsequence of a sequence A is a subsequence of A that is strictly increasing with the greatest possible length.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • The sum of N across all test cases is at most 2 \times 10^5.

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Here, \mathrm{case_i} represents the input for the i-th case. Each case is given in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answers in the following format:

\mathrm{answer}_1
\mathrm{answer}_2
\vdots
\mathrm{answer}_T

Here, \mathrm{answer}_i represents the output for the i-th case. For each case, let there be m indices t such that A_t is included in a longest increasing subsequence of A, which are i_1, i_2, \dots, i_m in ascending order. Print these in the following format:

m
i_1 i_2 \cdots i_m

Sample Input 1

1
5
2 1 4 5 3

Sample Output 1

4
1 2 3 4

One of the longest increasing subsequences is (2, 4, 5), with a length of 3. Another longest increasing subsequence is (1, 4, 5). However, no longest increasing subsequence includes A_5.

Therefore, print 1, 2, 3, 4.


Sample Input 2

2
6
2 5 3 4 3 4
5
10000 1000 100 1 10

Sample Output 2

5
1 3 4 5 6
2
4 5