A - Futon Pairs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君の旅館では N 日間の宿泊予約が入っています。 i 日目には大人が A_i 人、子供が B_i 人泊まります。

旅館では布団を 2 人で 1 組として敷きます。その日の宿泊者の合計人数が偶数ならちょうど組が作れますが、奇数なら 1 人だけ余ります。

各日について、宿泊者がちょうど組になるなら o 、 1 人余るなら x として、 1 日目から順に並べた長さ N の文字列を出力してください。

制約

  • 1 \leq N \leq 100
  • 0 \leq A_i \leq 100
  • 0 \leq B_i \leq 100
  • 入力はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目には、日数 N が与えられる。
  • 2 行目から N 行にわたり、 1 + i 行目には i 日目の大人の人数 A_i と子供の人数 B_i がスペース区切りで与えられる。

出力

長さ N の文字列を 1 行で出力せよ。


入力例 1

3
3 4
2 2
5 0

出力例 1

xox

入力例 2

1
1 2

出力例 2

x

入力例 3

2
4 6
7 8

出力例 3

ox

入力例 4

3
3 4
2 2
6 0

出力例 4

xoo

入力例 5

1
0 100

出力例 5

o

Score : 200 pts

Problem Statement

Takahashi's inn has lodging reservations for N days. On day i, A_i adults and B_i children will stay.

At the inn, futons are laid out in pairs of two people. If the total number of guests on a day is even, they form complete pairs, but if it is odd, one person is left over.

For each day, output o if the guests form complete pairs and x if one person is left over, as a string of length N listing the results in order starting from day 1.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq A_i \leq 100
  • 0 \leq B_i \leq 100
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains the number of days N.
  • For each of the N lines starting from the second line, line 1 + i contains the number of adults A_i and the number of children B_i on day i, separated by a space.

Output

Output a single string of length N on one line.


Sample Input 1

3
3 4
2 2
5 0

Sample Output 1

xox

Sample Input 2

1
1 2

Sample Output 2

x

Sample Input 3

2
4 6
7 8

Sample Output 3

ox

Sample Input 4

3
3 4
2 2
6 0

Sample Output 4

xoo

Sample Input 5

1
0 100

Sample Output 5

o
B - Steady Hours

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は自分の部屋の照明の状態を N 時間にわたって毎時記録しました。記録は長さ N の文字列 S で表され、 S の i 文字目が o なら i 時間目に照明はついており、 x なら消えていました。

2 時間目から N 時間目までのうち、直前の時間と照明の状態が同じだった時間の数を求めてください。

制約

  • 1 \leq N \leq 100
  • S は o と x のみからなる長さ N の文字列

入力

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

N
S
  • 1 行目には、記録した時間数 N が与えられる。
  • 2 行目には、記録を表す文字列 S が与えられる。

出力

直前の時間と状態が同じだった時間の数を 1 行で出力せよ。


入力例 1

6
ooxxox

出力例 1

2

入力例 2

4
oxox

出力例 2

0

入力例 3

5
oooox

出力例 3

3

入力例 4

74
ooxxxxoxoxxooxxxoooooxxxoooxxxooxoxoxoooooooxooxoooxoxxoooxxxooxxxoxoxoxxo

出力例 4

37

入力例 5

6
ooxoxo

出力例 5

1

Score : 233 pts

Problem Statement

Takahashi recorded the state of the lighting in his room every hour for N hours. The record is represented by a string S of length N, where the i-th character of S is o if the light was on during the i-th hour, and x if it was off.

Among the 2-nd through N-th hours, find the number of hours during which the state of the lighting was the same as in the previous hour.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting only of o and x.

Input

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

N
S
  • The first line contains N, the number of hours recorded.
  • The second line contains the string S representing the record.

Output

Print the number of hours whose state was the same as the previous hour in one line.


Sample Input 1

6
ooxxox

Sample Output 1

2

Sample Input 2

4
oxox

Sample Output 2

0

Sample Input 3

5
oooox

Sample Output 3

3

Sample Input 4

74
ooxxxxoxoxxooxxxoooooxxxoooxxxooxoxoxoooooooxooxoooxoxxoooxxxooxxxoxoxoxxo

