C - カップリング選抜

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

アイドルグループ Bit♡Beat では、新曲のカップリングとしてメンバーの中から 2 人のユニット(選抜)を組むことになりました。

グループには N 人のメンバーがおり、それぞれのメンバーにスキル値が定まっています。 i 人目のメンバー (1\le i\le N) のスキル値は A_i です。

プロデューサーであるあなたは、スキル値の合計が ちょうど S になるような 2 人のメンバーの選び方を探しています。

スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか求めてください。

制約

  • 2\le N\le 5\times 10^5
  • 1\le S \le 10^9
  • 1\le A_i\le 10^9
  • 入力される値は全て整数

入力

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

N S
A_1 A_2 \ldots A_N

出力

スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか出力せよ。


入力例 1

5 11
5 6 9 2 2

出力例 1

3

1 人目と 2 人目がユニットを組むと、スキル値の合計は 5+6=11 となります。

このほかにも、 3 人目と 4 人目、3 人目と 5 人目がユニットを組んでも条件を満たします。

条件を満たす 2 人のメンバーの選び方は 3 通りであるため、 3 を出力してください。


入力例 2

10 1000000000
1 1 1 1 1 1 1 1 1 1

出力例 2

0

条件を満たす 2 人のメンバーの選び方が存在しない場合もあります。


入力例 3

6 14
7 7 7 7 7 7

出力例 3

15

入力例 4

15 8
3 4 10 9 1 1 1 7 8 9 9 3 8 9 7

出力例 4

6
D - Number Box

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

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

NN 列のマス目があり、上から i 行目、左から j 列目のマスには数字 A_{i,j} が書かれています。

このマス目は上下および左右がつながっているものとします。つまり以下が全て成り立ちます。

  • (1,i) の上のマスは (N,i) であり、(N,i) の下のマスは (1,i) である。(1\le i\le N)
  • (i,1) の左のマスは (i,N) であり、(i,N) の右のマスは (i,1) である。(1\le i\le N)

高橋君は、上下左右および斜めの 8 方向のうちいずれかを初めに選びます。そして、好きなマスから決めた方向に 1 マス移動することを N-1 回繰り返します。

高橋君は N 個のマス上を移動することになりますが、高橋君が通ったマスに書かれている数字を左から通った順番に並べた整数としてあり得る最大のものを求めてください。

制約

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • 入力はすべて整数。

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

出力

答えを出力せよ。


入力例 1

4
1161
1119
7111
1811

出力例 1

9786

高橋君が上から 2 行目、左から 4 列目のマスから出発し、右下に進むことで、通ったマスに書かれた数字を並べ 9786 を作ることができます。 9786 より大きい値を作ることはできないため、9786 が解です。


入力例 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

出力例 2

1111111111

32bit整数型に答えが収まるとは限らないことに注意してください。

Score : 200 points

Problem Statement

You are given a positive integer N.

We have a grid with N rows and N columns, where the square at the i-th row from the top and j-th column from the left has a digit A_{i,j} written on it.

Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.

  • (N,i) is just above (1,i), and (1,i) is just below (N,i). (1\le i\le N).
  • (i,N) is just to the left of (i,1), and (i,1) is just to the right of (i,N). (1\le i\le N).

Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times.

In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.

Constraints

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

Output

Print the answer.


Sample Input 1

4
1161
1119
7111
1811

Sample Output 1

9786

If Takahashi starts on the square at the 2-nd row from the top and 4-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 9786. It is impossible to make a value greater than 9786, so the answer is 9786.


Sample Input 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

Sample Output 2

1111111111

Note that the answer may not fit into a 32-bit integer.

E - Variety Split Easy

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

この問題は F 問題の簡易版です。

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

A1 か所で区切って 2 個の空でない連続する部分列に分割するとき、数列に含まれる数の種類数の和としてありえる最大値を求めてください。

より厳密には、1 \leq i \leq N-1 を満たす整数 i について (A_1,A_2,\ldots,A_i), (A_{i+1},A_{i+2},\ldots,A_N) のそれぞれに含まれる整数の種類数の和としてありえる最大値を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • 入力はすべて整数である

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

5
  • i=1 としたとき、(3)(1,4,1,5) のそれぞれに含まれる整数の種類数は 1,3 で、これらの和は 4 です。
  • i=2 としたとき、(3,1)(4,1,5) のそれぞれに含まれる整数の種類数は 2,3 で、これらの和は 5 です。
  • i=3 としたとき、(3,1,4)(1,5) のそれぞれに含まれる整数の種類数は 3,2 で、これらの和は 5 です。
  • i=4 としたとき、(3,1,4,1)(5) のそれぞれに含まれる整数の種類数は 3,1 で、これらの和は 4 です。

