実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
N 個の料理があり、i 個目の料理の甘さは A_i、しょっぱさは B_i です。
高橋君はこれらの N 個の料理を好きな順番で並べ、その順に食べようとします。
高橋君は並べた順番の通りに料理を食べていきますが、食べた料理の甘さの合計が X より大きくなるかしょっぱさの合計が Y より大きくなるとその時点で食べるのをやめます。
高橋君が食べることになる料理の個数としてあり得る最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq X, Y \leq 2 \times 10^{14}
- 1 \leq A_i, B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
答えを出力せよ。
入力例 1
4 7 18 2 3 5 1 8 8 1 4
出力例 1
2
i 個目の料理のことを料理 i と書きます。
高橋君が 4 個の料理を料理 2, 3, 1, 4 の順に並べ替えたとき、料理 2, 3 を食べた時点での食べた料理の甘さの合計が 8 となり 7 より大きくなります。したがってこの場合は高橋君が食べることになる料理の個数は 2 個です。
高橋君が食べる料理の個数が 1 個以下になることはないため、2 を出力します。
入力例 2
5 200000000000000 200000000000000 1 1 1 1 1 2 2 2 2 2
出力例 2
5
入力例 3
8 30 30 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
出力例 3
6
Score : 250 points
Problem Statement
There are N dishes, and the i-th dish has a sweetness of A_i and a saltiness of B_i.
Takahashi plans to arrange these N dishes in any order he likes and eat them in that order.
He will eat the dishes in the arranged order, but he will stop eating as soon as the total sweetness of the dishes he has eaten exceeds X or the total saltiness exceeds Y.
Find the minimum possible number of dishes that he will end up eating.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq X, Y \leq 2 \times 10^{14}
- 1 \leq A_i, B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the answer.
Sample Input 1
4 7 18 2 3 5 1 8 8 1 4
Sample Output 1
2
The i-th dish will be denoted as dish i.
If he arranges the four dishes in the order 2, 3, 1, 4, as soon as he eats dishes 2 and 3, their total sweetness is 8, which is greater than 7. Therefore, in this case, he will end up eating two dishes.
The number of dishes he will eat cannot be 1 or less, so print 2.
Sample Input 2
5 200000000000000 200000000000000 1 1 1 1 1 2 2 2 2 2
Sample Output 2
5
Sample Input 3
8 30 30 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
Sample Output 3
6
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 行 N 列のグリッドが与えられます。ここで、N は偶数です。グリッドの上から i 行目、左から j 列目のマスをマス (i, j) と表記します。
グリッドの各マスは黒か白のいずれかで塗られており、A_{i, j} = # のときマス (i, j) は黒、A_{i, j} = . のときマス (i, j) は白で塗られています。
i = 1, 2, \ldots, \frac{N}{2} の順に以下の操作を行った後のグリッドの各マスの色を求めてください。
- i 以上 N + 1 - i 以下の整数 x, y について、マス (y, N + 1 - x) の色をマス (x, y) の色で置き換える。この置き換えは条件を満たすすべての整数 x, y について同時に行う。
制約
- N は 2 以上 3000 以下の偶数
- A_{i, j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1, 1}A_{1, 2}\ldotsA_{1, N}
A_{2, 1}A_{2, 2}\ldotsA_{2, N}
\vdots
A_{N, 1}A_{N, 2}\ldotsA_{N, N}
出力
すべての操作を終えた後、マス (i, j) の色が黒であるとき B_{i, j} = #、マス (i, j) の色が白であるとき B_{i, j} = . として以下の形式で出力せよ。
B_{1, 1}B_{1, 2}\ldotsB_{1, N}
B_{2, 1}B_{2, 2}\ldotsB_{2, N}
\vdots
B_{N, 1}B_{N, 2}\ldotsB_{N, N}
入力例 1
8 .......# .......# .####..# .####..# .##....# .##....# .####### .#######
出力例 1
........ #######. #.....#. #.###.#. #.#...#. #.#####. #....... ########
操作によってグリッドの各マスの色は以下のように変化します。
.......# ........ ........ ........ ........ .......# ######.. #######. #######. #######. .####..# ######.. #....##. #.....#. #.....#. .####..# → ##..##.. → #....##. → #.##..#. → #.###.#. .##....# ##..##.. #..####. #.##..#. #.#...#. .##....# ##...... #..####. #.#####. #.#####. .####### ##...... #....... #....... #....... .####### ######## ######## ######## ########
入力例 2
6 .#.#.# ##.#.. ...### ###... ..#.## #.#.#.
出力例 2
#.#.#. .#.#.# #.#.#. .#.#.# #.#.#. .#.#.#
入力例 3
12 .......#.### #...#...#..# ###.#..##### ..#.#.#.#... .#.....#.### .......#.#.. #...#..#.... #####....... ...#...#.#.# ..###..#..## #..#.#.#.#.# .####.......
出力例 3
.#..##...##. #.#.#.#.#... ###.##..#... #.#.#.#.#... #.#.##...##. ............ ............ .###.###.### ...#...#.#.. .###...#.### ...#...#...# .###...#.###
Score : 400 points
Problem Statement
You are given a grid with N rows and N columns, where N is an even number. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Each cell is painted black or white. If A_{i, j} = #, cell (i, j) is black; if A_{i, j} = ., it is white.
Find the color of each cell after performing the following operation for i = 1, 2, \ldots, \frac{N}{2} in this order.
- For all pairs of integers x, y between i and N + 1 - i, inclusive, replace the color of cell (y, N + 1 - x) with the color of cell (x, y). Perform these replacements simultaneously for all such pairs x, y.
Constraints
- N is an even number between 2 and 3000, inclusive.
- Each A_{i, j} is
#or..
Input
The input is given from Standard Input in the following format:
N
A_{1,1}A_{1,2}\ldots A_{1,N}
A_{2,1}A_{2,2}\ldots A_{2,N}
\vdots
A_{N,1}A_{N,2}\ldots A_{N,N}
Output
After all operations, let B_{i, j} = # if cell (i, j) is black, and B_{i, j} = . if it is white. Print the grid in the following format:
B_{1,1}B_{1,2}\ldots B_{1,N}
B_{2,1}B_{2,2}\ldots B_{2,N}
\vdots
B_{N,1}B_{N,2}\ldots B_{N,N}
Sample Input 1
8 .......# .......# .####..# .####..# .##....# .##....# .####### .#######
Sample Output 1
........ #######. #.....#. #.###.#. #.#...#. #.#####. #....... ########
The operations change the colors of the grid cells as follows:
.......# ........ ........ ........ ........ .......# ######.. #######. #######. #######. .####..# ######.. #....##. #.....#. #.....#. .####..# → ##..##.. → #....##. → #.##..#. → #.###.#. .##....# ##..##.. #..####. #.##..#. #.#...#. .##....# ##...... #..####. #.#####. #.#####. .####### ##...... #....... #....... #....... .####### ######## ######## ######## ########
Sample Input 2
6 .#.#.# ##.#.. ...### ###... ..#.## #.#.#.
Sample Output 2
#.#.#. .#.#.# #.#.#. .#.#.# #.#.#. .#.#.#
Sample Input 3
12 .......#.### #...#...#..# ###.#..##### ..#.#.#.#... .#.....#.### .......#.#.. #...#..#.... #####....... ...#...#.#.# ..###..#..## #..#.#.#.#.# .####.......
Sample Output 3
.#..##...##. #.#.#.#.#... ###.##..#... #.#.#.#.#... #.#.##...##. ............ ............ .###.###.### ...#...#.#.. .###...#.### ...#...#...# .###...#.###
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
縦 N 行 横 N 列のマスからなるグリッドがあります。ここで、N は 45 以下の奇数です。
上から i 行目、左から j 列目のマスをマス (i,j) と表します。
このグリッドに、以下の条件を満たすように高橋君と 1 から N^2-1 までの番号がついた N^2-1 個のパーツからなる 1 匹の龍を配置します。
- 高橋君はグリッドの中央、すなわちマス (\frac{N+1}{2},\frac{N+1}{2}) に配置しなければならない。
- 高橋君がいるマスを除く各マスには龍のパーツをちょうど 1 つ配置しなければならない。
- 2 \leq x \leq N^2-1 を満たす全ての整数 x について、龍のパーツ x はパーツ x-1 があるマスと辺で隣接するマスに配置しなければならない。
- マス (i,j) とマス (k,l) が辺で隣接するとは、|i-k|+|j-l|=1 であることを意味します。
条件を満たす配置方法を 1 つ出力してください。なお、条件を満たす配置は必ず存在します。
制約
- 3 \leq N \leq 45
- N は奇数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N 行出力せよ。
X_{i,j} を、マス (i,j) に高橋君を配置するとき T、パーツ x を配置するとき x とし、i 行目には X_{i,1},\ldots,X_{i,N} を空白区切りで出力せよ。
入力例 1
5
出力例 1
1 2 3 4 5 16 17 18 19 6 15 24 T 20 7 14 23 22 21 8 13 12 11 10 9
この他、以下の出力も条件をすべて満たすため正解となります。
9 10 11 14 15 8 7 12 13 16 5 6 T 18 17 4 3 24 19 20 1 2 23 22 21
一方、以下の出力はそれぞれ不正解となります。
高橋君が中央にいない。
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 T
パーツ 23 とパーツ 24 のあるマスが辺で隣接していない。
1 2 3 4 5 10 9 8 7 6 11 12 24 22 23 14 13 T 21 20 15 16 17 18 19
Score : 350 points
Problem Statement
There is a grid with N rows and N columns, where N is an odd number at most 45.
Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.
In this grid, you will place Takahashi and a dragon consisting of N^2-1 parts numbered 1 to N^2-1 in such a way that satisfies the following conditions:
- Takahashi must be placed at the center of the grid, that is, in cell (\frac{N+1}{2},\frac{N+1}{2}).
- Except for the cell where Takahashi is, exactly one dragon part must be placed in each cell.
- For every integer x satisfying 2 \leq x \leq N^2-1, the dragon part x must be placed in a cell adjacent by an edge to the cell containing part x-1.
- Cells (i,j) and (k,l) are said to be adjacent by an edge if and only if |i-k|+|j-l|=1.
Print one way to arrange the parts to satisfy the conditions. It is guaranteed that there is at least one arrangement that satisfies the conditions.
Constraints
- 3 \leq N \leq 45
- N is odd.
Input
The input is given from Standard Input in the following format:
N
Output
Print N lines.
The i-th line should contain X_{i,1},\ldots,X_{i,N} separated by spaces, where X_{i,j} is T when placing Takahashi in cell (i,j) and x when placing part x there.
Sample Input 1
5
Sample Output 1
1 2 3 4 5 16 17 18 19 6 15 24 T 20 7 14 23 22 21 8 13 12 11 10 9
The following output also satisfies all the conditions and is correct.
9 10 11 14 15 8 7 12 13 16 5 6 T 18 17 4 3 24 19 20 1 2 23 22 21
On the other hand, the following outputs are incorrect for the reasons given.
Takahashi is not at the center.
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 T
The cells containing parts 23 and 24 are not adjacent by an edge.
1 2 3 4 5 10 9 8 7 6 11 12 24 22 23 14 13 T 21 20 15 16 17 18 19
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
H 行 W 列のグリッド状に分割されたフィールドがあります。
北 (上側) から i 行目、西 (左側) から j 列目のマスは文字 A_{i, j} で表されます。各文字の意味は次の通りです。
.: 空きマス。進入できる。#: 障害物。進入できない。>,v,<,^: それぞれ東・南・西・北を向いている人がいるマス。進入できない。人の視線は 1 マス分の幅を持ち、人が向いている方向にまっすぐ伸び、障害物や別の人に遮られる。(入出力例 1 にある説明も参考にしてください。)S: スタート地点。進入できる。ちょうど 1 ヵ所だけ存在する。人の視線に入っていないことが保証される。G: ゴール地点。進入できる。ちょうど 1 ヵ所だけ存在する。人の視線に入っていないことが保証される。
ナオヒロくんはスタート地点にいて、東西南北への 1 マス分の移動を好きな回数行えます。ただし、進入できないマスへの移動やフィールドの外への移動はできません。
彼が人の視線に一度も入らずにゴール地点に到達できるか判定して、できる場合はそのために最小で何回の移動が必要か求めてください。
制約
- 2 \leq H, W \leq 2000
- A_{i,j} は
.,#,>,v,<,^,S,Gのいずれかである S,Gは A_{i, j} の中にちょうど 1 回ずつ現れる- スタート地点・ゴール地点はともに人の視線に入っていない
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1}A_{1,2}\dots A_{1,W}
A_{2,1}A_{2,2}\dots A_{2,W}
\vdots
A_{H,1}A_{H,2}\dots A_{H,W}
出力
ナオヒロ君が人の視線に一度も入らずにゴール地点に到達できる場合は、そのために必要な(最小の)移動回数を出力せよ。できない場合は -1 を出力せよ。
入力例 1
5 7 ....Sv. .>..... ....... >..<.#< ^G....>
出力例 1
15
入力例 1 について、1 人以上の視線に入っている空きマスを ! で表すと次の図のようになります。