Sample Output 4

37

Sample Input 5

6
ooxoxo

Sample Output 5

1
C - Orchard Harvest Plan

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は、広大な果樹園を管理しています。

この果樹園には N 本の果樹が一列に並んでおり、左から順に 1, 2, \ldots, N の番号が付けられています。果樹 i からは、1 時間あたり A_i 個の果実を収穫することができます。どの果樹にも十分な量の果実が実っているため、収穫時間によって果実が足りなくなることはありません。

高橋君は、収穫作業の計画を立てるために、以下の Q 個のクエリに答える必要があります。

j 番目のクエリでは、収穫範囲の左端 L_j、右端 R_j、および収穫時間 T_j が与えられます。果樹 L_j から果樹 R_j までの範囲にあるすべての果樹について、それぞれ T_j 時間の収穫を行います。果樹 i(L_j \leq i \leq R_j)からの収穫個数は A_i \times T_j 個です。この範囲全体で収穫できる果実の合計個数、すなわち

\sum_{i=L_j}^{R_j} A_i \times T_j

を求めてください。

なお、各クエリは独立であり、あるクエリでの収穫が他のクエリに影響することはありません。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • 1 \leq T_j \leq 10^4 (1 \leq j \leq Q)
  • 入力はすべて整数

入力

N Q
A_1 A_2 \ldots A_N
L_1 R_1 T_1
L_2 R_2 T_2
\vdots
L_Q R_Q T_Q
  • 1 行目には、果樹の本数を表す整数 N と、クエリの個数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各果樹の 1 時間あたりの収穫個数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 続く Q 行にわたって、各クエリの情報が与えられる。
  • 2 + j 行目では、j 番目のクエリにおける収穫範囲の左端 L_j、右端 R_j、収穫時間 T_j が、スペース区切りで与えられる。

出力

Q 行にわたって、各クエリの答えを整数で出力してください。j 行目には、j 番目のクエリに対する答え、すなわち果樹 L_j から果樹 R_j までの各果樹から T_j 時間で収穫できる果実の合計個数を出力してください。


入力例 1

5 3
2 5 3 1 4
1 3 2
2 5 3
1 1 5

出力例 1

20
39
10

入力例 2

7 5
10 20 30 40 50 60 70
1 7 1
3 5 2
1 4 3
6 7 4
4 4 10

出力例 2

280
240
300
520
400

入力例 3

10 8
100 200 150 300 250 400 350 500 450 600
1 10 1
1 5 2
6 10 3
3 7 5
1 1 100
5 5 50
2 9 10
1 10 10000

出力例 3

3300
2000
6900
7250
10000
12500
26000
33000000

Score : 333 pts

Problem Statement

Takahashi manages a vast orchard.

In this orchard, there are N fruit trees lined up in a row, numbered 1, 2, \ldots, N from left to right. From fruit tree i, A_i fruits can be harvested per hour. Each tree has a sufficient amount of fruit, so the supply never runs out regardless of the harvesting time.

Takahashi needs to answer the following Q queries to plan the harvesting work.

In the j-th query, the left endpoint L_j, right endpoint R_j of the harvesting range, and the harvesting time T_j are given. For all fruit trees in the range from tree L_j to tree R_j, harvesting is performed for T_j hours each. The number of fruits harvested from tree i (L_j \leq i \leq R_j) is A_i \times T_j. Find the total number of fruits that can be harvested across this entire range, namely

\sum_{i=L_j}^{R_j} A_i \times T_j

Note that each query is independent, and harvesting in one query does not affect any other query.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • 1 \leq T_j \leq 10^4 (1 \leq j \leq Q)
  • All inputs are integers

Input

N Q
A_1 A_2 \ldots A_N
L_1 R_1 T_1
L_2 R_2 T_2
\vdots
L_Q R_Q T_Q
  • The first line contains an integer N representing the number of fruit trees and an integer Q representing the number of queries, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the number of fruits that can be harvested per hour from each fruit tree, separated by spaces.
  • The following Q lines provide the information for each query.
  • The (2 + j)-th line contains the left endpoint L_j, right endpoint R_j of the harvesting range, and the harvesting time T_j for the j-th query, separated by spaces.

