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
配点 : 300 点
問題文
以下の条件を満たす正整数 x を 321-like Number と呼びます。 この定義は A 問題と同様です。
- x の各桁を上から見ると狭義単調減少になっている。
- すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
- ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )
なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。
例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。
K 番目に小さい 321-like Number を求めてください。
制約
- 入力は全て整数
- 1 \le K
- 321-like Number は K 個以上存在する
入力
入力は以下の形式で標準入力から与えられる。
K
出力
K 番目に小さい 321-like Number を整数として出力せよ。
入力例 1
15
出力例 1
32
321-like Number は小さいものから順に (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) です。
このうち 15 番目に小さいものは 32 です。
入力例 2
321
出力例 2
9610
入力例 3
777
出力例 3
983210
Score : 300 points
Problem Statement
A positive integer x is called a 321-like Number when it satisfies the following condition. This definition is the same as the one in Problem A.
- The digits of x are strictly decreasing from top to bottom.
- In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
- (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).
Note that all one-digit positive integers are 321-like Numbers.
For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.
Find the K-th smallest 321-like Number.
Constraints
- All input values are integers.
- 1 \le K
- At least K 321-like Numbers exist.
Input
The input is given from Standard Input in the following format:
K
Output
Print the K-th smallest 321-like Number as an integer.
Sample Input 1
15
Sample Output 1
32
The 321-like Numbers are (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) from smallest to largest.
The 15-th smallest of them is 32.
Sample Input 2
321
Sample Output 2
9610
Sample Input 3
777
Sample Output 3
983210
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
高橋くんは睡眠記録をつけています。 睡眠記録は奇数長の数列 A=(A _ 1(=0), A _ 2,\ldots,A _ N) で表され、奇数番目は起床時刻を、偶数番目は就寝時刻を表しています。 より厳密には、睡眠記録をつけている間に高橋くんは次のような睡眠をとりました。
- すべての 1\leq i\leq\dfrac{N-1}2 を満たす整数 i について、睡眠記録をつけ始めてから A _ {2i} 分後ちょうどに寝て、A _ {2i+1} 分後ちょうどに起きた。
- それ以外の時間に寝ることも起きることもなかった。
次の Q 個の質問に答えてください。 i 番目の質問では、0\leq l _ i\leq r _ i\leq A _ N を満たす整数の組 (l _ i,r _ i) が与えられます。
- 睡眠記録をつけ始めてから l _ i 分後ちょうどから r _ i 分後ちょうどまでの r _ i-l _ i 分のうち、高橋くんが寝ていたのは何分間ですか?
制約
- 3\leq N\lt2\times10^5
- N は奇数
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
出力
答えを Q 行で出力せよ。 i 行目には i 番目の質問の答えを整数として出力せよ。
入力例 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
出力例 1
480 0 960
高橋くんは、以下の図のように睡眠をとりました。

それぞれの質問の答えは以下のようになります。
- 睡眠記録をつけ始めてから 480 分後から 1920 分後の間、高橋くんは 480 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 1920 分後の 3 つの睡眠をとりました。睡眠時間の合計は 240+120+120=480 分です。
- 睡眠記録をつけ始めてから 720 分後から 1200 分後の間、高橋くんは睡眠をとりませんでした。睡眠時間の合計は 0 分です。
- 睡眠記録をつけ始めてから 0 分後から 2160 分後の間、高橋くんは 240 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 2160 分後の 3 つの睡眠をとりました。睡眠時間の合計は 480+120+360=960 分です。
よって、それぞれの行に 480,0,960 と出力してください。
入力例 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
出力例 2
296 150 150 49 89 20 279 183 61 177
Score : 450 points
Problem Statement
Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A _ 1(=0), A _ 2,\ldots,A _ N), where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log.
- For every integer i such that 1\leq i\leq\dfrac{N-1}2, he fell asleep exactly A _ {2i} minutes after starting the sleep log and woke up exactly A _ {2i+1} minutes after starting the sleep log.
- He did not fall asleep or wake up at any other time.
Answer the following Q questions. For the i-th question, you are given a pair of integers (l _ i,r _ i) such that 0\leq l _ i\leq r _ i\leq A _ N.
- What is the total number of minutes for which Takahashi was asleep during the r _ i-l _ i minutes from exactly l _ i minutes to r _ i minutes after starting the sleep log?
Constraints
- 3\leq N\lt2\times10^5
- N is odd.
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 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 Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
Output
Print the answer in Q lines. The i-th line should contain an integer answering to the i-th question.
Sample Input 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
Sample Output 1
480 0 960
Takahashi slept as shown in the following figure.