よって、 i=2,3 のときに、最大値 5 を取ります。


入力例 2

10
2 5 6 5 2 1 7 9 7 2

出力例 2

8

Score : 350 points

Problem Statement

This problem is a simplified version of Problem F.

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

When splitting A at one position into two non-empty (contiguous) subarrays, find the maximum possible sum of the counts of distinct integers in those subarrays.

More formally, find the maximum sum of the following two values for an integer i such that 1 \leq i \leq N-1: the count of distinct integers in (A_1, A_2, \ldots, A_i), and the count of distinct integers in (A_{i+1}, A_{i+2}, \ldots, A_N).

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • 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.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

5
  • For i=1, (3) contains 1 distinct integer, and (1,4,1,5) contains 3 distinct integers, for a total of 4.
  • For i=2, (3,1) contains 2 distinct integers, and (4,1,5) contains 3 distinct integers, for a total of 5.
  • For i=3, (3,1,4) contains 3 distinct integers, and (1,5) contains 2 distinct integers, for a total of 5.
  • For i=4, (3,1,4,1) contains 3 distinct integers, and (5) contains 1 distinct integer, for a total of 4.

Therefore, the maximum sum is 5 for i=2,3.


Sample Input 2

10
2 5 6 5 2 1 7 9 7 2

Sample Output 2

8
F - Kaiten Sushi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

とある回転寿司に、1 から N までの番号が付けられた N 人の人が訪れています。 人 i美食度A_i です。

今からベルトコンベア上を M 個の寿司が流れます。 j 番目に流れる寿司の 美味しさB_j です。 それぞれの寿司は、人 1,2,\dots,N の前をこの順に流れていきます。 それぞれの人は、美味しさが自分の美食度以上である寿司が自分の前に流れてきたときはその寿司を取って食べ、それ以外のときは何もしません。 人 i が取って食べた寿司は、人 j\ (j > i) の前にはもう流れてきません。

M 個の寿司それぞれについて、その寿司を誰が食べるか、あるいは誰も食べないかどうかを求めてください。

制約

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_i \leq 2\times 10^5
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

M 行出力せよ。 j\ (1\leq j \leq M) 行目には、j 番目に流れる寿司を食べる人がいるならばその人の番号を、そうでないならば -1 を出力せよ。


入力例 1

3 3
3 8 2
5 2 1

出力例 1

1
3
-1
  • 1 番目の寿司について、
    • まず人 1 の前を流れます。B_1 \geq A_1 なので、人 1 はこれを取って食べます。
    • 2,3 の前にはこの寿司は流れてきません。
  • 2 番目の寿司について、
    • まず人 1 の前を流れます。B_2 < A_1 なので、人 1 は何もしません。
    • 次に人 2 の前を流れます。B_2 < A_2 なので、人 2 は何もしません。
    • 最後に人 3 の前を流れます。B_2 \geq A_3 なので、人 3 はこれを取って食べます。
  • 3 番目の寿司について、
    • まず人 1 の前を流れます。B_3 < A_1 なので、人 1 は何もしません。
    • 次に人 2 の前を流れます。B_3 < A_2 なので、人 2 は何もしません。
    • 最後に人 3 の前を流れます。B_3 < A_3 なので、人 3 は何もしません。
    • よって、誰もこの寿司を食べません。

入力例 2

3 3
1 1 1
1 1 1

出力例 2

1
1
1

入力例 3

10 5
60 83 76 45 70 91 37 58 94 22
70 39 52 33 18

出力例 3

1
7
4
10
-1

Score: 350 points

Problem Statement

There are N people numbered from 1 to N visiting a conveyor belt sushi restaurant. The gourmet level of person i is A_i.

Now, M pieces of sushi will be placed on the conveyor belt. The deliciousness of the j-th sushi is B_j. Each piece of sushi passes in front of people 1, 2, \dots, N in this order. Each person, when a sushi whose deliciousness is not less than their gourmet level passes in front of them, will take and eat that sushi; otherwise, they do nothing. A sushi that person i takes and eats will no longer pass in front of person j\ (j > i).

For each of the M pieces of sushi, determine who eats that sushi, or if nobody eats it.

Constraints

  • 1 \leq N, M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 2 \times 10^5
  • 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
B_1 B_2 \dots B_M

Output

Print M lines. The j-th line (1 \leq j \leq M) should contain the number representing the person who eats the j-th sushi, or -1 if nobody eats it.


