A - Airport Bus

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

高橋空港には,毎日飛行機で N 人の乗客が到着します. i 番目の乗客は時刻 T_i に到着します.

高橋空港に到着する乗客は全員バスで市内へ移動します.どのバスも定員は C 人であり,C 人以下の乗客を乗せることができます. 飛行機の乗客は,飛行機の到着時刻よりも早く出発するバスには乗ることができません. また,飛行機の到着時刻から K の時間が経過した後にもバスに乗れていないと,怒り出してしまいます. そのため,i 番目の乗客は,出発時刻が T_i 以上 T_i + K 以下であるようなバスに乗れるようにしないといけません.

この条件のもとで,うまくバスの出発時刻を定めるとき,必要なバスの数の最小値を求めてください. ただし,バスの出発時刻は必ずしも整数である必要はなく,同じ時刻に出発するバスが複数あってもかまいません.

制約

  • 2 \leq N \leq 100000
  • 1 \leq C \leq 10^9
  • 1 \leq K \leq 10^9
  • 1 \leq T_i \leq 10^9
  • C, K, T_i は整数

入力

入力は以下の形式で標準入力から与えられる。

N C K
T_1
T_2
:
T_N

出力

必要なバスの数の最小値を出力せよ.


入力例 1

5 3 5
1
2
3
6
12

出力例 1

3

例えば,時刻 4.5, 6, 12 にバスを出発させ,次のように乗客をバスに乗せるとよいです.

  • 時刻 4.5 に出発するバスには,時刻 2, 3 に到着する乗客を乗せる.
  • 時刻 6 に出発するバスには,時刻 1, 6 に到着する乗客を乗せる.
  • 時刻 12 に出発するバスには,時刻 12 に到着する乗客を乗せる.

入力例 2

6 3 3
7
6
2
8
10
6

出力例 2

3

Score : 300 points

Problem Statement

Every day, N passengers arrive at Takahashi Airport. The i-th passenger arrives at time T_i.

Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers. Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport. Also, a passenger will get angry if he/she is still unable to take a bus K units of time after the arrival of the airplane. For that reason, it is necessary to arrange buses so that the i-th passenger can take a bus departing at time between T_i and T_i + K (inclusive).

When setting the departure times for buses under this condition, find the minimum required number of buses. Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.

Constraints

  • 2 \leq N \leq 100000
  • 1 \leq C \leq 10^9
  • 1 \leq K \leq 10^9
  • 1 \leq T_i \leq 10^9
  • C, K and T_i are integers.

Input

The input is given from Standard Input in the following format:

N C K
T_1
T_2
:
T_N

Output

Print the minimum required number of buses.


Sample Input 1

5 3 5
1
2
3
6
12

Sample Output 1

3

For example, the following three buses are enough:

  • A bus departing at time 4.5, that carries the passengers arriving at time 2 and 3.
  • A bus departing at time 6, that carries the passengers arriving at time 1 and 6.
  • A bus departing at time 12, that carries the passenger arriving at time 12.

Sample Input 2

6 3 3
7
6
2
8
10
6

Sample Output 2

3
B - Colorful Creatures

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

すぬけ君は,N 匹の変わった生き物を見つけました. それぞれの生き物には色と大きさが定まっており,i 番目の生き物の色は i,大きさは A_i で表されます.

どの生き物も,大きさが自分の 2 倍以下であるような他の生き物を吸収することができます. 大きさ A,色 B の生き物が大きさ C,色 D の生き物を吸収すると (C \leq 2 \times A),合体して大きさ A+C,色 B の生き物になります. ここで,2 匹の生き物の大きさによっては,どちらも他方を吸収することが可能な場合があります.

すぬけ君がこの生き物たちを観察していると,合体を繰り返して,最終的に 1 匹になりました. このとき,残った生き物の色として考えられるものは何種類あるかを求めてください.

制約

  • 2 \leq N \leq 100000
  • 1 \leq A_i \leq 10^9
  • A_i は整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2A_N

出力

この生き物たちが合体を繰り返して,最終的に 1 匹になったとき,残った生き物の色として考えられるものは何通りあるかを出力せよ.


入力例 1

3
3 1 4

出力例 1