Output

Output the answer to each query as an integer over Q lines. On the j-th line, output the answer to the j-th query, namely the total number of fruits that can be harvested in T_j hours from all fruit trees from tree L_j to tree R_j.


Sample Input 1

5 3
2 5 3 1 4
1 3 2
2 5 3
1 1 5

Sample Output 1

20
39
10

Sample Input 2

7 5
10 20 30 40 50 60 70
1 7 1
3 5 2
1 4 3
6 7 4
4 4 10

Sample Output 2

280
240
300
520
400

Sample Input 3

10 8
100 200 150 300 250 400 350 500 450 600
1 10 1
1 5 2
6 10 3
3 7 5
1 1 100
5 5 50
2 9 10
1 10 10000

Sample Output 3

3300
2000
6900
7250
10000
12500
26000
33000000
D - Color Arrangement in Connected Containers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

1 から N までの番号が付いた N 個の容器があります。各容器には赤い液体か青い液体が入っています。最初の状態は長さ N の文字列 S で与えられ、S の i 文字目が 1 なら容器 i には赤い液体が、0 なら青い液体が入っています。

交換装置が M 台あります。装置 j は容器 U_j と容器 V_j を接続しています。この装置を使うと、その二つの容器の内容物を丸ごと交換できます。液体が混ざることはありません。同じ容器を接続する装置や、同じ二つの容器を接続する複数の装置がある場合もあります。

高橋君は、好きな装置を選んで内容物を交換する操作を、好きな順序で何回でも行えます。一度も操作しなくてもかまいません。

操作後の各容器の色を番号順に並べた文字列として、あり得るものは何種類ありますか。答えを 998244353 で割った余りを求めてください。同じ色の液体どうしは区別せず、交換に使った装置や操作順が異なっても、各容器の色が同じなら同じ状態とします。

制約

  • 1\leq N\leq200000
  • 0\leq M\leq200000
  • S は 0 と 1 からなる長さ N の文字列である。
  • 1\leq U_j,V_j\leq N (1\leq j\leq M)
  • N,M,U_j,V_j は整数である。

入力

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

N M
S
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、容器の個数 N と交換装置の台数 M が与えられます。
  • 2 行目には、最初の色配置を表す文字列 S が与えられます。
  • 続く M 行のうち j 行目には、装置 j が接続する容器の番号 U_j,V_j が与えられます。
  • M=0 の場合、3 行目以降はありません。

出力

操作後の色配置としてあり得るものの種類数を 998244353 で割った余りを出力してください。


入力例 1

3 2
100
1 2
2 3

出力例 1

3

入力例 2

4 0
1010

出力例 2

1

入力例 3

6 3
110010
1 2
2 3
4 5

出力例 3

6

入力例 4

10 7
1010110010
1 2
2 3
3 4
4 5
6 7
7 8
8 9

出力例 4

60

入力例 5

1 1
1
1 1

出力例 5

1

Score : 400 pts

Problem Statement

There are N containers numbered 1 to N. Each container contains either red liquid or blue liquid. The initial state is given by a string S of length N; if the i-th character of S is 1, container i contains red liquid, and if it is 0, container i contains blue liquid.

There are M swapping devices. Device j connects container U_j and container V_j. Using this device allows you to completely swap the contents of these two containers. The liquids do not mix. There may be devices connecting the same container to itself, or multiple devices connecting the same pair of containers.

Takahashi can choose any device and swap the contents any number of times in any order. He may also choose not to perform any operations.

How many different strings can be formed by concatenating the colors of the containers in ascending order of their numbers after operations? Find the count modulo 998244353. Liquids of the same color are indistinguishable, and two states are considered the same if each container has the same color, regardless of which devices were used or the order of operations.

Constraints

  • 1\leq N\leq200000
  • 0\leq M\leq200000
  • S is a string of length N consisting of 0 and 1.
  • 1\leq U_j,V_j\leq N (1\leq j\leq M)
  • N,M,U_j,V_j are integers.

Input

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

