Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
左右に広がった長さ L の道路があります。この道路上に N 体のゾンビがおり、 i 体目は道路の左端から距離 A_i の地点にいます。 ただし、 L および A_i はすべて偶数です。
あなたはゾンビの餌を K 個持っており、好きな時刻に、道路の両端を含む道路上の好きな位置に餌を置くことができます。 ただし、道路上に同時に 2 つ以上の餌が存在する時刻があってはいけません。 道路上に餌が存在するとき、各ゾンビは餌に向かって単位時間あたり 1 の速さで進みます。ある餌と同じ位置に 1 体以上のゾンビが辿り着いたとき、その餌は食べられて瞬時に消滅します。
道路上に餌が存在しないとき、ゾンビは移動しません。 道路上に餌を置く時刻と位置を適切に定めたときの、道路上に餌が存在する時間の総和の最大値を求めてください。 ただし、答えが整数になることが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。
制約
- 1 \le T \le 10^5
- 1 \le N \le 2 \times 10^5
- 1 \le K \le 10^9
- 2 \le L \le 10^9
- 0 \le A_i \le L
- L は偶数
- A_i はすべて偶数
- 全てのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K L A_1 A_2 \ldots A_N
出力
T 行出力せよ。
i 行目には i 番目のテストケースについて、道路上に餌が存在する時間の総和の最大値を出力せよ。
入力例 1
3 2 2 20 4 18 8 9 14 0 2 4 6 8 10 12 14 3 3 140 120 70 20
出力例 1
18 40 160
1 つ目のテストケースについては、以下のように餌を置くのが最適です。
- まず、位置 11 に餌を置く。2 体のゾンビは餌を置いてから 7 単位時間後に同時に位置 11 に到達し、餌を食べる。
- その後、位置 0 に餌を置く。2 体のゾンビは餌を置いてから 11 単位時間後に同時に位置 0 に到達し、餌を食べる。
道路上に餌が存在する時間の総和は 7 + 11 = 18 となります。
Score : 500 points
Problem Statement
There is a road of length L extending left and right. There are N zombies on this road, and the i-th zombie is at a distance of A_i from the left end of the road. Here, L and all A_i are even.
You have K pieces of bait for the zombies, and you can place a piece of bait at any position on the road, including both ends, at any time. However, there must never be a moment when two or more pieces of bait exist on the road simultaneously. When bait exists on the road, each zombie moves toward the bait at a speed of 1 per unit time. When one or more zombies reach the same position as the bait, the bait is eaten and disappears instantly.
When no bait exists on the road, zombies do not move. Find the maximum total amount of time bait exists on the road when the times and positions to place the bait are chosen optimally. It can be proved that the answer is an integer.
You are given T test cases; solve each one.
Constraints
- 1 \le T \le 10^5
- 1 \le N \le 2 \times 10^5
- 1 \le K \le 10^9
- 2 \le L \le 10^9
- 0 \le A_i \le L
- L is even.
- All A_i are even.
- 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:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K L A_1 A_2 \ldots A_N
Output
Output T lines.
The i-th line should contain the maximum total amount of time bait exists on the road for the i-th test case.
Sample Input 1
3 2 2 20 4 18 8 9 14 0 2 4 6 8 10 12 14 3 3 140 120 70 20
Sample Output 1
18 40 160
For the first test case, it is optimal to place the bait as follows.
- First, place bait at position 11. The two zombies simultaneously reach position 11 after 7 units of time and eat the bait.
- Then, place bait at position 0. The two zombies simultaneously reach position 0 after 11 units of time and eat the bait.
The total amount of time bait exists on the road is 7 + 11 = 18.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2N 個の宝石が左右一列に並んでおり、左から i 番目の宝石の種類は A_i です。ここで、 A_i は 1 以上 N 以下の整数であり、どの種類の宝石も 2 つずつ存在します。
ここに以下の条件を満たすように仕切りを入れたいです。
- それぞれの仕切りの位置は整数 i (1 \le i \le 2N - 1) により表され、左から i 番目の宝石と i + 1 番目の宝石の間を仕切ることを意味する。
- 同じ位置に 2 つ以上の仕切りは存在しない。
- 仕切りの個数 K は N 以下である。
- 仕切りを K 個入れることにより、宝石の列は K + 1 個の区間に分かれる。このとき、左から奇数番目の区間にある宝石をすべて集めると、種類 1,2,\ldots,N の宝石がちょうど 1 つずつ得られる。
この条件を満たすように仕切りを入れる方法を 1 つ出力してください。ただし、このような方法が必ず存在することが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。
制約
- 1 \le T \le 10^5
- 1 \le N \le 2 \times 10^5
- 1 \le A_i \le N (1 \le i \leq 2N)
- どの種類の宝石も 2 つずつ存在する、つまり各 x (1 \le x \le N) について、A_i = x なる i はちょうど 2 つ
- 全てのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N
A_1 A_2 \ldots A_{2N}
出力
T 個のテストケースについて順に出力せよ。
各テストケースについて、条件を満たす仕切りの入れ方を出力せよ。 具体的には、仕切りの個数を K、仕切りを入れる位置を昇順に C_1,C_2,\ldots,C_K として、以下の形式で出力せよ。
K C_1 C_2 \ldots C_K
ここで K は N 以下である必要がある。
入力例 1
2 3 1 2 2 3 3 1 5 1 2 3 4 5 5 4 3 2 1
出力例 1
3 1 2 4 1 5
1 つ目のテストケースについては、位置 1,2,4 で仕切ることによって、宝石は (1),(2),(2,3),(3,1) のように区間に分かれ、左から奇数番目の区間にある宝石をすべて集めると、種類 1,2,3 の宝石がちょうど 1 つずつ得られます。
Score : 500 points
Problem Statement
There are 2N gems arranged in a row from left to right, and the type of the i-th gem from the left is A_i. Here, A_i is an integer between 1 and N, inclusive, and there are exactly two gems of each type.
You want to insert dividers satisfying the following conditions.
- Each divider's position is represented by an integer i (1 \le i \le 2N - 1), meaning it divides between the i-th and (i+1)-th gems from the left.
- No two or more dividers exist at the same position.
- The number of dividers K is at most N.
- Inserting K dividers divides the row of gems into K + 1 segments. Here, collecting all gems in the odd-numbered (first, third, ...) segments from the left yields exactly one gem of each type 1, 2, \ldots, N.
Output one way to insert dividers satisfying this condition. It can be proved that such a way always exists.
You are given T test cases; solve each one.
Constraints
- 1 \le T \le 10^5
- 1 \le N \le 2 \times 10^5
- 1 \le A_i \le N (1 \le i \leq 2N)
- There are exactly two gems of each type. That is, for each x (1 \le x \le N), there are exactly two indices i with A_i = x.
- 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:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N
A_1 A_2 \ldots A_{2N}
Output
Output for the T test cases in order.
For each test case, output a way to insert dividers satisfying the condition. Specifically, let K be the number of dividers and C_1, C_2, \ldots, C_K be the positions of the dividers in ascending order, and output in the following format:
K C_1 C_2 \ldots C_K
Here, K must be at most N.
Sample Input 1
2 3 1 2 2 3 3 1 5 1 2 3 4 5 5 4 3 2 1
Sample Output 1
3 1 2 4 1 5
For the first test case, inserting dividers at positions 1, 2, 4 divides the gems into segments (1),(2),(2,3),(3,1), and collecting all gems in the odd-numbered segments from the left yields exactly one gem of each type 1, 2, 3.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
異なる名字を持つ N 人の人がおり、i 番目の人の名字はパラメータ (X_i,Y_i,Z_i) を持っています。異なる名字が同一のパラメータを持つこともあります。 今から N - 1 回、以下のことが起こります。
- ある二人が親となり、その子供が一人出現する。親の二人は亡くなる。子供は、両親の名字のうち強い方を選んで自分の名字とする。パラメータ (x_1,y_1,z_1) を持つ名字がパラメータ (x_2,y_2,z_2) を持つ名字よりも強いとは、x_1 > x_2 かつ y_1 > y_2 かつ z_1 > z_2 を満たすことである。親の名字に強弱が付けられないとき、子供は名字を両親の名字からランダムに選ぶ。
N 人のうち、その人の名字が最後の一人の名字となりうる人の数を求めて下さい。
T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。
制約
- 1 \le T \le 2 \times 10^5
- 2 \le N \le 2 \times 10^5
- 1 \le X_i, Y_i, Z_i \le N
- 全てのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
出力
T 行出力せよ。
i 行目には、 i 番目のテストケースについて、その人の名字が最後の一人の名字となりうる人の数を出力せよ。
入力例 1
3 4 3 4 4 2 2 2 4 3 3 1 1 1 3 2 1 1 1 2 1 1 1 2 3 2 2 2 2 2 2 1 1 1
出力例 1
2 3 2
1 つ目のテストケースについては、例えば以下のようにイベントが起こることが考えられます。
i 番目の人の名字を名字 i と呼ぶことにする。
- 名字 2 と名字 4 の人が親になる。名字 2 の方が強いため、子は名字 2 を持つ。
- 名字 2 と名字 3 の人が親になる。名字 3 の方が強いため、子は名字 3 を持つ。
- 名字 1 と名字 3 の人が親になる。名字 1 と名字 3 では強弱が付けられないため、子は名字 1 と 3 のいずれかを選ぶ。
名字 2 や名字 4 が最後の一人の名字になることはないため、答えは 2 になります。
Score : 600 points
Problem Statement
There are N people with distinct surnames, and the surname of the i-th person has parameters (X_i, Y_i, Z_i). Different surnames may have identical parameters. The following event occurs N - 1 times.
- Two people become parents and one child appears. The two parents die. The child chooses the stronger of the two parents' surnames as their own. A surname with parameters (x_1, y_1, z_1) is stronger than a surname with parameters (x_2, y_2, z_2) if x_1 > x_2, y_1 > y_2, and z_1 > z_2 all hold. If neither parent's surname is stronger than the other's, the child randomly chooses one of the two parents' surnames.
Find the number of people among the N people whose surname can be the surname of the last remaining person.
You are given T test cases; solve each one.
Constraints
- 1 \le T \le 2 \times 10^5
- 2 \le N \le 2 \times 10^5
- 1 \le X_i, Y_i, Z_i \le N
- 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:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
Output
Output T lines.
The i-th line should contain the number of people whose surname can be the surname of the last remaining person for the i-th test case.
Sample Input 1
3 4 3 4 4 2 2 2 4 3 3 1 1 1 3 2 1 1 1 2 1 1 1 2 3 2 2 2 2 2 2 1 1 1
Sample Output 1
2 3 2
For the first test case, below is a possible sequence of events.
Let surname i denote the surname of the i-th person.
- The people with surnames 2 and 4 become parents. Surname 2 is stronger, so the child has surname 2.
- The people with surnames 2 and 3 become parents. Surname 3 is stronger, so the child has surname 3.
- The people with surnames 1 and 3 become parents. Neither surname 1 nor surname 3 is stronger than the other, so the child chooses either surname 1 or 3.
Neither surname 2 nor surname 4 can be the surname of the last remaining person, so the answer is 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
長さ N+1 の数列 A = (A_1,A_2, \ldots, A_{N + 1}) に対して、以下のような方法で長さ N の数列 S = (S_1, S_2, \ldots, S_{N}) を得ることを考えます。
- 各 i (1 \le i \le N) について、 S_i=A_i+A_{i + 1} とする。
以下の条件を全て満たす長さ N の数列 S の個数を 10^9 + 7 で割った余りを求めて下さい。
- 広義単調増加である。
- ある 0 以上 M 以下の整数からなる長さ N+1 の数列 A であって、A から前述の方法で S を得ることができるものが存在する。
制約
- 1 \le N, M \le 10^7
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
答えを 1 行に出力せよ。
入力例 1
2 1
出力例 1
5
A として考えられるのは以下の 8 通りです。
- A = (0,0,0) のとき、S = (0,0)
- A = (0,0,1) のとき、S = (0,1)
- A = (0,1,0) のとき、S = (1,1)
- A = (0,1,1) のとき、S = (1,2)
- A = (1,0,0) のとき、S = (1,0)
- A = (1,0,1) のとき、S = (1,1)
- A = (1,1,0) のとき、S = (2,1)
- A = (1,1,1) のとき、S = (2,2)
このうち広義単調増加な S は、(0,0),(0,1),(1,1),(1,2),(2,2) の 5 種類です。
入力例 2
869121 10000000
出力例 2
767557322
Score : 600 points
Problem Statement
For a sequence A = (A_1, A_2, \ldots, A_{N+1}) of length N+1, consider obtaining a sequence S = (S_1, S_2, \ldots, S_N) of length N in the following way.
- For each i (1 \le i \le N), let S_i = A_i + A_{i+1}.
Find the number, modulo 10^9 + 7, of sequences S of length N satisfying all of the following conditions.
- S is non-decreasing.
- There exists a sequence A of length N+1 consisting of integers between 0 and M, inclusive, such that S can be obtained from A in the way described above.
Constraints
- 1 \le N, M \le 10^7
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Output the answer on a single line.
Sample Input 1
2 1
Sample Output 1
5
The possible sequences A are the following eight:
- A = (0,0,0), for which S = (0,0)
- A = (0,0,1), for which S = (0,1)
- A = (0,1,0), for which S = (1,1)
- A = (0,1,1), for which S = (1,2)
- A = (1,0,0), for which S = (1,0)
- A = (1,0,1), for which S = (1,1)
- A = (1,1,0), for which S = (2,1)
- A = (1,1,1), for which S = (2,2)
Among these, we have five distinct non-decreasing S: (0,0),(0,1),(1,1),(1,2),(2,2).
Sample Input 2
869121 10000000
Sample Output 2
767557322
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
長さ N の 0 と 1 からなる文字列 A = A_1 A_2 \ldots A_N および B = B_1 B_2 \ldots B_N が与えられます。
M 種類の操作を行うことができます。i 種類目の操作は 1 以上 N 以下の整数の組 (x_i, y_i) によって表され、 A_{x_i} = 1 のときに A_{y_i} の値を反転させる操作です。x_i = y_i である場合もありえます。ただし、反転とは、0 を 1 に、 1 を 0 に変更する操作です。
A に対して 2N 回以内の操作を行うことによって B と等しい文字列に変換する方法が存在するか判定し、もし存在するなら 1 つ構築してください。
T 個のテストケースが与えられるので、それぞれについて答えを求めて下さい。
制約
- 1 \le T \le 2 \times 10^5
- 1 \le N \le 2 \times 10^5
- 1 \le M \le 2 \times 10^5
- A, B は、0 と 1 からなる長さ N の文字列である。
- A \neq B
- 1 \le x_i \le N (1 \le i \le M)
- 1 \le y_i \le N (1 \le i \le M)
- 全てのテストケースにおける N, M の総和は 2 \times 10^5 以下
- T, N, M, x_i, y_i は整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N A B M x_1 y_1 x_2 y_2 \vdots x_M y_M
出力
T 個のテストケースについて順に出力せよ。
各テストケースについて、以下のように出力せよ。
条件を満たす操作列が存在しない場合、 -1 を一行に出力せよ。
条件を満たす操作列が存在する場合、操作列の長さを K 、i 番目に行う操作の番号を C_i として、
K C_1 C_2 \ldots C_K
のように出力せよ。ここで K は 2N 以下である必要がある。また、A_{x_i} = 0 のときに操作 i を行ってはならない。
入力例 1
3 4 0100 1010 4 1 2 2 1 2 3 4 3 2 10 01 1 1 2 2 01 00 3 1 1 1 2 2 1
出力例 1
3 2 3 1 -1 3 3 2 1
1 つ目のテストケースについては、以下のように操作を行えばよいです。
- はじめ、 A = 0100 である。
- 操作 2 を行う。 A = 1100 となる。
- 操作 3 を行う。 A = 1110 となる。
- 操作 1 を行う。 A = 1010 となる。
2 つ目のテストケースでは、どのように操作を行っても A_1 を 0 にすることはできません。
3 つ目のテストケースのように、x_i = y_i である場合もありえることに注意してください。
Score : 700 points
Problem Statement
You are given binary strings A = A_1 A_2 \ldots A_N and B = B_1 B_2 \ldots B_N of length N.
You can perform M types of operations. The i-th type of operation is represented by a pair of integers (x_i, y_i) with 1 \le x_i, y_i \le N, and it flips the value of A_{y_i} when A_{x_i} = 1. It is possible that x_i = y_i. Here, flipping means changing 0 to 1 or 1 to 0.
Determine whether it is possible to convert A into a string equal to B by performing at most 2N operations on A, and if so, construct one such way.
You are given T test cases; solve each one.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le N \le 2 \times 10^5
- 1 \le M \le 2 \times 10^5
- A and B are binary strings of length N.
- A \neq B
- 1 \le x_i \le N (1 \le i \le M)
- 1 \le y_i \le N (1 \le i \le M)
- The sum of N and M over all test cases is at most 2 \times 10^5.
- T, N, M, x_i, y_i are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N A B M x_1 y_1 x_2 y_2 \vdots x_M y_M
Output
Output for the T test cases in order.
For each test case, output as follows.
If no sequence of operations satisfying the condition exists, output -1 on a single line.
If a sequence of operations satisfying the condition exists, let K be the length of the sequence of operations and C_i be the index of the i-th operation performed, and output as follows:
K C_1 C_2 \ldots C_K
Here, K must be at most 2N, and operation i must not be performed when A_{x_i} = 0.
Sample Input 1
3 4 0100 1010 4 1 2 2 1 2 3 4 3 2 10 01 1 1 2 2 01 00 3 1 1 1 2 2 1
Sample Output 1
3 2 3 1 -1 3 3 2 1
For the first test case, you can perform the operations as follows.
- Initially, A = 0100.
- Perform operation 2. Now, A = 1100.
- Perform operation 3. Now, A = 1110.
- Perform operation 1. Now, A = 1010.
For the second test case, no matter what operations are performed, it is impossible to make A_1 equal to 0.
Note that, as in the third test case, x_i = y_i is possible.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
頂点に 1 から N までの番号が付いた N 頂点の木が与えられます。 i 番目 (1 \le i \le N - 1) の辺は頂点 A_i と頂点 B_i を結んでいます。
この木のパスについて、パスから最も遠い頂点までの距離をスコアとします。 ただし、パスからの距離とは、パスの頂点との距離の最小値を表します。
スコアが最小になるようなパスを良いパスと呼びます。
k = 1,2,\ldots, N それぞれについて、頂点数が k であるような良いパスの個数を求めて下さい。 ただし、パスは頂点集合が異なるときにのみ区別します。
制約
- 2 \le N \le 2 \times 10^5
- 1 \le A_i < B_i \le N (1 \le i \le N - 1)
- 与えられるグラフは木
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
出力
N 行出力せよ。
k 行目には、頂点数が k であるような良いパスの個数を出力せよ。
入力例 1
5 1 2 2 3 2 4 3 5
出力例 1
0 1 3 2 0
スコアの最小値は 1 です。
良いパスは以下の 6 つです。
- 頂点 2 と頂点 3 を結ぶ頂点数 2 のパス
- 頂点 1 と頂点 3 を結ぶ頂点数 3 のパス
- 頂点 2 と頂点 5 を結ぶ頂点数 3 のパス
- 頂点 3 と頂点 4 を結ぶ頂点数 3 のパス
- 頂点 1 と頂点 5 を結ぶ頂点数 4 のパス
- 頂点 4 と頂点 5 を結ぶ頂点数 4 のパス
入力例 2
8 1 2 2 3 2 4 3 5 5 6 3 7 7 8
出力例 2
1 3 7 8 5 0 0 0
入力例 3
5 1 2 2 3 3 4 4 5
出力例 3
0 0 0 0 1
Score : 700 points
Problem Statement
You are given a tree with N vertices numbered 1 to N. The i-th edge (1 \le i \le N - 1) connects vertices A_i and B_i. For a path in this tree, define the score of the path as the distance from the path to the farthest vertex. Here, the distance from a path is defined as the minimum distance to a vertex on the path.
A path with the minimum score is called a good path.
For each k = 1, 2, \ldots, N, find the number of good paths with exactly k vertices. Paths are distinguished only if their vertex sets differ.
Constraints
- 2 \le N \le 2 \times 10^5
- 1 \le A_i < B_i \le N (1 \le i \le N - 1)
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
Output
Output N lines.
The k-th line should contain the number of good paths with exactly k vertices.
Sample Input 1
5 1 2 2 3 2 4 3 5
Sample Output 1
0 1 3 2 0
The minimum score is 1.
The good paths are the following six paths.
- The path with two vertices connecting vertices 2 and 3
- The path with three vertices connecting vertices 1 and 3
- The path with three vertices connecting vertices 2 and 5
- The path with three vertices connecting vertices 3 and 4
- The path with four vertices connecting vertices 1 and 5
- The path with four vertices connecting vertices 4 and 5
Sample Input 2
8 1 2 2 3 2 4 3 5 5 6 3 7 7 8
Sample Output 2
1 3 7 8 5 0 0 0
Sample Input 3
5 1 2 2 3 3 4 4 5
Sample Output 3
0 0 0 0 1