A - 最適な練習相手

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 233

問題文

高橋君は剣道部に所属しており、今日は練習試合の相手を選ぶことになりました。

剣道部には高橋君以外に N 人の部員がいて、それぞれの部員には実力値が設定されています。部員には 1 から N までの番号が付けられており、部員 i (1 \leq i \leq N) の実力値は D_i です。

高橋君の現在の実力値は L です。高橋君が練習試合で部員 i に勝てる条件は、L \geq D_i が成り立つことです。すなわち、自分の実力値が相手の実力値以上であれば勝つことができます。

コーチは高橋君に「最も成長できる練習相手」を推薦します。コーチの方針では、高橋君が勝てる相手の中でなるべく自分に近い実力を持つ部員、すなわち実力値が最も高い部員と練習することで、最大限の成長が見込めると考えています。

具体的には、L \geq D_i を満たす部員の中で、D_i が最大となる部員が推薦されます。そのような部員が複数いる場合は、部員番号が最も小さい部員が選ばれます。

コーチが推薦する部員の番号を求めてください。

ただし、高橋君が勝てる部員が 1 人も存在しない場合は、-1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq 10^9
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N L
D_1 D_2 \ldots D_N
  • 1 行目には、高橋君以外の部員の数を表す整数 N と、高橋君の現在の実力値を表す整数 L が、スペース区切りで与えられる。
  • 2 行目には、各部員の実力値を表す整数 D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。

出力

コーチが推薦する部員の番号を 1 行で出力せよ。高橋君が勝てる部員が存在しない場合は -1 を出力せよ。


入力例 1

5 10
3 14 10 7 10

出力例 1

3

入力例 2

3 5
8 12 6

出力例 2

-1

入力例 3

10 50
45 60 50 30 50 70 48 55 49 50

出力例 3

3

入力例 4

20 1000000000
999999999 1000000000 999999998 500000000 1000000000 999999997 800000000 1000000000 999999999 600000000 700000000 900000000 1000000000 999999996 850000000 1000000000 750000000 950000000 999999999 1000000000

出力例 4

2

入力例 5

1 1
1

出力例 5

1

Score : 233 pts

Problem Statement

Takahashi is a member of the kendo club, and today he needs to choose an opponent for a practice match.

There are N members in the kendo club other than Takahashi, and each member has a skill value assigned to them. The members are numbered from 1 to N, and the skill value of member i (1 \leq i \leq N) is D_i.

Takahashi's current skill value is L. The condition for Takahashi to win a practice match against member i is that L \geq D_i holds. In other words, he can win if his skill value is greater than or equal to the opponent's skill value.

The coach will recommend the "best practice partner for growth" to Takahashi. According to the coach's policy, practicing against the member who Takahashi can beat but whose skill is as close to his own as possible — that is, the member with the highest skill value among those he can beat — is expected to yield the maximum growth.

Specifically, among the members satisfying L \geq D_i, the member with the maximum D_i is recommended. If there are multiple such members, the one with the smallest member number is chosen.

Find the number of the member recommended by the coach.

However, if there is no member that Takahashi can beat, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq 10^9
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N L
D_1 D_2 \ldots D_N
  • The first line contains an integer N representing the number of members other than Takahashi, and an integer L representing Takahashi's current skill value, separated by a space.
  • The second line contains integers D_1, D_2, \ldots, D_N representing the skill values of each member, separated by spaces.

Output

Output the number of the member recommended by the coach in a single line. If there is no member that Takahashi can beat, output -1.


Sample Input 1

5 10
3 14 10 7 10

Sample Output 1

3

Sample Input 2

3 5
8 12 6

Sample Output 2

-1

Sample Input 3

10 50
45 60 50 30 50 70 48 55 49 50

Sample Output 3

3

Sample Input 4

20 1000000000
999999999 1000000000 999999998 500000000 1000000000 999999997 800000000 1000000000 999999999 600000000 700000000 900000000 1000000000 999999996 850000000 1000000000 750000000 950000000 999999999 1000000000

Sample Output 4

2

Sample Input 5

