実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正の整数 L に対して、 レベル L のダンゴ文字列とは、以下の条件を満たす文字列です。
oと-からなる長さ L+1 の文字列である。- 先頭の文字と末尾の文字のうちちょうど一方が
-であり、そのほかの L 文字はすべてoである。
例えば、ooo- はレベル 3 のダンゴ文字列ですが、-ooo- や oo や o-oo- などはダンゴ文字列ではありません(より正確には、どのような正の整数 L に対してもレベル L のダンゴ文字列ではありません)。
2 種類の文字 o - からなる、長さ N の文字列 S が与えられます。
次の条件を満たすような正整数 X のうち、最大のものを求めてください。
- S の連続する部分文字列であって、レベル X のダンゴ文字列であるものが存在する。
ただし、そのような整数が存在しない場合、-1 と出力してください。
制約
- 1\leq N\leq 2\times10^5
- S は
o-からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S にレベル X のダンゴ文字列が含まれるような最大の X の値を 1 行で出力せよ。
そのような値が存在しない場合、-1 を出力せよ。
入力例 1
10 o-oooo---o
出力例 1
4
たとえば、S の 3 文字目から 7 文字目までに対応する部分文字列 oooo- は、レベル 4 のダンゴ文字列です。
S の部分文字列であってレベル 5 以上のダンゴ文字列であるようなものは存在しないため、4 と出力してください。
入力例 2
1 -
出力例 2
-1
S の連続する部分文字列は空文字列と - の 2 種類だけです。
これらはダンゴ文字列ではないため、-1 と出力してください。
入力例 3
30 -o-o-oooo-oo-o-ooooooo--oooo-o
出力例 3
7
Score : 300 points
Problem Statement
For a positive integer L, a level-L dango string is a string that satisfies the following conditions.
- It is a string of length L+1 consisting of
oand-. - Exactly one of the first character and the last character is
-, and the other L characters areo.
For instance, ooo- is a level-3 dango string, but none of -ooo-, oo, and o-oo- is a dango string (more precisely, none of them is a level-L dango string for any positive integer L).
You are given a string S of length N consisting of the two characters o and -.
Find the greatest positive integer X that satisfies the following condition.
- There is a contiguous substring of S that is a level-X dango string.
If there is no such integer, print -1.
Constraints
- 1\leq N\leq 2\times10^5
- S is a string of length N consisting of
oand-.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the greatest positive integer X such that S contains a level-X dango string, or -1 if there is no such integer.
Sample Input 1
10 o-oooo---o
Sample Output 1
4
For instance, the substring oooo- corresponding to the 3-rd through 7-th characters of S is a level-4 dango string.
No substring of S is a level-5 dango string or above, so you should print 4.
Sample Input 2
1 -
Sample Output 2
-1
Only the empty string and - are the substrings of S.
They are not dango strings, so you should print -1.
Sample Input 3
30 -o-o-oooo-oo-o-ooooooo--oooo-o
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
二次元平面上に高橋君がいます。高橋君は原点から移動を N 回行いました。
N 回の移動は長さ N の文字列で表され、意味は次の通りです。
- i 回目の高橋君の移動後の座標は、移動前の座標を (x,y) として、
- S の i 文字目が
Rであるとき (x+1,y) - S の i 文字目が
Lであるとき (x-1,y) - S の i 文字目が
Uであるとき (x,y+1) - S の i 文字目が
Dであるとき (x,y-1)
- S の i 文字目が
N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあるかどうかを判定してください。
制約
- 1 \leq N \leq 2\times 10^5
- N は整数
- S は
R,L,U,Dのみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあれば Yes、なければ No と出力せよ。
入力例 1
5 RLURU
出力例 1
Yes
高橋君のいる座標は (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2) と変化します。
入力例 2
20 URDDLLUUURRRDDDDLLLL
出力例 2
No
Score : 300 points
Problem Statement
Takahashi is on a two-dimensional plane. Starting from the origin, he made N moves.
The N moves are represented by a string of length N as described below:
-
Takahashi's coordinates after the i-th move are:
- (x+1,y) if the i-th character of S is
R; - (x-1,y) if the i-th character of S is
L; - (x,y+1) if the i-th character of S is
U; and - (x,y-1) if the i-th character of S is
D,
where (x,y) is his coordinates before the move.
- (x+1,y) if the i-th character of S is
Determine if Takahashi visited the same coordinates multiple times in the course of the N moves (including the starting and ending points).
Constraints
- 1 \leq N \leq 2\times 10^5
- N is an integer.
- S is a string of length N consisting of
R,L,U, andD.
Input
The input is given from Standard Input in the following format:
N S
Output
Print Yes if Takahashi visited the same coordinates multiple times in the course of the N moves; print No otherwise.
Sample Input 1
5 RLURU
Sample Output 1
Yes
Takahashi's coordinates change as follows: (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2).
Sample Input 2
20 URDDLLUUURRRDDDDLLLL
Sample Output 2
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
AtCoder 国は無限に広がる直交座標の上にあります。
AtCoder 国には N 個の街があり、 1,2,\dots,N と番号が付けられています。街 i は地点 (x_i, y_i) にあり、2 つの異なる番号の街が同じ座標にあることはありません。
AtCoder 国には転移魔法(以下、魔法と表記)があります。魔法は整数の組 (a,b) によって識別されていて、地点 (x,y) にいるときに魔法 (a,b) を使うと (x+a,y+b) にワープすることができます。
すぬけ君は、任意の整数の組 (a,b) を選んで魔法 (a,b) を覚えることができる大魔術師です。また、すぬけ君は何種類でも魔法を覚えることができます。
魔法を使って街と街の間を移動したくなったすぬけ君は、全ての相異なる街の組 (i,j) について次の行動を取れるようにいくつかの魔法を覚えることにしました。
- 覚えた魔法のうち 1 種類の魔法のみ を選ぶ。その後、選んだ魔法 のみ を繰り返し使って街 i から 街 j に移動する。
すぬけ君が上の条件を満たすように魔法を覚えるとき、少なくとも何種類の魔法を覚えればよいですか?
制約
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- i \neq j ならば (x_i, y_i) \neq (x_j, y_j) である。
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
すぬけ君が覚える必要がある魔法の個数の最小値を出力せよ。
入力例 1
3 1 2 3 6 7 4
出力例 1
6
AtCoder 国の街の位置を図示したのが下の図です。(わかりやすさのために四隅の座標を表示しています。)

