Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
xy 座標平面上に N 人の人がいます。人 i は (X_i, Y_i) にいます。すべての人は異なる地点にいます。
L, R からなる長さ N の文字列 S があります。
人 i は S_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。
たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i \leq 10^9
- 0 \leq Y_i \leq 10^9
- i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
- X_i, Y_i はすべて整数である。
- S は
LおよびRからなる長さ N の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N S
出力
衝突が発生するならば Yes を、発生しないならば No を出力せよ。
入力例 1
3 2 3 1 1 4 1 RRL
出力例 1
Yes
この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。
入力例 2
2 1 1 2 1 RR
出力例 2
No
人 1 と人 2 は同じ向きに歩いているので衝突することはありません。
入力例 3
10 1 3 1 4 0 0 0 2 0 4 3 1 2 4 4 2 4 4 3 3 RLRRRLRLRR
出力例 3
Yes
Score : 300 points
Problem Statement
There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.
We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.
For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i \leq 10^9
- 0 \leq Y_i \leq 10^9
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
- All X_i and Y_i are integers.
- S is a string of length N consisting of
LandR.
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 S
Output
If there will be a collision, print Yes; otherwise, print No.
Sample Input 1
3 2 3 1 1 4 1 RRL
Sample Output 1
Yes
This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.
Sample Input 2
2 1 1 2 1 RR
Sample Output 2
No
Since Person 1 and Person 2 walk in the same direction, they never collide.
Sample Input 3
10 1 3 1 4 0 0 0 2 0 4 3 1 2 4 4 2 4 4 3 3 RLRRRLRLRR
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。
以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。2 x: S の x 番目の文字を出力する。
制約
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S は英小文字からなる。
2 xの形式のクエリが 1 個以上与えられる。- N,Q,x はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
それぞれのクエリは以下の形式で与えられる。ここで、t は 1 または 2 である。
t x
出力
2 x の形式の各クエリについて、答えを一行に出力せよ。
入力例 1
3 3 abc 2 2 1 1 2 2
出力例 1
b a
1 個目のクエリのとき、S は abc なので 2 文字目の b を出力します。
2 個目のクエリのとき、S は abc から cab に変わります。
3 個目のクエリのとき、S は cab なので 2 文字目の a を出力します。
入力例 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
出力例 2
c u c u
Score : 300 points
Problem Statement
You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.
Process Q queries. Each query is of one of the following two types.
1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.2 x: Print the x-th character of S.
Constraints
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S consists of lowercase English letters.
- At least one query in the format
2 x. - N, Q, x are all integers.
Input
Input is given from Standard Input in the following format:
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format, where t is 1 or 2:
t x
Output
For each query in the format 2 x, print the answer in a single line.
Sample Input 1
3 3 abc 2 2 1 1 2 2
Sample Output 1
b a
In the 1-st query, S is abc, so the 2-nd character b should be printed.
In the 2-nd query, S is changed from abc to cab.
In the 3-rd query, S is cab, so the 2-nd character a should be printed.
Sample Input 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
Sample Output 2
c u c u
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
いくつかの正方形を辺でつなげてできる、連結な多角形の形をしたパズルのピースのことを ポリオミノ と呼びます。
縦 4 マス、横 4 マスのグリッドと、グリッドに収まる大きさの 3 個のポリオミノがあります。
i 番目のポリオミノの形は 16 個の文字 P_{i,j,k} (1 \leq j, k \leq 4) によって表されます。P_{i, j, k} は何も置かれていないグリッドに i 番目のポリオミノを置いたときの状態を意味して、P_{i, j, k} が # の場合は上から j 行目、左から k 列目のマスにポリオミノが置かれていることを、. の場合は置かれていないことを意味します。(入出力例 1 の図も参考にしてください。)
あなたは次の条件を全て満たすように 3 個のポリオミノ全てをグリッドに敷き詰めることにしました。
- グリッドの全てのマスはポリオミノで覆われている。
- ポリオミノ同士が重なるように置くことはできない。
- ポリオミノがグリッドからはみ出るように置くことはできない。
- ポリオミノの平行移動と回転は自由に行うことができるが、裏返すことはできない。
条件を満たすようにグリッドにポリオミノを敷き詰めることは可能ですか?
制約
- P_{i, j, k} は
#または. - 与えられるポリオミノは連結である。つまり、ポリオミノを構成する正方形同士は、正方形のみを上下左右に辿って互いに行き来できる
- 与えられるポリオミノは空でない
入力
入力は以下の形式で標準入力から与えられる。
P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}
出力
問題文の条件を満たすようにポリオミノを敷き詰めることが可能である場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
.... ###. .#.. .... .... .### .##. .... ..#. .##. .##. .##.
出力例 1
Yes
入力例 1 に対応するポリオミノの形は次の図のようになります。