N M
S
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains the number of containers N and the number of swapping devices M.
  • The second line contains the string S representing the initial color configuration.
  • The j-th of the following M lines contains the container numbers U_j,V_j connected by device j.
  • If M=0, there are no lines after the second line.

Output

Print the number of possible color configurations after operations, modulo 998244353.


Sample Input 1

3 2
100
1 2
2 3

Sample Output 1

3

Sample Input 2

4 0
1010

Sample Output 2

1

Sample Input 3

6 3
110010
1 2
2 3
4 5

Sample Output 3

6

Sample Input 4

10 7
1010110010
1 2
2 3
3 4
4 5
6 7
7 8
8 9

Sample Output 4

60

Sample Input 5

1 1
1
1 1

Sample Output 5

1
E - Journey of Refueling

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は、東西に一直線に並んだ N 個の町を西から東へ順番にドライブしています。町には西から順に 1, 2, \ldots, N の番号が付けられています。高橋君は途中で引き返すことはできず、常に東方向(番号が増える方向)にのみ進みます。

高橋君は町 1 にガソリンが 0 リットルの状態でスタートし、町 N に到達することが目標です。高橋君の車のガソリンタンクの容量は T リットルであり、タンクに T リットルを超えるガソリンを入れることはできません。

隣接する町 i と町 i + 1 (1 \leq i \leq N-1) の間を移動するには、ちょうど D_i リットルのガソリンが消費されます。移動は一度開始すると途中で止まることはできず、タンクに残っているガソリンが D_i リットル未満の場合、町 i から町 i+1 へ移動することはできません。

各町 i (1 \leq i \leq N) にはガソリンスタンドがあり、1 リットルあたり C_i 円でガソリンを購入できます。高橋君は各町に滞在している間に、購入後のタンク内のガソリン量が T リットルを超えない範囲で、0 以上の任意の実数リットルのガソリンを購入できます。

高橋君の行動は以下の手順を繰り返すことで進みます:

  1. 町 1 にいる状態からスタートする(タンクのガソリンは 0 リットル)。
  2. 現在いる町のガソリンスタンドで、好きな量のガソリンを購入する(0 リットルでもよい。ただし購入後のタンク内のガソリン量が T リットルを超えてはならない)。
  3. 現在いる町が町 N であれば終了する。そうでなければ、次の町へ移動する(町 i にいるなら町 i+1 へ移動し、タンクから D_i リットルのガソリンが消費される)。移動後、手順 2 に戻る。

高橋君が町 1 から町 N まで到達するために必要なガソリン代の合計の最小値を求めてください。

すべての i (1 \leq i \leq N-1) について D_i \leq T であれば町 N に到達することが可能であり、そうでなければ(すなわち、ある i について D_i > T であれば)到達は不可能です。到達不可能な場合は -1 を出力してください。

なお、到達が可能な場合、最適な購入戦略においては各町で整数リットルのガソリンを購入すれば十分であることが示せるため、最小コストは必ず整数になります。

(注:C_N は入力形式の統一のため与えられますが、町 N に到着した後は移動がないため、町 N でガソリンを購入する必要はありません。また、制約より、到達可能な場合の答えは符号付き 64-bit 整数に収まります。)

制約

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N-1)
  • \displaystyle \sum_{i=1}^{N-1} D_i \leq 10^9
  • 入力はすべて整数である。

入力

N T
C_1 C_2 \ldots C_N
D_1 D_2 \ldots D_{N-1}
  • 1 行目には、町の数を表す整数 N と、ガソリンタンクの容量を表す整数 T が、スペース区切りで与えられる。
  • 2 行目には、各町 i でのガソリンの 1 リットルあたりの価格を表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。
  • 3 行目には、隣接する町 i と町 i+1 の間の移動に必要なガソリン量を表す整数 D_1, D_2, \ldots, D_{N-1} が、スペース区切りで与えられる。

出力

高橋君が町 1 から町 N まで到達可能な場合、必要なガソリン代の合計の最小値を整数で 1 行に出力してください。到達が不可能な場合は -1 を出力してください。


入力例 1

4 10
5 2 4 1
4 6 3

出力例 1

38

入力例 2

3 5
3 1 4
4 6

出力例 2

-1

入力例 3

