Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
各桁が 1 以上 9 以下の整数である 3 桁の整数 N が与えられます。
N の 100 の位を a、10 の位を b、1 の位を c としたとき、b,c,a をこの順に並べた整数と c,a,b をこの順に並べた整数をそれぞれ出力してください。
制約
- N は各桁が 1 以上 9 以下の整数である 3 桁の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
b,c,a をこの順に並べた整数と c,a,b をこの順に並べた整数をこの順で空白区切りで出力せよ。
入力例 1
379
出力例 1
793 937
379 の 100 の位は 3、10 の位は 7、1 の位は 9 です。よって、793 と 937 をそれぞれ出力します。
入力例 2
919
出力例 2
199 991
919 の 100 の位は 9、10 の位は 1、1 の位は 9 です。よって、199 と 991 をそれぞれ出力します。
Score : 100 points
Problem Statement
You are given a three-digit integer N where each digit is an integer between 1 and 9, inclusive.
Let a, b, c be the hundreds, tens, ones digits of N, respectively. Print an integer formed by arranging b, c, a in this order, and an integer formed by arranging c, a, b in this order.
Constraints
- N is a three-digit integer where each digit is an integer between 1 and 9, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print two integers separated by a space in the following order: an integer formed by arranging b, c, a in this order, and an integer formed by arranging c, a, b in this order.
Sample Input 1
379
Sample Output 1
793 937
The hundreds, tens, ones digits of 379 are 3, 7, 9, respectively, so print 793 and 937.
Sample Input 2
919
Sample Output 2
199 991
The hundreds, tens, ones digits of 919 are 9, 1, 9, respectively, so print 199 and 991.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は歯が左右一列に N 本生えています。現在の高橋君の歯の状態はある文字列 S によって表されます。
S の i 文字目が O のとき、左から i 番目の歯が丈夫であることを表します。S の i 文字目が X のとき、左から i 番目の歯が虫歯にかかっていることを表します。丈夫である歯は虫歯にかかっていません。
高橋君はある連続する K 本の歯が丈夫であるとき、その K 本の歯を使ってイチゴを 1 個食べることができます。イチゴを食べると、その K 本の歯が虫歯にかかり丈夫でなくなります。
このとき、高橋君は最大で何個のイチゴを食べることができるか求めてください。
制約
- 1 \leq K \leq N \leq 100
- N,K は整数
- S は
OとXからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
7 3 OOXOOOO
出力例 1
1
左から 4 本目の歯から左から 6 本目の歯までの連続する 3 本の丈夫な歯を使ってイチゴを 1 個食べることができます。これ以降、イチゴを食べることができません。また、他にどのような方法でイチゴを食べても 1 個以下しか食べることができません。よって、1 を出力します。
入力例 2
12 2 OXXOOOXOOOOX
出力例 2
3
入力例 3
22 5 XXOOOOOOOOXXOOOOOXXXXX
出力例 3
2
Score : 200 points
Problem Statement
Takahashi has N teeth arranged in a single row from left to right. The current condition of his teeth is represented by a string S.
If the i-th character of S is O, it means that the i-th tooth from the left is healthy. If it is X, it means that the i-th tooth has a cavity. Healthy teeth do not have cavities.
When he has K consecutive healthy teeth, he can eat one strawberry using those K teeth. After eating a strawberry, those K teeth develop cavities and become unhealthy.
Find the maximum number of strawberries he can eat.
Constraints
- 1 \leq K \leq N \leq 100
- N and K are integers.
- S is a string of length N consisting of
OandX.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
7 3 OOXOOOO
Sample Output 1
1
He can eat one strawberry by using the three consecutive healthy teeth from the 4th to 6th tooth from the left. After this, he cannot eat any more strawberries. Besides, there is no way for him to eat more than one strawberry. Therefore, print 1.
Sample Input 2
12 2 OXXOOOXOOOOX
Sample Output 2
3
Sample Input 3
22 5 XXOOOOOOOOXXOOOOOXXXXX
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から N まで番号がつけられた N 個のマスが一列に並んでいます。最初、 M 個のマスに石が入っており、マス X_i には A_i 個 (1 \leq i \leq M) の石が入っています。
あなたは以下の操作を好きな回数( 0 回でもよい)行うことができます。
- マス i (1 \leq i \leq N-1) に石があるとき、マス i から石を 1 つマス i+1 に移動させる。
N 個のマスすべてに石がちょうど 1 個ずつ入っている状態にするために必要な操作回数の最小値を求めてください。ただし、不可能な場合は -1 を出力してください。
制約
- 2 \leq N \leq 2 \times 10^{9}
- 1 \leq M \leq 2 \times 10^{5}
- M \leq N
- 1 \leq X_i \leq N (1 \leq i \leq M)
- X_i \neq X_j (1 \leq i < j \leq M)
- 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 X_2 \ldots X_M A_1 A_2 \ldots A_M
出力
答えを出力せよ。
入力例 1
5 2 1 4 3 2
出力例 1
4
以下の 4 回の操作で、5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることができます。
- マス 1 の石を 1 つマス 2 に移動させる。
- マス 2 の石を 1 つマス 3 に移動させる。
- マス 4 の石を 1 つマス 5 に移動させる。
- マス 1 の石を 1 つマス 2 に移動させる。
また、3 回以下の操作では 5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、4 を出力します。
入力例 2
10 3 1 4 8 4 2 4
出力例 2
-1
どのように操作を行っても 10 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、-1 を出力します。
Score : 300 points
Problem Statement
There are N cells numbered from 1 to N in a row. Initially, M cells contain stones, and cell X_i contains A_i stones (1 \leq i \leq M).
You can perform the following operation any number of times (possibly zero):
- If cell i (1 \leq i \leq N-1) contains a stone, move one stone from cell i to cell i+1.
Find the minimum number of operations required to reach a state where each of the N cells contains exactly one stone. If it is impossible, print -1.
Constraints
- 2 \leq N \leq 2 \times 10^{9}
- 1 \leq M \leq 2 \times 10^{5}
- M \leq N
- 1 \leq X_i \leq N (1 \leq i \leq M)
- X_i \neq X_j (1 \leq i < j \leq M)
- 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 X_2 \ldots X_M A_1 A_2 \ldots A_M
Output
Print the answer.
Sample Input 1
5 2 1 4 3 2
Sample Output 1
4
You can reach a state where each of the five cells contains exactly one stone with four operations as follows:
- Move one stone from cell 1 to cell 2.
- Move one stone from cell 2 to cell 3.
- Move one stone from cell 4 to cell 5.
- Move one stone from cell 1 to cell 2.
It is impossible to achieve the goal in three or fewer operations. Therefore, print 4.
Sample Input 2
10 3 1 4 8 4 2 4
Sample Output 2
-1
No matter how you perform the operations, you cannot reach a state where all ten cells contain exactly one stone. Therefore, print -1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は 10^{100} 個の植木鉢を持っています。最初、高橋君は植物を 1 個も育てていません。
Q 個のクエリが与えられるので、順に処理してください。
クエリは次の 3 種類です。
1: 植物が植えられていない植木鉢を 1 個用意し、その植木鉢に植物を植える。このとき植物の高さは 0 である。2 T: T 日待つ。このとき植えてあるすべての植物の高さが T 増加する。3 H: 高さが H 以上の植物をすべて収穫し、収穫した植物の数を出力する。収穫した植物は植木鉢から取り除かれる。
ただし、高橋君が 1 種類目と 3 種類目のクエリを行うとき、かかる時間は 0 であるとします。
制約
- 1 \leq Q \leq 2 \times 10^{5}
- 1 \leq T,H \leq 10^{9}
- 3 種類目のクエリが 1 つ以上存在する
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
各クエリは以下のいずれかの形式で与えられる。
1
2 T
3 H
出力
3 種類目のクエリが K 個あるとき、K 行出力せよ。 i 行目 (1\leq i\leq K) には、i 個目の 3 種類目のクエリに対する答えを出力せよ。
入力例 1
6 1 2 15 1 3 10 2 20 3 20
出力例 1
1 1
クエリは次の順で処理されます。
- 1 個目のクエリでは高さ 0 の植物が 1 個植えられます。
- 2 個目のクエリでは高さ 0 の植物が高さ 15 になります。
- 3 個目のクエリでは高さ 0 の植物が 1 個植えられます。このとき、高さ 0 と高さ 15 の植物が 1 個ずつあります。
- 4 個目のクエリでは高さ 10 以上の植物が収穫されます。このとき、高さ 15 の植物が 1 個収穫されて高さ 0 の植物が 1 個残ります。1 個の植物を収穫したため、1 行目に 1 と出力します。
- 5 個目のクエリでは高さ 0 の植物が高さ 20 になります。
- 6 個目のクエリでは高さ 20 以上の植物が収穫されます。このとき、高さ 20 の植物が 1 個収穫されます。よって、2 行目に 1 と出力します。
入力例 2
15 1 1 2 226069413 3 1 1 1 2 214168203 1 3 214168203 1 1 1 2 314506461 2 245642315 3 1
出力例 2
2 2 4
Score : 400 points
Problem Statement
Takahashi has 10^{100} flower pots. Initially, he is not growing any plants.
You are given Q queries to process in order.
There are three types of queries as follows.
1: Prepare one empty flower pot and put a plant in it. Here, the plant's height is 0.2 T: Wait for T days. During this time, the height of every existing plants increases by T.3 H: Harvest all plants with a height of at least H, and output the number of plants harvested. The harvested plants are removed from their flower pots.
Assume that performing queries of the first and third types takes zero time.
Constraints
- 1 \leq Q \leq 2 \times 10^{5}
- 1 \leq T,H \leq 10^{9}
- There is at least one query of the third type.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Each query is given in one of the following formats:
1
2 T
3 H
Output
Let there be K queries of the third type, and print K lines. The i-th line (1 \leq i \leq K) should contain the answer to the i-th query of type 3.
Sample Input 1
6 1 2 15 1 3 10 2 20 3 20
Sample Output 1
1 1
Queries are processed in the following order:
- In the first query, a plant of height 0 is planted.
- In the second query, the height of the plant increases to 15.
- In the third query, another plant of height 0 is planted. Now there is one plant of height 15 and one plant of height 0.
- In the fourth query, all plants with height at least 10 are harvested. Here, one plant of height 15 gets harvested, and one plant of height 0 remains. Since one plant was harvested, print 1 on the first line.
- In the fifth query, the height of the remaining plant increases to 20.
- In the sixth query, all plants with height at least 20 are harvested. Here, one plant of height 20 gets harvested. Thus, print 1 on the second line.
Sample Input 2
15 1 1 2 226069413 3 1 1 1 2 214168203 1 3 214168203 1 1 1 2 314506461 2 245642315 3 1
Sample Output 2
2 2 4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
1 から 9 までの数字からなる長さ N の文字列 S が与えられます。
整数の組 (i,j) \ (1\leq i\leq j\leq N) に対して、 f(i,j) を「 S の i 文字目から j 文字目までの連続部分文字列を 10 進法の整数としてみなしたときの値」とします。\displaystyle \sum_{i=1}^N \sum_{j=i}^N f(i,j) を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- N は整数
- S は
1から9までの数字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
3 379
出力例 1
514
答えは f(1,1)+f(1,2)+f(1,3)+f(2,2)+f(2,3)+f(3,3)=3+37+379+7+79+9=514 です。
入力例 2
30 314159265358979323846264338327
出力例 2
369673254065355789035427227741
Score : 475 points
Problem Statement
You are given a string S of length N consisting of digits from 1 through 9.
For each pair of integers (i,j) \ (1\leq i\leq j\leq N), define f(i, j) as the value obtained by interpreting the substring of S from the i-th through the j-th character as a decimal integer. Find \displaystyle \sum_{i=1}^N \sum_{j=i}^N f(i, j).
Constraints
- 1 \leq N \leq 2 \times 10^5
- N is an integer.
- S is a string of length N consisting of digits from
1through9.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
3 379
Sample Output 1
514
The answer is f(1,1) + f(1,2) + f(1,3) + f(2,2) + f(2,3) + f(3,3) = 3 + 37 + 379 + 7 + 79 + 9 = 514.
Sample Input 2
30 314159265358979323846264338327
Sample Output 2
369673254065355789035427227741
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
ビル 1, ビル 2, \ldots, ビル N の N 棟がこの順で東西に一列に並んでおり、ビル 1 が最も西に、ビル N が最も東に建っています。ビル i\ (1\leq i\leq N) の高さは H_i です。
整数の組 (i,j)\ (1\leq i\lt j\leq N) に対して、以下の条件を満たすとき ビル i からビル j を見ることができます。
- ビル i とビル j の間にビル j より高いビルが存在しない。すなわち、H_k\gt H_j を満たす整数 k\ (i\lt k\lt j) が存在しない。
Q 個の質問に答えてください。i 番目の質問では整数の組 (l_i,r_i)\ (l_i\lt r_i) が与えられるので、ビル r_i より東にあるビル(ビル r_i+1, ビル r_i+2,\ldots,ビル N )のうちビル l_i とビル r_i の両方から見ることができるものの個数を答えてください。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq Q\leq 2\times 10^5
- 1\leq H_i\leq N
- H_i\neq H_j\ (i\neq j)
- 1\leq l_i\lt r_i\leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q H_1 H_2 \ldots H_N l_1 r_1 l_2 r_2 \vdots l_Q r_Q
出力
Q 行出力せよ。i\ (1\leq i\leq Q) 行目には i 番目の質問に対する答えを出力せよ。
入力例 1
5 3 2 1 4 3 5 1 2 3 5 1 4
出力例 1
2 0 1
- 1 つ目の質問について、ビル 2 より東にあるビルのうち ビル 1 とビル 2 の両方から見ることができるものはビル 3,5 の 2 つです。
- 2 つ目の質問について、ビル 5 より東にあるビルは存在しません。
- 3 つ目の質問について、ビル 4 より東にあるビルのうち、ビル 1,4 の両方から見ることができるビルはビル 5 の 1 つです。
入力例 2
10 10 2 1 5 3 4 6 9 8 7 10 3 9 2 5 4 8 5 6 3 8 2 10 7 8 6 7 8 10 4 10
出力例 2
1 3 1 2 1 0 1 1 0 0
Score : 550 points
Problem Statement
There are N buildings, building 1, building 2, \ldots, building N, arranged in this order in a straight line from west to east. Building 1 is the westernmost, and building N is the easternmost. The height of building i\ (1\leq i\leq N) is H_i.
For a pair of integers (i,j)\ (1\leq i\lt j\leq N), building j can be seen from building i if the following condition is satisfied.
- There is no building taller than building j between buildings i and j. In other words, there is no integer k\ (i\lt k\lt j) such that H_k > H_j.
You are given Q queries. In the i-th query, given a pair of integers (l_i,r_i)\ (l_i\lt r_i), find the number of buildings to the east of building r_i (that is, buildings r_i + 1, r_i + 2, \ldots, N) that can be seen from both buildings l_i and r_i.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq H_i \leq N
- H_i\neq H_j\ (i\neq j)
- 1 \leq l_i < r_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q H_1 H_2 \ldots H_N l_1 r_1 l_2 r_2 \vdots l_Q r_Q
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.
Sample Input 1
5 3 2 1 4 3 5 1 2 3 5 1 4
Sample Output 1
2 0 1
- For the first query, among the buildings to the east of building 2, buildings 3 and 5 can be seen from both buildings 1 and 2, so the answer is 2.
- For the second query, there are no buildings to the east of building 5.
- For the third query, among the buildings to the east of building 4, building 5 can be seen from both buildings 1 and 4, so the answer is 1.
Sample Input 2
10 10 2 1 5 3 4 6 9 8 7 10 3 9 2 5 4 8 5 6 3 8 2 10 7 8 6 7 8 10 4 10
Sample Output 2
1 3 1 2 1 0 1 1 0 0
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
1,2,3,? からなる H 行 W 列のグリッド S が与えられます。上から i 行目、左から j 列目の文字は S_{i,j} です。
S の各 ? を 1,2,3 のいずれかに置き換えて得られるグリッドは ? の個数を q として 3^q 通りありますが、そのうち以下の条件を満たすものはいくつありますか? 998244353 で割った余りを出力してください。
- 隣接する(辺を共有する)どの 2 つのマスにも異なる数字が書かれている。
制約
- 1\leq H,W
- H\times W\leq 200
- H,W は整数
- S は
1,2,3,?からなる H 行 W 列のグリッド
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\ldots S_{1,W}
S_{2,1}S_{2,2}\ldots S_{2,W}
\vdots
S_{H,1}S_{H,2}\ldots S_{H,W}
出力
答えを出力せよ。
入力例 1
2 2 1? ??
出力例 1
6
S の各 ? を 1,2,3 のいずれかに置き換えて得られるグリッドのうち、条件を満たすものは以下の 6 つです。
12 12 12 13 13 13 21 23 31 21 31 32
入力例 2
2 3 123 3?1
出力例 2
0
S の各 ? を 1,2,3 のいずれかに置き換えて得られるグリッドはすべて条件を満たしません。
入力例 3
8 8 3?1????? ???1???? ??????2? ???????? ???????? ????13?? ??13?1?? ????????
出力例 3
779135038
Score : 575 points
Problem Statement
You are given a grid S with H rows and W columns consisting of 1, 2, 3, and ?. The character at the i-th row and j-th column is S_{i,j}.
By replacing each ? in S with 1, 2, or 3, we can obtain 3^q different grids, where q is the number of ?. Among these grids, how many satisfy the following condition? Print the count modulo 998244353.
- Any two adjacent (edge-sharing) cells contain different digits.
Constraints
- 1 \leq H, W
- H \times W \leq 200
- H and W are integers.
- S is a grid with H rows and W columns consisting of
1,2,3, and?.
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\ldots S_{1,W}
S_{2,1}S_{2,2}\ldots S_{2,W}
\vdots
S_{H,1}S_{H,2}\ldots S_{H,W}
Output
Print the answer.
Sample Input 1
2 2 1? ??
Sample Output 1
6
Among the grids obtained by replacing each ? in S with 1, 2, or 3, the following six grids satisfy the condition.
12 12 12 13 13 13 21 23 31 21 31 32
Sample Input 2
2 3 123 3?1
Sample Output 2
0
None of the grids obtained by replacing ? satisfies the condition.
Sample Input 3
8 8 3?1????? ???1???? ??????2? ???????? ???????? ????13?? ??13?1?? ????????
Sample Output 3
779135038