すぬけ君は次の 6 種類の魔法を覚えれば、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 1 回使うことで街 j に着くことができるので条件を満たします。
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
次の 6 種類の魔法も条件を満たします。このときすぬけ君は、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 2 回使うことで街 j に着くことができます。
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
条件を満たす魔法の組み合わせのうち 6 種類より少ないものは存在しないので、 6 を出力します。
入力例 2
3 1 2 2 2 4 2
出力例 2
2
次の 2 種類の魔法を覚えるのが最適です。
- (1, 0)
- (-1, 0)
入力例 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
出力例 3
8
Score : 400 points
Problem Statement
The Republic of AtCoder lies on a Cartesian coordinate plane.
It has N towns, numbered 1,2,\dots,N. Town i is at (x_i, y_i), and no two different towns are at the same coordinates.
There are teleportation spells in the nation. A spell is identified by a pair of integers (a,b), and casting a spell (a, b) at coordinates (x, y) teleports you to (x+a, y+b).
Snuke is a great magician who can learn the spell (a, b) for any pair of integers (a, b) of his choice. The number of spells he can learn is also unlimited.
To be able to travel between the towns using spells, he has decided to learn some number of spells so that it is possible to do the following for every pair of different towns (i, j).
- Choose just one spell among the spells learned. Then, repeatedly use just the chosen spell to get from Town i to Town j.
At least how many spells does Snuke need to learn to achieve the objective above?
Constraints
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- (x_i, y_i) \neq (x_j, y_j) if i \neq j.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 x_2 y_2 \vdots x_N y_N
Output
Print the minimum number of spells Snuke needs to learn.
Sample Input 1
3 1 2 3 6 7 4
Sample Output 1
6
The figure below illustrates the positions of the towns (along with the coordinates of the four corners).

