実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
上下左右に広がる N\times N のマス目があり、最初全てのマスは白く塗られています。このマス目の上から i 行目、左から j 列目のマスを (i,j) で表します。
高橋君は 1 以上 N 以下の整数 A, B を持っており、次のような操作を行います。
- \max(1-A,1-B)\leq k\leq \min(N-A,N-B) をみたす全ての整数 k について、(A+k,B+k) を黒く塗る。
- \max(1-A,B-N)\leq k\leq \min(N-A,B-1) をみたす全ての整数 k について、(A+k,B-k) を黒く塗る。
この操作を行った後のマス目について、P\leq i\leq Q かつ R\leq j\leq S をみたす各マス (i,j) がそれぞれ何色で塗られているか求めてください。
制約
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A B P Q R S
出力
Q-P+1 行出力せよ。
各行は # と . のみからなる長さ S-R+1 の文字列であり、
i 行目の文字列の j 番目の文字が
# であることは (P+i-1,R+j-1) が黒く塗られていることを、
. であることは (P+i-1,R+j-1) が白く塗られていることをさす。
入力例 1
5 3 2 1 5 1 5
出力例 1
...#. #.#.. .#... #.#.. ...#.
1 つめの操作で (2,1), (3,2), (4,3), (5,4) の 4 マスが、
2 つめの操作で (4,1), (3,2), (2,3), (1,4) の 4 マスが黒く塗られます。
よって、P=1, Q=5, R=1, S=5 より、上のように出力します。
入力例 2
5 3 3 4 5 2 5
出力例 2
#.#. ...#
操作によって、
(1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5) の 9 マスが
黒く塗られます。
P=4, Q=5, R=2, S=5 より、上のように出力します。
入力例 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
出力例 3
#.# .#. #.#
入力が 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
There is an N\times N grid with horizontal rows and vertical columns, where all squares are initially painted white. Let (i,j) denote the square at the i-th row and j-th column.
Takahashi has integers A and B, which are between 1 and N (inclusive). He will do the following operations.
- For every integer k such that \max(1-A,1-B)\leq k\leq \min(N-A,N-B), paint (A+k,B+k) black.
- For every integer k such that \max(1-A,B-N)\leq k\leq \min(N-A,B-1), paint (A+k,B-k) black.
In the grid after these operations, find the color of each square (i,j) such that P\leq i\leq Q and R\leq j\leq S.
Constraints
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B P Q R S
Output
Print Q-P+1 lines.
Each line should contain a string of length S-R+1 consisting of # and ..
The j-th character of the string in the i-th line should be # to represent that (P+i-1, R+j-1) is painted black, and . to represent that (P+i-1, R+j-1) is white.
Sample Input 1
5 3 2 1 5 1 5
Sample Output 1
...#. #.#.. .#... #.#.. ...#.
The first operation paints the four squares (2,1), (3,2), (4,3), (5,4) black, and the second paints the four squares (4,1), (3,2), (2,3), (1,4) black.
Thus, the above output should be printed, since P=1, Q=5, R=1, S=5.
Sample Input 2
5 3 3 4 5 2 5
Sample Output 2
#.#. ...#
The operations paint the nine squares (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5).
Thus, the above output should be printed, since P=4, Q=5, R=2, S=5.
Sample Input 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
Sample Output 3
#.# .#. #.#
Note that the input may not fit into a 32-bit integer type.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 行 N 列のマス目があり、それぞれのマスは白または黒で塗られています。
マス目の状態は N 個の文字列 S_i で表され、
S_i の j 文字目が # であることはマス目の上から i 行目、左から j 列目のマスが黒く塗られていることを、
. であることは白く塗られていることをさします。
高橋君はこのマス目のうち高々 2 つの白く塗られているマスを選び、黒く塗ることができます。
マス目の中に、黒く塗られたマスが縦、横、ななめのいずれかの向きに 6 つ以上連続するようにできるか判定してください。
ただし、黒く塗られたマスがななめに 6 つ以上連続するとは、N 行 N 列のマス目に完全に含まれる 6 行 6 列のマス目であって、その少なくとも一方の対角線上のマスがすべて黒く塗られているようなものが存在する事をさします。
制約
- 6 \leq N \leq 1000
- \lvert S_i\rvert =N
- S_i は
#と.のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
高々 2 つのマス目を黒く塗ることで条件をみたすようにできるなら Yes を、そうでないならば No を出力せよ。
入力例 1
8 ........ ........ .#.##.#. ........ ........ ........ ........ ........
出力例 1
Yes
上から 3 行目の左から 3, 6 番目のマスを塗ることで横方向に 6 つの黒く塗られたマスを連続させることができます。
入力例 2
6 ###### ###### ###### ###### ###### ######
出力例 2
Yes
高橋君はマス目を新たに黒く塗ることはできませんが、すでにこのマス目は条件をみたしています。
入力例 3
10 .......... #..##..... .......... .......... ....#..... ....#..... .#...#..#. .......... .......... ..........
出力例 3
No
Score : 300 points
Problem Statement
There is a grid with N horizontal rows and N vertical columns, where each square is painted white or black.
The state of the grid is represented by N strings, S_i.
If the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is painted black.
If the character is ., then the square is painted white.
Takahashi can choose at most two of these squares that are painted white, and paint them black.
Determine if it is possible to make the grid contain 6 or more consecutive squares painted black aligned either vertically, horizontally, or diagonally.
Here, the grid is said to be containing 6 or more consecutive squares painted black aligned diagonally if the grid with N rows and N columns completely contains a subgrid with 6 rows and 6 columns such that all the squares on at least one of its diagonals are painted black.
Constraints
- 6 \leq N \leq 1000
- \lvert S_i\rvert =N
- S_i consists of
#and..
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
If it is possible to fulfill the condition by painting at most two squares, then print Yes; otherwise, print No.
Sample Input 1
8 ........ ........ .#.##.#. ........ ........ ........ ........ ........
Sample Output 1
Yes
By painting the 3-rd and the 6-th squares from the left in the 3-rd row from the top, there will be 6 squares painted black aligned horizontally.
Sample Input 2
6 ###### ###### ###### ###### ###### ######
Sample Output 2
Yes
While Takahashi cannot choose a square to paint black, the grid already satisfies the condition.
Sample Input 3
10 .......... #..##..... .......... .......... ....#..... ....#..... .#...#..#. .......... .......... ..........
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
1 から N の番号がついた N 人を横一列に並べる方法のうち、以下の形式の M 個の条件全てを満たすものが存在するか判定してください。
- 条件:人 A_i と人 B_i は隣り合っている
制約
- 2 \leq N \leq 10^5
- 0 \leq M \leq 10^5
- 1\leq A_i < B_i \leq N
- (A_i,B_i) は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
条件を満たす並べ方が存在するなら Yes、存在しないなら No と出力せよ。
入力例 1
4 2 1 3 2 3
出力例 1
Yes
例えば 4,1,3,2 の順に並べることで全ての条件を満たすことができます。
入力例 2
4 3 1 4 2 4 3 4
出力例 2
No
どのように並べても全ての条件を満たすことはできません。
Score : 400 points
Problem Statement
Determine whether there is a way to line up N people, numbered 1 to N, in a row side by side to satisfy all of the M conditions in the following format.
- Condition: Person A_i and Person B_i are adjacent.
Constraints
- 2 \leq N \leq 10^5
- 0 \leq M \leq 10^5
- 1\leq A_i < B_i \leq N
- All pairs (A_i,B_i) are distinct.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
If there is a way to line up people to satisfy the conditions, print Yes; if not, print No.
Sample Input 1
4 2 1 3 2 3
Sample Output 1
Yes
One way to satisfy all the conditions is to line them up in the order 4,1,3,2.
Sample Input 2
4 3 1 4 2 4 3 4
Sample Output 2
No
There is no way to line them up to satisfy all the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
ある国には都市が N 個あります。
あなたは、都市 1 にある営業所から 0 個以上の都市を経由して都市 N にある訪問先へ移動しようとしています。
移動手段は社用車と電車の 2 種類があります。都市 i から都市 j へ移動するときの所要時間は以下の通りです。
- 社用車を使った場合 : D_{i,j} \times A 分
- 電車を使った場合 : D_{i,j} \times B + C 分
ただし、社用車から電車に乗り換えることはできますが、電車から社用車に乗り換えることはできません。
また、乗り換えは各都市のみで行え、乗り換えに時間はかかりません。
都市 1 から都市 N に移動するのにかかる時間は最短で何分ですか?
制約
- 2 \leq N \leq 1000
- 1 \leq A, B, C \leq 10^6
- D_{i,j} \leq 10^6
- D_{i,i} = 0
- D_{i,j} = D_{j,i} > 0 (i \neq j)
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A B C
D_{1,1} D_{1,2} \ldots D_{1,N}
D_{2,1} D_{2,2} \ldots D_{2,N}
\vdots
D_{N,1} D_{N,2} \ldots D_{N,N}
出力
答えを整数として出力せよ。
入力例 1
4 8 5 13 0 6 2 15 6 0 3 5 2 3 0 13 15 5 13 0
出力例 1
78
以下のように移動することで合計 78 分で都市 1 から都市 4 に移動することができます。
- 都市 1 から都市 3 まで社用車で移動する。この移動には 2 \times 8 = 16 分かかる。
- 都市 3 から都市 2 まで社用車で移動する。この移動には 3 \times 8 = 24 分かかる。
- 都市 2 から都市 4 まで電車で移動する。この移動には 5 \times 5 + 13 = 38 分かかる。
78 分未満の時間で都市 1 から都市 4 に移動することはできません。
入力例 2
3 1 1000000 1000000 0 10 1 10 0 10 1 10 0
出力例 2
1
入力例 3
5 954257 954213 814214 0 84251 214529 10017 373342 84251 0 91926 32336 164457 214529 91926 0 108914 57762 10017 32336 108914 0 234705 373342 164457 57762 234705 0
出力例 3
168604826785
Score : 450 points
Problem Statement
There are N cities in a certain country.
You will travel from your office in city 1 to a destination in city N, via zero or more cities.
Two types of transportation are available: company car and train. The time required to travel from city i to city j is as follows:
- D_{i,j} \times A minutes by company car, and
- D_{i,j} \times B + C minutes by train.
You can switch from company car to train, but not vice versa.
You can do so without spending time, but only in a city.
What is the minimum time in minutes to travel from city 1 to city N?
Constraints
- 2 \leq N \leq 1000
- 1 \leq A, B, C \leq 10^6
- D_{i,j} \leq 10^6
- D_{i,i} = 0
- D_{i,j} = D_{j,i} > 0 (i \neq j)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A B C
D_{1,1} D_{1,2} \ldots D_{1,N}
D_{2,1} D_{2,2} \ldots D_{2,N}
\vdots
D_{N,1} D_{N,2} \ldots D_{N,N}
Output
Print the answer as an integer.
Sample Input 1
4 8 5 13 0 6 2 15 6 0 3 5 2 3 0 13 15 5 13 0
Sample Output 1
78
You can travel from city 1 to city 4 in a total of 78 minutes by moving as follows.
- Travel by company car from city 1 to city 3. This takes 2 \times 8 = 16 minutes.
- Travel by company car from city 3 to city 2. This takes 3 \times 8 = 24 minutes.
- Travel by train from city 2 to city 4. This takes 5 \times 5 + 13 = 38 minutes.
It is impossible to travel from city 1 to city 4 in less than 78 minutes.
Sample Input 2
3 1 1000000 1000000 0 10 1 10 0 10 1 10 0
Sample Output 2
1
Sample Input 3
5 954257 954213 814214 0 84251 214529 10017 373342 84251 0 91926 32336 164457 214529 91926 0 108914 57762 10017 32336 108914 0 234705 373342 164457 57762 234705 0
Sample Output 3
168604826785
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ M の整数列 A, B, C があります。
C は整数 x_1, \dots, x_N, y_1, \dots, y_N によって表されます。C の先頭 y_1 項は x_1 であり、続く y_2 項は x_2 であり、\ldots、末尾の y_N 項は x_N です。
B は B_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M) によって定められます。
A は A_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M) によって定められます。
A_1, \dots, A_M の最大値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 2 \times 10^5
- 1 つのファイルに含まれるテストケースについて、N の総和は 2 \times 10^5 以下
- 1 \leq M \leq 10^9
- |x_i| \leq 4 \, (1 \leq i \leq N)
- y_i \gt 0 \, (1 \leq i \leq N)
- \sum_{k = 1}^N y_k = M
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N M x_1 y_1 \vdots x_N y_N
出力
T 行出力せよ。i \, (1 \leq i \leq T) 行目には、i 個目のテストケースに対する答えを出力せよ。
入力例 1
3 3 7 -1 2 2 3 -3 2 10 472 -4 12 1 29 2 77 -1 86 0 51 3 81 3 17 -2 31 -4 65 4 23 1 1000000000 4 1000000000
出力例 1
4 53910 2000000002000000000
1 つ目のテストケースにおいて、
- C = (-1, -1, 2, 2, 2, -3, -3)
- B = (-1, -2, 0, 2, 4, 1, -2)
- A = (-1, -3, -3, -1, 3, 4, 2)
であるので、A_1, \dots, A_M の最大値は 4 です。
Score : 500 points
Problem Statement
There are integer sequences A, B, C of length M each.
C is represented by integers x_1, \dots, x_N, y_1, \dots, y_N. The first y_1 terms of C are x_1, the subsequent y_2 terms are x_2, \ldots, the last y_N terms are x_N.
B is defined by B_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M).
A is defined by A_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M).
Find the maximum value among A_1, \dots, A_M.
You will be given T test cases to solve.
Constraints
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 2 \times 10^5
- The sum of N in a single file is at most 2 \times 10^5.
- 1 \leq M \leq 10^9
- |x_i| \leq 4 \, (1 \leq i \leq N)
- y_i \gt 0 \, (1 \leq i \leq N)
- \sum_{k = 1}^N y_k = M
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
Each case is in the following format:
N M x_1 y_1 \vdots x_N y_N
Output
Print T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.
Sample Input 1
3 3 7 -1 2 2 3 -3 2 10 472 -4 12 1 29 2 77 -1 86 0 51 3 81 3 17 -2 31 -4 65 4 23 1 1000000000 4 1000000000
Sample Output 1
4 53910 2000000002000000000
In the first test case, we have:
- C = (-1, -1, 2, 2, 2, -3, -3)
- B = (-1, -2, 0, 2, 4, 1, -2)
- A = (-1, -3, -3, -1, 3, 4, 2)
Thus, the maximum value among A_1, \dots, A_M is 4.