この場合、次の図のようにポリオミノを配置することで、問題文の条件を満たすようにグリッドにポリオミノを敷き詰めることができます。

よって答えは Yes になります。
入力例 2
###. #.#. ##.. .... .... ..#. .... .... #### ##.. #... #...
出力例 2
Yes
入力例 2 の 1 番目のポリオミノのように、ポリオミノは穴の空いた多角形の形をしている場合があります。
入力例 3
##.. #..# #### .... .... ##.. .##. .... .#.. .#.. .#.. .#..
出力例 3
No
ポリオミノを敷き詰めるときに、ポリオミノを裏返してはならないのに注意してください。
入力例 4
.... ..#. .... .... .... ..#. .... .... .... ..#. .... ....
出力例 4
No
入力例 5
.... #### #... #... .... #### ...# ..## .... ..## ..#. ..##
出力例 5
No
入力例 6
###. .##. ..#. .### .... ...# ..## ...# .... #... #... #...
出力例 6
Yes
Score : 400 points
Problem Statement
A polyomino is a puzzle piece in the shape of a connected polygon made by connecting several squares by their edges.
There is a grid with four rows and four columns, and three polyominoes that fit within the grid.
The shape of the i-th polyomino is represented by 16 characters P_{i,j,k} (1 \leq j, k \leq 4). They describe the state of the grid when the i-th polyomino is placed on it. If P_{i, j, k} is #, the square at the j-th row from the top and k-th column from the left is occupied by the polyomino; if it is ., the square is not occupied. (Refer to the figures at Sample Input/Output 1.)
You want to fill the grid with all three polyominoes so that all of the following conditions are satisfied.
- All squares of the grid are covered by the polyominoes.
- The polyominoes must not overlap each other.
- The polyominoes must not stick out of the grid.
- The polyominoes may be freely translated and rotated but may not be flipped over.
Can the grid be filled with the polyominoes to satisfy these conditions?
Constraints
- P_{i, j, k} is
#or.. - The given polyominoes are connected. In other words, the squares that make up a polyomino can be reached from each other by following only the squares up, down, left, and right.
- The given polyominoes are not empty.
Input
The input is given from Standard Input in the following format:
P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}
Output
If it is possible to fill the grid with the polyominoes to satisfy the conditions in the problem statement, print Yes; otherwise, print No.
Sample Input 1
.... ###. .#.. .... .... .### .##. .... ..#. .##. .##. .##.
Sample Output 1
Yes
The figure below shows the shapes of the polyominoes corresponding to Sample Input 1.

In this case, you can fill the grid with them to satisfy the conditions in the problem statement by placing them as shown in the figure below.