2

最終的に残った生き物の色としては色 1, 3 が考えられます. 例えば,色 3 の生き物が色 2 の生き物を吸収し,次に色 1 の生き物が色 3 の生き物と合体すると,色 1 の生き物のみが残ります.


入力例 2

5
1 1 1 1 1

出力例 2

5

同じ大きさの生き物が複数いる場合もあります.


入力例 3

6
40 1 30 2 7 20

出力例 3

4

Score : 400 points

Problem Statement

Snuke found N strange creatures. Each creature has a fixed color and size. The color and size of the i-th creature are represented by i and A_i, respectively.

Every creature can absorb another creature whose size is at most twice the size of itself. When a creature of size A and color B absorbs another creature of size C and color D (C \leq 2 \times A), they will merge into one creature of size A+C and color B. Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.

Snuke has been watching these creatures merge over and over and ultimately become one creature. Find the number of the possible colors of this creature.

Constraints

  • 2 \leq N \leq 100000
  • 1 \leq A_i \leq 10^9
  • A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2A_N

Output

Print the number of the possible colors of the last remaining creature after the N creatures repeatedly merge and ultimately become one creature.


Sample Input 1

3
3 1 4

Sample Output 1

2

The possible colors of the last remaining creature are colors 1 and 3. For example, when the creature of color 3 absorbs the creature of color 2, then the creature of color 1 absorbs the creature of color 3, the color of the last remaining creature will be color 1.


Sample Input 2

5
1 1 1 1 1

Sample Output 2

5

There may be multiple creatures of the same size.


Sample Input 3

6
40 1 30 2 7 20

Sample Output 3

4
C - Squared Graph

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 800

問題文

高橋君は,N 頂点 1, 2, ..., N からなる無向グラフをもらいました. このグラフの辺は (u_i, v_i) で表されます. このグラフには,自己辺や多重辺は存在しません.

高橋君は,このグラフをもとに,N^2 頂点 (a, b) (1 \leq a \leq N, 1 \leq b \leq N) からなるグラフを作ることにしました. このグラフの辺は,次の規則で定まります.

  • 元のグラフにおいて aa' の間および bb' の間の両方に辺があるとき,またそのときに限り,(a, b)(a', b') の間に辺を張る.

このようにして作ったグラフの連結成分の個数を求めてください.

制約

  • 2 \leq N \leq 100,000
  • 0 \leq M \leq 200,000
  • 1 \leq u_i < v_i \leq N
  • u_i = u_j かつ v_i = v_j を満たすような異なる i, j の組は存在しない

入力

入力は以下の形式で標準入力から与えられる。

N M
u_1 v_1
u_2 v_2
:
u_M v_M

出力

高橋君の作ったグラフの連結成分の個数を出力せよ.


入力例 1

3 1
1 2

出力例 1

7

高橋君の作ったグラフは下のようになります.


入力例 2

7 5
1 2
3 4
3 5
4 5
2 6

出力例 2

18

Score : 800 points

Problem Statement

Takahashi has received an undirected graph with N vertices, numbered 1, 2, ..., N. The edges in this graph are represented by (u_i, v_i). There are no self-loops and multiple edges in this graph.

Based on this graph, Takahashi is now constructing a new graph with N^2 vertices, where each vertex is labeled with a pair of integers (a, b) (1 \leq a \leq N, 1 \leq b \leq N). The edges in this new graph are generated by the following rule:

  • Span an edge between vertices (a, b) and (a', b') if and only if both of the following two edges exist in the original graph: an edge between vertices a and a', and an edge between vertices b and b'.

How many connected components are there in this new graph?

Constraints

  • 2 \leq N \leq 100,000
  • 0 \leq M \leq 200,000
  • 1 \leq u_i < v_i \leq N
  • There exists no pair of distinct integers i and j such that u_i = u_j and v_i = v_j.

Input

The input is given from Standard Input in the following format:

N M
u_1 v_1
u_2 v_2
:
u_M v_M

Output

Print the number of the connected components in the graph constructed by Takahashi.


Sample Input 1

3 1
1 2

Sample Output 1

7

The graph constructed by Takahashi is as follows.


Sample Input 2