いくつかのマスについて具体的に説明すると次のようになります。(ここで、北から i 行目、西から j 列目のマスを (i, j) と表します。)
- (2, 4) は (2, 2) にいる東を向いている人からの視線に入っているマスである。
- (2, 6) は (2, 2) にいる東を向いている人と (1, 6) にいる南を向いている人の 2 人の視線に入っているマスである。
- (4, 5) は誰の視線にも入っていないマスである。(4, 7) にいる西を向いている人の視線は (4, 6) の障害物に遮られていて、(4, 1) にいる東を向いている人の視線は (4, 4) の人に遮られている。
ナオヒロ君は進入できないマス・視線に入っているマスのどちらも通らずにゴール地点へ行く必要があります。
入力例 2
4 3 S.. .<. .>. ..G
出力例 2
-1
ナオヒロ君がゴール地点に到達できない場合は -1 を出力してください。
Score : 425 points
Problem Statement
There is a field divided into a grid of H rows and W columns.
The square at the i-th row from the north (top) and the j-th column from the west (left) is represented by the character A_{i, j}. Each character represents the following.
.: An empty square. Passable.#: An obstacle. Impassable.>,v,<,^: Squares with a person facing east, south, west, and north, respectively. Impassable. The person's line of sight is one square wide and extends straight in the direction the person is facing, and is blocked by an obstacle or another person. (See also the description at Sample Input/Output 1.)S: The starting point. Passable. There is exactly one starting point. It is guaranteed not to be in a person's line of sight.G: The goal. Passable. There is exactly one goal. It is guaranteed not to be in a person's line of sight.
Naohiro is at the starting point and can move one square to the east, west, south, and north as many times as he wants. However, he cannot enter an impassable square or leave the field.
Determine if he can reach the goal without entering a person's line of sight, and if so, find the minimum number of moves required to do so.
Constraints
- 2 \leq H, W \leq 2000
- A_{i,j} is
.,#,>,v,<,^,S, orG. - Each of
SandGoccurs exactly once among A_{i, j}. - Neither the starting point nor the goal is in a person's line of sight.
Input
The input is given from Standard Input in the following format:
H W
A_{1,1}A_{1,2}\dots A_{1,W}
A_{2,1}A_{2,2}\dots A_{2,W}
\vdots
A_{H,1}A_{H,2}\dots A_{H,W}
Output
If Naohiro can reach the goal without entering a person's line of sight, print the (minimum) number of moves required to do so. Otherwise, print -1.
Sample Input 1
5 7 ....Sv. .>..... ....... >..<.#< ^G....>
Sample Output 1
15
For Sample Input 1, the following figure shows the empty squares that are in the lines of sight of one or more people as !.

