Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、0 と 1 からなる長さ N の文字列 S によって表され、
S の i 文字目が 0 であるとき i 番目の人が子供であることを、1 であるとき i 番目の人が大人であることをさします。
ロボットである高橋君に対して実数 X を設定すると、
高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。
X が実数全体を動くとき、f(X) の最大値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- S は
0と1からなる長さ N の文字列 - 1\leq W_i\leq 10^9
- N,W_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S W_1 W_2 \ldots W_N
出力
f(X) の最大値を整数で一行に出力せよ。
入力例 1
5 10101 60 45 30 40 80
出力例 1
4
X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。
よって、f(50)=4 です。
5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。
入力例 2
3 000 1 2 3
出力例 2
3
例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。
入力例 3
5 10101 60 50 50 50 60
出力例 3
4
例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。
Score : 300 points
Problem Statement
There are N people, each of whom is either a child or an adult. The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1.
If the i-th character of S is 0, then the i-th person is a child; if it is 1, then the i-th person is an adult.
When Takahashi the robot is given a real number X,
Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.
Find the maximum value of f(X) for all real values of X.
Constraints
- 1\leq N\leq 2\times 10^5
- S is a string of length N consisting of
0and1. - 1\leq W_i\leq 10^9
- N and W_i are integers.
Input
Input is given from Standard Input in the following format:
N S W_1 W_2 \ldots W_N
Output
Print the maximum value of f(X) as an integer in a single line.
Sample Input 1
5 10101 60 45 30 40 80
Sample Output 1
4
When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged.
Thus, f(50)=4.
This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.
Sample Input 2
3 000 1 2 3
Sample Output 2
3
For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.
Sample Input 3
5 10101 60 50 50 50 60
Sample Output 3
4
For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.
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 つに分ける。 これによって A を 9 つの 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 は次のようになっています。

