実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
Alice と Bob がゲームをします。
最初、山には N 個の石があり、先手から順に交互に山から石を 1 個ずつ取り除いていきます。
S が 0 のとき Alice が先手で、1 のとき Bob が先手です。
T が 0 のとき最後の石を取り除いた人が勝ちで、1 のとき最後の石を取り除いた人が負け(他方が勝ち)です。
どちらが勝つか判定してください。
制約
- 1 \leq N \leq 100
- N は整数
- S,T は 0 または 1
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
Alice が勝つとき Alice、Bobが勝つとき Bob と出力せよ。
入力例 1
3 0 1
出力例 1
Bob
Alice が先手でゲームが行われます。
- 最初、山には 3 個の石がある。
- Alice が山から石を 1 個取り除く。山には 2 個石が残っている。
- Bob が山から石を 1 個取り除く。山には 1 個石が残っている。
- Alice が山から石を 1 個取り除く。最後の石を取り除いたので Alice の負けである。
入力例 2
2 1 0
出力例 2
Alice
Bob が先手でゲームが行われます。
- 最初、山には 2 個の石がある。
- Bob が山から石を 1 個取り除く。山には 1 個石が残っている。
- Alice が山から石を 1 個取り除く。最後の石を取り除いたので Alice の勝ちである。
Problem Statement
Alice and Bob play a game.
Initially, the pile has N stones. The two players alternately remove one stone from the pile.
If S is 0, Alice goes first; if it is 1, Bob goes first.
If T is 0, the player who removes the last stone wins; if it is 1, the player who removes the last stone loses (and the other player wins).
Determine which player will win.
Constraints
- 1 \leq N \leq 100
- N is an integer.
- S is 0 or 1, and so is T.
Input
The input is given from Standard Input in the following format:
N S T
Output
Print Alice if Alice will win; print Bob if Bob will win.
Sample Input 1
3 0 1
Sample Output 1
Bob
The game starts with Alice going first.
- The pile has initially 3 stones.
- Alice removes one stone from the pile. The pile has 2 remaining stones.
- Bob removes one stone from the pile. The pile has 1 remaining stone.
- Alice removes one stone from the pile. She loses because she has removed the last stone.
Sample Input 2
2 1 0
Sample Output 2
Alice
The game starts with Bob going first.
- The pile has 2 stones.
- Bob removes one stone from the pile. The pile has 1 remaining stone.
- Alice removes one stone from the pile. She wins because she has removed the last stone.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 8 点
問題文
小数 A,B が小数点第 D 位まで表示した形式で与えられます。
A+B を誤差なく計算して同様の形式で出力してください。
制約
- 1 \leq D \leq 100
- D は整数である。
- 1 \leq A \lt 100
- 1 \leq B \lt 100
- A,B は (整数部を先行ゼロなしで表示したもの),
., (小数部を小数点第 D 位まで表示したもの) をつなげた形式で入力される小数である。
入力
入力は以下の形式で標準入力から与えられる。
D A B
出力
答えを (整数部を先行ゼロなしで表示したもの), ., (小数部を小数点第 D 位まで表示したもの) という形式で出力せよ。出力に誤差がある場合は誤答とみなすことに注意せよ。
また、先行ゼロも認めない。 たとえば答えが 3.50 のときに 03.50 のように出力してはならない。
入力例 1
1 1.2 1.8
出力例 1
3.0
1.2 + 1.8 = 3 で、これを小数点第 1 位まで表示した 3.0 という形式で出力してください。3, 3.00, 003.0 のような表記は所定の形式を守っていないので誤答として扱われます。
入力例 2
3 1.234 7.890
出力例 2
9.124
入力例 3
4 1.2345 8.7655
出力例 3
10.0000
Score : 8 points
Problem Statement
You are given decimals A and B with D decimal places.
Precisely compute A+B and print the result in the same format.
Constraints
- 1 \leq D \leq 100
- D is an integer.
- 1 \leq A \lt 100
- 1 \leq B \lt 100
- Each of A and B is given as a concatenation of (the decimal part without leading zeros),
., and (the fractional part represented by D decimal places).
Input
The input is given from Standard Input in the following format:
D A B
Output
Print the result as a concatenation of (the decimal part without leading zeros), ., and (the fractional part represented by D decimal places). Note that your output is considered incorrect if it has an error from the true value.
Also, leading zeros are not permitted. For example, if the answer is 3.50, do not print, for instance, 03.50.
Sample Input 1
1 1.2 1.8
Sample Output 1
3.0
1.2 + 1.8 = 3, so 3.0 should be printed, which contains one decimal place. Representations like 3, 3.00, and 003.0 are not accepted, because they violate the specified format.
Sample Input 2
3 1.234 7.890
Sample Output 2
9.124
Sample Input 3
4 1.2345 8.7655
Sample Output 3
10.0000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
長さ N の数列 (1, 2, 3, \ldots, N) を並べ替えて得られる数列 P = (P_1, P_2, \ldots, P_N) が与えられます。
K = 1, 2, \ldots, N のそれぞれについて、K が数列 P の何番目の要素であるかを出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq P_i \leq N
- i \neq j \implies P_i \neq P_j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N
出力
K = 1, 2, \ldots, N のそれぞれに対する答え X_K を、下記の形式にしたがって空白区切りで出力せよ。
X_1 X_2 \ldots X_N
入力例 1
5 5 2 4 1 3
出力例 1
4 2 5 3 1
- K = 1 について、1 は数列 P の 4 番目の要素です。
- K = 2 について、2 は数列 P の 2 番目の要素です。
- K = 3 について、3 は数列 P の 5 番目の要素です。
- K = 4 について、4 は数列 P の 3 番目の要素です。
- K = 5 について、5 は数列 P の 1 番目の要素です。
入力例 2
1 1
出力例 2
1
入力例 3
10 10 2 3 7 8 6 1 9 5 4
出力例 3
7 2 3 10 9 6 4 5 8 1
Problem Statement
You are given a sequence P = (P_1, P_2, \ldots, P_N) that is a permutation of a length-N sequence (1, 2, 3, \ldots, N).
For each K = 1, 2, \ldots, N, find the integer X_K such that K is the X_K-th element of the sequence P.
Constraints
- 1 \leq N \leq 100
- 1 \leq P_i \leq N
- i \neq j \implies P_i \neq P_j
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N
Output
Print the answers X_K for K = 1, 2, \ldots, N in the following format, separated by spaces.
X_1 X_2 \ldots X_N
Sample Input 1
5 5 2 4 1 3
Sample Output 1
4 2 5 3 1
- For K = 1, 1 is the 4-th element of the sequence P.
- For K = 2, 2 is the 2-nd element of the sequence P.
- For K = 3, 3 is the 5-th element of the sequence P.
- For K = 4, 4 is the 3-rd element of the sequence P.
- For K = 5, 5 is the 1-st element of the sequence P.
Sample Input 2
1 1
Sample Output 2
1
Sample Input 3
10 10 2 3 7 8 6 1 9 5 4
Sample Output 3
7 2 3 10 9 6 4 5 8 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
アイテムを買って使うことで、体力 H の敵を倒すゲームをします。アイテムは以下の 2 種類です。
-
アイテム 1: 使うと敵の体力を A 減らします。値段は B 円です。
-
アイテム 2: 使うと敵の体力を C 減らします。体力を C 減らした後の敵の体力を h として、h>0 ならばさらに敵の体力を \lfloor \frac{h}{2}\rfloor 減らします。値段は D 円です。ここで、\lfloor A \rfloor は A の小数点以下を切り捨てた値を指します。
アイテムは使い切りであり 1 回使うと消えてしまいますが、好きな個数買うことができます。また、使う順番は自由です。
敵の体力を 0 以下にするために必要なお金の最小値を求めてください。
制約
- 1\leq H,A,B,C,D \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H A B C D
出力
答えを出力せよ。
入力例 1
13 6 5 1 7
出力例 1
12
アイテム 1 を 1 個、アイテム 2 を 1 個買い、以下の順番で使います。
-
アイテム 2 を使う。敵の体力は 1 減り、12 となる。さらに敵の体力は \lfloor \frac{12}{2}\rfloor=6 減り、 6 となる。
-
アイテム 1 を使う。敵の体力は 6 減り、0 となる。
これで敵の体力を 0 以下にすることができました。必要なお金の合計は、 5 + 7 = 12 円であり、これが最小値であることが証明可能です。
入力例 2
1000 1 1 1 10000
出力例 2
1000
Problem Statement
You are playing a game, where you buy and use items to beat an enemy with stamina H. There are two kinds of items.
-
Item 1: when you use it, the enemy's stamina is reduced by A. The price is B yen (the currency in Japan).
-
Item 2: when you use it, the enemy's stamina is reduced by C. Let h be the resulting stamina. Then, if h>0, the enemy's stamina is further reduced by \lfloor \frac{h}{2}\rfloor, where \lfloor A \rfloor denotes the value A rounded down to an integer.
An item disappears once you use it, but you may buy any number of the items. Also, you may use items in any order.
Find the minimum amount of money required to make the enemy's stamina 0 or less.
Constraints
- 1\leq H,A,B,C,D \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
H A B C D
Output
Print the answer.
Sample Input 1
13 6 5 1 7
Sample Output 1
12
You can buy one item 1 and one item 2 and use them in the following order:
-
Use item 2 to reduce the enemy's stamina by 1, making it 12. Then, the enemy's stamina is further reduced by \lfloor \frac{12}{2}\rfloor=6 and becomes 6.
-
Use item 1 to reduce the enemy's stamina by 6, making it 0.
This way, you can make the enemy's stamina 0 or less. The total amount of money required is 5 + 7 = 12, which we can prove is the minimum.
Sample Input 2
1000 1 1 1 10000
Sample Output 2
1000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
# と . からなる H 行 W 列の図形 S,T が与えられます。
図形 S は H 個の文字列 S _ 1,S _ 2,\ldots,S _ H として与えられ、 S _ i の j 文字目は S の i 行 j 列にある要素を表します。 T についても同様です。
S を行ごとに並べ替えて T と等しくできるか判定してください。
ただし、図形 X を行ごとに並べ替えるとは、以下の操作を言います。
- i=1,2,\ldots,H のそれぞれについて、独立に次の操作を行う。
- (1,2,\ldots,W) の順列 P=(P _ 1,P _ 2,\ldots,P _ W) をひとつ選択する。
- 1 \leq j \leq W を満たす全ての整数 j について同時に、 X の i 行 j 列にある要素を i 行 P_j 列にある要素に置き換える。
異なる i に対して異なる順列 P を選んでもよいことに注意してください。
制約
- H,W は整数
- 1 \leq H,W
- 1 \leq H \times W \leq 4 \times 10 ^ 5
- S _ i,T _ i は
#と.からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
出力
S を T と等しくできるなら Yes 、 そうでないなら No と出力せよ。
入力例 1
3 4 ##.# ##.. .#.. ###. #..# ...#
出力例 1
Yes
例えば i=1,2,3 について P としてそれぞれ (4,2,1,3),(1,3,4,2),(4,1,3,2) を選ぶと、 S を T と等しくできます。
入力例 2
3 4 #... #..# .### ..## #..# ##..
出力例 2
No
入力例 3
2 1 # . # .
出力例 3
Yes
S=T である場合もあります。
入力例 4
8 7 ..#.#.# ..#.... #.#.... ..#.#.# ..#..#. ..#...# ..#.... #.#.... ##.#... ...#... #..#... ..#..## ...#.#. ....#.# ......# #....#.
出力例 4
Yes
Problem Statement
You are given figures S and T with H rows and W columns consisting of # and ..
The figure S is given as H strings S _ 1,S _ 2,\ldots,S _ H; the j-th character of S _ i represents the element at the i-th row and j-th column of S. The figure T is given similarly.
Determine whether one can rearrange each row of S to make S equal T.
Here, rearranging each row of a figure X is the following operation.
- For each i=1,2,\ldots,H, perform the following procedure independently.
- Choose a permutation P=(P _ 1,P _ 2,\ldots,P _ W) of (1,2,\ldots,W).
- For all integers j such that 1 \leq j \leq W, simultaneously replace the element at the i-th row and j-th column of X with the one at the i-th row and P_j-th column.
Note that you may choose different permutations P for different i.
Constraints
- H and W are integers.
- 1 \leq H,W
- 1 \leq H \times W \leq 4 \times 10 ^ 5
- S_i and T_i are strings of length W consisting of
#and..
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
Output
Print Yes if one can make S equal T; print No otherwise.
Sample Input 1
3 4 ##.# ##.. .#.. ###. #..# ...#
Sample Output 1
Yes
For example, if you choose (4,2,1,3),(1,3,4,2),(4,1,3,2) for i=1,2,3, respectively, you can make S equal T.
Sample Input 2
3 4 #... #..# .### ..## #..# ##..
Sample Output 2
No
Sample Input 3
2 1 # . # .
Sample Output 3
Yes
S=T may hold.
Sample Input 4
8 7 ..#.#.# ..#.... #.#.... ..#.#.# ..#..#. ..#...# ..#.... #.#.... ##.#... ...#... #..#... ..#..## ...#.#. ....#.# ......# #....#.
Sample Output 4
Yes
実行時間制限: 4 sec / メモリ制限: 1024 MiB
問題文
N 個の整数からなる整数の集合 S=\{S_1,S_2,\dots,S_N\} が与えられるので、以下の Q 個の質問に答えてください。
- M 個の整数からなる整数の集合 T=\{T_1,T_2,\dots,T_M\} が与えられるので、 S と T の和集合には何個の整数が含まれるか答えよ。
制約
- 入力は全て整数
- 1 \le N \le 2 \times 10^5
- 1 \le S_i \le 10^9
- i \neq j ならば S_i \neq S_j
- 全ての質問は以下の制約を満たす
- 1 \le M
- i \neq j ならば T_i \neq T_j
- ひとつの入力に含まれる M の総和は 2 \times 10^5 を超えない
入力
入力は以下の形式で標準入力から与えられる。
N
S_1 S_2 \dots S_N
Q
\rm{Query}_{1}
\rm{Query}_{2}
\vdots
\rm{Query}_{Q}
但し、 \rm{Query}_{i} は i 個目の質問を表す。 各質問は以下の形式で与えられる。
M T_1 T_2 \dots T_M
出力
Q 行にわたって出力せよ。
i 行目には i 個目の質問に対する答えを整数として出力せよ。
入力例 1
5 2 3 5 7 11 5 6 1 3 5 7 9 11 6 12 10 8 6 4 2 3 5 7 3 3 8 4 6 12 3 2 1 6 5 4 9 8 7 12 11 10
出力例 1
7 10 5 8 12
この入力では、 S=\{2,3,5,7,11\} であり、質問が 5 つ与えられます。
- 1 つ目の質問では、 T=\{1,3,5,7,9,11\} です。 S と T の和集合は \{1,2,3,5,7,9,11\} であり、ここには 7 個の整数が含まれます。
- 2 つ目の質問では、 T=\{12,10,8,6,4,2\} です。 S と T の和集合は \{2,3,4,5,6,7,8,10,11,12\} であり、ここには 10 個の整数が含まれます。
Problem Statement
Given a set of N integers, S=\{S_1,S_2,\dots,S_N\}, answer Q queries in the following format:
- given a set of M integers, T=\{T_1,T_2,\dots,T_M\}, find how many integers the union of S and T contains.
Constraints
- All values in the input are integers.
- 1 \le N \le 2 \times 10^5
- 1 \le S_i \le 10^9
- If i \neq j, then S_i \neq S_j.
- All queries satisfy the following conditions:
- 1 \le M
- If i \neq j, then T_i \neq T_j.
- The sum of M in an input does not exceed 2 \times 10^5.
Input
The input is given from Standard Input in the following format:
N
S_1 S_2 \dots S_N
Q
\rm{Query}_{1}
\rm{Query}_{2}
\vdots
\rm{Query}_{Q}
Here, \rm{Query}_{i} denotes the i-th query. Each query is given in the following format:
M T_1 T_2 \dots T_M
Output
Print Q lines.
The i-th line should contain the answer to the i-th query as an integer.
Sample Input 1
5 2 3 5 7 11 5 6 1 3 5 7 9 11 6 12 10 8 6 4 2 3 5 7 3 3 8 4 6 12 3 2 1 6 5 4 9 8 7 12 11 10
Sample Output 1
7 10 5 8 12
In this input, S=\{2,3,5,7,11\}, and five queries are given.
- In the first query, T=\{1,3,5,7,9,11\}. The union of S and T is \{1,2,3,5,7,9,11\}, which contains seven integers.
- In the second query, T=\{12,10,8,6,4,2\}. The union of S and T is \{2,3,4,5,6,7,8,10,11,12\}, which contains ten integers.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
縦 H 行横 W 列のマス目があり、上から i 行目、左から j 列目のマスには数 A_{i,j} が書かれています。
A_{i,j} はそれぞれ 1 以上 HW 以下の整数であり相異なります。
1\leq x < y \leq HW を満たす整数の組 (x,y) であって、x が書かれているマスと y が書かれているマスが隣り合っているようなものを全て求めてください。
なお、2 つのマスが隣り合っているとは、2 つのマスが辺を共有していることを指します。
制約
- 1 \leq H,W
- 2 \leq H \times W \leq 2\times 10^5
- 1 \leq A_{i,j} \leq H \times W
- H と W は整数である
- A_{i,j} は相異なる整数である
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1} \ldots A_{1,W}
\vdots
A_{H,1} \ldots A_{H,W}
出力
1\leq x < y \leq HW を満たす整数の組 (x,y) であって、x が書かれているマスと y が書かれているマスが隣り合っているようなものの個数を K とする。
K 行出力せよ。i 行目には、そのような組のうち辞書順で i 番目のものについて、x,y を空白区切りで出力せよ。
入力例 1
2 3 2 3 4 1 6 5
出力例 1
1 2 1 6 2 3 3 4 3 6 4 5 5 6
1 が書かれているマスと 2 が書かれているマスは隣り合っています。
入力例 2
3 1 1 2 3
出力例 2
1 2 2 3
Problem Statement
There is a grid with H horizontal rows and W vertical columns. The square at the i-th row from the top and j-th column from the left has a number A_{i,j} written on it.
A_{i,j} are distinct integers between 1 and HW.
Find all the integer pairs (x,y) satisfying 1\leq x < y \leq HW such that the square with x written on it is adjacent to the one with y.
A square is said to be adjacent to another if these two squares share a side.
Constraints
- 1 \leq H,W
- 2 \leq H \times W \leq 2\times 10^5
- 1 \leq A_{i,j} \leq H \times W
- H and W are integers.
- A_{i,j} are distinct integers.
Input
The input is given from Standard Input in the following format:
H W
A_{1,1} \ldots A_{1,W}
\vdots
A_{H,1} \ldots A_{H,W}
Output
Let K be the number of integer pairs (x,y) satisfying 1\leq x < y \leq HW such that the square with x written on it is adjacent to the one with y.
Print K lines. The i-th line should contain x and y for the i-th lexicographically smallest such pair, separated by a space.
Sample Input 1
2 3 2 3 4 1 6 5
Sample Output 1
1 2 1 6 2 3 3 4 3 6 4 5 5 6
The square with 1 written on it is adjacent to the one with 2.
Sample Input 2
3 1 1 2 3
Sample Output 2
1 2 2 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
あなたは N 枚のカードを手札に持っており、i \, (1 \leq i \leq N) 枚目のカードには整数 A_i が書かれています。あなたは次の操作を好きな回数行うことができます。
- 書かれている整数が連続しているような 3 枚のカードを選び、それらを手札から捨てる。言い換えると、ある整数 x に対し、x, x+1, x+2 が書かれているカードを 1 枚ずつ選び、それらを手札から捨てる。
手札のカードを最小で何枚に減らすことができますか?
制約
- 3 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq 10^{18} \, (1 \leq i \leq N)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
7 2 4 2 1 6 3 5
出力例 1
1
次の 2 つの操作を行うことで、手札に残るカードは 1 枚になり、これが最小です。
- 1, 2, 3 が書かれたカードを選び、これらを捨てる。
- 4, 5, 6 が書かれたカードを選び、これらを捨てる。
入力例 2
10 1 2 2 4 6 7 9 9 10 12
出力例 2
10
入力例 3
3 999999998 999999999 1000000000
出力例 3
0
Problem Statement
You have a hand of N cards. The i-th card (1 \leq i \leq N) has an integer A_i written on it. You can perform the following operation any number of times.
- Choose three cards with consecutive integers written on them, and discard them from hand. In other words, for some integer x, choose a card with x, a card with x+1, and a card with x+2 written on them, and discard them from hand.
What is the minimum possible number of cards in your final hand?
Constraints
- 3 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq 10^{18} \, (1 \leq i \leq N)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
7 2 4 2 1 6 3 5
Sample Output 1
1
After the following two operations, your hand will have one card, which is the minimum possible.
- Choose cards with 1, 2, 3 written on them, and discard them.
- Choose cards with 4, 5, 6 written on them, and discard them.
Sample Input 2
10 1 2 2 4 6 7 9 9 10 12
Sample Output 2
10
Sample Input 3
3 999999998 999999999 1000000000
Sample Output 3
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
1, \dots, N と番号付けられた N 人のプレイヤーが机を囲んでカードゲームで対戦します。カードは 26 種類あり、それぞれ A、B、…、Z が書かれています。カードは机の上に 5 枚置かれ、各プレイヤーに 4 枚配られます。
机の上に置かれたカードは長さ 5 の文字列 X で表され、プレイヤー i \, (1 \leq i \leq N) に配られたカードは長さ 4 の文字列 S_i で表されます。X, S_1, \dots, S_N は全て英大文字からなり、それぞれの文字はカードがどの種類のものであるかを表しています。
各プレイヤーは、自分に配られたカードから 2 枚を、机の上のカードから 3 枚を選んで 5 枚のカードの組み合わせを作ります。この組み合わせをそのプレイヤーのハンドと呼びます。
各 i \, (1 \leq i \leq N) について、n_i および c_i を次のように定めます。
- プレイヤー i のハンドに存在する同じ種類のカードのうち、最も枚数が多いものが n 枚あり、書かれている文字が c であるとする。ただし、そのようなものが複数考えられる場合は、c がアルファベット順で最も小さくなるようにする。この (n, c) について、n_i = n, c_i = c と定める。
プレイヤー A, B \, (A \neq B) のどちらが優れているかは、次のようにして定まります。
- n_A > n_B ならば A の方が優れており、n_B > n_A ならば B の方が優れている。
- n_A = n_B の場合は次のように定まる。
- c_A \neq c_B のとき、c_A がアルファベット順で c_B より小さければ A の方が優れており、大きければ B の方が優れている。
- c_A = c_B のとき、A < B ならば A の方が優れており、B < A ならば B の方が優れている。
全てのプレイヤーが最適に行動するとき、最も優れたプレイヤーは誰になるかを求めてください。すなわち、以下の条件が成り立つ i を求めてください。
- 全ての j \neq i に対し、プレイヤー i がプレイヤー j より優れている。
制約
- 2 \leq N \leq 1000
- N は整数
- X は英大文字からなる長さ 5 の文字列
- S_i \, (1 \leq i \leq N) は英大文字からなる長さ 4 の文字列
入力
入力は以下の形式で標準入力から与えられる。
N X S_1 \vdots S_N
出力
答えを出力せよ。
入力例 1
3 AABCD DDBC ABBQ XYZA
出力例 1
2
ここでは、カードに書かれた英大文字を 5 つ並べた文字列としてハンドを表すことにします。
たとえば、プレイヤー 1 が手元から D、B が書かれたカードを選び、机の上から A、B、C が書かれたカードを選んだとします。このとき、ハンドは DBABC あるいは ABBCD のように表されます (どちらも同じハンドを表します) 。
以下に (最適とは限らない) プレイの一例を示します。
- プレイヤー 1 が
BCDDD、プレイヤー 2 がAABBQというハンドを作った場合、n_1 = 3、c_1 =D、n_2 = 2、c_2 =Aとなります。この場合、プレイヤー 1 の方が優れています。 - プレイヤー 1 が
BCDDD、プレイヤー 2 がAAABBというハンドを作った場合、n_1 = 3、c_1 =D、n_2 = 3、c_2 =Aとなります。この場合、プレイヤー 2 の方が優れています。 - プレイヤー 2 が
AAABB、プレイヤー 3 がAAABZというハンドを作った場合、n_2 = 3、c_2 =A、n_3 = 3、c_3 =Aとなります。この場合、プレイヤー 2 の方が優れています。
3 人のプレイヤーの最適な行動の一例は、プレイヤー 1 が ABDDD、プレイヤー 2 が AAABB、プレイヤー 3 が AAABZ というハンドを作ることです。このとき、プレイヤー 2 はプレイヤー 1, 3 の両方よりも優れているので、答えは 2 となります。
入力例 2
4 ABBDE ADDZ BEEZ DDDD BDDE
出力例 2
2
Problem Statement
N players numbered 1, \dots, N sitting around a table play a card game. There are 26 kinds of cards, each of which has A, B, …, or Z written on it. Five cards are placed on the table, and four are dealt to each player.
The cards on the table are represented by a string X of length five, and the cards dealt to player i \, (1 \leq i \leq N) are represented by a string S_i of length four. X, S_1, \dots, and S_N consist of uppercase English letters, representing the kinds of the cards.
Each player chooses two cards from those dealt to the player and three from those on the table to make a set of five cards. This set is called the player's hand.
For each i \, (1 \leq i \leq N), define n_i and c_i as follows:
- let n_i = n and c_i = c, where n is the maximum integer such that player i's hand contains n cards of the same kind, and c is the character written on them. If there are multiple candidates, choose the alphabetically smallest c.
The superiority between two players A and B (A \neq B) is determined as follows:
- A is superior if n_A > n_B, and B is superior if n_B > n_A.
- If n_A = n_B, it is determined as follows:
- If c_A \neq c_B, A is superior if c_A is alphabetically smaller than c_B, and B is superior if it is larger.
- If c_A = c_B, A is superior if A < B, and B is superior if B > A.
Find the supreme player if all players play optimally. In other words, find i such that:
- for all j \neq i, player i is superior to player j.
Constraints
- 2 \leq N \leq 1000
- N is an integer.
- X is a string of length 5 consisting of uppercase English letters.
- S_i \, (1 \leq i \leq N) is a string of length 4 consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
N X S_1 \vdots S_N
Output
Print the answer.
Sample Input 1
3 AABCD DDBC ABBQ XYZA
Sample Output 1
2
Here, a hand is represented by a string consisting of the five uppercase English letters written on the cards.
For example, suppose that player 1 chooses the cards with D and B from the dealt ones and those with A, B, and C from those on the table; then the hand is represented by, for instance, DBABC or ABBCD (both represent the same hand).
The following are examples of (not necessarily optimal) plays.
- If player 1 makes a hand
BCDDDand player 2 makesAABBQ, we have n_1 = 3, c_1 =D, n_2 = 2, and c_2 =A. In this case, player 1 is superior. - If player 1 makes a hand
BCDDDand player 2 makesAAABB, we have n_1 = 3, c_1 =D, n_2 = 3, and c_2 =A. In this case, player 2 is superior. - If player 2 makes a hand
AAABBand player 3 makesAAABZ, we have n_2 = 3, c_2 =A, n_3 = 3, and c_3 =A. In this case, player 2 is superior.
One possible optimal hand of each player is as follows: player 1 makes ABDDD, player 2 makes AAABB, and player 3 makes AAABZ. Since player 2 is superior to both players 1 and 3, the answer is 2.
Sample Input 2
4 ABBDE ADDZ BEEZ DDDD BDDE
Sample Output 2
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
# と . からなる H 行 W 列の図形 S,T が与えられます。
図形 S は H 個の文字列 S _ 1,S _ 2,\ldots,S _ H として与えられ、 S _ i の j 文字目は S の i 行 j 列にある要素を表します。 T についても同様です。
S の列をシフトして T と等しくできるか判定してください。
ただし、図形 X の列をシフトするとは、以下の操作を言います。
- 1 以上 W 以下の整数 s をひとつ選択する。
- その後、全ての 1 \leq i \leq H を満たす整数 i について、以下の操作を同時に行う。
- 1 \leq j \leq W を満たす全ての整数 j について同時に、 X の i 行 j 列にある要素を i 行 j+s 列にある要素に置き換える。
ただし、ここでは W \gt x となる x に対して x 列と書いたとき、x-y が W の倍数となるような唯一の整数 y\ (1\leq y\leq W) について y 列のことを指すとします。
制約
- H,W は整数
- 1 \le H,W
- 1 \le H \times W \le 4 \times 10^5
- S_i,T_i は
#と.からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
出力
S を T と等しくできるなら Yes 、 そうでないなら No と出力せよ。
入力例 1
3 4 ##.# ##.. .#.. .### ..## ...#
出力例 1
Yes
s=2 とすると、S を T と等しくできます。
入力例 2
3 4 ##.. #..# ..## ..## #..# ##..
出力例 2
No
列をシフトすることはできますが、図形を反転させることはできません。
入力例 3
2 1 # . # .
出力例 3
Yes
S=T である場合もあります。
入力例 4
8 7 .#..... ..#.... ....#.. #.....# .#..... ..#.... ....#.. #.....# .....#. ......# .#..... ...##.. .....#. ......# .#..... ...##..
出力例 4
Yes
Problem Statement
You are given figures S and T with H rows and W columns consisting of # and ..
The figure S is given as H strings S _ 1,S _ 2,\ldots,S _ H; the j-th character of S _ i represents the element at the i-th row and j-th column of S. The figure T is given similarly.
Determine whether it is possible to shift the columns of S so that S will equal T.
Here, shifting the columns of S is the following operation.
- Choose an integer s between 1 and W, inclusive.
- Then, for every integer i such that 1 \leq i \leq H, simultaneously perform the following:
- for every integer j such that 1 \leq j \leq W, simultaneously replace the element at the i-th row and j-th column with that of the i-th row and (j+s)-th column of X.
Here, for x such that W \gt x, the x-th column means the y-th column where y\ (1\leq y\leq W) is the only integer such that x-y is a multiple of W.
Constraints
- H and W are integers.
- 1 \le H,W
- 1 \le H \times W \le 4 \times 10^5
- S_i and T_i are strings of length W consisting of
#and..
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
Output
If it is possible to make S equal T, print Yes; otherwise, print No.
Sample Input 1
3 4 ##.# ##.. .#.. .### ..## ...#
Sample Output 1
Yes
Choosing s=2 will make S equal T.
Sample Input 2
3 4 ##.. #..# ..## ..## #..# ##..
Sample Output 2
No
You can only shift the columns and not turn over the figure.
Sample Input 3
2 1 # . # .
Sample Output 3
Yes
It may be the case that S=T.
Sample Input 4
8 7 .#..... ..#.... ....#.. #.....# .#..... ..#.... ....#.. #.....# .....#. ......# .#..... ...##.. .....#. ......# .#..... ...##..
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
高橋君と青木君が N ラウンドからなるゲームをしています。 i=1,\ldots,N に対し、i 番目のラウンドでは以下の手順を上から順に行います。
- i \geq 2 かつ i-1 番目のラウンドで高橋君にペナルティが与えられていた場合、以降の手順を行わずに i 番目のラウンドを終了する。
- 高橋君に a_i 枚の金貨が配られる。
- 高橋君が以下のうち一方の行動を選ぶ。
- 袋に \left \lfloor \frac{a_i \times t}{100} \right \rfloor 枚の金貨を入れて青木君に示す。 (\lfloor x \rfloor は x を超えない最大の整数) ここで、\left \lfloor \frac{a_i \times t}{100} \right \rfloor \geq 1 であることが保証される。
- 袋に何も入れずに青木君に示す。
- 青木君が 1 以上 100 以下の整数のうち 1 つを等確率で選び、x とする。
- x\leq p の場合、袋の中を確認する。もし袋に金貨が入っていなかった場合、高橋君に \left \lfloor \frac{a_i \times t}{100} \right \rfloor 枚の金貨を袋に入れさせた上で高橋君にペナルティを与える。
- x \gt p の場合、何もしない。
- このラウンドで配られた金貨のうち袋に入れられていない分を高橋君が獲得する。
なお、青木君は各ラウンドで独立に x を選びます。
高橋君は、 N ラウンド全体で獲得する金貨の枚数の総和の期待値 E が最大となるように各ラウンドでの行動を選びます。この時の E の値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq t,p \leq 99
- 1 \leq a_i \leq 10^9
- \left \lfloor \frac{a_i \times t}{100} \right \rfloor \geq 1
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N t p a_1 \ldots a_N
出力
答えを出力せよ。解との絶対誤差または相対誤差が 10^{−6} 以下であれば正解として扱われる。
入力例 1
3 10 50 100 10 300
出力例 1
384.50000000000000000000
高橋君は以下のようにすることで獲得する金貨の総和の期待値が最大となります。
- 1 番目のラウンドでは袋に何も入れない
- 2 番目のラウンドでは(1 番目のラウンドでペナルティが与えられなかった場合)袋に規定の枚数の金貨を入れる
- 3 番目のラウンドでは袋に何も入れない
入力例 2
3 1 10 192 191 190
出力例 2
570.89999999999997726263
この入力に対する答えは 570.9 です。出力はこの値に一致していませんが、誤差が十分に小さいため正解として扱われます。
入力例 3
4 17 55 32 10 77 1000000000
出力例 3
906500100.00000000000000000000
Problem Statement
Takahashi and Aoki are playing a game with N rounds. For i=1,\ldots,N, the i-th round consists of the following steps in the order from top to bottom.
- If i \geq 2 and Takahashi received a penalty in the (i-1)-th round, the i-th round ends without performing the steps below.
- Takahashi is dealt a_i gold coins.
- Takahashi does one of the following actions.
- Put \left \lfloor \frac{a_i \times t}{100} \right \rfloor gold coins into a bag and show it to Aoki. (\lfloor x \rfloor is the greatest integer not exceeding x.) Here, it is guaranteed that \left \lfloor \frac{a_i \times t}{100} \right \rfloor \geq 1.
- Show an empty bag to Aoki.
- Aoki chooses an integer x between 1 and 100, inclusive, with equal probability.
- If x\leq p, he examines the bag. If it contains no gold coins, Takahashi puts \left \lfloor \frac{a_i \times t}{100} \right \rfloor gold coins into the bag and receives a penalty.
- If x \gt p, he does nothing.
- Takashi earns all gold coins dealt in this round that are not in the bag now.
Here, Aoki chooses x independently in each round.
Takahashi will make his choice in each round to maximize the expected value E of the total number of gold coins earned in all N rounds. Find this maximized value of E.
Constraints
- 1 \leq N \leq 100
- 1 \leq t,p \leq 99
- 1 \leq a_i \leq 10^9
- \left \lfloor \frac{a_i \times t}{100} \right \rfloor \geq 1
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N t p a_1 \ldots a_N
Output
Print the answer. Your output is considered correct if its absolute or relative error from the true answer is at most 10^{−6}.
Sample Input 1
3 10 50 100 10 300
Sample Output 1
384.50000000000000000000
Here is a way for Takahashi to maximize the expected value of the total number of gold coins earned.
- In the first round, put nothing into the bag.
- In the second round, put the specified number of gold coins into the bag (if he did not receive a penalty in the first round).
- In the third round, put nothing into the bag.
Sample Input 2
3 1 10 192 191 190
Sample Output 2
570.89999999999997726263
The true answer to this input is 570.9. The above output is not identical to this value, but the error is small enough to be considered correct.
Sample Input 3
4 17 55 32 10 77 1000000000
Sample Output 3
906500100.00000000000000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
1,2,\dots,N の番号が付いた N 人の人がいます。
この N 人は全員同じゲームをしています。このゲームは 2 人で対戦するゲームです。N 人の人の実力は全て異なり、ゲームを行ったとき、実力が高い人が必ず勝ちます。
M 回ゲームを行いました。i 回目のゲームは人 A_i と B_i で行い、人 A_i が勝ちました。
N 人をゲームの実力が強い順に並べた列としてあり得る列のうち、辞書順最小のものを出力してください。
制約
- 2 \le N \le 2 \times 10^5
- 0 \le M \le 2 \times 10^5
- 1 \le A_i,B_i \le N
- A_i \neq B_i
- 入力は全て整数である。
- M 個の条件は矛盾しない。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
N 人をゲームの実力が強い順に並べた列としてあり得る列のうち、辞書順最小のものを空白区切りで出力せよ。
入力例 1
3 1 3 1
出力例 1
2 3 1
N 人のゲームの実力が強い順に並べた列として条件を満たす列は (2,3,1),(3,1,2),(3,2,1) です。このうち辞書順最小である (2,3,1) が答えとなります。
入力例 2
7 0
出力例 2
1 2 3 4 5 6 7
Problem Statement
There are N players numbered 1,2,\dots, and N.
These N players are playing the same game. In this game, two players play a match against each other. The N players have distinct strengths; the player with the greater strength always wins a match.
There were M matches. The i-th match was between players A_i and B_i, and player A_i won.
Find the lexicographically smallest possible sequence of the N players sorted by their strengths in descending order.
Constraints
- 2 \le N \le 2 \times 10^5
- 0 \le M \le 2 \times 10^5
- 1 \le A_i,B_i \le N
- A_i \neq B_i
- All values in the input are integers.
- The M conditions are consistent.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Find the lexicographically smallest possible sequence of the N players sorted by their strengths in descending order, separated by spaces.
Sample Input 1
3 1 3 1
Sample Output 1
2 3 1
(2,3,1), (3,1,2), and (3,2,1) are the possible sequences of the N players sorted by their strengths in descending order that satisfy the conditions. The answer is (2,3,1), which is the lexicographically smallest one among them.
Sample Input 2
7 0
Sample Output 2
1 2 3 4 5 6 7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
1 から N の番号がついた N 個の荷物があります。荷物 i の大きさは A_i です。
1 から M の番号がついた M 個の箱が番号順に一列に並んでいます。最も左に箱 1 があり、最も右に箱 M があります。
箱 i の大きさは B_i です。
それぞれの箱には、荷物の大きさの合計が箱の大きさ以下である限り、何個でも荷物を入れることができます。
高橋君は番号の小さな荷物から順に、以下のルールで荷物を箱に入れます。
- まだ荷物を入れることができる箱のうち、最も左にある箱に荷物を入れる
全ての荷物を箱に入れることが可能かどうか判定してください。
さらに、可能な場合は、全ての荷物を箱に入れた状態で各箱に入っている荷物の大きさの合計を求め、不可能な場合は、箱に入れることができない荷物の番号の最小値を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
全ての荷物を箱に入れることが可能なとき:
1 行目に Yes と出力せよ。
2 行目には、全ての荷物を箱に入れた状態で箱 i に入っている荷物の大きさの合計を C_i としたとき、C_1,C_2,\ldots,C_M をこの順に空白区切りで出力せよ。
全ての荷物を箱に入れることが不可能なとき:
1 行目に No と出力せよ。
2 行目には、箱に入れることができない荷物の番号の最小値を出力せよ。
入力例 1
3 3 4 7 3 10 4 8
出力例 1
Yes 7 0 7
高橋君は次のように行動します。
- 荷物 1 は箱 1,2,3 に入れることができるので、最も左にある箱 1 に入れる。
- 荷物 2 は箱 3 に入れることができるので、箱 3 に入れる。
- 荷物 3 は箱 1,2 に入れることができるので、最も左にある箱 1 に入れる。
荷物 2 を箱に入れようとする時点では、荷物 1 が箱 1 に入っているため、荷物 2 を箱 1 に入れることはできません。
全ての荷物を箱に入れた状態で各箱に入っている荷物の大きさの合計は
- 箱 1 には荷物 1,3 が入っており 3+4=7
- 箱 2 には荷物が入っておらず 0
- 箱 3 には荷物 2 が入っており 7
となります。
入力例 2
3 2 4 7 3 10 4
出力例 2
No 2
高橋君は次のように行動します。
- 荷物 1 は箱 1,2 に入れることができるので、最も左にある箱 1 に入れる。
- 荷物 2 を入れることができる箱は存在しない。
入力例 3
5 5 5 4 3 2 1 1 2 3 4 5
出力例 3
Yes 1 2 3 4 5
Problem Statement
We have N parcels numbered 1 to N. Parcel i has a size of A_i.
There are M boxes numbered 1 to M arranged in a row in numerical order: box 1 is the leftmost, and box M is the rightmost.
Box i has a size of B_i.
Each box can contain any number of parcels as long as the total size of the parcels is not greater than the size of the box.
For each parcel in the order from parcel 1 to parcel N, Takahashi will try to do the following:
- put the parcel into the leftmost box that can contain it.
Determine whether Takahashi can put all parcels into boxes.
Additionally, if he can, find the total size of parcels in each box after putting all parcels into boxes; otherwise, find the smallest number of a parcel that he cannot put into a box.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
Output
If Takahashi can put all parcels into boxes:
The first line should contain Yes.
The second line should contain C_1,C_2,\ldots,C_M with spaces in between, where C_i is the total size of the parcels in box i after putting all parcels into boxes.
If Takahashi cannot put all parcels into boxes:
The first line should contain No.
The second line should contain the smallest number of a parcel that he cannot put into a box.
Sample Input 1
3 3 4 7 3 10 4 8
Sample Output 1
Yes 7 0 7
Takahashi will do the following.
- Parcel 1 can be put into box 1, 2, or 3. He puts it into the leftmost of them, box 1.
- Parcel 2 can be put into box 3. He puts it into this box.
- Parcel 3 can be put into box 1 or 2. He puts it into the leftmost of them, box 1.
Note that when he tries to put parcel 2, parcel 1 is already in box 1, so parcel 2 cannot be put into this box.
When all parcels are in boxes,
- box 1 contains parcels 1 and 3, for a total size of 3+4=7,
- box 2 contains no parcels, for a total size of 0, and
- box 3 contains parcel 2, for a total size of 7.
Sample Input 2
3 2 4 7 3 10 4
Sample Output 2
No 2
Takahashi will do the following.
- Parcel 1 can be put into box 1 or 2. He puts it into the leftmost of them, box 1.
- Parcel 2 cannot be put into any box.
Sample Input 3
5 5 5 4 3 2 1 1 2 3 4 5
Sample Output 3
Yes 1 2 3 4 5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
問題文
次のような問題を考えます。
高橋君はある日 (これを 1 日目とします) の朝の時点で X kg のゴミを持っています。ゴミは毎日夜に 1 kg 増えます。
ゴミを捨てることのできる機会は最大 N 回あります。i = 1, 2, \dots, N について、d_i 日目の朝の時点でゴミが a_i kg 以上あるならば、「1 円を払って a_i kg のゴミを捨てる」ことを 0 回または 1 回行うことができます。
高橋君は D 日目の朝の時点でゴミが C kg 以下になるようにしたいです。そのために払う金額としてありうる最小値を求めてください。
N, C, D, d_i, a_i は入力で与えられます。いま、X を非負整数の範囲で動かすことを考えます。
上記の問題において高橋君が D 日目の朝の時点でゴミを C kg 以下にすることができるような X に対する、問題の答えの最小値を求めてください。 ただし、どのような X に対しても D 日目の朝の時点でゴミを C kg 以下にすることができないならば、-1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C \leq 10^9
- 1 \leq d_1 \lt d_2 \lt \dots \lt d_N \lt D \leq 10^9
- 1 \leq a_i \leq 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N C D d_1 a_1 d_2 a_2 \vdots d_N a_N
出力
答えを出力せよ。
入力例 1
2 1 4 1 3 3 4
出力例 1
1
例えば、X = 2 のときの最適な行動の一例は次のようになります。
- 1 日目の朝には X = 2 kg のゴミがある。d_1 = 1 だが、ゴミの重さが a_1 = 3 kg 未満なので、この日にゴミを捨てることはできない。
- 1 日目の夜にゴミが 3 kg になる。
- 2 日目の夜にゴミが 4 kg になる。
- 3 日目の朝に 1 円を払いゴミを a_2 = 4 kg 捨てる。d_2 = 3 かつゴミの重さが a_2 = 4 kg 以上なので、この動作は可能である。この動作により、ゴミは 0 kg になる。
- 3 日目の夜にゴミが 1 kg になる。
- D = 4 日目の朝の時点でゴミは 1 kg であり、これは C kg 以下であるので条件を満たす。払った金額の合計は 1 円である。
入力例 2
3 10 100 10 20 20 20 30 20
出力例 2
-1
どのような X に対しても D 日目の朝の時点でゴミを C kg 以下にすることはできません。
入力例 3
4 4 10 2 3 4 5 6 1 8 4
出力例 3
2
Problem Statement
Consider the following problem.
Takahashi has X kilograms of garbage in the morning of some day (let us call it day 1). Each night, the amount of garbage increases by 1 kilogram.
There will be N days when he can take out the garbage. For i = 1, 2, \dots, N, if he has a_i or more kilograms of garbage in the morning of day d_i, he can take out a_i kilograms of garbage at the cost of 1 yen zero times or once.
Takahashi wants to have at most C kilograms of garbage in the morning of day D. Find the minimum amount of money needed to achieve this.
You are given N, C, D, d_i, a_i as the input. Now, consider X as a non-negative integer variable.
Find the minimum answer to the above problem for an X such that Takahashi can have at most C kilograms of garbage in the morning of day D. If there is no X such that he can have at most C kilograms of garbage in the morning of day D, print -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C \leq 10^9
- 1 \leq d_1 \lt d_2 \lt \dots \lt d_N \lt D \leq 10^9
- 1 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N C D d_1 a_1 d_2 a_2 \vdots d_N a_N
Output
Print the answer.
Sample Input 1
2 1 4 1 3 3 4
Sample Output 1
1
For instance, below is an optimal sequence of actions for X = 2.
- In the morning of day 1, he has X = 2 kilograms of garbage. We have d_1 = 1, but the amount of garbage is less than a_1 = 3 kilograms, so he cannot take out the garbage this day.
- After the night of day 1, he has 3 kilograms of garbage.
- After the night of day 2, he has 4 kilograms of garbage.
- In the morning of day 3, he takes out a_2 = 4 kilograms of garbage at the cost of 1 yen, wihch is allowed because d_2 = 3 and he has not less than a_2 = 4 kilograms of garbage. Then, he has 0 kilograms of garbage.
- After the night of day 3, he has 1 kilogram of garbage.
- In the morning of day 4, he has 1 kilogram of garbage, which is not more than C kilograms and thus satisfies the objective. He paid 1 yen in total.
Sample Input 2
3 10 100 10 20 20 20 30 20
Sample Output 2
-1
There is no X such that he can have at most C kilograms of garbage in the morning of day D.
Sample Input 3
4 4 10 2 3 4 5 6 1 8 4
Sample Output 3
2
実行時間制限: 6 sec / メモリ制限: 1024 MiB
問題文
0 以上 10 以下の整数からなる長さ N の数列 A = (A_1, A_2, \dots, A_N) があります。
以下に説明するクエリを与えられる順に Q 個処理してください。
各クエリでは 3 つの整数 C, L, R が与えられます。C の値に応じて次の処理を行ってください。
- C = 1 のとき : A_L, A_{L+1}, \dots, A_R を値が昇順に並ぶようにソートする。
- C = 2 のとき : A_L, A_{L+1}, \dots, A_R を値が降順に並ぶようにソートする。
- C = 3 のとき : \displaystyle \sum_{i=L}^R A_i を出力する。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq Q \leq 2 \times 10^4
- 0 \leq A_i \leq 10
- 1 \leq C \leq 3
- 1 \leq L \leq R \leq N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。ここで \text{Query}_i は i 番目のクエリを意味する。
N Q
A_1 A_2 \dots A_N
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q
クエリは次の形式で与えられる。
C L R
出力
C = 3 であるクエリの個数を T として、T 行出力せよ。
i 行目 (1 \leq i \leq T) には i 番目の C = 3 であるクエリに対する答えを出力せよ。
入力例 1
5 5 1 0 8 2 10 3 2 4 1 1 4 3 2 4 2 3 5 3 2 4
出力例 1
10 11 19
はじめ、A = (1, 0, 8, 2, 10) です。
1 番目のクエリの答えは 0 + 8 + 2 = 10 です。
2 番目のクエリを処理した後の A は (0, 1, 2, 8, 10) です。
3 番目のクエリの答えは 1 + 2 + 8 = 11 です。
4 番目のクエリを処理した後の A は (0, 1, 10, 8, 2) です。
5 番目のクエリの答えは 1 + 10 + 8 = 19 です。
Problem Statement
There is a sequence of N integers between 0 and 10, inclusive: A = (A_1, A_2, \dots, A_N).
Process Q queries described below in the order they are given.
Each query has three integers C, L, and R. Do the following according to the value of C.
- If C = 1: sort A_L, A_{L+1}, \dots, A_R in ascending order.
- If C = 2: sort A_L, A_{L+1}, \dots, A_R in descending order.
- If C = 3: print \displaystyle \sum_{i=L}^R A_i.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq Q \leq 2 \times 10^4
- 0 \leq A_i \leq 10
- 1 \leq C \leq 3
- 1 \leq L \leq R \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \text{Query}_i denotes the i-th query:
N Q
A_1 A_2 \dots A_N
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q
Each query is in the following format:
C L R
Output
Print T lines, where T is the number of queries with C = 3.
The i-th line (1 \leq i \leq T) should contain the answer to the i-th query with C = 3.
Sample Input 1
5 5 1 0 8 2 10 3 2 4 1 1 4 3 2 4 2 3 5 3 2 4
Sample Output 1
10 11 19
Initially, A = (1, 0, 8, 2, 10).
For the first query, the answer is 0 + 8 + 2 = 10.
After the second query, A is (0, 1, 2, 8, 10).
For the third query, the answer is 1 + 2 + 8 = 11.
After the fourth query, A is (0, 1, 10, 8, 2).
For the fifth query, the answer is 1 + 10 + 8 = 19.