Let us describe some of the squares. (Let (i, j) denote the square in the i-th row from the north and the j-th column from the west.)
- (2, 4) is a square in the line of sight of the east-facing person at (2, 2).
- (2, 6) is a square in the lines of sight of two people, one facing east at (2, 2) and the other facing south at (1, 6).
- The square (4, 5) is not in anyone's line of sight. The line of sight of the west-facing person at (4, 7) is blocked by the obstacle at (4, 6), and the line of sight of the east-facing person at (4, 1) is blocked by the person at (4, 4).
Naohiro must reach the goal without passing through impassable squares or squares in a person's line of sight.
Sample Input 2
4 3 S.. .<. .>. ..G
Sample Output 2
-1
Print -1 if he cannot reach the goal.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
縦 N 行、横 M 列のマス目に、黒い飛車の駒 B 個と白い飛車の駒 W 個を設置することを考えましょう。
以下の条件をすべて満たす設置の仕方を いい配置 と呼びます。
- B+W 個の駒すべてが設置されている。
- 1 つのマスに置かれている駒の数は高々 1 つである。
- ある白い駒と黒い駒の組であって、互いが互いを攻撃しているようなものが存在しない。すなわち、ある白い駒と黒い駒の組であって、一方が 1 手の移動によってもう片方が置かれているマスに到達できるようなものが存在しない。
ここで、飛車の駒は、今いる位置から上、下、右、左のいずれかの方向に伸びる直線上にあり、かつ他の駒を飛び越えずに到達できるマスに 1 手で移動することができます。
いい配置としてあり得るものは何通りありますか?答えは非常に大きくなることがあるので、998244353 で割ったあまりを出力してください。
同じ色の駒同士は区別しないものとします。
制約
- 1 \leq N,M \leq 50
- 1 \leq B,W \leq 2500
- B+W \leq N \times M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M B W
出力
答えを 998244353 で割ったあまりを出力せよ。
入力例 1
2 2 1 1
出力例 1
4
いい配置としてあり得るものは以下の 4 通りです。

