実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君と青木君が石取りゲームで対戦します。
テーブルの上に N 個の石の山があり、それぞれの山には 1 から N までの番号が付けられています。山 i には A_i 個の石が積まれています。
ゲームは高橋君から始まり、二人は交互に以下の操作を行います:
- まだ石が残っている山を 1 つ選び、その山から石を 1 個取り除く。
- 石が 0 個になった山は空になり、以降選ぶことができなくなる。
すべての山が空になったとき、最後の石を取ったプレイヤーが勝ちとなります。
両者が最適に行動したとき、どちらのプレイヤーが勝つか判定してください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
N A_1 A_2 \ldots A_N
- 1 行目には、石の山の数 N が与えられる。
- 2 行目には、各山の石の個数を表す A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
高橋君が勝つ場合は Takahashi と、青木君が勝つ場合は Aoki と出力してください。
入力例 1
3 1 2 1
出力例 1
Aoki
入力例 2
2 3 4
出力例 2
Takahashi
入力例 3
8 7 1 4 2 6 3 5 8
出力例 3
Aoki
入力例 4
20 1000000000 999999999 123456789 987654321 500000000 400000000 300000000 200000000 100000000 999999998 7654321 8765432 135791357 246802468 111111111 222222222 333333333 444444444 555555555 666666667
出力例 4
Takahashi
入力例 5
1 1000000000
出力例 5
Aoki
Score : 233 pts
Problem Statement
Takahashi and Aoki are playing a stone taking game.
There are N piles of stones on the table, and each pile is numbered from 1 to N. Pile i contains A_i stones.
The game starts with Takahashi, and the two players alternately perform the following operation:
- Choose one pile that still has stones remaining, and remove 1 stone from that pile.
- A pile with 0 stones becomes empty and can no longer be chosen.
When all piles become empty, the player who took the last stone wins.
Determine which player wins when both players play optimally.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- All inputs are integers
Input
N A_1 A_2 \ldots A_N
- The first line gives the number of piles of stones N.
- The second line gives A_1, A_2, \ldots, A_N, the number of stones in each pile, separated by spaces.
Output
If Takahashi wins, print Takahashi. If Aoki wins, print Aoki.
Sample Input 1
3 1 2 1
Sample Output 1
Aoki
Sample Input 2
2 3 4
Sample Output 2
Takahashi
Sample Input 3
8 7 1 4 2 6 3 5 8
Sample Output 3
Aoki
Sample Input 4
20 1000000000 999999999 123456789 987654321 500000000 400000000 300000000 200000000 100000000 999999998 7654321 8765432 135791357 246802468 111111111 222222222 333333333 444444444 555555555 666666667
Sample Output 4
Takahashi
Sample Input 5
1 1000000000
Sample Output 5
Aoki
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は N 人の友人と待ち合わせをすることになりました。
友人たちは数直線上に位置しており、 i 番目の友人は座標 X_i にいます(異なる友人が同じ座標にいることもあります)。
高橋君は待ち合わせ場所を 1 か所だけ決めようとしています。待ち合わせ場所を数直線上の整数座標 P に設定すると、各友人はその場所まで移動します。このとき、友人全員の移動距離の合計を「総移動距離」と呼びます。すなわち、総移動距離は |X_1 - P| + |X_2 - P| + \cdots + |X_N - P| です。なお、高橋君自身の移動距離は総移動距離に含みません。
高橋君は、友人たちの負担をできるだけ減らすため、P を最適に選んで総移動距離を最小にしたいと考えています。
総移動距離の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i \leq 10^9
- 入力はすべて整数
入力
N X_1 X_2 \cdots X_N
- 1 行目には、友人の人数を表す整数 N が与えられる。
- 2 行目には、各友人の座標を表す整数 X_1, X_2, \ldots, X_N がスペース区切りで与えられる。
出力
総移動距離の最小値を整数で 1 行に出力せよ。
入力例 1
3 1 2 10
出力例 1
9
入力例 2
4 0 0 5 9
出力例 2
14
入力例 3
10 -12 4 7 -3 0 9 9 15 -8 2
出力例 3
65
入力例 4
20 12 -5 30 7 7 -18 42 0 3 3 3 50 -1 9 -15 100 -40 8 21 21
出力例 4
363
入力例 5
1 -1000000000
出力例 5
0
Score : 300 pts
Problem Statement
Takahashi is going to meet up with N friends.
His friends are located on a number line, and the i-th friend is at coordinate X_i (different friends may be at the same coordinate).
Takahashi wants to decide on exactly one meeting place. If he sets the meeting place at an integer coordinate P on the number line, each friend will travel to that location. The sum of all friends' travel distances is called the "total travel distance." That is, the total travel distance is |X_1 - P| + |X_2 - P| + \cdots + |X_N - P|. Note that Takahashi's own travel distance is not included in the total travel distance.
Takahashi wants to minimize the total travel distance by choosing P optimally, in order to reduce the burden on his friends as much as possible.
Find the minimum value of the total travel distance.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i \leq 10^9
- All inputs are integers.
Input
N X_1 X_2 \cdots X_N
- The first line contains an integer N, representing the number of friends.
- The second line contains integers X_1, X_2, \ldots, X_N separated by spaces, representing the coordinates of each friend.
Output
Print the minimum value of the total travel distance as an integer on a single line.
Sample Input 1
3 1 2 10
Sample Output 1
9
Sample Input 2
4 0 0 5 9
Sample Output 2
14
Sample Input 3
10 -12 4 7 -3 0 9 9 15 -8 2
Sample Output 3
65
Sample Input 4
20 12 -5 30 7 7 -18 42 0 3 3 3 50 -1 9 -15 100 -40 8 21 21
Sample Output 4
363
Sample Input 5
1 -1000000000
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は直線状の道路沿いで、受信する信号の強度の合計が最大となる地点を探しています。
道路は数直線で表され、座標が増加する方向を右、減少する方向を左とします。道路上には N 基の電波塔が設置されており、塔 1, 塔 2, \ldots, 塔 N と番号が付けられています。塔 i は座標 X_i の位置に建っており、X_1 \leq X_2 \leq \cdots \leq X_N を満たします(同じ座標に複数の塔が建っていることもあります)。
塔 i の電波は、塔の位置から左方向に距離 L_i、右方向に距離 R_i だけ届きます。すなわち、座標 X_i - L_i 以上 X_i + R_i 以下の範囲をカバーします。塔 i のカバー範囲内にいるとき、受信できる信号の強度は C_i です。
高橋君は道路上の任意の整数座標の地点を 1 つ選んで立ちます(負の座標も選べます)。高橋君が座標 p(整数)に立ったとき、受信できる信号の強度の合計は
\displaystyle\sum_{\substack{1 \leq i \leq N \\ X_i - L_i \leq p \leq X_i + R_i}} C_i
です。すなわち、p がカバー範囲に含まれるすべての塔の信号の強度の和です。どの塔のカバー範囲にも含まれない地点を選んだ場合、信号の強度の合計は 0 です。
高橋君が立つ地点を最適に選んだとき、受信できる信号の強度の合計の最大値を求めてください。
制約
- 1 \leq N \leq 10^5
- 0 \leq X_i \leq 10^9
- X_1 \leq X_2 \leq \cdots \leq X_N
- 0 \leq L_i \leq 10^9
- 0 \leq R_i \leq 10^9
- 1 \leq C_i \leq 10^4
- 入力はすべて整数である。
入力
N X_1 L_1 R_1 C_1 X_2 L_2 R_2 C_2 \vdots X_N L_N R_N C_N
1 行目には、電波塔の数を表す整数 N が与えられる。続く N 行のうち i 行目 (1 \leq i \leq N) には、塔 i の座標 X_i、電波が左に届く距離 L_i、電波が右に届く距離 R_i、信号の強度 C_i が、スペース区切りで与えられる。
出力
高橋君が受信できる信号の強度の合計の最大値を整数として 1 行で出力せよ。
入力例 1
3 2 1 2 5 5 2 0 4 6 1 1 3
出力例 1
9
入力例 2
4 0 0 0 7 0 1 2 3 3 1 0 5 10 0 0 1
出力例 2
10
入力例 3
8 1 1 0 2 4 2 3 5 6 0 2 4 8 3 1 6 10 5 0 3 10 0 4 7 13 2 2 1 20 10 0 8
出力例 3
18
入力例 4
15 0 0 5 2 2 1 2 4 4 3 0 6 7 2 5 3 9 0 0 8 12 4 1 5 15 5 5 7 18 3 2 4 18 0 6 9 23 10 0 1 25 2 3 6 30 8 4 5 35 0 0 10 40 7 7 2 50 20 0 8
出力例 4
21
入力例 5
1 1000000000 1000000000 1000000000 10000
出力例 5
10000
Score : 366 pts
Problem Statement
Takahashi is searching for a point along a straight road where the total strength of received signals is maximized.
The road is represented by a number line, where the direction of increasing coordinates is called right and the direction of decreasing coordinates is called left. There are N radio towers installed along the road, numbered Tower 1, Tower 2, \ldots, Tower N. Tower i is built at coordinate X_i, satisfying X_1 \leq X_2 \leq \cdots \leq X_N (multiple towers may be built at the same coordinate).
The radio waves from Tower i reach a distance of L_i to the left and R_i to the right from the tower's position. That is, it covers the range from coordinate X_i - L_i to coordinate X_i + R_i, inclusive. When within the coverage area of Tower i, the strength of the signal that can be received is C_i.
Takahashi will choose and stand at any single point on the road with an integer coordinate (negative coordinates are also allowed). When Takahashi stands at coordinate p (an integer), the total strength of the signals he can receive is
\displaystyle\sum_{\substack{1 \leq i \leq N \\ X_i - L_i \leq p \leq X_i + R_i}} C_i
That is, it is the sum of signal strengths of all towers whose coverage area includes p. If the chosen point is not within the coverage area of any tower, the total signal strength is 0.
Find the maximum total signal strength Takahashi can receive when he optimally chooses where to stand.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq X_i \leq 10^9
- X_1 \leq X_2 \leq \cdots \leq X_N
- 0 \leq L_i \leq 10^9
- 0 \leq R_i \leq 10^9
- 1 \leq C_i \leq 10^4
- All inputs are integers.
Input
N X_1 L_1 R_1 C_1 X_2 L_2 R_2 C_2 \vdots X_N L_N R_N C_N
The first line contains an integer N representing the number of radio towers. The i-th of the following N lines (1 \leq i \leq N) contains the coordinate X_i of Tower i, the distance L_i the radio waves reach to the left, the distance R_i the radio waves reach to the right, and the signal strength C_i, separated by spaces.
Output
Output the maximum total signal strength that Takahashi can receive, as an integer on a single line.
Sample Input 1
3 2 1 2 5 5 2 0 4 6 1 1 3
Sample Output 1
9
Sample Input 2
4 0 0 0 7 0 1 2 3 3 1 0 5 10 0 0 1
Sample Output 2
10
Sample Input 3
8 1 1 0 2 4 2 3 5 6 0 2 4 8 3 1 6 10 5 0 3 10 0 4 7 13 2 2 1 20 10 0 8
Sample Output 3
18
Sample Input 4
15 0 0 5 2 2 1 2 4 4 3 0 6 7 2 5 3 9 0 0 8 12 4 1 5 15 5 5 7 18 3 2 4 18 0 6 9 23 10 0 1 25 2 3 6 30 8 4 5 35 0 0 10 40 7 7 2 50 20 0 8
Sample Output 4
21
Sample Input 5
1 1000000000 1000000000 1000000000 10000
Sample Output 5
10000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君の家の前に、N 枚の板からなるフェンスがあります。板には左から順に 1 から N までの番号が付いています。
最初、すべての板の色番号は 0(未塗装)です。
高橋君はこれから M 回の塗り替えを順に行います。i 回目 (1 \leq i \leq M) の塗り替えでは、板 L_i から板 R_i まで(両端を含む)のすべての板を色番号 C_i で上書きします。すでに色が付いている板も、新しい色番号 C_i に変わります。
すべての塗り替えが終わった後の、各板の最終的な色番号を求めてください。
制約
- 1 \leq N \leq 5 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 (1 \leq i \leq M)
- 入力はすべて整数である。
入力
N M L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_M R_M C_M
1 行目には、板の枚数 N と塗り替えの回数 M がスペース区切りで与えられる。続く M 行のうち i 行目には、i 回目の塗り替えで塗る区間の左端 L_i、右端 R_i、色番号 C_i がスペース区切りで与えられる。
出力
最終的な各板の色番号を、板 1 から板 N までの順にスペース区切りで 1 行に出力してください。
入力例 1
5 3 1 3 2 2 5 4 4 4 7
出力例 1
2 4 4 7 4
入力例 2
6 4 3 6 1 1 2 9 2 5 3 6 6 8
出力例 2
9 3 3 3 3 8
入力例 3
12 7 1 12 5 3 8 2 6 10 9 2 2 4 11 12 7 5 7 1 9 9 6
出力例 3
5 4 2 2 1 1 1 9 6 9 7 7
入力例 4
20 11 1 5 3 6 10 4 11 15 5 16 20 6 4 17 8 2 2 1 19 20 9 7 13 2 1 20 10 5 5 11 10 18 12
出力例 4
10 10 10 10 11 10 10 10 10 12 12 12 12 12 12 12 12 12 10 10
入力例 5
1 0
出力例 5
0
Score : 400 pts
Problem Statement
There is a fence consisting of N boards in front of Takahashi's house. The boards are numbered from 1 to N from left to right.
Initially, the color number of every board is 0 (unpainted).
Takahashi will perform M repainting operations in order. In the i-th (1 \leq i \leq M) repainting operation, he overwrites all boards from board L_i to board R_i (inclusive) with color number C_i. Even boards that are already painted will be changed to the new color number C_i.
Determine the final color number of each board after all repainting operations are completed.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 (1 \leq i \leq M)
- All inputs are integers.
Input
N M L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_M R_M C_M
The first line contains the number of boards N and the number of repainting operations M, separated by a space. Each of the following M lines contains, for the i-th repainting operation, the left endpoint L_i, the right endpoint R_i, and the color number C_i, separated by spaces.
Output
Print the final color numbers of each board, from board 1 to board N, separated by spaces, on a single line.
Sample Input 1
5 3 1 3 2 2 5 4 4 4 7
Sample Output 1
2 4 4 7 4
Sample Input 2
6 4 3 6 1 1 2 9 2 5 3 6 6 8
Sample Output 2
9 3 3 3 3 8
Sample Input 3
12 7 1 12 5 3 8 2 6 10 9 2 2 4 11 12 7 5 7 1 9 9 6
Sample Output 3
5 4 2 2 1 1 1 9 6 9 7 7
Sample Input 4
20 11 1 5 3 6 10 4 11 15 5 16 20 6 4 17 8 2 2 1 19 20 9 7 13 2 1 20 10 5 5 11 10 18 12
Sample Output 4
10 10 10 10 11 10 10 10 10 12 12 12 12 12 12 12 12 12 10 10
Sample Input 5
1 0
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は、N 個の街からなる王国に住んでいます。街には 1 から N の番号が付けられています。
街と街の間には一方通行の道がいくつか整備されており、合計 M 本の道の情報が与えられます。i 番目の道(1 \leq i \leq M)は街 U_i から街 V_i への一方通行の道で(U_i \neq V_i)、「魔力値」と呼ばれる非負整数 W_i が設定されています。同じ順序付きの組 (U_i, V_i) が複数回与えられることはありません。すなわち、任意の順序付きの街の組 (u, v)(u \neq v)に対して、街 u から街 v への一方通行の道は高々 1 本です。なお、街 u から街 v への道と街 v から街 u への道は別々に存在しうることに注意してください。街 u から街 v への道が存在するとき、その魔力値を W(u, v) と書きます。
高橋君は街 1 から出発し、道をちょうど K 回通って街 N にたどり着きたいと考えています。より正確には、K+1 個の街の番号からなる列 v_0, v_1, \ldots, v_K であって以下の条件をすべて満たすものを 経路 と呼びます。
- v_0 = 1
- v_K = N
- すべての j = 1, 2, \ldots, K に対して、街 v_{j-1} から街 v_j への道が存在する
同じ街や同じ道を複数回通ることも許されます。また、途中で街 N を経由すること(すなわち v_1, v_2, \ldots, v_{K-1} のいずれかが N であること)も許されます。
経路 v_0, v_1, \ldots, v_K の 総魔力 を、通った K 本の道の魔力値の積として定義します。すなわち、
\prod_{j=1}^{K} W(v_{j-1}, v_j)
です。
高橋君は、考えられるすべての経路について総魔力を計算し、それらの 総和 を求めたいと考えています。
この値は非常に大きくなる可能性があるため、998244353 で割った余りを求めてください。ここで、998244353 は素数です。答えは 0 以上 998244352 以下の整数として出力してください。
制約
- 2 \leq N \leq 100
- 0 \leq M \leq N(N-1)
- 1 \leq K \leq 10^9
- 1 \leq U_i \leq N
- 1 \leq V_i \leq N
- U_i \neq V_i
- 0 \leq W_i \leq 10^9
- 同じ順序付きの組 (U_i, V_i) が複数回与えられることはない
- 入力はすべて整数
入力
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- 1 行目には、街の数 N、道の数 M、通る道の本数 K が、スペース区切りで与えられる。
- 続く M 行で、各道の情報が与えられる(M = 0 の場合、この部分は存在しない)。
- そのうち i 行目(1 \leq i \leq M)では、i 番目の道の出発地 U_i、到着地 V_i、魔力値 W_i が、スペース区切りで与えられる。
出力
ちょうど K 本の道を通って街 1 から街 N に到着するすべての経路について、総魔力(通った道の魔力値の積)の総和を 998244353 で割った余りを 1 行で出力せよ。条件を満たす経路が 1 つも存在しない場合は 0 を出力せよ。
入力例 1
4 4 2 1 2 2 1 3 3 2 4 5 3 4 4
出力例 1
22
入力例 2
3 1 2 1 2 5
出力例 2
0
入力例 3
5 8 3 1 2 3 1 3 2 2 3 4 2 4 1 3 4 5 3 5 2 4 5 3 4 2 1
出力例 3
63
入力例 4
10 15 1000000000 1 2 100 2 3 200 3 4 300 4 5 400 5 6 500 6 7 600 7 8 700 8 9 800 9 10 900 1 5 10 5 10 20 3 7 50 2 10 1000000000 1 10 999999999 7 10 42
出力例 4
0
入力例 5
2 0 1
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi lives in a kingdom consisting of N cities. The cities are numbered from 1 to N.
There are several one-way roads between cities, and information about a total of M roads is given. The i-th road (1 \leq i \leq M) is a one-way road from city U_i to city V_i (U_i \neq V_i), and has a non-negative integer W_i called its "magic value". The same ordered pair (U_i, V_i) is never given more than once. That is, for any ordered pair of cities (u, v) (u \neq v), there is at most one one-way road from city u to city v. Note that a road from city u to city v and a road from city v to city u may exist independently. When a road from city u to city v exists, we denote its magic value as W(u, v).
Takahashi wants to start from city 1 and arrive at city N by traversing exactly K roads. More precisely, a sequence of K+1 city numbers v_0, v_1, \ldots, v_K that satisfies all of the following conditions is called a path:
- v_0 = 1
- v_K = N
- For all j = 1, 2, \ldots, K, there exists a road from city v_{j-1} to city v_j
Visiting the same city or traversing the same road multiple times is allowed. Passing through city N along the way (i.e., having N appear among v_1, v_2, \ldots, v_{K-1}) is also allowed.
The total magic of a path v_0, v_1, \ldots, v_K is defined as the product of the magic values of the K roads traversed. That is,
\prod_{j=1}^{K} W(v_{j-1}, v_j)
Takahashi wants to compute the total magic for every possible path and find their sum.
Since this value can be extremely large, find the remainder when divided by 998244353. Here, 998244353 is a prime number. Output the answer as an integer between 0 and 998244352, inclusive.
Constraints
- 2 \leq N \leq 100
- 0 \leq M \leq N(N-1)
- 1 \leq K \leq 10^9
- 1 \leq U_i \leq N
- 1 \leq V_i \leq N
- U_i \neq V_i
- 0 \leq W_i \leq 10^9
- The same ordered pair (U_i, V_i) is never given more than once
- All input values are integers
Input
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- The first line contains the number of cities N, the number of roads M, and the number of roads to traverse K, separated by spaces.
- The following M lines give the information for each road (if M = 0, this part does not exist).
- The i-th of these lines (1 \leq i \leq M) contains the starting city U_i, the destination city V_i, and the magic value W_i of the i-th road, separated by spaces.
Output
Output in a single line the remainder when the sum of total magic values (products of the magic values of traversed roads) over all paths that arrive at city N from city 1 by traversing exactly K roads is divided by 998244353. If no path satisfying the conditions exists, output 0.
Sample Input 1
4 4 2 1 2 2 1 3 3 2 4 5 3 4 4
Sample Output 1
22
Sample Input 2
3 1 2 1 2 5
Sample Output 2
0
Sample Input 3
5 8 3 1 2 3 1 3 2 2 3 4 2 4 1 3 4 5 3 5 2 4 5 3 4 2 1
Sample Output 3
63
Sample Input 4
10 15 1000000000 1 2 100 2 3 200 3 4 300 4 5 400 5 6 500 6 7 600 7 8 700 8 9 800 9 10 900 1 5 10 5 10 20 3 7 50 2 10 1000000000 1 10 999999999 7 10 42
Sample Output 4
0
Sample Input 5
2 0 1
Sample Output 5
0