Thus, the answer is Yes.
Sample Input 2
###. #.#. ##.. .... .... ..#. .... .... #### ##.. #... #...
Sample Output 2
Yes
As in the first polyomino in Sample Input 2, a polyomino may be in the shape of a polygon with a hole.
Sample Input 3
##.. #..# #### .... .... ##.. .##. .... .#.. .#.. .#.. .#..
Sample Output 3
No
Note that the polyominoes may not be flipped over when filling the grid.
Sample Input 4
.... ..#. .... .... .... ..#. .... .... .... ..#. .... ....
Sample Output 4
No
Sample Input 5
.... #### #... #... .... #### ...# ..## .... ..## ..#. ..##
Sample Output 5
No
Sample Input 6
###. .##. ..#. .### .... ...# ..## ...# .... #... #... #...
Sample Output 6
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
1 から N までの番号が付けられた N 頂点からなる木があります。 各 i\ (2 \leq i \leq N) について、頂点 i と頂点 \lfloor \frac{i}{2} \rfloor を結ぶ辺が張られています。 逆に、これら以外の辺は存在しません。
この木において、頂点 X との距離が K である頂点の数を求めてください。 ただし、2 頂点 u,v の距離は、頂点 u,v を結ぶ単純パスに含まれる辺の個数として定義されます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 10^5
- 1\leq N \leq 10^{18}
- 1\leq X \leq N
- 0\leq K \leq N-1
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。 ここで、\mathrm{test}_i は i 番目のテストケースを意味する。
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
各テストケースは以下の形式で与えられる。
N X K
出力
T 行出力せよ。
i\ (1 \leq i \leq T) 行目には、i 番目のテストケースに対する答えを整数として出力せよ。
入力例 1
5 10 2 0 10 2 1 10 2 2 10 2 3 10 2 4
出力例 1
1 3 4 2 0
N=10 のとき、木は以下の図のようになります。

このとき、
- 頂点 2 との距離が 0 である頂点は 2 の 1 つです。
- 頂点 2 との距離が 1 である頂点は 1,4,5 の 3 つです。
- 頂点 2 との距離が 2 である頂点は 3,8,9,10 の 4 つです。
- 頂点 2 との距離が 3 である頂点は 6,7 の 2 つです。
- 頂点 2 との距離が 4 である頂点は存在しません。
入力例 2
10 822981260158260522 52 20 760713016476190629 2314654 57 1312150450968417 1132551176249851 7 1000000000000000000 1083770654 79 234122432773361868 170290518806790 23 536187734191890310 61862 14 594688604155374934 53288633578 39 1000000000000000000 120160810 78 89013034180999835 14853481725739 94 463213054346948152 825589 73
出力例 2
1556480 140703128616960 8 17732923532771328 65536 24576 2147483640 33776997205278720 7881299347898368 27021597764222976
Score : 450 points
Problem Statement
There is a tree with N vertices numbered 1 to N. For each i\ (2 \leq i \leq N), there is an edge connecting vertex i and vertex \lfloor \frac{i}{2} \rfloor. There are no other edges.
In this tree, find the number of vertices whose distance from vertex X is K. Here, the distance between two vertices u and v is defined as the number of edges in the simple path connecting vertices u and v.
You have T test cases to solve.
Constraints
- 1\leq T \leq 10^5
- 1\leq N \leq 10^{18}
- 1\leq X \leq N
- 0\leq K \leq N-1
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{test}_i represents the i-th test case:
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
Each test case is given in the following format:
N X K
Output
Print T lines.
The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case as an integer.
Sample Input 1
5 10 2 0 10 2 1 10 2 2 10 2 3 10 2 4
Sample Output 1
1 3 4 2 0
The tree for N=10 is shown in the following figure.