The answers to each question are as follows.
- Between 480 minutes and 1920 minutes after starting the sleep log, Takahashi slept from 480 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 1920 minutes in 3 sleep sessions. The total sleep time is 240+120+120=480 minutes.
- Between 720 minutes and 1200 minutes after starting the sleep log, Takahashi did not sleep. The total sleep time is 0 minutes.
- Between 0 minutes and 2160 minutes after starting the sleep log, Takahashi slept from 240 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 2160 minutes in 3 sleep sessions. The total sleep time is 480+120+360=960 minutes.
Therefore, the three lines of the output should contain 480, 0, and 960.
Sample Input 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
Sample Output 2
296 150 150 49 89 20 279 183 61 177
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
整数 N,M と長さ N の整数列 X=(X_1,X_2,\ldots,X_N) 、長さ M の整数列 Y=(Y_1,Y_2,\ldots,Y_M) が与えられます。
以下の条件を全て満たす N 行 M 列の整数行列 A=(A_{i,j}) (1\le i\le N,\ 1\le j\le M) が存在するか判定し、存在する場合は一つ求めてください。
- 1\le A_{i,j} \le N\times M
- A_{i,j} の N\times M 個の要素は相異なる
- i=1,2,\ldots,N に対し \displaystyle \max_{1\le j\le M} A_{i,j} = X_i が成り立つ
- j=1,2,\ldots,M に対し \displaystyle \max_{1\le i\le N} A_{i,j} = Y_j が成り立つ
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 10^5
- 1\le N,M
- 全てのテストケースにおける N\times M の総和は 2\times 10^5 以下
- 1\le X_i,Y_j\le N\times M
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N M X_1 X_2 \ldots X_N Y_1 Y_2 \ldots Y_M
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、条件を全て満たす A が存在しない場合は No を出力せよ。
そうでない場合、条件を全て満たす A を以下の形式で出力せよ。
Yes
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
条件を満たす A が複数存在する場合、どれを出力しても正答となる。
入力例 1
3 2 3 5 6 5 3 6 3 3 5 4 6 6 2 4 5 4 18 20 19 14 17 18 20 14 15
出力例 1
Yes 5 1 4 2 3 6 No Yes 18 12 4 9 13 20 1 10 16 19 6 8 2 5 14 3 11 17 7 15
1 つ目のテストケースについて考えます。
出力例の A の要素は全て 1 以上 6 以下で相異なり、さらに
- \displaystyle \max_{1\le j\le 3} A_{1,j} =\max \lbrace 5,1,4\rbrace = 5 = X_1
- \displaystyle\max_{1\le j\le 3} A_{2,j} =\max \lbrace 2,3,6\rbrace = 6 = X_2
- \displaystyle\max_{1\le i\le 2} A_{i,1} =\max \lbrace 5,2\rbrace = 5 = Y_1
- \displaystyle\max_{1\le i\le 2} A_{i,2} =\max \lbrace 1,3\rbrace = 3 = Y_2
- \displaystyle\max_{1\le i\le 2} A_{i,3} =\max \lbrace 4,6\rbrace = 6 = Y_3
より全ての条件を満たしていることが分かります。
そのほかにも、例えば以下のような出力も正答となります。
Yes 5 3 1 4 2 6
Score : 450 points
Problem Statement
You are given integers N,M, a sequence of N integers X=(X_1,X_2,\ldots,X_N), and a sequence of M integers Y=(Y_1,Y_2,\ldots,Y_M).
Determine whether there exists an N row by M column integer matrix A=(A_{i,j}) (1\le i\le N,\ 1\le j\le M) that satisfies all of the following conditions, and if so, find one such matrix.
- 1\le A_{i,j} \le N\times M
- All N\times M elements of A_{i,j} are distinct.
- \displaystyle \max_{1\le j\le M} A_{i,j} = X_i for i=1,2,\ldots,N.
- \displaystyle \max_{1\le i\le N} A_{i,j} = Y_j for j=1,2,\ldots,M.
You are given T test cases; solve each of them.
Constraints
- 1\le T\le 10^5
- 1\le N,M
- The sum of N\times M over all test cases is at most 2\times 10^5.
- 1\le X_i,Y_j\le N\times M
- 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 M X_1 X_2 \ldots X_N Y_1 Y_2 \ldots Y_M
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, if there is no A that satisfies all the conditions, output No.
Otherwise, output A that satisfies all the conditions in the following format:
Yes
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
If there are multiple A that satisfy the conditions, any of them will be accepted.
Sample Input 1
3 2 3 5 6 5 3 6 3 3 5 4 6 6 2 4 5 4 18 20 19 14 17 18 20 14 15
Sample Output 1
Yes 5 1 4 2 3 6 No Yes 18 12 4 9 13 20 1 10 16 19 6 8 2 5 14 3 11 17 7 15
Consider the first test case.
In the sample output, all elements of A are between 1 and 6 and are distinct, and furthermore,
- \displaystyle \max_{1\le j\le 3} A_{1,j} =\max \lbrace 5,1,4\rbrace = 5 = X_1
- \displaystyle\max_{1\le j\le 3} A_{2,j} =\max \lbrace 2,3,6\rbrace = 6 = X_2
- \displaystyle\max_{1\le i\le 2} A_{i,1} =\max \lbrace 5,2\rbrace = 5 = Y_1
- \displaystyle\max_{1\le i\le 2} A_{i,2} =\max \lbrace 1,3\rbrace = 3 = Y_2
- \displaystyle\max_{1\le i\le 2} A_{i,3} =\max \lbrace 4,6\rbrace = 6 = Y_3
so all conditions are satisfied.
Other outputs, such as the following, are also accepted.
Yes 5 3 1 4 2 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 人の人が一列に並んでおり、人 i は先頭から i 番目に並んでいます。
以下の操作を、列に並んでいる人が 1 人になるまで繰り返します。
- 先頭に並んでいる人を \frac{1}{2} の確率で列から取り除き、そうでない場合は列の末尾に移す。
人 i=1,2,\ldots,N それぞれについて、人 i が最後まで列に並んでいる 1 人になる確率を \text{mod }998244353 で求めて下さい。(取り除くかどうかの選択はすべてランダムかつ独立です。)
確率 \text{mod } 998244353 の定義
この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。
このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。
制約
- 2\leq N\leq 3000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
人 i=1,2,\ldots,N に対する答えを空白区切りで出力せよ。
入力例 1
2
出力例 1
332748118 665496236
人 1 が最後まで列に並んでいる 1 人になる確率は \frac{1}{3} です。
人 2 が最後まで列に並んでいる 1 人になる確率は \frac{2}{3} です。
入力例 2
5
出力例 2
235530465 792768557 258531487 238597268 471060930
Score : 550 points
Problem Statement
There are N people standing in a line, with person i standing at the i-th position from the front.
Repeat the following operation until there is only one person left in the line:
- Remove the person at the front of the line with a probability of \frac{1}{2}, otherwise move them to the end of the line.
For each person i=1,2,\ldots,N, find the probability that person i is the last person remaining in the line, modulo 998244353. (All choices to remove or not are random and independent.)
How to find probabilities modulo 998244353
The probabilities sought in this problem can be proven to always be a rational number. Furthermore, the constraints of this problem guarantee that if the sought probability is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.
Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.
Constraints
- 2\leq N\leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer for people i=1,2,\ldots,N, separated by spaces.
Sample Input 1
2
Sample Output 1
332748118 665496236
Person 1 will be the last person remaining in the line with probability \frac{1}{3}.
Person 2 will be the last person remaining in the line with probability \frac{2}{3}.
Sample Input 2
5
Sample Output 2
235530465 792768557 258531487 238597268 471060930