マス目 A は 3 つの条件をすべてみたしているため、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.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N)、および正整数 K が与えられます。
i=K,K+1,\ldots,N について、以下を求めてください。
- P の先頭 i 項のうち、K 番目に大きい値
制約
- 1 \leq K \leq N \leq 5 \times 10^5
- (P_1,P_2,\ldots,P_N) は (1,2,\ldots,N) の並び替えによって得られる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \ldots P_N
出力
i=K,K+1,\ldots,N についてこの順に、問題文中で問われている値を改行区切りで出力せよ。
入力例 1
3 2 1 2 3
出力例 1
1 2
- P の先頭 2 項、すなわち (P_1,P_2)=(1,2) の中で K=2 番目に大きい値は 1 となります。
- P の先頭 3 項、すなわち (P_1,P_2,P_3)=(1,2,3) の中で K=2 番目に大きい値は 2 となります。
入力例 2
11 5 3 7 2 5 11 6 1 9 8 10 4
出力例 2
2 3 3 5 6 7 7
Score : 400 points
Problem Statement
Given are a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) and a positive integer K.
For each i=K,K+1,\ldots,N, find the following.
- The K-th greatest value among the first i terms of P.
Constraints
- 1 \leq K \leq N \leq 5 \times 10^5
- (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K P_1 P_2 \ldots P_N
Output
For each i=K, K+1, \ldots, N, in this order, print the specified value in Problem Statement, separated by newlines.
Sample Input 1
3 2 1 2 3
Sample Output 1
1 2
- The (K=) 2-nd greatest value among the first 2 terms of P, (P_1,P_2)=(1,2), is 1.
- The (K=) 2-nd greatest value among the first 3 terms of P, (P_1,P_2,P_3)=(1,2,3), is 2.
Sample Input 2
11 5 3 7 2 5 11 6 1 9 8 10 4
Sample Output 2
2 3 3 5 6 7 7
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
高橋君ははじめ高橋君の家におり、これから青木君の家に遊びに行きます。
2 人の家の間には 1 から N までの番号がつけられた N 個のバス停があり、高橋君はそれらの間を下記の方法で移動できます。
- 高橋君の家からバス停 1 まで X だけの時間をかけて徒歩で移動できます。
- 各 i = 1, 2, \ldots, N-1 について、バス停 i からは P_i の倍数である時刻それぞれにバスが出発し、そのバスに乗ることで T_i だけの時間をかけてバス停 (i+1) に移動できます。ここで、1 \leq P_i \leq 8 が制約として保証されます。
- バス停 N から青木君の家まで、Y だけの時間をかけて徒歩で移動できます。
各 i = 1, 2, \ldots, Q に対して下記のクエリを処理してください。
高橋君が高橋君の家を時刻 q_i に出発するときの、高橋君が青木君の家に到着する時刻としてあり得る最も早いものを求めよ。
なお、バスの出発時刻ちょうどにそのバスが出発するバス停に到着した場合であっても、そのバスに乗ることができます。
制約
- 2 \leq N \leq 10^5
- 1 \leq X, Y \leq 10^9
- 1 \leq P_i \leq 8
- 1 \leq T_i \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq q_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y
P_1 T_1
P_2 T_2
\vdots
P_{N-1} T_{N-1}
Q
q_1
q_2
\vdots
q_Q
出力
Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
4 2 3 5 4 6 6 3 1 7 13 0 710511029 136397527 763027379 644706927 447672230
出力例 1
34 22 710511052 136397548 763027402 644706946 447672250
1 番目のクエリについて、高橋君は下記の通りに移動を行って、時刻 34 に青木君の家に到着することができます。
- 時刻 13 に高橋君の家を出発する。
- 高橋君の家から徒歩で移動し、時刻 15 にバス停 1 に到着する。
- 時刻 15 にバス停 1 を出発するバスに乗り、時刻 19 にバス停 2 に到着する。
- 時刻 24 にバス停 2 を出発するバスに乗り、時刻 30 にバス停 3 に到着する。
- 時刻 30 にバス停 3 を出発するバスに乗り、時刻 31 にバス停 4 に到着する。
- バス停 4 から徒歩で移動し、時刻 34 に青木君の家に到着する。
2 番目のクエリについて、高橋君は下記の通りに移動を行って、時刻 22 に青木君の家に到着することができます。
- 時刻 0 に高橋君の家を出発する。
- 高橋君の家から徒歩で移動し、時刻 2 にバス停 1 に到着する。
- 時刻 5 にバス停 1 を出発するバスに乗り、時刻 9 にバス停 2 に到着する。
- 時刻 12 にバス停 2 を出発するバスに乗り、時刻 18 にバス停 3 に到着する。
- 時刻 18 にバス停 3 を出発するバスに乗り、時刻 19 にバス停 4 に到着する。
- バス停 4 から徒歩で移動し、時刻 22 に青木君の家に到着する。
Score : 450 points
Problem Statement
Takahashi is initially at his house and is about to visit Aoki's house.
There are N bus stops numbered 1 to N between the two houses, and Takahashi can move between them in the following ways:
- He can walk from his house to bus stop 1 in X units of time.
- For each i = 1, 2, \ldots, N-1, a bus departs from bus stop i at each time that is a multiple of P_i, and by taking this bus, he can get to bus stop (i+1) in T_i units of time. Here, the constraints guarantee that 1 \leq P_i \leq 8.
- Takahashi can walk from bus stop N to Aoki's house in Y units of time.
For each i = 1, 2, \ldots, Q, process the following query.
Find the earliest time that Takahashi can arrive at Aoki's house when he leaves his house at time q_i.
Note that if he arrives at a bus stop exactly at the departure time of a bus, he can take that bus.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq X, Y \leq 10^9
- 1 \leq P_i \leq 8
- 1 \leq T_i \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq q_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y
P_1 T_1
P_2 T_2
\vdots
P_{N-1} T_{N-1}
Q
q_1
q_2
\vdots
q_Q
Output
Print Q lines. For each i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th query.
Sample Input 1
4 2 3 5 4 6 6 3 1 7 13 0 710511029 136397527 763027379 644706927 447672230
Sample Output 1
34 22 710511052 136397548 763027402 644706946 447672250
For the first query, Takahashi can move as follows to arrive at Aoki's house at time 34.
- Leave his house at time 13.
- Walk from his house and arrive at bus stop 1 at time 15.
- Take the bus departing from bus stop 1 at time 15 and arrive at bus stop 2 at time 19.
- Take the bus departing from bus stop 2 at time 24 and arrive at bus stop 3 at time 30.
- Take the bus departing from bus stop 3 at time 30 and arrive at bus stop 4 at time 31.
- Walk from bus stop 4 and arrive at Aoki's house at time 34.
For the second query, Takahashi can move as follows and arrive at Aoki's house at time 22.
- Leave his house at time 0.
- Walk from his house and arrive at bus stop 1 at time 2.
- Take the bus departing from bus stop 1 at time 5 and arrive at bus stop 2 at time 9.
- Take the bus departing from bus stop 2 at time 12 and arrive at bus stop 3 at time 18.
- Take the bus departing from bus stop 3 at time 18 and arrive at bus stop 4 at time 19.
- Walk from bus stop 4 and arrive at Aoki's house at time 22.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の箱 1,2,\dots,N と、 10^{100} 個のボール 1,2,\dots,10^{100} があります。 最初、箱 i にはボール i のみが入っています。
ここに以下の操作が合計 Q 回行われるので、処理してください。
操作にはタイプ 1,2,3 の 3 種類があります。
タイプ 1 : 箱 X に箱 Y の中身を全て入れる。 この操作では X \neq Y が保証される。
1 X Y
タイプ 2 : 現在いずれかの箱に入っているボールの数の合計を k とすると、箱 X にボール k+1 を入れる。
2 X
タイプ 3 : ボール X が入っている箱の番号を答える。
3 X
制約
- 入力は全て整数
- 2 \le N \le 3 \times 10^5
- 1 \le Q \le 3 \times 10^5
- タイプ 1 の操作について、 1 \le X,Y \le N かつ X \neq Y
- タイプ 2 の操作について、 1 \le X \le N
- タイプ 3 の操作について、その時点でボール X がいずれかの箱に入っている
- タイプ 3 の操作が少なくとも 1 つ与えられる
入力
入力は以下の形式で標準入力から与えられる。
但し、 op_i は i 回目の操作を表す。
N Q op_1 op_2 \vdots op_Q
出力
各タイプ 3 の操作に対して、答えを 1 行に 1 つ、整数として出力せよ。
入力例 1
5 10 3 5 1 1 4 2 1 2 4 3 7 1 3 1 3 4 1 1 4 3 7 3 6
出力例 1
5 4 3 1 3
この入力は 10 個の操作を含みます。
- 1 回目の操作はタイプ 3 です。ボール 5 は箱 5 に入っています。
- 2 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
- 箱 1 の中身はボール 1,4 、箱 4 の中身は空になります。
- 3 回目の操作はタイプ 2 です。箱 1 にボール 6 を入れます。
- 4 回目の操作はタイプ 2 です。箱 4 にボール 7 を入れます。
- 5 回目の操作はタイプ 3 です。ボール 7 は箱 4 に入っています。
- 6 回目の操作はタイプ 1 です。箱 3 に箱 1 の中身を全て入れます。
- 箱 3 の中身はボール 1,3,4,6 、箱 1 の中身は空になります。
- 7 回目の操作はタイプ 3 です。ボール 4 は箱 3 に入っています。
- 8 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
- 箱 1 の中身はボール 7 、箱 4 の中身は空になります。
- 9 回目の操作はタイプ 3 です。ボール 7 は箱 1 に入っています。
- 10 回目の操作はタイプ 3 です。ボール 6 は箱 3 に入っています。
Score : 500 points
Problem Statement
There are N boxes numbered 1,2,\ldots,N, and 10^{100} balls numbered 1,2,\dots,10^{100}. Initially, box i contains just ball i.
Process a total of Q operations that will be performed.
There are three types of operations: 1, 2, and 3.
Type 1: Put all contents of box Y into box X. It is guaranteed that X \neq Y.
1 X Y
Type 2: Put ball k+1 into box X, where k is the current total number of balls contained in the boxes.
2 X
Type 3: Report the number of the box that contains ball X.
3 X
Constraints
- All values in the input are integers.
- 2 \le N \le 3 \times 10^5
- 1 \le Q \le 3 \times 10^5
- For each type-1 operation, 1 \le X,Y \le N and X \neq Y.
- For each type-2 operation, 1 \le X \le N.
- For each type-3 operation, ball X is contained in some box at that point.
- There is at least one type-3 operation.
Input
The input is given from Standard Input in the following format.
Here, op_i represents the i-th operation.
N Q op_1 op_2 \vdots op_Q
Output
For each type-3 operation, print a line containing the response as an integer.
Sample Input 1
5 10 3 5 1 1 4 2 1 2 4 3 7 1 3 1 3 4 1 1 4 3 7 3 6
Sample Output 1
5 4 3 1 3
This input contains ten operations.
- The first operation is of type 3. Ball 5 is in box 5.
- The second operation is of type 1. Put all contents of box 4 into box 1.
- Box 1 now contains balls 1 and 4, and box 4 is now empty.
- The third operation is of type 2. Put ball 6 into box 1.
- The fourth operation is of type 2. Put ball 7 into box 4.
- The fifth operation is of type 3. Ball 7 is in box 4.
- The sixth operation is of type 1. Put all contents of box 1 into box 3.
- Box 3 now contains balls 1, 3, 4, and 6, and box 1 is now empty.
- The seventh operation is of type 3. Ball 4 is in box 3.
- The eighth operation is of type 1. Put all contents of box 4 into box 1.
- Box 1 now contains ball 7, and box 4 is now empty.
- The ninth operation is of type 3. Ball 7 is in box 1.
- The tenth operation is of type 3. Ball 6 is in box 3.