Sample Input 1

3 3
3 8 2
5 2 1

Sample Output 1

1
3
-1
  • For the 1st sushi:
    • It first passes in front of person 1. Since B_1 \geq A_1, person 1 takes and eats it.
    • It will not pass in front of person 2 and 3.
  • For the 2nd sushi:
    • It first passes in front of person 1. Since B_2 < A_1, person 1 does nothing.
    • Next, it passes in front of person 2. Since B_2 < A_2, person 2 does nothing.
    • Finally, it passes in front of person 3. Since B_2 \geq A_3, person 3 takes and eats it.
  • For the 3rd sushi:
    • It first passes in front of person 1. Since B_3 < A_1, person 1 does nothing.
    • Next, it passes in front of person 2. Since B_3 < A_2, person 2 does nothing.
    • Finally, it passes in front of person 3. Since B_3 < A_3, person 3 does nothing.
    • Therefore, nobody eats this sushi.

Sample Input 2

3 3
1 1 1
1 1 1

Sample Output 2

1
1
1

Sample Input 3

10 5
60 83 76 45 70 91 37 58 94 22
70 39 52 33 18

Sample Output 3

1
7
4
10
-1
G - Switch Seats

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 組のカップルが一列に座っています。
2 組のカップルであって、もともと両方のカップルは隣り合わせで座っておらず、かつ 4 人の間で席を交換することで両方のカップルが隣り合わせで座れるようになる組の個数を数えてください。

長さ 2N の数列 A=(A_1,A_2,\dots,A_{2N}) があります。A には 1, 2, \dots, N がそれぞれ 2 回ずつ登場します。

1 \leq a \lt b \leq N を満たす整数対 (a, b) であって以下の条件を全て満たすものの個数を求めてください。

  • A 内において a 同士は隣接していない。
  • A 内において b 同士は隣接していない。
  • 次の操作を 1 回以上自由な回数行うことで、A 内において a 同士が隣接していて、かつ b 同士が隣接している状態にすることができる。
    • A_i = a, A_j = b を満たす整数対 (i, j) (1 \leq i \leq 2N, 1 \leq j \leq 2N) を選び、A_iA_j を入れ替える。

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • A には 1, 2, \dots, N がそれぞれ 2 回ずつ登場する
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{case}_ii 番目のテストケースを意味する。

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

各テストケースは以下の形式で与えられる。

N
A_1 A_2 \dots A_{2N}

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。


入力例 1

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

出力例 1

1
0
4

1 番目のテストケースについて考えます。
(a, b)=(1, 2) は問題文の条件を満たします。理由は次の通りです。

  • A 内において 1 同士は隣接していない。
  • A 内において 2 同士は隣接していない。
  • (i,j)=(1, 6) として A_1A_6 を入れ替える操作を行うことで、1 同士が隣接していて、かつ 2 同士が隣接している状態にすることができる。

問題文の条件を満たす (a, b)(1, 2) のみです。

Score : 400 points

Problem Statement

N couples are seated in a line.
Count the number of pairs of couples such that neither couple was originally sitting next to each other, and both couples can end up sitting next to each other by swapping seats among those four people.

There is a sequence A = (A_1, A_2, \dots, A_{2N}) of length 2N. Each of the integers 1, 2, \dots, N appears exactly twice in A.

Find the number of integer pairs (a, b) satisfying 1 \leq a < b \leq N and all of the following conditions:

  • The two occurrences of a in A are not adjacent.
  • The two occurrences of b in A are not adjacent.
  • By performing the following operation one or more times in any order, it is possible to reach a state where the two occurrences of a in A are adjacent and the two occurrences of b in A are also adjacent.
    • Choose an integer pair (i, j) (1 \leq i \leq 2N, 1 \leq j \leq 2N) such that A_i = a and A_j = b, and swap A_i with A_j.

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

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • Each of 1, 2, \dots, N appears exactly twice in A.
  • The sum of N over all test cases is at most 2 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

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

Each test case is given in the following format:

N
A_1 A_2 \dots A_{2N}

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

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

Sample Output 1

1
0
4

Consider the first test case.
(a, b) = (1, 2) satisfies the conditions in the problem statement, for the following reasons:

  • The two occurrences of 1 in A are not adjacent.
  • The two occurrences of 2 in A are not adjacent.
  • By performing the operation where (i, j) = (1, 6) and swapping A_1 with A_6, you can reach a state where the two occurrences of 1 are adjacent and the two occurrences of 2 are also adjacent.

(1, 2) is the only pair (a, b) that satisfies the conditions.