7 5
1 2
3 4
3 5
4 5
2 6

Sample Output 2

18
D - Half Reflector

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

高橋君は,ある特殊な装置をたくさん持っています. この装置は筒状で,左右からボールを入れることができます. また,この装置には 2 種類の状態 A, B があります. 各状態について,装置にボールを入れたときの動作は次のようになっています.

  • 状態 A の装置にボールを入れると,入れた側と同じ側からボールが飛び出してきて,その後すぐに装置の状態は B に変化します.
  • 状態 B の装置にボールを入れると,入れた側と反対側からボールが飛び出してきて,その後すぐに装置の状態は A に変化します.

装置の状態の変化はとても速いので,1 つの装置にボールを入れた後,次にボールが入ってくるまでの間には必ず状態の変化は終わっています.

高橋君は,この装置を N 個つなげたものを作りました.つまり,

  • 左から i (1 \leq i \leq N-1) 番目の装置の右端から飛び出したボールは,すぐに左から i+1 番目の装置に左端から入ります.
  • 左から i (2 \leq i \leq N) 番目の装置の左端から飛び出したボールは,すぐに左から i-1 番目の装置に右端から入ります.

左から i 番目の装置の最初の状態は,文字列 Si 番目の文字で表されます. 次に高橋君は,一番左の装置の左端からボールを入れ,ボールがどちらかの端から出てくるまで待つということを K 回行いました. ここで,ボールがいつまで経っても出てこないということは起きないことが証明できます. K 回ボールを入れた後の各装置の状態を求めてください.

制約

  • 1 \leq N \leq 200,000
  • 1 \leq K \leq 10^9
  • |S|=N
  • S の各文字は A または B である

入力

入力は以下の形式で標準入力から与えられる。

N K
S

出力

ボールを入れる操作を K 回行った後の,各装置の状態を表す文字列を出力せよ. この文字列は N 文字からなり,i 番目の文字は左から i 番目の装置の状態と同じでなければならない.


入力例 1

5 1
ABAAA

出力例 1

BBAAA

この入力では,一番左の装置の左端からボールを入れると,同じところからボールが出てきます.


入力例 2

5 2
ABAAA

出力例 2

ABBBA

入力例 3

4 123456789
AABB

出力例 3

BABA

Score : 900 points

Problem Statement

Takahashi has a lot of peculiar devices. These cylindrical devices receive balls from left and right. Each device is in one of the two states A and B, and for each state, the device operates as follows:

  • When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B.
  • When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A.

The transition of the state of a device happens momentarily and always completes before it receives another ball.

Takahashi built a contraption by concatenating N of these devices. In this contraption,

  • A ball that was thrown out from the right side of the i-th device from the left (1 \leq i \leq N-1) immediately enters the (i+1)-th device from the left side.
  • A ball that was thrown out from the left side of the i-th device from the left (2 \leq i \leq N) immediately enters the (i-1)-th device from the right side.

The initial state of the i-th device from the left is represented by the i-th character in a string S. From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end. Here, it can be proved that the ball always comes out of the contraption after a finite time. Find the state of each device after K balls are processed.

Constraints

  • 1 \leq N \leq 200,000
  • 1 \leq K \leq 10^9
  • |S|=N
  • Each character in S is either A or B.

Input

The input is given from Standard Input in the following format:

N K
S

Output

Print a string that represents the state of each device after K balls are processed. The string must be N characters long, and the i-th character must correspond to the state of the i-th device from the left.


Sample Input 1

5 1
ABAAA

Sample Output 1

BBAAA

In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place.


Sample Input 2

5 2
ABAAA

Sample Output 2

ABBBA

Sample Input 3

4 123456789
AABB

Sample Output 3

BABA
E - Increasing Numbers

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1300

問題文

10 進法で表記したとき,桁同士が隣り合っているところではすべて,右にある桁の値のほうが左にある桁の値以上であるような 0 以上の整数を,増加的と呼ぶことにします. たとえば,15581130 は増加的ですが,1020170312 は増加的ではありません.

すぬけ君は,整数 N を持っています. N が最小で何個の増加的な数の和として表されるかを求めてください.

制約

  • 1 \leq N \leq 10^{500000}