10 15
8 6 7 3 9 2 5 4 10 1
5 4 7 3 6 8 2 5 4

出力例 3

179

入力例 4

25 50
30 28 35 20 25 18 40 22 16 19 27 15 21 14 26 13 24 12 23 11 29 10 31 9 8
12 18 7 20 15 10 22 9 14 16 8 19 11 13 17 6 21 5 23 4 24 3 25 2

出力例 4

5269

入力例 5

2 1000000000
1000000000 1
1000000000

出力例 5

1000000000000000000

Score : 466 pts

Problem Statement

Takahashi is driving from west to east through N towns lined up in a straight line. The towns are numbered 1, 2, \ldots, N from west to east. Takahashi cannot turn back and always travels only in the eastward direction (the direction in which the town numbers increase).

Takahashi starts at Town 1 with 0 liters of fuel in his tank, and his goal is to reach Town N. The capacity of the fuel tank of Takahashi's car is T liters, and he cannot put more than T liters of fuel in the tank.

Moving between adjacent towns i and i + 1 (1 \leq i \leq N-1) consumes exactly D_i liters of fuel. Once a move starts, he cannot stop midway. If the fuel remaining in the tank is less than D_i liters, he cannot move from Town i to Town i+1.

Each town i (1 \leq i \leq N) has a gas station where fuel can be purchased at C_i yen per liter. While staying in each town, Takahashi can purchase any non-negative real number of liters of fuel, as long as the amount of fuel in the tank after purchase does not exceed T liters.

Takahashi's actions proceed by repeating the following steps:

  1. Start at Town 1 (with 0 liters of fuel in the tank).
  2. Purchase any amount of fuel at the gas station of the current town (it can be 0 liters, but the amount of fuel in the tank after purchase must not exceed T liters).
  3. If the current town is Town N, terminate. Otherwise, move to the next town (if currently in Town i, move to Town i+1, consuming D_i liters of fuel from the tank). After moving, return to Step 2.

Find the minimum total cost of fuel required for Takahashi to reach Town N from Town 1.

If D_i \leq T for all i (1 \leq i \leq N-1), it is possible to reach Town N; otherwise (i.e., if D_i > T for some i), it is impossible. If it is impossible to reach Town N, output -1.

Note that if it is possible to reach the destination, it can be shown that purchasing an integer number of liters of fuel at each town is sufficient in an optimal strategy, so the minimum cost is always an integer.

(Note: C_N is provided to unify the input format, but since there is no travel after arriving at Town N, there is no need to purchase fuel at Town N. Also, under the constraints, the answer fits within a signed 64-bit integer when reaching the destination is possible.)

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N-1)
  • \displaystyle \sum_{i=1}^{N-1} D_i \leq 10^9
  • All input values are integers.

Input

N T
C_1 C_2 \ldots C_N
D_1 D_2 \ldots D_{N-1}
  • The first line contains the integer N, representing the number of towns, and the integer T, representing the capacity of the fuel tank, separated by a space.
  • The second line contains the integers C_1, C_2, \ldots, C_N, representing the price of fuel per liter at each town i, separated by spaces.
  • The third line contains the integers D_1, D_2, \ldots, D_{N-1}, representing the amount of fuel required to move between adjacent towns i and i+1, separated by spaces.

Output

If Takahashi can reach Town N from Town 1, output the minimum total cost of fuel as an integer in a single line. If it is impossible to reach Town N, output -1.


Sample Input 1

4 10
5 2 4 1
4 6 3

Sample Output 1

38

Sample Input 2

3 5
3 1 4
4 6

Sample Output 2

-1

Sample Input 3

10 15
8 6 7 3 9 2 5 4 10 1
5 4 7 3 6 8 2 5 4

Sample Output 3

179

Sample Input 4

25 50
30 28 35 20 25 18 40 22 16 19 27 15 21 14 26 13 24 12 23 11 29 10 31 9 8
12 18 7 20 15 10 22 9 14 16 8 19 11 13 17 6 21 5 23 4 24 3 25 2

Sample Output 4

5269

Sample Input 5

2 1000000000
1000000000 1
1000000000

Sample Output 5

1000000000000000000