Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 個の箱が横一列に並んでおり、そのうちのいくつかの箱にはクッキーが入っています。
各箱の状態は長さ N の文字列 S によって表されます。
具体的には、左から i\ (1\leq i\leq N) 番目の箱は、S の i 文字目が @
のときクッキーが 1 枚入っており、.
のとき空き箱です。
高橋君は今からの D 日間、一日一回ずつ、これらの箱のいずれかに入ったクッキーを 1 枚選んで食べます。
N 個の箱のうち、D 日間が経過した後に空き箱であるものはいくつあるか求めてください。 (この値は高橋君が各日でどのクッキーを選ぶかによらないことが証明できます。)
なお、S には @
が D 個以上含まれることが保証されます。
制約
- 1\leq D \leq N \leq 100
- N,D は整数
- S は
@
と.
からなる長さ N の文字列 - S には
@
が D 個以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
N D S
出力
N 個の箱のうち、D 日間が経過した後に空き箱であるものの個数を出力せよ。
入力例 1
5 2 .@@.@
出力例 1
4
例えば、高橋君は以下のように行動する可能性があります。
- 1 日目:左から 2,3,5 番目の箱にクッキーが入っている。左から 2 番目の箱に入っているクッキーを選んで食べる。
- 2 日目:左から 3,5 番目の箱にクッキーが入っている。左から 5 番目の箱に入っているクッキーを選んで食べる。
- 2 日間が経過した後、左から 3 番目の箱にのみクッキーが入っている。よって、5 個の箱のうち 4 箱が空き箱である。
高橋君が各日で選ぶクッキーはこの例とは異なる可能性もありますが、いずれにせよ 2 日間が経過した後には 4 箱が空き箱です。 よって答えは 4 です。
入力例 2
3 3 @@@
出力例 2
3
入力例 3
10 4 @@@.@@.@@.
出力例 3
7
Score: 100 points
Problem Statement
There are N boxes arranged in a row, and some of these boxes contain cookies.
The state of these boxes is represented by a string S of length N.
Specifically, the i-th box (1\leq i \leq N) from the left contains one cookie if the i-th character of S is @
, and is empty if it is .
.
Over the next D days, Takahashi will choose and eat one cookie per day from among the cookies in these boxes.
Determine how many of the N boxes will be empty after D days have passed. (It can be proved that this value does not depend on which cookies Takahashi chooses each day.)
It is guaranteed that S contains at least D occurrences of @
.
Constraints
- 1 \leq D \leq N \leq 100
- N and D are integers.
- S is a string of length N consisting of
@
and.
. - S contains at least D occurrences of
@
.
Input
The input is given from Standard Input in the following format:
N D S
Output
Print the number of boxes that will be empty after D days have passed among the N boxes.
Sample Input 1
5 2 .@@.@
Sample Output 1
4
For example, Takahashi might act as follows:
- Day 1: There are cookies in the 2nd, 3rd, and 5th boxes from the left. He chooses the cookie in the 2nd box to eat.
- Day 2: There are cookies in the 3rd and 5th boxes. He chooses the cookie in the 5th box to eat.
- After two days have passed, only the 3rd box from the left contains a cookie. Therefore, four out of the five boxes are empty.
Even though Takahashi might choose differently on each day than in this example, there will still be four empty boxes after two days. Therefore, the answer is 4.
Sample Input 2
3 3 @@@
Sample Output 2
3
Sample Input 3
10 4 @@@.@@.@@.
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
あるプログラミングコンテストでは、以下のルールに従って参加者に T シャツをプレゼントします。
- 上位 A 位までの参加者は、必ず T シャツが貰える。
- 加えて、上位 A+1 位から B 位までの参加者のうち C 人が一様ランダムに選ばれ、選ばれた参加者は T シャツを貰える。
コンテストには 1000 人が参加し、全ての参加者が相異なる順位を取りました。
このコンテストの参加者であるいろはちゃんは、X 位を取りました。
このとき、いろはちゃんが T シャツを貰える確率を求めてください。
制約
- 入力はすべて整数
- 1 \le A < B \le 1000
- 1 \le C \le B-A
- 1 \le X \le 1000
入力
入力は以下の形式で標準入力から与えられる。
A B C X
出力
答えを出力せよ。 なお、想定解との絶対誤差または相対誤差が 10^{−6} 以下であれば、正解として扱われる。
入力例 1
30 500 20 103
出力例 1
0.042553191489
いろはちゃんは 103 位を取りました。
31 位から 500 位までの 470 人の参加者の中から 20 人が一様ランダムに選ばれ、ここで選ばれるといろはちゃんは T シャツを貰えます。この確率は \frac{20}{470}=0.04255319\dots です。
入力例 2
50 500 100 1
出力例 2
1.000000000000
いろはちゃんは 1 位を取りました。この入力において、いろはちゃんは確実に T シャツを貰えます。
入力例 3
1 2 1 1000
出力例 3
0.000000000000
いろはちゃんは 1000 位を取りました。この入力において、いろはちゃんが T シャツを貰えることはありません。
Score : 100 points
Problem Statement
In a certain programming contest, T-shirts are awarded to participants according to the following rules.
- All participants who ranked A-th or higher get a T-shirt.
- Additionally, from the participants who ranked between (A+1)-th and B-th (inclusive), C participants chosen uniformly at random get a T-shirt.
There were 1000 participants in this contest, and all of them got different ranks.
Iroha-chan, who participated in this contest, ranked X-th.
Find the probability that she gets a T-shirt.
Constraints
- All values in input are integers.
- 1 \le A < B \le 1000
- 1 \le C \le B-A
- 1 \le X \le 1000
Input
Input is given from Standard Input in the following format:
A B C X
Output
Print the answer. Your output will be considered correct if the absolute or relative error from the judge's answer is at most 10^{−6}.
Sample Input 1
30 500 20 103
Sample Output 1
0.042553191489
Iroha-chan ranked 103-rd.
She will get a T-shirt if she is among the 20 participants chosen uniformly at random from the 470 participants who ranked between 31-st and 500-th, which happens with probability \frac{20}{470}=0.04255319\dots.
Sample Input 2
50 500 100 1
Sample Output 2
1.000000000000
Iroha-chan ranked 1-st. This time, she is guaranteed to get a T-shirt.
Sample Input 3
1 2 1 1000
Sample Output 3
0.000000000000
Iroha-chan ranked 1000-th. This time, she will never get a T-shirt.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は置き時計を買いました。
この時計は、現在の時刻が 24 時制で \mathrm{AB} 時 \mathrm{CD} 分であるときに図 1 のように時刻を表します。
例えば図 2 では、時計は 7 時 58 分を示しています。
時刻の表示方法をより形式的に説明すると次のようになります。
現在の時刻が 24 時制で h 時 m 分であるとします。ここで 24 時制とは、時間を 0 以上 23 以下の整数で、分を 0 以上 59 以下の整数で表す時刻の表現方法を言います。
h の 10 の位を A, 1 の位を B, m の 10 の位を C, 1 の位を D とします。(ただし h, m が 1 桁である場合は先行ゼロを追加して考えます。)
このとき時計は左上に A を、左下に B を、右上に C を、右下に D を表示します。
高橋君は、次の条件を満たす時刻を 見間違えやすい時刻 と呼ぶことにしました。
- 時計の表示の右上と左下を入れ替えても、それに対応する 24 時制の時刻が存在する。
例えば 図 3 は 20 時 13 分を示していますが、時計の表示の右上と左下を入れ替えると 21 時 3 分を意味する表示になります。よって 20 時 13 分は見間違えやすい時刻です。
今、時計は H 時 M 分を示しています。
(現時点も含めて)以降はじめて訪れる見間違えやすい時刻を 24 時制で答えてください。
制約
- 0 \leq H \leq 23
- 0 \leq M \leq 59
- H, M は整数
入力
入力は以下の形式で標準入力から与えられる。
H M
出力
答えを h 時 m 分とする。ここで h, m は 0 \leq h \leq 23, 0 \leq m \leq 59 である必要がある。
このとき h, m を以下の形式で出力せよ。
h m
なお、h, m を 2 桁に揃えるために先行ゼロをつけた形式で出力しても正答と見なされる。
入力例 1
1 23
出力例 1
1 23
1 時 23 分は見間違えやすい時刻です。なぜならば、時計の表示の右上と左下を入れ替えると 2 時 13 分を意味する表示になるからです。
よって答えは 1 時 23 分です。
なお、01 23
のように先行ゼロをつけた形式で出力しても正答として扱われます。
入力例 2
19 57
出力例 2
20 0
19 時 57 分以降ではじめて訪れる見間違えやすい時刻は 20 時 0 分です。
入力例 3
20 40
出力例 3
21 0
24 時制では 24 時 0 分という表記は合法でないのに注意してください。
Score : 200 points
Problem Statement
Takahashi bought a table clock.
The clock shows the time as shown in Figure 1 at \mathrm{AB}:\mathrm{CD} in the 24-hour system.
For example, the clock in Figure 2 shows 7:58.
The format of the time is formally described as follows.
Suppose that the current time is m minutes past h in the 24-hour system. Here, the 24-hour system represents the hour by an integer between 0 and 23 (inclusive), and the minute by an integer between 0 and 59 (inclusive).
Let A be the tens digit of h, B be the ones digit of h, C be the tens digit of m, and D be the ones digit of m. (Here, if h has only one digit, we consider that it has a leading zero; the same applies to m.)
Then, the clock shows A in its top-left, B in its bottom-left, C in its top-right, and D in its bottom-right.
Takahashi has decided to call a time a confusing time if it satisfies the following condition:
- after swapping the top-right and bottom-left digits on the clock, it still reads a valid time in the 24-hour system.
For example, the clock in Figure 3 shows 20:13. After swapping its top-right and bottom-left digits, it reads 21:03. Thus, 20:13 is a confusing time.
The clock now shows H:M.
Find the next confusing time (including now) in the 24-hour system.
Constraints
- 0 \leq H \leq 23
- 0 \leq M \leq 59
- H and M are integers.
Input
The input is given from Standard Input in the following format:
H M
Output
Let h:m be the answer, where h and m must satisfy 0 \leq h \leq 23 and 0 \leq m \leq 59.
Print h and m in the following format:
h m
Your answer is considered correct even if h contains a leading zero to represent it as a 2-digit integer; the same applies to m.
Sample Input 1
1 23
Sample Output 1
1 23
1:23 is a confusing time because, after swapping its top-right and bottom-left digits on the clock, it reads 2:13.
Thus, the answer is 1:23.
Your answer is considered correct even if you print 01 23
with a leading zero.
Sample Input 2
19 57
Sample Output 2
20 0
The next confusing time after 19:57 is 20:00.
Sample Input 3
20 40
Sample Output 3
21 0
Note that 24:00 is an invalid notation in the 24-hour system.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号がついた N 個の数列が与えられます。
数列 i は、長さが L_i で j (1 \leq j \leq L_i) 番目の要素が a_{i,j} であるような数列です。
数列 i と 数列 j は、 L_i = L_j かつすべての k (1 \leq k \leq L_i) に対して a_{i,k} = a_{j,k} が成り立つ時に同じであるとみなします。
同じ数列は 1 種類として数えるとき、数列 1 から 数列 N の中に全部で何種類の数列がありますか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_i \leq 2 \times 10^5 (1 \leq i \leq N)
- 0 \leq a_{i,j} \leq 10^{9} (1 \leq i \leq N, 1 \leq j \leq L_i)
- すべての数列の要素の個数の和、すなわち \sum_{i=1}^N L_i は 2 \times 10^5 を超えない。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N L_1 a_{1,1} a_{1,2} \dots a_{1,L_1} L_2 a_{2,1} a_{2,2} \dots a_{2,L_2} \vdots L_N a_{N,1} a_{N,2} \dots a_{N,L_N}
出力
数列の種類数を出力せよ。
入力例 1
4 2 1 2 2 1 1 2 2 1 2 1 2
出力例 1
3
入力例 1 で与えられている数列は以下の 4 個です。
- 数列 1 : (1, 2)
- 数列 2 : (1, 1)
- 数列 3 : (2, 1)
- 数列 4 : (1, 2)
このうち数列 1 と数列 4 は同じ数列で、それ以外は互いに異なる数列なので全部で 3 種類の数列があります。
入力例 2
5 1 1 1 1 1 2 2 1 1 3 1 1 1
出力例 2
4
入力例 2 で与えられている数列は以下の 5 個です。
- 数列 1 : (1)
- 数列 2 : (1)
- 数列 3 : (2)
- 数列 4 : (1, 1)
- 数列 5 : (1, 1, 1)
入力例 3
1 1 1
出力例 3
1
Score : 200 points
Problem Statement
You are given N sequences numbered 1 to N.
Sequence i has a length of L_i and its j-th element (1 \leq j \leq L_i) is a_{i,j}.
Sequence i and Sequence j are considered the same when L_i = L_j and a_{i,k} = a_{j,k} for every k (1 \leq k \leq L_i).
How many different sequences are there among Sequence 1 through Sequence N?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_i \leq 2 \times 10^5 (1 \leq i \leq N)
- 0 \leq a_{i,j} \leq 10^{9} (1 \leq i \leq N, 1 \leq j \leq L_i)
- The total number of elements in the sequences, \sum_{i=1}^N L_i, does not exceed 2 \times 10^5.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L_1 a_{1,1} a_{1,2} \dots a_{1,L_1} L_2 a_{2,1} a_{2,2} \dots a_{2,L_2} \vdots L_N a_{N,1} a_{N,2} \dots a_{N,L_N}
Output
Print the number of different sequences.
Sample Input 1
4 2 1 2 2 1 1 2 2 1 2 1 2
Sample Output 1
3
Sample Input 1 contains four sequences:
- Sequence 1 : (1, 2)
- Sequence 2 : (1, 1)
- Sequence 3 : (2, 1)
- Sequence 4 : (1, 2)
Except that Sequence 1 and Sequence 4 are the same, these sequences are pairwise different, so we have three different sequences.
Sample Input 2
5 1 1 1 1 1 2 2 1 1 3 1 1 1
Sample Output 2
4
Sample Input 2 contains five sequences:
- Sequence 1 : (1)
- Sequence 2 : (1)
- Sequence 3 : (2)
- Sequence 4 : (1, 1)
- Sequence 5 : (1, 1, 1)
Sample Input 3
1 1 1
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
AtCoder 王国の 1 週間は A+B 日からなり、1 日目から A 日目が休日で、A+1 日目から A+B 日目が平日です。
高橋くんは N 個の予定があり、i 番目の予定は今日から D_i 日後です。
高橋くんは今日が 1 週間の何日目かを忘れてしまいました。高橋くんの N 個の予定が全て休日である可能性があるかを判定してください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A,B\leq 10^9
- 1\leq D_1<D_2<\ldots<D_N\leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N A B D_1 D_2 \ldots D_N
出力
高橋くんの N 個の予定が全て休日である可能性がある場合は Yes
を、そうでない場合は No
を一行に出力せよ。
入力例 1
3 2 5 1 2 9
出力例 1
Yes
入力では 1 週間は 7 日からなり、1 日目から 2 日目が休日、3 日目から 7 日目が平日です。
今日が 1 週間の 7 日目だとします。このとき、1 日後は 1 週間の 1 日目、2 日後は 1 週間の 2 日目、9 日後は 1 週間の 2 日目となり、全ての予定が休日となります。そのため、高橋くんの N 個の予定が全て休日である可能性があります。
入力例 2
2 5 10 10 15
出力例 2
No
入力例 3
4 347 347 347 700 705 710
出力例 3
Yes
Score: 350 points
Problem Statement
In the Kingdom of AtCoder, a week consists of A+B days, with the first through A-th days being holidays and the (A+1)-th through (A+B)-th being weekdays.
Takahashi has N plans, and the i-th plan is scheduled D_i days later.
He has forgotten what day of the week it is today. Determine if it is possible for all of his N plans to be scheduled on holidays.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A,B\leq 10^9
- 1\leq D_1<D_2<\ldots<D_N\leq 10^9
Input
The input is given from Standard Input in the following format:
N A B D_1 D_2 \ldots D_N
Output
Print Yes
in a single line if it is possible for all of Takahashi's N plans to be scheduled on holidays, and No
otherwise.
Sample Input 1
3 2 5 1 2 9
Sample Output 1
Yes
In this input, a week consists of seven days, with the first through second days being holidays and the third through seventh days being weekdays.
Let us assume today is the seventh day of the week. In this case, one day later would be the first day of the week, two days later would be the second day of the week, and nine days later would also be the second day of the week, making all plans scheduled on holidays. Therefore, it is possible for all of Takahashi's N plans to be scheduled on holidays.
Sample Input 2
2 5 10 10 15
Sample Output 2
No
Sample Input 3
4 347 347 347 700 705 710
Sample Output 3
Yes
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: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
湖の周りに N 個の休憩所があります。
休憩所には時計回りに 1,2,\dots,N の番号が付いています。
休憩所 i から休憩所 i+1 まで時計回りに歩くには A_i 歩かかります ( 但し、休憩所 N+1 は休憩所 1 を指します ) 。
休憩所 s から休憩所 t ( s \neq t ) まで時計回りに歩くのにかかる最小の歩数は M の倍数でした。
(s,t) の組として考えられるものの数を求めてください。
制約
- 入力は全て整数
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 1 \le M \le 10^6
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 3 2 1 4 3
出力例 1
4
- 休憩所 1 から休憩所 2 まで時計回りに歩くのにかかる最小の歩数は 2 歩で、これは 3 の倍数ではありません。
- 休憩所 1 から休憩所 3 まで時計回りに歩くのにかかる最小の歩数は 3 歩で、これは 3 の倍数です。
- 休憩所 1 から休憩所 4 まで時計回りに歩くのにかかる最小の歩数は 7 歩で、これは 3 の倍数ではありません。
- 休憩所 2 から休憩所 3 まで時計回りに歩くのにかかる最小の歩数は 1 歩で、これは 3 の倍数ではありません。
- 休憩所 2 から休憩所 4 まで時計回りに歩くのにかかる最小の歩数は 5 歩で、これは 3 の倍数ではありません。
- 休憩所 2 から休憩所 1 まで時計回りに歩くのにかかる最小の歩数は 8 歩で、これは 3 の倍数ではありません。
- 休憩所 3 から休憩所 4 まで時計回りに歩くのにかかる最小の歩数は 4 歩で、これは 3 の倍数ではありません。
- 休憩所 3 から休憩所 1 まで時計回りに歩くのにかかる最小の歩数は 7 歩で、これは 3 の倍数ではありません。
- 休憩所 3 から休憩所 2 まで時計回りに歩くのにかかる最小の歩数は 9 歩で、これは 3 の倍数です。
- 休憩所 4 から休憩所 1 まで時計回りに歩くのにかかる最小の歩数は 3 歩で、これは 3 の倍数です。
- 休憩所 4 から休憩所 2 まで時計回りに歩くのにかかる最小の歩数は 5 歩で、これは 3 の倍数ではありません。
- 休憩所 4 から休憩所 3 まで時計回りに歩くのにかかる最小の歩数は 6 歩で、これは 3 の倍数です。
以上より、求めるべき (s,t) の組の数は 4 個です。
入力例 2
2 1000000 1 1
出力例 2
0
入力例 3
9 5 9 9 8 2 4 4 3 5 3
出力例 3
11
Score : 400 points
Problem Statement
There are N rest areas around a lake.
The rest areas are numbered 1, 2, ..., N in clockwise order.
It takes A_i steps to walk clockwise from rest area i to rest area i+1 (where rest area N+1 refers to rest area 1).
The minimum number of steps required to walk clockwise from rest area s to rest area t (s \neq t) is a multiple of M.
Find the number of possible pairs (s,t).
Constraints
- All input values are integers
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 1 \le M \le 10^6
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 3 2 1 4 3
Sample Output 1
4
- The minimum number of steps to walk clockwise from rest area 1 to rest area 2 is 2, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 1 to rest area 3 is 3, which is a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 1 to rest area 4 is 7, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 2 to rest area 3 is 1, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 2 to rest area 4 is 5, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 2 to rest area 1 is 8, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 3 to rest area 4 is 4, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 3 to rest area 1 is 7, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 3 to rest area 2 is 9, which is a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 4 to rest area 1 is 3, which is a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 4 to rest area 2 is 5, which is not a multiple of 3.
- The minimum number of steps to walk clockwise from rest area 4 to rest area 3 is 6, which is a multiple of 3.
Therefore, there are four possible pairs (s,t).
Sample Input 2
2 1000000 1 1
Sample Output 2
0
Sample Input 3
9 5 9 9 8 2 4 4 3 5 3
Sample Output 3
11
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder 王国では、N 種類のたこ焼きが売られています。i 種類目のたこ焼きの値段は A_i 円です。
高橋君は、合計で 1 個以上のたこ焼きを買います。このとき、同じたこ焼きを複数個買うことも許されます。
高橋君が支払う金額としてあり得るもののうち、安い方から K 番目の金額を求めてください。ただし、同じ金額を支払う方法が複数存在する場合は 1 回だけ数えます。
制約
- 1 \le N \le 10
- 1 \le K \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 6 20 25 30 100
出力例 1
50
AtCoder 王国で売られている 4 種類のたこ焼きは、それぞれ 20 円、25 円、30 円、100 円です。
高橋君の支払う金額としてあり得るものは、安い方から 6 個を列挙すると 20 円、25 円、30 円、40 円、45 円、50 円となります。よって、答えは 50 円です。
合計で 1 個以上たこ焼きを買う必要があることに注意してください。
入力例 2
2 10 2 1
出力例 2
10
同じ金額の買い方が何通りかあっても、重複してカウントしないことに注意してください。
入力例 3
10 200000 955277671 764071525 871653439 819642859 703677532 515827892 127889502 881462887 330802980 503797872
出力例 3
5705443819
Score : 500 points
Problem Statement
In AtCoder Kingdom, N kinds of takoyakis (ball-shaped Japanese food) are sold. A takoyaki of the i-th kind is sold for A_i yen.
Takahashi will buy at least one takoyaki in total. He is allowed to buy multiple takoyakis of the same kind.
Find the K-th lowest price that Takahashi may pay. Here, if there are multiple sets of takoyakis that cost the same price, the price is counted only once.
Constraints
- 1 \le N \le 10
- 1 \le K \le 2 \times 10^5
- 1 \le A_i \le 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 6 20 25 30 100
Sample Output 1
50
The four kinds of takoyakis sold in AtCoder Kingdom cost 20 yen, 25 yen, 30 yen, and 100 yen.
The six lowest prices that Takahashi may pay are 20 yen, 25 yen, 30 yen, 40 yen, 45 yen, and 50 yen. Thus, the answer is 50.
Note that at least one takoyaki must be bought.
Sample Input 2
2 10 2 1
Sample Output 2
10
Note that a price is not counted more than once even if there are multiple sets of takoyakis costing that price.
Sample Input 3
10 200000 955277671 764071525 871653439 819642859 703677532 515827892 127889502 881462887 330802980 503797872
Sample Output 3
5705443819
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
キーエンスは即納で有名です。
この問題において、暦は 1 日、 2 日、 3 日、 \dots と続いています。
注文 1,2,\dots,N があり、注文 i は T_i 日に発生することが分かっています。
これらの注文に対し、以下のルールに従って出荷を行います。
- 出荷は注文 K 個分までまとめて行うことができる。
- 注文 i は、 T_i 日以降にしか出荷できない。
- 一度出荷すると、その出荷の X 日後になるまで次の出荷が行えない。
- すなわち、 a 日に出荷を行った時、次の出荷ができるのは a+X 日である。
注文から出荷までにかかった日数 1 日につき、不満度が 1 蓄積します。
すなわち、注文 i が S_i 日に出荷されたとき、その注文によって蓄積する不満度は (S_i - T_i) です。
出荷するタイミングを上手く定めた時、全ての注文において蓄積した不満度の総和として達成可能な最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K \le N \le 100
- 1 \le X \le 10^9
- 1 \le T_1 \le T_2 \le \dots \le T_N \le 10^{12}
入力
入力は以下の形式で標準入力から与えられる。
N K X T_1 T_2 \dots T_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 1 5 6 10 12
出力例 1
2
例えば、次の通り出荷することで不満度の総和を 2 にすることができ、これが達成可能な最小です。
- 注文 1 を 1 日に出荷する。
- これにより不満度は (1-1) = 0 蓄積し、次の出荷ができるのは 4 日である。
- 注文 2,3 を 6 日に出荷する。
- これにより不満度は (6-5) + (6-6) = 1 蓄積し、次の出荷ができるのは 9 日である。
- 注文 4 を 10 日に出荷する。
- これにより不満度は (10-10)=0 蓄積し、次の出荷ができるのは 13 日である。
- 注文 5 を 13 日に出荷する。
- これにより不満度は (13-12)=1 蓄積し、次の出荷ができるのは 16 日である。
入力例 2
1 1 1000000000 1000000000000
出力例 2
0
入力例 3
15 4 5 1 3 3 6 6 6 10 10 10 10 15 15 15 15 15
出力例 3
35
Score : 550 points
Problem Statement
KEYENCE is famous for quick delivery.
In this problem, the calendar proceeds as Day 1, Day 2, Day 3, \dots.
There are orders 1,2,\dots,N, and it is known that order i will be placed on Day T_i.
For these orders, shipping is carried out according to the following rules.
- At most K orders can be shipped together.
- Order i can only be shipped on Day T_i or later.
- Once a shipment is made, the next shipment cannot be made until X days later.
- That is, if a shipment is made on Day a, the next shipment can be made on Day a+X.
For each day that passes from order placement to shipping, dissatisfaction accumulates by 1 per day.
That is, if order i is shipped on Day S_i, the dissatisfaction accumulated for that order is (S_i - T_i).
Find the minimum possible total dissatisfaction accumulated over all orders when you optimally schedule the shipping dates.
Constraints
- All input values are integers.
- 1 \le K \le N \le 100
- 1 \le X \le 10^9
- 1 \le T_1 \le T_2 \le \dots \le T_N \le 10^{12}
Input
The input is given from Standard Input in the following format:
N K X T_1 T_2 \dots T_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 1 5 6 10 12
Sample Output 1
2
For example, by scheduling shipments as follows, we can achieve a total dissatisfaction of 2, which is the minimum possible.
- Ship order 1 on Day 1.
- This results in dissatisfaction of (1-1) = 0, and the next shipment can be made on Day 4.
- Ship orders 2 and 3 on Day 6.
- This results in dissatisfaction of (6-5) + (6-6) = 1, and the next shipment can be made on Day 9.
- Ship order 4 on Day 10.
- This results in dissatisfaction of (10-10) = 0, and the next shipment can be made on Day 13.
- Ship order 5 on Day 13.
- This results in dissatisfaction of (13-12) = 1, and the next shipment can be made on Day 16.
Sample Input 2
1 1 1000000000 1000000000000
Sample Output 2
0
Sample Input 3
15 4 5 1 3 3 6 6 6 10 10 10 10 15 15 15 15 15
Sample Output 3
35