入力

入力は以下の形式で標準入力から与えられる。

N

出力

N が最小で何個の増加的な数の和として表されるかを出力せよ.


入力例 1

80

出力例 1

2

例えば,80 = 77 + 3 として表すことができます.


入力例 2

123456789

出力例 2

1

123456789 はそれ自体が増加的なので,1 個の増加的な数の和で表すことができます.


入力例 3

20170312

出力例 3

4

入力例 4

7204647845201772120166980358816078279571541735614841625060678056933503

出力例 4

31

Score : 1300 points

Problem Statement

We will call a non-negative integer increasing if, for any two adjacent digits in its decimal representation, the digit to the right is greater than or equal to the digit to the left. For example, 1558, 11, 3 and 0 are all increasing; 10 and 20170312 are not.

Snuke has an integer N. Find the minimum number of increasing integers that can represent N as their sum.

Constraints

  • 1 \leq N \leq 10^{500000}

Input

The input is given from Standard Input in the following format:

N

Output

Print the minimum number of increasing integers that can represent N as their sum.


Sample Input 1

80

Sample Output 1

2

One possible representation is 80 = 77 + 3.


Sample Input 2

123456789

Sample Output 2

1

123456789 in itself is increasing, and thus it can be represented as the sum of one increasing integer.


Sample Input 3

20170312

Sample Output 3

4

Sample Input 4

7204647845201772120166980358816078279571541735614841625060678056933503

Sample Output 4

31
F - Train Service Planning

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1700

問題文

高橋王国には,1 本の鉄道路線が走っています. この路線は N 個の区間 1, 2, ..., NN+1 個の駅 0, 1, ..., N からなり,区間 i は駅 i-1 と駅 i を直接結んでいます. 列車が区間 i を走行するには,方向によらず,ちょうど A_i 分かかります. また,N 個の区間のそれぞれは,区間全体にわたって複線であるか,区間全体にわたって単線であるかのどちらかです. B_i = 1 のときは区間 i は単線,B_i = 2 のときは区間 i は複線です. 複線の区間では両方向の列車がすれ違うことができますが,単線の区間ではすれ違うことはできません. ただし,列車が駅にいる場合は列車同士がすれ違うことができます.

すぬけ君は,この路線に図のように K 分間隔で列車を走らせようとしています.ここで,太線は列車が走行している位置を表します. (詳しくは,入出力例 1 を見てください.)

このとき,駅 0 から駅 N までの列車の所要時間と,駅 N から駅 0 までの列車の所要時間の合計の最小値を求めてください. この問題の制約の下,適切な列車の走らせ方が存在するとき,この最小値は必ず整数になることが証明できます.

なお,列車の発車時刻や到着時刻が満たすべき条件は,厳密に書くと下のようになります.

  • どの列車も,駅 0 を出発して駅 N まで向かうか,駅 N を出発して駅 0 まで向かうかのいずれかである.
  • どの列車も,区間 i をちょうど A_i 分かけて走行する.例えば,駅 N 行きのある列車が駅 i-1 を時刻 t に出発したなら,この列車は駅 i にちょうど時刻 t+A_i に到着する.
  • ある駅で,時刻 s に駅 N 行きの列車が到着し,その列車が時刻 t に発車したとすると,駅 N 行きの次の列車は時刻 s+K に到着し,時刻 t+K に発車する.また,駅 N 行きの前の列車は時刻 s-K に到着し,時刻 t-K に発車する.駅 0 行きの列車についても同様である.
  • 異なる方向の列車が,同時に同じ単線区間(両端の駅を除く)を走行していてはならない.

制約

  • 1 \leq N \leq 100000
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • A_i は整数
  • B_i = 1 または B_i = 2

部分点

  • 500 点分のテストケースでは,すべての区間が単線である.すなわち,B_i = 1 が成り立つ.
  • 別の 500 点分のテストケースでは,N \leq 200 が成り立つ.

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 B_1
A_2 B_2
:
A_N B_N

出力

0 から駅 N までの列車の所要時間と,駅 N から駅 0 までの列車の所要時間の合計の最小値を表す整数を出力せよ. ただし,条件をすべて満たすように列車を走らせることが不可能な場合は,-1 を出力せよ.


