実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
各マスの状態は文字 C_{i,j} で表されます。C_{i,j} が .
ならば (i, j) には何も置かれておらず、 #
ならば箱が 1 個置かれています。
1 \leq j \leq W を満たす整数 j に対して、整数 X_j を次のように定義します。
- j 列目に置かれている箱の個数。言い換えると、C_{i,j} が
#
であるような整数 i の個数。
X_1, X_2, \dots, X_W をすべて求めてください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H, W は整数
- C_{i, j} は
.
または#
入力
入力は以下の形式で標準入力から与えられる。
H W C_{1,1}C_{1,2}\dots C_{1,W} C_{2,1}C_{2,2}\dots C_{2,W} \vdots C_{H,1}C_{H,2}\dots C_{H,W}
出力
X_1, X_2, \dots, X_W を以下の形式に従って出力せよ。
X_1 X_2 \dots X_W
入力例 1
3 4 #..# .#.# .#.#
出力例 1
1 2 0 3
1 列目の箱が置かれているマスは (1, 1) の 1 ヵ所です。よって X_1 = 1 です。
2 列目の箱が置かれているマスは (2, 2), (3, 2) の 2 ヵ所です。よって X_2 = 2 です。
3 列目の箱が置かれているマスは存在しません。よって X_3 = 0 です。
4 列目の箱が置かれているマスは (1, 4), (2, 4), (3, 4) の 3 ヵ所です。よって X_4 = 3 です。
よって (X_1, X_2, X_3, X_4) = (1, 2, 0, 3) が答えとなります。
入力例 2
3 7 ....... ....... .......
出力例 2
0 0 0 0 0 0 0
箱が置かれているマスが存在しない場合もあります。
入力例 3
8 3 .#. ### .#. .#. .## ..# ##. .##
出力例 3
2 7 4
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
Score : 200 points
Problem Statement
There is a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
The squares are described by characters C_{i,j}. If C_{i,j} is .
, (i, j) is empty; if it is #
, (i, j) contains a box.
For integers j satisfying 1 \leq j \leq W, let the integer X_j defined as follows.
- X_j is the number of boxes in the j-th column. In other words, X_j is the number of integers i such that C_{i,j} is
#
.
Find all of X_1, X_2, \dots, X_W.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H and W are integers.
- C_{i, j} is
.
or#
.
Input
The input is given from Standard Input in the following format:
H W C_{1,1}C_{1,2}\dots C_{1,W} C_{2,1}C_{2,2}\dots C_{2,W} \vdots C_{H,1}C_{H,2}\dots C_{H,W}
Output
Print X_1, X_2, \dots, X_W in the following format:
X_1 X_2 \dots X_W
Sample Input 1
3 4 #..# .#.# .#.#
Sample Output 1
1 2 0 3
In the 1-st column, one square, (1, 1), contains a box. Thus, X_1 = 1.
In the 2-nd column, two squares, (2, 2) and (3, 2), contain a box. Thus, X_2 = 2.
In the 3-rd column, no squares contain a box. Thus, X_3 = 0.
In the 4-th column, three squares, (1, 4), (2, 4), and (3, 4), contain a box. Thus, X_4 = 3.
Therefore, the answer is (X_1, X_2, X_3, X_4) = (1, 2, 0, 3).
Sample Input 2
3 7 ....... ....... .......
Sample Output 2
0 0 0 0 0 0 0
There may be no square that contains a box.
Sample Input 3
8 3 .#. ### .#. .#. .## ..# ##. .##
Sample Output 3
2 7 4
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
H 行 W 列の行列 A が与えられます。
A の上から i 行目、左から j 列目の要素は A_{i,j} です。
ここで、W 行 H 列の行列 B を、上から i 行目、左から j 列目の要素が A_{j,i} と一致するような行列として定めます。
すなわち、B は A の転置行列です。
B を出力してください。
制約
- 1\leq H,W \leq 10^5
- H \times W \leq 10^5
- 1 \leq A_{i,j} \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
H W A_{1,1} A_{1,2} \ldots A_{1,W} A_{2,1} A_{2,2} \ldots A_{2,W} \vdots A_{H,1} A_{H,2} \ldots A_{H,W}
出力
B を以下の形式で出力せよ。
B_{1,1} B_{1,2} \ldots B_{1,H} B_{2,1} B_{2,2} \ldots B_{2,H} \vdots B_{W,1} B_{W,2} \ldots B_{W,H}
入力例 1
4 3 1 2 3 4 5 6 7 8 9 10 11 12
出力例 1
1 4 7 10 2 5 8 11 3 6 9 12
たとえば A_{2,1}=4 なので、転置行列 B の上から 1 行目、左から 2 列目の要素は 4 になります。
入力例 2
2 2 1000000000 1000000000 1000000000 1000000000
出力例 2
1000000000 1000000000 1000000000 1000000000
Score : 200 points
Problem Statement
You are given an H-by-W matrix A.
The element at the i-th row from the top and j-th column from the left of A is A_{i,j}.
Let B be a W-by-H matrix whose element at the i-th row from the top and j-th column from the left equals A_{j, i}.
That is, B is the transpose of A.
Print B.
Constraints
- 1\leq H,W \leq 10^5
- H \times W \leq 10^5
- 1 \leq A_{i,j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W A_{1,1} A_{1,2} \ldots A_{1,W} A_{2,1} A_{2,2} \ldots A_{2,W} \vdots A_{H,1} A_{H,2} \ldots A_{H,W}
Output
Print B in the following format:
B_{1,1} B_{1,2} \ldots B_{1,H} B_{2,1} B_{2,2} \ldots B_{2,H} \vdots B_{W,1} B_{W,2} \ldots B_{W,H}
Sample Input 1
4 3 1 2 3 4 5 6 7 8 9 10 11 12
Sample Output 1
1 4 7 10 2 5 8 11 3 6 9 12
For example, we have A_{2,1}=4, so the element at the 1-st row from the top and 2-nd column from the left of the transpose B is 4.
Sample Input 2
2 2 1000000000 1000000000 1000000000 1000000000
Sample Output 2
1000000000 1000000000 1000000000 1000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 人の巨人がいます。巨人にはそれぞれ 1, 2, \ldots, N の名前がついており、巨人 i が地面に立ったとき、肩の高さは A_i、頭の高さは B_i となります。
あなたは (1, 2, \ldots, N) を並べ替えて得られる数列 (P_1, P_2, \ldots, P_N) を選び、以下の規則に従って N 人の巨人を積み上げることができます。
-
まず地面に巨人 P_1 を立たせる。巨人 P_1 の肩は地面を基準として A_{P_1}、頭は地面を基準として B_{P_1} の高さとなる。
-
i = 1, 2, \ldots, N - 1 の順に巨人 P_i の肩の上に巨人 P_{i + 1} を立たせる。巨人 P_i の肩が地面を基準として高さ t のとき、巨人 P_{i + 1} の肩は地面を基準として t + A_{P_{i + 1}}、頭は地面を基準として t + B_{P_{i + 1}} の高さとなる。
一番上に立っている巨人、すなわち巨人 P_N の地面を基準とした頭の高さとして実現できる最大値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
3 4 10 5 8 2 9
出力例 1
18
(P_1, P_2, P_3) = (2, 1, 3) とすると、地面を基準として巨人 2 は肩の高さが 5、頭の高さが 8、巨人 1 は肩の高さが 9、頭の高さが 15、巨人 3 は肩の高さが 11、頭の高さが 18 となります。
一番上に立っている巨人の頭の高さが地面を基準として 18 より大きくなることはないため 18 を出力します。
入力例 2
5 1 1 1 1 1 1 1 1 1 1
出力例 2
5
入力例 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
出力例 3
7362669937
Score: 300 points
Problem Statement
There are N giants, named 1 to N. When giant i stands on the ground, their shoulder height is A_i, and their head height is B_i.
You can choose a permutation (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) and stack the N giants according to the following rules:
-
First, place giant P_1 on the ground. The giant P_1's shoulder will be at a height of A_{P_1} from the ground, and their head will be at a height of B_{P_1} from the ground.
-
For i = 1, 2, \ldots, N - 1 in order, place giant P_{i + 1} on the shoulders of giant P_i. If giant P_i's shoulders are at a height of t from the ground, then giant P_{i + 1}'s shoulders will be at a height of t + A_{P_{i + 1}} from the ground, and their head will be at a height of t + B_{P_{i + 1}} from the ground.
Find the maximum possible height of the head of the topmost giant P_N from the ground.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer.
Sample Input 1
3 4 10 5 8 2 9
Sample Output 1
18
If (P_1, P_2, P_3) = (2, 1, 3), then measuring from the ground, giant 2 has a shoulder height of 5 and a head height of 8, giant 1 has a shoulder height of 9 and a head height of 15, and giant 3 has a shoulder height of 11 and a head height of 18.
The head height of the topmost giant from the ground cannot be greater than 18, so print 18.
Sample Input 2
5 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Sample Input 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
Sample Output 3
7362669937
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
キーエンス本社に勤務する人数が増えてきたので、本社に存在する部署を 2 つのグループに分け、昼休みの時間帯を分けることにしました。
キーエンス本社には N 個の部署が存在し、i 番目 (1\leq i\leq N) の部署に所属する人数は K_i 人です。
それぞれの部署をグループ A, B のいずれか一方に割り当て、グループごとに同時に昼休みをとり、
かつグループ A, B の昼休みの時間が重ならないようにしたとき、同時に昼休みをとる最大人数としてあり得る最小の値を求めてください。
すなわち、グループ A に割り当てられた部署に所属する人数の合計とグループ B に割り当てられた部署に所属する人数の合計
のうち大きい方の値としてあり得る最小の値を求めてください。
制約
- 2\leq N \leq 20
- 1\leq K_i \leq 10^8
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K_1 K_2 \ldots K_N
出力
同時に昼休みを取る最大人数としてあり得る最小の値を出力せよ。
入力例 1
5 2 3 5 10 12
出力例 1
17
1,2,5 番目の部署をグループ A に、3,4 番目の部署をグループ B に割り当てたとき、 グループ A に割り当てられた部署に所属する人数の合計は 2+3+12=17 、 グループ B に割り当てられた部署に所属する人数の合計は 5+10=15 となり、 このとき同時に昼休みを取る最大人数は 17 となります。
一方で、グループ A,B それぞれに割り当てられた部署に所属する人数の合計がいずれも 16 以下になるように 部署を割り当てることはできないため、17 を出力します。
入力例 2
2 1 1
出力例 2
1
同一人数の部署が複数存在する可能性もあります。
入力例 3
6 22 25 26 45 22 31
出力例 3
89
例えば、1,4,5 番目の部署をグループ A に、2,3,6 番目の部署をグループ B に割り当てたとき同時に昼休みを取る最大人数は 89 となります。
Score : 300 points
Problem Statement
As KEYENCE headquarters have more and more workers, they decided to divide the departments in the headquarters into two groups and stagger their lunch breaks.
KEYENCE headquarters have N departments, and the number of people in the i-th department (1\leq i\leq N) is K_i.
When assigning each department to Group A or Group B, having each group take lunch breaks at the same time, and ensuring that the lunch break times of Group A and Group B do not overlap, find the minimum possible value of the maximum number of people taking a lunch break at the same time.
In other words, find the minimum possible value of the larger of the following: the total number of people in departments assigned to Group A, and the total number of people in departments assigned to Group B.
Constraints
- 2 \leq N \leq 20
- 1 \leq K_i \leq 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K_1 K_2 \ldots K_N
Output
Print the minimum possible value of the maximum number of people taking a lunch break at the same time.
Sample Input 1
5 2 3 5 10 12
Sample Output 1
17
When assigning departments 1, 2, and 5 to Group A, and departments 3 and 4 to Group B, Group A has a total of 2+3+12=17 people, and Group B has a total of 5+10=15 people. Thus, the maximum number of people taking a lunch break at the same time is 17.
It is impossible to assign the departments so that both groups have 16 or fewer people, so print 17.
Sample Input 2
2 1 1
Sample Output 2
1
Multiple departments may have the same number of people.
Sample Input 3
6 22 25 26 45 22 31
Sample Output 3
89
For example, when assigning departments 1, 4, and 5 to Group A, and departments 2, 3, and 6 to Group B, the maximum number of people taking a lunch break at the same time is 89.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
20230322
は並べ替えると 02320232
となり、これは 0232
を 2 度繰り返しています。
このように、数字のみからなる文字列であって、適切に文字を並び替える (そのままでもよい) ことによって同じ列を 2 度繰り返すようにできるものを 嬉しい列 と呼びます。
数字のみからなる文字列 S が与えられるので、以下の条件を全て満たす整数の組 (l,r) はいくつあるか求めてください。
- 1 \le l \le r \le |S| ( |S| は S の長さ)
- S の l 文字目から r 文字目までの (連続する) 部分文字列は嬉しい列である。
制約
- S は数字のみからなる長さ 1 以上 5 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
20230322
出力例 1
4
S= 20230322
です。
条件を満たす整数組 (l,r) は (1,6),(1,8),(2,7),(7,8) の 4 つです。
入力例 2
0112223333444445555556666666777777778888888889999999999
出力例 2
185
S の先頭が 0
である場合もあります。
入力例 3
3141592653589793238462643383279502884197169399375105820974944
出力例 3
9
Score : 400 points
Problem Statement
The string 20230322
can be rearranged into 02320232
, which is a repetition of 0232
twice.
Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice.
You are given a string S consisting of digits. Find the number of pairs of integers (l,r) satisfying all of the following conditions.
- 1 \le l \le r \le |S|. (|S| is the length of S.)
- The (contiguous) substring formed of the l-th through r-th characters of S is happy.
Constraints
- S is a string consisting of digits whose length is between 1 and 5 \times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print an integer representing the answer.
Sample Input 1
20230322
Sample Output 1
4
We have S= 20230322
.
Here are the four pairs of integers (l,r) that satisfy the condition: (1,6), (1,8), (2,7), and (7,8).
Sample Input 2
0112223333444445555556666666777777778888888889999999999
Sample Output 2
185
S may begin with 0
.
Sample Input 3
3141592653589793238462643383279502884197169399375105820974944
Sample Output 3
9