1 1
1

Sample Output 5

1
B - ロッカーの整理

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333

問題文

高橋君は、学校の体育館にある N 個のロッカーの管理を任されています。ロッカーには 1 から N までの番号が付けられています。最初、すべてのロッカーは閉まっています。

高橋君はこれから N 回の操作を行います。i 回目(i = 1, 2, \ldots, N)の操作では、番号が i の倍数であるすべてのロッカー(番号 i のロッカー自体も含む)について、開いているものは閉め、閉まっているものは開けます。

例えば、1 回目の操作では番号が 1 の倍数であるロッカー、すなわち 1, 2, \ldots, N 番のすべてのロッカーの開閉状態を切り替えます。2 回目の操作では番号が 2 の倍数である N 以下の番号のロッカー、すなわち 2, 4, 6, \ldots 番のロッカーの開閉状態を切り替えます。3 回目の操作では番号が 3 の倍数である N 以下の番号のロッカー、すなわち 3, 6, 9, \ldots 番のロッカーの開閉状態を切り替えます。

すべての操作が終わった後、開いているロッカーの個数を求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • N は整数

入力

N

ロッカーの個数を表す整数 N1 行で与えられる。

出力

すべての操作が終わった後に開いているロッカーの個数を整数で 1 行に出力せよ。


入力例 1

10

出力例 1

3

入力例 2

100

出力例 2

10

入力例 3

1000000000000000000

出力例 3

1000000000

Score : 333 pts

Problem Statement

Takahashi is in charge of managing N lockers in the school gymnasium. The lockers are numbered from 1 to N. Initially, all lockers are closed.

Takahashi will perform N operations. In the i-th operation (i = 1, 2, \ldots, N), for every locker whose number is a multiple of i (including locker number i itself), he toggles its state: if it is open, he closes it, and if it is closed, he opens it.

For example, in the 1st operation, he toggles the state of all lockers whose numbers are multiples of 1, namely lockers 1, 2, \ldots, N. In the 2nd operation, he toggles the state of lockers whose numbers are multiples of 2 and at most N, namely lockers 2, 4, 6, \ldots. In the 3rd operation, he toggles the state of lockers whose numbers are multiples of 3 and at most N, namely lockers 3, 6, 9, \ldots.

After all operations are completed, find the number of lockers that are open.

Constraints

  • 1 \leq N \leq 10^{18}
  • N is an integer

Input

N

An integer N representing the number of lockers is given on a single line.

Output

Print the number of lockers that are open after all operations are completed, as an integer on a single line.


Sample Input 1

10

Sample Output 1

3

Sample Input 2

100

Sample Output 2

10

Sample Input 3

1000000000000000000

Sample Output 3

1000000000
C - 座席の配置

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は、文化祭の会場にある一列に並んだ N 個の座席の配置を担当しています。座席には左から順に番号 1 から N が振られています。

現在、いくつかの座席にはすでにお客さんが座っています。具体的には、座席 i の状態が S_i = 1 のとき人が座っており、S_i = 0 のとき空席です。

高橋君は、空いている座席のうちから 0 個以上好きなだけ選び、選んだ各座席に新たなお客さんを 1 人ずつ案内することができます。ただし、すでに座っているお客さんを移動させたり退席させたりすることはできません。また、各座席には最大 1 人しか座れません。さらに、会場の換気ルールにより、以下の条件を満たさなければなりません。

  • すべての案内が完了した後の座席の状態において、連続する 3 つの座席がすべて人が座っている状態である箇所が存在してはならない。

すなわち、すべての案内が完了した後の座席 i の状態を T_i(人が座っていれば 1、空席であれば 0)としたとき、1 \leq i \leq N-2 を満たすどの i についても T_i = T_{i+1} = T_{i+2} = 1 とはならないようにしなければなりません。なお、初期状態においてこの条件はすでに満たされていることが入力で保証されます。