入力例 1

3 10
4 1
3 1
4 1

出力例 1

26

例えば,下図に示すように列車を走らせると,所要時間の合計が 26 分になります.

例えば,赤線で示した列車は,時刻 0 に駅 0 を出発し,時刻 4 に駅 1 に到着し,時刻 5 に駅 1 を出発し,時刻 8 に駅 2 に到着します.


入力例 2

1 10
10 1

出力例 2

-1

入力例 3

6 4
1 1
1 1
1 1
1 1
1 1
1 1

出力例 3

12

入力例 4

20 987654321
129662684 2
162021979 1
458437539 1
319670097 2
202863355 1
112218745 1
348732033 1
323036578 1
382398703 1
55854389 1
283445191 1
151300613 1
693338042 2
191178308 2
386707193 1
204580036 1
335134457 1
122253639 1
824646518 2
902554792 2

出力例 4

14829091348

Score : 1700 points

Problem Statement

There is a railroad in Takahashi Kingdom. The railroad consists of N sections, numbered 1, 2, ..., N, and N+1 stations, numbered 0, 1, ..., N. Section i directly connects the stations i-1 and i. A train takes exactly A_i minutes to run through section i, regardless of direction. Each of the N sections is either single-tracked over the whole length, or double-tracked over the whole length. If B_i = 1, section i is single-tracked; if B_i = 2, section i is double-tracked. Two trains running in opposite directions can cross each other on a double-tracked section, but not on a single-tracked section. Trains can also cross each other at a station.

Snuke is creating the timetable for this railroad. In this timetable, the trains on the railroad run every K minutes, as shown in the following figure. Here, bold lines represent the positions of trains running on the railroad. (See Sample 1 for clarification.)

When creating such a timetable, find the minimum sum of the amount of time required for a train to depart station 0 and reach station N, and the amount of time required for a train to depart station N and reach station 0. It can be proved that, if there exists a timetable satisfying the conditions in this problem, this minimum sum is always an integer.

Formally, the times at which trains arrive and depart must satisfy the following:

  • Each train either departs station 0 and is bound for station N, or departs station N and is bound for station 0.
  • Each train takes exactly A_i minutes to run through section i. For example, if a train bound for station N departs station i-1 at time t, the train arrives at station i exactly at time t+A_i.
  • Assume that a train bound for station N arrives at a station at time s, and departs the station at time t. Then, the next train bound for station N arrives at the station at time s+K, and departs the station at time t+K. Additionally, the previous train bound for station N arrives at the station at time s-K, and departs the station at time t-K. This must also be true for trains bound for station 0.
  • Trains running in opposite directions must not be running on the same single-tracked section (except the stations at both ends) at the same time.

Constraints

  • 1 \leq N \leq 100000
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • A_i is an integer.
  • B_i is either 1 or 2.

Partial Score

  • In the test set worth 500 points, all the sections are single-tracked. That is, B_i = 1.
  • In the test set worth another 500 points, N \leq 200.

Input

The input is given from Standard Input in the following format:

N K
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print an integer representing the minimum sum of the amount of time required for a train to depart station 0 and reach station N, and the amount of time required for a train to depart station N and reach station 0. If it is impossible to create a timetable satisfying the conditions, print -1 instead.


Sample Input 1

3 10
4 1
3 1
4 1

Sample Output 1

26

For example, the sum of the amount of time in question will be 26 minutes in the following timetable:

In this timetable, the train represented by the red line departs station 0 at time 0, arrives at station 1 at time 4, departs station 1 at time 5, arrives at station 2 at time 8, and so on.


Sample Input 2

1 10
10 1

Sample Output 2

-1

Sample Input 3

6 4
1 1
1 1
1 1
1 1
1 1
1 1

Sample Output 3

12

Sample Input 4

20 987654321
129662684 2
162021979 1
458437539 1
319670097 2
202863355 1
112218745 1
348732033 1
323036578 1
382398703 1
55854389 1
283445191 1
151300613 1
693338042 2
191178308 2
386707193 1
204580036 1
335134457 1
122253639 1
824646518 2
902554792 2

Sample Output 4

14829091348