If Snuke learns the six spells below, he can get from Town i to Town j by using one of the spells once for every pair (i,j) (i \neq j), achieving his objective.
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
Another option is to learn the six spells below. In this case, he can get from Town i to Town j by using one of the spells twice for every pair (i,j) (i \neq j), achieving his objective.
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
There is no combination of spells that consists of less than six spells and achieve the objective, so we should print 6.
Sample Input 2
3 1 2 2 2 4 2
Sample Output 2
2
The optimal choice is to learn the two spells below:
- (1, 0)
- (-1, 0)
Sample Input 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
高橋君は N 曲からなるプレイリストを持っています。
曲 i (1 \leq i \leq N) の長さは T_i 秒です。
高橋君は時刻 0 にプレイリストのランダム再生を開始しました。
ランダム再生では、N 曲の中から等確率で 1 つを選びその曲を最後まで再生することが繰り返されます。 ここで、曲の再生は休みなく行われ、1 つの曲が終わったらすぐに次に選ばれた曲が始まります。 また、同じ曲が連続して選ばれる事もあります。
時刻 0 から (X+0.5) 秒後に曲 1 が再生されている確率を \text{mod}998244353 で求めてください。
確率 \text{mod } 998244353 の定義
この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。
このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。
制約
- 2 \leq N\leq 10^3
- 0 \leq X\leq 10^4
- 1 \leq T_i\leq 10^4
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X T_1 T_2 \ldots T_N
出力
時刻 0 から (X+0.5) 秒後にプレイリストの 1 番目の曲が再生されている確率を \text{mod}998244353 で出力せよ。
入力例 1
3 6 3 5 6
出力例 1
369720131
時刻 0 から 6.5 秒後に曲 1 が流れているパターンとしてあり得るのは、
- 曲 1 \to 曲 1 \to 曲 1
- 曲 2 \to 曲 1
- 曲 3 \to 曲 1
の順で音楽が再生された場合であり、これらのいずれかが起こる確率は \frac{7}{27} となります。
369720131\times 27\equiv 7 \pmod{998244353} であるため、369720131 を出力します。
入力例 2
5 0 1 2 1 2 1
出力例 2
598946612
時刻 0 から 0.5 秒後には最初に再生された曲が再生されているため、求める確率は \frac{1}{5} となります。
同じ長さの異なる曲が存在することがあることに注意してください。
入力例 3
5 10000 1 2 3 4 5
出力例 3
586965467
Score : 450 points
Problem Statement
Takahashi has a playlist with N songs.
Song i (1 \leq i \leq N) lasts T_i seconds.
Takahashi has started random play of the playlist at time 0.
Random play repeats the following: choose one song from the N songs with equal probability and play that song to the end. Here, songs are played continuously: once a song ends, the next chosen song starts immediately. The same song can be chosen consecutively.
Find the probability that song 1 is being played (X + 0.5) seconds after time 0, modulo 998244353.
How to print a probability modulo 998244353
It can be proved that the probability to be found in this problem is always a rational number. Also, the constraints of this problem guarantee that when the probability to be found is expressed as an irreducible fraction \frac{y}{x}, x is not divisible by 998244353.
Then, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.
Constraints
- 2 \leq N\leq 10^3
- 0 \leq X\leq 10^4
- 1 \leq T_i\leq 10^4
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X T_1 T_2 \ldots T_N
Output
Print the probability, modulo 998244353, that the first song in the playlist is being played (X+0.5) seconds after time 0.
Sample Input 1
3 6 3 5 6
Sample Output 1
369720131
Song 1 will be playing 6.5 seconds after time 0 if songs are played in one of the following orders.
- Song 1 \to Song 1 \to Song 1
- Song 2 \to Song 1
- Song 3 \to Song 1
The probability that one of these occurs is \frac{7}{27}.
We have 369720131\times 27\equiv 7 \pmod{998244353}, so you should print 369720131.
Sample Input 2
5 0 1 2 1 2 1
Sample Output 2
598946612
0.5 seconds after time 0, the first song to be played is still playing, so the sought probability is \frac{1}{5}.
Note that different songs may have the same length.
Sample Input 3
5 10000 1 2 3 4 5
Sample Output 3
586965467
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
あなたは店で N 個の商品を買おうとしています。 i 個目の商品の定価は P_i 円です。
また、あなたは M 枚のクーポンを持っています。i 枚目のクーポンを使うと、定価が L_i 円以上の商品を一つ選び、その商品を定価より D_i 円低い価格で買うことができます。
ここで、一つのクーポンは一回までしか使えません。また、複数のクーポンを同じ商品に重ねて使うことはできません。
クーポンを使わなかった商品は定価で買うことになります。 N 個すべての商品を買うのに必要な最小の金額を求めてください。
制約
- 1\leq N,M\leq 2\times 10^5
- 1\leq P_i\leq 10^9
- 1\leq D_i \leq L_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P_1 \ldots P_N L_1 \ldots L_M D_1 \ldots D_M
出力
答えを整数として出力せよ。
入力例 1
3 3 4 3 1 4 4 2 2 3 1
出力例 1
4
2 枚目のクーポンを 1 個目の商品に、 3 枚目のクーポンを 2 個目の商品に使うことを考えます。
このとき、1 個目の商品を 4-3=1 円、2 個目の商品を 3-1=2 円、3 個目の商品を 1 円で買うことになるので、 1+2+1=4 円で全ての商品を買うことができます。
入力例 2
10 5 9 7 1 5 2 2 5 5 7 6 7 2 7 8 2 3 2 4 1 2
出力例 2
37
Score : 500 points
Problem Statement
You are in a store to buy N items. The regular price of the i-th item is P_i yen (the currency in Japan).
You have M coupons. You can use the i-th coupon to buy an item whose regular price is at least L_i yen at a D_i-yen discount.
Here, each coupon can be used only once. Besides, multiple coupons cannot be used for the same item.
If no coupon is used for an item, you will buy it for a regular price. Find the minimum possible total amount of money required to buy all the N items.
Constraints
- 1\leq N,M\leq 2\times 10^5
- 1\leq P_i\leq 10^9
- 1\leq D_i \leq L_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P_1 \ldots P_N L_1 \ldots L_M D_1 \ldots D_M
Output
Print the answer as an integer.
Sample Input 1
3 3 4 3 1 4 4 2 2 3 1
Sample Output 1
4
Consider using the 2-nd coupon for the 1-st item, and the 3-rd coupon for the 2-nd item.
Then, you buy the 1-st item for 4-3=1 yen, 2-nd item for 3-1=2 yen, and 3-rd item for 1 yen. Thus, you can buy all the items for 1+2+1=4 yen.
Sample Input 2
10 5 9 7 1 5 2 2 5 5 7 6 7 2 7 8 2 3 2 4 1 2
Sample Output 2
37