高橋君は、この条件を満たしつつ、できるだけ多くのお客さんを新たに案内したいと思っています。新たに案内できるお客さんの最大人数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S_i \in \{0, 1\}1 \leq i \leq N
  • 入力で与えられる初期状態において、連続する 3 つの座席がすべて人が座っている箇所は存在しない。すなわち、S_i = S_{i+1} = S_{i+2} = 1 となるような i1 \leq i \leq N-2)は存在しない。
  • 入力はすべて整数である。

入力

N
S_1 S_2 \ldots S_N
  • 1 行目には、座席の数を表す整数 N が与えられる。
  • 2 行目には、各座席に人が座っているかを表す N 個の整数 S_1, S_2, \ldots, S_N がスペース区切りで与えられる。S_i = 1 のとき座席 i に人が座っており、S_i = 0 のとき空席である。

出力

条件を満たしつつ新たに案内できるお客さんの最大人数を 1 行で出力せよ。


入力例 1

5
0 0 1 0 0

出力例 1

2

入力例 2

7
1 0 0 0 0 0 1

出力例 2

3

入力例 3

15
0 0 0 1 0 0 0 0 1 1 0 0 0 0 0

出力例 3

7

入力例 4

30
0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0

出力例 4

12

入力例 5

1
0

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is in charge of arranging N seats lined up in a row at a cultural festival venue. The seats are numbered 1 through N from left to right.

Currently, some seats are already occupied by guests. Specifically, seat i is occupied if S_i = 1, and vacant if S_i = 0.

Takahashi can select 0 or more vacant seats as he likes and guide one new guest to each selected seat. However, he cannot move or remove guests who are already seated. Also, each seat can hold at most 1 person. Furthermore, due to the venue's ventilation rules, the following condition must be satisfied:

  • After all guests have been guided to their seats, there must be no three consecutive seats that are all occupied.

In other words, if we denote the state of seat i after all guidance is complete as T_i (1 if occupied, 0 if vacant), then for every i satisfying 1 \leq i \leq N-2, it must not be the case that T_i = T_{i+1} = T_{i+2} = 1. It is guaranteed in the input that the initial state already satisfies this condition.

Takahashi wants to guide as many new guests as possible while satisfying this condition. Find the maximum number of new guests that can be guided.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S_i \in \{0, 1\} (1 \leq i \leq N)
  • In the initial state given in the input, there are no three consecutive seats that are all occupied. That is, there is no i (1 \leq i \leq N-2) such that S_i = S_{i+1} = S_{i+2} = 1.
  • All input values are integers.

Input

N
S_1 S_2 \ldots S_N
  • The first line contains an integer N representing the number of seats.
  • The second line contains N integers S_1, S_2, \ldots, S_N separated by spaces, representing whether each seat is occupied. S_i = 1 means seat i is occupied, and S_i = 0 means it is vacant.

Output

Print in one line the maximum number of new guests that can be guided while satisfying the condition.


Sample Input 1

5
0 0 1 0 0

Sample Output 1

2

Sample Input 2

7
1 0 0 0 0 0 1

Sample Output 2

3

Sample Input 3

15
0 0 0 1 0 0 0 0 1 1 0 0 0 0 0

Sample Output 3

7

Sample Input 4

30
0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0

Sample Output 4

12

Sample Input 5

1
0

Sample Output 5

1
D - 配送センターの稼働計画

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は配送センターの管理者です。配送センターでは N 個の荷物を届ける予定があり、それぞれの荷物には届け先が指定した「希望配達日」があります。i 番目の荷物の希望配達日は D_i 日目です。

配送センターはちょうど連続する K 日間だけ稼働させることができます。具体的には、任意の整数 s を選び、s 日目から s + K - 1 日目までの K 日間(両端を含む)を稼働期間とします。

高橋君は、すべての荷物をこの稼働期間内に配達しなければなりません。i 番目の荷物の配達日を x_i とすると、x_is \leq x_i \leq s + K - 1 を満たす整数でなければなりません。1 日に配達できる荷物の個数に上限はなく、同じ日に複数の荷物を配達することもできます。

i 番目の荷物を x_i 日目に配達した場合、その荷物の不満度は |D_i - x_i| です。希望配達日ちょうどに届ければ不満度は 0 ですが、希望日から離れるほど不満度は大きくなります。