入力例 2
1 2 1 1
出力例 2
0
いい配置としてあり得るものが存在しない場合もあります。
入力例 3
40 40 30 30
出力例 3
467620384
998244353 で割ったあまりを出力することに注意してください。
Score : 500 points
Problem Statement
Consider placing B black rooks and W white rooks on a grid with N horizontal rows and M vertical columns.
A placement of the rooks satisfying all of the following conditions is called a good placement.
- All B+W rooks are placed on the grid.
- At most one rook is placed in the same square.
- There is no pair of white rook and black rook attacking each other. That is, there is no pair of white rook and black rook such that one of them can reach the square in which the other is placed in one move.
Here, in one move, a rook can reach any square that is on a horizontal or vertical line from its current position and is reachable without jumping over another rook.
How many good placements are there? Since this count can be enormous, print it modulo 998244353.
Rooks in the same color are not distinguished.
Constraints
- 1 \leq N,M \leq 50
- 1 \leq B,W \leq 2500
- B+W \leq N \times M
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M B W
Output
Print the count modulo 998244353.
Sample Input 1
2 2 1 1
Sample Output 1
4
There are four good placements as follows.

Sample Input 2
1 2 1 1
Sample Output 2
0
There may be no good placement.
Sample Input 3
40 40 30 30
Sample Output 3
467620384
Be sure to print the count modulo 998244353.