実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 以上 N 以下の整数からなる長さ N の数列 a = (a_1, \dots, a_N) が与えられます。
以下の条件を全て満たす整数 i, j の組の総数を求めてください。
- 1 \leq i \lt j \leq N
- \min(a_i, a_j) = i
- \max(a_i, a_j) = j
制約
- 2 \leq N \leq 5 \times 10^5
- 1 \leq a_i \leq N \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
4 1 3 2 4
出力例 1
2
(i, j) = (1, 4), (2, 3) が条件を満たします。
入力例 2
10 5 8 2 2 1 6 7 2 9 10
出力例 2
8
Score : 300 points
Problem Statement
You are given a sequence a = (a_1, \dots, a_N) of length N consisting of integers between 1 and N.
Find the number of pairs of integers i, j that satisfy all of the following conditions:
- 1 \leq i \lt j \leq N
- \min(a_i, a_j) = i
- \max(a_i, a_j) = j
Constraints
- 2 \leq N \leq 5 \times 10^5
- 1 \leq a_i \leq N \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
4 1 3 2 4
Sample Output 1
2
(i, j) = (1, 4), (2, 3) satisfy the conditions.
Sample Input 2
10 5 8 2 2 1 6 7 2 9 10
Sample Output 2
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
10^9 行 10^9 列のグリッドがあり、このグリッドの上から i 番目、左から j 番目のマスをマス (i, j) と表記します。
グリッド上には N 人の人がおり、はじめ i 人目の人はマス (R_i, C_i) にいます。
はじめ時刻は 0 であり、各人は、時刻 1, 2, 3, 4, \ldots に以下のような移動をすることができます。
- その場に留まるか、8 近傍のマスに移動する。ただし、グリッドの外側に出ることはできない。厳密には、現在いるマスをマス (i, j) としてマス (i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j), (i, j + 1), (i + 1, j - 1), (i + 1, j), (i + 1, j + 1) のうち存在するマスのいずれかに移動する。また、移動には時間がかからないものとする。
N 人の人が全員同じマスに集まる時刻として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_i, C_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R_1 C_1 R_2 C_2 \vdots R_N C_N
出力
答えを出力せよ。
入力例 1
3 2 3 5 1 8 1
出力例 1
3
以下のようにそれぞれの人が移動することによって、全員が時刻 3 にマス (5, 4) に集まります。
-
時刻 1 では、1 人目の人はマス (3, 4)、2 人目の人はマス (6, 2)、3 人目の人はマス (7, 2) に移動する。
-
時刻 2 では、1 人目の人はマス (4, 4)、2 人目の人はマス (5, 3)、3 人目の人はマス (6, 3) に移動する。
-
時刻 3 では、1 人目の人はマス (5, 4)、2 人目の人はマス (5, 4)、3 人目の人はマス (5, 4) に移動する。
入力例 2
5 6 7 6 7 6 7 6 7 6 7
出力例 2
0
はじめからすべての人は同じマスにいます。
入力例 3
6 91 999999986 53 999999997 32 999999932 14 999999909 49 999999985 28 999999926
出力例 3
44
Score : 300 points
Problem Statement
There is a grid with 10^9 rows and 10^9 columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
There are N people on the grid. Initially, the i-th person is at square (R_i, C_i).
The time starts at 0. Each person can do the following move at times 1, 2, 3, 4, \ldots.
- Stay at the current position, or move to an 8-adjacent square. It is forbidden to leave the grid. Formally, let square (i, j) be the current square, and move to one of the squares (i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j), (i, j + 1), (i + 1, j - 1), (i + 1, j), (i + 1, j + 1) that exists. Assume that the move takes no time.
Find the minimum possible time when the N people are at the same square.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_i, C_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N R_1 C_1 R_2 C_2 \vdots R_N C_N
Output
Output the answer.
Sample Input 1
3 2 3 5 1 8 1
Sample Output 1
3
All people will be at square (5, 4) at time 3 if each person moves as follows.
-
At time 1, the 1st person moves to square (3, 4), the 2nd person moves to square (6, 2), and the 3rd person moves to square (7, 2).
-
At time 2, the 1st person moves to square (4, 4), the 2nd person moves to square (5, 3), and the 3rd person moves to square (6, 3).
-
At time 3, the 1st person moves to square (5, 4), the 2nd person moves to square (5, 4), and the 3rd person moves to square (5, 4).
Sample Input 2
5 6 7 6 7 6 7 6 7 6 7
Sample Output 2
0
All people start at the same square.
Sample Input 3
6 91 999999986 53 999999997 32 999999932 14 999999909 49 999999985 28 999999926
Sample Output 3
44
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
(1,2,\dots,N) を並び替えて得られる数列 P=(P_1,P_2,\dots,P_N) が与えられます。
長さ K の正整数列 (i_1,i_2,\dots,i_K) であって、以下の条件を共に満たすものを良い添字列と呼びます。
- 1\leq i_1 < i_2 < \dots < i_K \leq N
- (P_{i_1},P_{i_2},\dots,P_{i_K}) はある連続する K 個の整数を並び替えることで得られる。
厳密には、ある整数 a が存在して、\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace。
全ての良い添字列における i_K-i_1 の最小値を求めてください。 なお、本問題の制約下では良い添字列が必ず 1 つ以上存在することが示せます。
制約
- 1\leq K \leq N \leq 2\times 10^5
- 1\leq P_i\leq N
- i\neq j ならば P_i\neq P_j
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \dots P_N
出力
全ての良い添字列における i_K-i_1 の最小値を出力せよ。
入力例 1
4 2 2 3 1 4
出力例 1
1
良い添字列は (1,2),(1,3),(2,4) の 3 つです。 例えば (i_1,i_2)=(1,3) は、 1\leq i_1 < i_2 \leq N かつ (P_{i_1},P_{i_2})=(2,1) が連続する 2 つの整数 1,2 の並び替えなので良い添字列です。
これらの良い添字列のうち i_K-i_1 の値が最小となるのは (1,2) で、そのときの値は 2-1=1 です。
入力例 2
4 1 2 3 1 4
出力例 2
0
どの良い添字列においても i_K-i_1=i_1-i_1=0 です。
入力例 3
10 5 10 1 6 8 7 2 5 9 3 4
出力例 3
5
Score: 425 points
Problem Statement
You are given a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N).
A length-K sequence of indices (i_1, i_2, \dots, i_K) is called a good index sequence if it satisfies both of the following conditions:
- 1 \leq i_1 < i_2 < \dots < i_K \leq N.
- The subsequence (P_{i_1}, P_{i_2}, \dots, P_{i_K}) can be obtained by rearranging some consecutive K integers.
Formally, there exists an integer a such that \lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace.
Find the minimum value of i_K - i_1 among all good index sequences. It can be shown that at least one good index sequence exists under the constraints of this problem.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- P_i \neq P_j if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K P_1 P_2 \dots P_N
Output
Print the minimum value of i_K - i_1 among all good index sequences.
Sample Input 1
4 2 2 3 1 4
Sample Output 1
1
The good index sequences are (1,2),(1,3),(2,4). For example, (i_1, i_2) = (1,3) is a good index sequence because 1 \leq i_1 < i_2 \leq N and (P_{i_1}, P_{i_2}) = (2,1) is a rearrangement of two consecutive integers 1, 2.
Among these good index sequences, the smallest value of i_K - i_1 is for (1,2), which is 2-1=1.
Sample Input 2
4 1 2 3 1 4
Sample Output 2
0
i_K - i_1 = i_1 - i_1 = 0 in all good index sequences.
Sample Input 3
10 5 10 1 6 8 7 2 5 9 3 4
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
正整数 N に対し、N が 400 number であることは以下の 2 つの条件をともに満たすことであると定義します。
- N の素因数はちょうど 2 種類である。
- N の各素因数 p について、N が p で割り切れる回数は偶数回である。より厳密には、N の各素因数 p について p^k が N の約数であるような最大の非負整数 k は偶数である。
Q 個のクエリが与えられるので、それぞれについて答えてください。各クエリでは、整数 A が与えられるので、A 以下の最大の 400 number の値を求めてください。ただし、本問題の制約下では A 以下の 400 number は常に存在します。
制約
- 1 \leq Q \leq 2 \times 10^5
- 各クエリについて、36 \leq A \leq 10^{12}
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ここで、\text{query}_i は i 番目のクエリであり、以下の形式で与えられる。
A
出力
Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
5 404 36 60 1000000000000 123456789
出力例 1
400 36 36 1000000000000 123454321
1 番目のクエリについて説明します。
400 の素因数は 2, 5 のちょうど 2 種類であり、400 が 2 で割り切れる回数は 4 回、5 で割り切れる回数は 2 回であるため 400 は 400 number です。401, 402, 403, 404 は 400 number ではないため答えは 400 となります。
Score : 425 points
Problem Statement
A positive integer N is a 400 number if and only if it satisfies both of the following two conditions:
- N has exactly 2 distinct prime factors.
- For each prime factor p of N, p divides N an even number of times. More formally, the maximum non-negative integer k such that p^k divides N is even.
Process Q queries. Each query gives you an integer A, so find the largest 400 number not exceeding A. Under the constraints of this problem, a 400 number not exceeding A always exists.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- For each query, 36 \leq A \leq 10^{12}.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i is the i-th query, given in the following format:
A
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Sample Input 1
5 404 36 60 1000000000000 123456789
Sample Output 1
400 36 36 1000000000000 123454321
Let us explain the first query.
There are exactly 2 prime factors of 400: 2 and 5. Also, 2 divides 400 four times and 5 divides it twice, so 400 is a 400 number. None of 401, 402, 403, and 404 is a 400 number, so the answer is 400.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
黒板に 1 以上 M 以下の整数からなる集合 N 個 S_1,S_2,\dots,S_N が書かれています。ここで、S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace です。
あなたは、以下の操作を好きな回数(0 回でもよい)行うことが出来ます。
- 1 個以上の共通した要素を持つ 2 個の集合 X,Y を選ぶ。X,Y の 2 個を黒板から消し、新たに X\cup Y を黒板に書く。
ここで、X\cup Y とは X か Y の少なくともどちらかに含まれている要素のみからなる集合を意味します。
1 と M が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。
制約
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
1 と M が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1 を出力せよ。
入力例 1
3 5 2 1 2 2 2 3 3 3 4 5
出力例 1
2
まず、\lbrace 1,2 \rbrace と \lbrace 2,3 \rbrace を選んで消し、\lbrace 1,2,3 \rbrace を追加します。
そして、\lbrace 1,2,3 \rbrace と \lbrace 3,4,5 \rbrace を選んで消し、\lbrace 1,2,3,4,5 \rbrace を追加します。
すると 2 回の操作で 1 と M を両方含む集合を作ることが出来ます。1 回の操作では目標を達成できないため、答えは 2 です。
入力例 2
1 2 2 1 2
出力例 2
0
始めから S_1 が 1,M を共に含むため、必要な操作回数の最小値は 0 回です。
入力例 3
3 5 2 1 3 2 2 4 3 2 4 5
出力例 3
-1
入力例 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
出力例 4
2
Score : 500 points
Problem Statement
On a blackboard, there are N sets S_1,S_2,\dots,S_N consisting of integers between 1 and M. Here, S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace.
You may perform the following operation any number of times (possibly zero):
- choose two sets X and Y with at least one common element. Erase them from the blackboard, and write X\cup Y on the blackboard instead.
Here, X\cup Y denotes the set consisting of the elements contained in at least one of X and Y.
Determine if one can obtain a set containing both 1 and M. If it is possible, find the minimum number of operations required to obtain it.
Constraints
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
If one can obtain a set containing both 1 and M, print the minimum number of operations required to obtain it; if it is impossible, print -1 instead.
Sample Input 1
3 5 2 1 2 2 2 3 3 3 4 5
Sample Output 1
2
First, choose and remove \lbrace 1,2 \rbrace and \lbrace 2,3 \rbrace to obtain \lbrace 1,2,3 \rbrace.
Then, choose and remove \lbrace 1,2,3 \rbrace and \lbrace 3,4,5 \rbrace to obtain \lbrace 1,2,3,4,5 \rbrace.
Thus, one can obtain a set containing both 1 and M with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is 2.
Sample Input 2
1 2 2 1 2
Sample Output 2
0
S_1 already contains both 1 and M, so the minimum number of operations required is 0.
Sample Input 3
3 5 2 1 3 2 2 4 3 2 4 5
Sample Output 3
-1
Sample Input 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
Sample Output 4
2