高橋君は稼働期間の開始日 s を適切に選び、さらに各荷物の配達日を稼働期間内で適切に決めることで、すべての荷物の不満度の合計 \displaystyle \sum_{i=1}^{N} |D_i - x_i| を最小化したいと考えています。その最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq D_i \leq 10^9
  • 入力はすべて整数

入力

N K
D_1 D_2 \ldots D_N
  • 1 行目には、荷物の個数を表す N と、稼働期間の日数を表す K が、スペース区切りで与えられる。
  • 2 行目には、各荷物の希望配達日を表す D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。

出力

すべての荷物の不満度の合計の最小値を 1 行で出力せよ。


入力例 1

5 3
1 2 3 4 5

出力例 1

2

入力例 2

4 10
5 8 12 20

出力例 2

6

入力例 3

7 5
100 102 105 110 115 118 120

出力例 3

34

Score : 400 pts

Problem Statement

Takahashi is the manager of a delivery center. The delivery center has N packages to deliver, and each package has a "preferred delivery date" specified by its recipient. The preferred delivery date of the i-th package is day D_i.

The delivery center can operate for exactly K consecutive days. Specifically, Takahashi chooses any integer s and sets the operation period to be the K days from day s to day s + K - 1 (inclusive).

Takahashi must deliver all packages within this operation period. If the delivery date of the i-th package is x_i, then x_i must be an integer satisfying s \leq x_i \leq s + K - 1. There is no upper limit on the number of packages that can be delivered in a single day, and multiple packages can be delivered on the same day.

If the i-th package is delivered on day x_i, the dissatisfaction for that package is |D_i - x_i|. If the package is delivered exactly on the preferred delivery date, the dissatisfaction is 0, but the further the delivery date is from the preferred date, the greater the dissatisfaction.

Takahashi wants to choose the starting day s of the operation period appropriately, and also decide the delivery date of each package within the operation period appropriately, in order to minimize the total dissatisfaction of all packages \displaystyle \sum_{i=1}^{N} |D_i - x_i|. Find this minimum value.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq D_i \leq 10^9
  • All inputs are integers

Input

N K
D_1 D_2 \ldots D_N
  • The first line contains N, the number of packages, and K, the number of days in the operation period, separated by a space.
  • The second line contains the preferred delivery dates D_1, D_2, \ldots, D_N of each package, separated by spaces.

Output

Print the minimum value of the total dissatisfaction of all packages in one line.


Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

2

Sample Input 2

4 10
5 8 12 20

Sample Output 2

6

Sample Input 3

7 5
100 102 105 110 115 118 120

Sample Output 3

34
E - 散歩とバリケード

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 466

問題文

高橋君は、1 次元の数直線上を散歩しています。高橋君は最初、座標 0 にいます。

高橋君は N 個の移動プランを持っています。i 番目 (1 \leq i \leq N) のプランを実行すると、現在の座標に D_i が加算されます。D_i0 でない整数で、正のとき正の方向へ、負のとき負の方向へ移動します。

数直線上には M 個のバリケードが設置されており、j 番目 (1 \leq j \leq M) のバリケードは座標 X_j にあります。あるプランの実行により現在の座標 s から座標 s + D_i へ移動するとき、ss + D_i の間(両端を含む)にバリケードが 1 つでも存在する場合、すなわち \min(s,\, s + D_i) \le X_j \le \max(s,\, s + D_i) を満たす X_j が存在する場合、そのプランは衝突が起こるため実行できません。

高橋君はプランを 1 番目から N 番目まで順番に検討します。各プランについて、衝突が起こる場合はそのプランをスキップしなければなりません。衝突が起こらない場合は、そのプランを実行するかスキップするかを自由に選べます。

すべてのプランの検討が終わった後の、高橋君の座標としてありえる最大値を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^4
  • 0 \leq M \leq 2 \times 10^5
  • -5 \times 10^4 \leq D_i \leq 5 \times 10^4
  • D_i \neq 0
  • \displaystyle \sum_{i=1}^{N} |D_i| \leq 5 \times 10^4
  • -10^9 \leq X_j \leq 10^9
  • X_1, X_2, \ldots, X_M はすべて相異なる
  • 入力はすべて整数である