Here,
- There is 1 vertex, 2, whose distance from vertex 2 is 0.
- There are 3 vertices, 1,4,5, whose distance from vertex 2 is 1.
- There are 4 vertices, 3,8,9,10, whose distance from vertex 2 is 2.
- There are 2 vertices, 6,7, whose distance from vertex 2 is 3.
- There are no vertices whose distance from vertex 2 is 4.
Sample Input 2
10 822981260158260522 52 20 760713016476190629 2314654 57 1312150450968417 1132551176249851 7 1000000000000000000 1083770654 79 234122432773361868 170290518806790 23 536187734191890310 61862 14 594688604155374934 53288633578 39 1000000000000000000 120160810 78 89013034180999835 14853481725739 94 463213054346948152 825589 73
Sample Output 2
1556480 140703128616960 8 17732923532771328 65536 24576 2147483640 33776997205278720 7881299347898368 27021597764222976
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
数直線上の座標 0 から座標 X_N まで行き、折り返して座標 0 まで帰ってくる計画を立てています。ただし、往路では正の方向、復路では負の方向にしか進めません。
移動は車で行います。車は距離 1 進むごとに 1 リットルの燃料を消費します。燃料は H リットルまで所持することができ、燃料を所持していない状態で進むことはできません。
各 i = 1, 2, \ldots, N-1 について、座標 X_i にはガソリンスタンドがあり、P_i 円払うと F_i リットルの燃料が得られます。ただし、H リットルを超えて燃料を所持することはできません。より厳密には、x リットルの燃料を持っているときに座標 X_i にあるガソリンスタンドを使うと P_i 円を払う必要があり、持っている燃料は \min(x + F_i, H) リットルとなります。 各ガソリンスタンドは、往路と復路で合わせて 1 回までしか使うことができません。
はじめに燃料を H リットル所持しているとき、この計画を達成することができるか判定し、可能ならば必要な金額の最小値を求めてください。
制約
- 1 \leq N, H \leq 300
- 0 < X_1 < X_2 < \ldots < X_N \leq 10^5
- 1 \leq P_i \leq 10^5
- 1 \leq F_i \leq H
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H
X_1 X_2 \ldots X_N
P_1 F_1
P_2 F_2
\vdots
P_{N-1} F_{N-1}
出力
計画を達成できる場合は必要な金額の最小値を、できない場合は -1 を出力せよ。
入力例 1
4 10 2 5 9 11 8 10 5 8 4 9
出力例 1
9
往路で座標 5 の、復路で座標 9 のガソリンスタンドを用いることにより合計 9 円払うことで計画を達成することができます。
計画を達成するためにかかる金額を 8 円以下にすることはできません。往路と復路で同じガソリンスタンドを使うことができないことに注意してください。
入力例 2
1 1 100000
出力例 2
-1
入力例 3
5 20 4 13 16 18 23 1 16 2 8 4 11 8 13
出力例 3
13
Score : 550 points
Problem Statement
You are planning to travel from coordinate 0 to coordinate X_N on a number line, then turn around and return to coordinate 0. Here, you can only move in the positive direction on the outbound trip and in the negative direction on the return trip.
You will travel by car. The car consumes one liter of fuel for every unit distance it travels. You can carry up to H liters of fuel and cannot move without fuel.
For each i = 1, 2, \ldots, N-1, there is a gas station at coordinate X_i, where you can get F_i liters of fuel for P_i yen. However, you cannot carry more than H liters of fuel. More precisely, if you have x liters of fuel and use the gas station at coordinate X_i, you must pay P_i yen, and your amount of fuel becomes \min(x + F_i, H) liters. Each gas station can be used at most once in total during the round trip.
Determine if you can achieve this plan when you initially have H liters of fuel, and if it is possible, find the minimum amount of money required.
Constraints
- 1 \leq N, H \leq 300
- 0 < X_1 < X_2 < \ldots < X_N \leq 10^5
- 1 \leq P_i \leq 10^5
- 1 \leq F_i \leq H
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H
X_1 X_2 \ldots X_N
P_1 F_1
P_2 F_2
\vdots
P_{N-1} F_{N-1}
Output
If the plan can be achieved, print the minimum amount of money required; otherwise, print -1.
Sample Input 1
4 10 2 5 9 11 8 10 5 8 4 9
Sample Output 1
9
You can achieve the plan by using the gas station at coordinate 5 on the outbound trip and the one at coordinate 9 on the return trip, paying a total of 9 yen.
It is impossible to achieve the plan by paying 8 yen or less. Note that you cannot use the same gas station on both the outbound and return trips.
Sample Input 2
1 1 100000
Sample Output 2
-1
Sample Input 3
5 20 4 13 16 18 23 1 16 2 8 4 11 8 13
Sample Output 3
13