入力

N M
D_1 D_2 \ldots D_N
X_1 X_2 \ldots X_M
  • 1 行目には、プランの個数 N とバリケードの個数 M がスペース区切りで与えられる。
  • 2 行目には、各プランによる座標の変化量 D_1, D_2, \ldots, D_N がスペース区切りで与えられる。
  • 3 行目には、各バリケードの座標 X_1, X_2, \ldots, X_M がスペース区切りで与えられる。ただし、M = 0 の場合、3 行目は与えられない。

出力

最適にプランを選択したときの、最終的な高橋君の座標の最大値を 1 行で出力せよ。


入力例 1

4 2
3 -2 4 -1
2 5

出力例 1

0

入力例 2

5 0
-3 1 2 -1 4

出力例 2

7

入力例 3

10 6
2 5 -3 6 -4 7 -8 3 4 -2
-7 8 14 19 -11 25

出力例 3

7

入力例 4

20 15
5 4 -6 7 -3 8 -10 6 9 -5 4 -7 12 -8 3 11 -9 2 6 -4
-20 -13 -1 10 18 27 35 44 -7 5 14 22 31 40 50

出力例 4

4

入力例 5

1 1
50000
0

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is taking a walk on a one-dimensional number line. Initially, Takahashi is at coordinate 0.

Takahashi has N movement plans. Executing the i-th (1 \leq i \leq N) plan adds D_i to his current coordinate. D_i is a non-zero integer; when positive, he moves in the positive direction, and when negative, he moves in the negative direction.

There are M barricades placed on the number line. The j-th (1 \leq j \leq M) barricade is at coordinate X_j. When executing a plan moves Takahashi from his current coordinate s to coordinate s + D_i, if there exists at least one barricade between s and s + D_i (inclusive of both endpoints), that is, if there exists an X_j satisfying \min(s,\, s + D_i) \le X_j \le \max(s,\, s + D_i), then a collision occurs and the plan cannot be executed.

Takahashi considers the plans in order from the 1-st to the N-th. For each plan, if a collision would occur, he must skip that plan. If no collision would occur, he may freely choose to either execute or skip the plan.

Find the maximum possible coordinate of Takahashi after all plans have been considered.

Constraints

  • 1 \leq N \leq 5 \times 10^4
  • 0 \leq M \leq 2 \times 10^5
  • -5 \times 10^4 \leq D_i \leq 5 \times 10^4
  • D_i \neq 0
  • \displaystyle \sum_{i=1}^{N} |D_i| \leq 5 \times 10^4
  • -10^9 \leq X_j \leq 10^9
  • X_1, X_2, \ldots, X_M are all distinct
  • All input values are integers

Input

N M
D_1 D_2 \ldots D_N
X_1 X_2 \ldots X_M
  • The first line contains the number of plans N and the number of barricades M, separated by a space.
  • The second line contains the coordinate changes D_1, D_2, \ldots, D_N for each plan, separated by spaces.
  • The third line contains the coordinates X_1, X_2, \ldots, X_M of each barricade, separated by spaces. However, if M = 0, the third line is not given.

Output

Print in one line the maximum possible final coordinate of Takahashi when plans are chosen optimally.


Sample Input 1

4 2
3 -2 4 -1
2 5

Sample Output 1

0

Sample Input 2

5 0
-3 1 2 -1 4

Sample Output 2

7

Sample Input 3

10 6
2 5 -3 6 -4 7 -8 3 4 -2
-7 8 14 19 -11 25

Sample Output 3

7

Sample Input 4

20 15
5 4 -6 7 -3 8 -10 6 9 -5 4 -7 12 -8 3 11 -9 2 6 -4
-20 -13 -1 10 18 27 35 44 -7 5 14 22 31 40 50

Sample Output 4

4

Sample Input 5

1 1
50000
0

Sample Output 5

0