実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は全 10^9 巻の漫画『すぬけ君』を読むことにしました。
初め、高橋君は『すぬけ君』の単行本を N 冊持っています。i 番目の単行本は a_i 巻です。
高橋君は『すぬけ君』を読み始める前に限り次の操作を 0 回以上何度でも繰り返せます。
- 現在持っている単行本が 1 冊以下の場合、何もしない。そうでない場合、現在持っている単行本から 2 冊を選んで売り、代わりに好きな巻を選んで 1 冊買う
その後、高橋君は『すぬけ君』を 1 巻、2 巻、3 巻、\ldots と順番に読みます。ただし、次に読むべき巻を持っていない状態になった場合、(他の巻を持っているかどうかに関わらず)その時点で『すぬけ君』を読むのをやめます。
高橋君が『すぬけ君』を最大で何巻まで読めるかを求めてください。ただし、1 巻を読めない場合の答えは 0 とします。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
6 1 2 4 6 7 271
出力例 1
4
『すぬけ君』を読み始める前に「7 巻と 271 巻を選んで売り、代わりに 3 巻を選んで 1 冊買う」という内容で操作をすると、高橋君は 1 巻、2 巻、3 巻、4 巻、6 巻を 1 冊ずつ持っている状態になります。
この状態から『すぬけ君』を読み始めると、高橋君は 1 巻、2 巻、3 巻、4 巻を読み、続いて 5 巻を読もうとしますが持っていないためこの時点で『すぬけ君』を読むのをやめます。
操作の方法を変えても 5 巻を読むことは出来ないため、4 が答えです。
入力例 2
10 1 1 1 1 1 1 1 1 1 1
出力例 2
5
高橋君は同じ巻を 2 冊以上持っているかもしれません。
この入力に対しては以下のように 4 回操作をしてから『すぬけ君』を読み始めることで 5 巻まで読むことが出来、これが最大です。
- 1 巻を 2 冊選んで売り、代わりに 2 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 3 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 4 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 5 巻を選んで 1 冊買う
入力例 3
1 5
出力例 3
0
高橋君は 1 巻を読めません。
Score : 300 points
Problem Statement
Takahashi is going to read a manga series "Snuke-kun" in 10^9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume a_i.
Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:
- Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.
Then, Takahashi reads Volume 1, Volume 2, Volume 3, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).
Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 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 a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
6 1 2 4 6 7 271
Sample Output 1
4
Before he begins to read the series, he may do the following operation: "sell books of Volumes 7 and 271 and buy one book of Volume 3 instead." Then, he has one book each of Volumes 1, 2, 3, 4, and 6.
If he then begins to read the series, he reads Volumes 1, 2, 3, and 4, then tries to read Volume 5. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 5, so the answer is 4.
Sample Input 2
10 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Takahashi may have multiple books of the same volume.
For this input, if he performs the following 4 operations before he begins to read the series, he can read up to Volume 5, which is the maximum:
- Sell two books of Volume 1 and buy a book of Volume 2 instead.
- Sell two books of Volume 1 and buy a book of Volume 3 instead.
- Sell two books of Volume 1 and buy a book of Volume 4 instead.
- Sell two books of Volume 1 and buy a book of Volume 5 instead.
Sample Input 3
1 5
Sample Output 3
0
Takahashi cannot read Volume 1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。
i 番目のクエリは以下の通りです。
- 整数 X_i と文字 C_i が与えられるので、 S の X_i 番目の文字を C_i に置き換える。その後、 S に文字列
ABCが部分文字列として何箇所含まれるかを出力する。
ここで、 S の 部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ab は abc の部分文字列ですが、ac は abc の部分文字列ではありません。
制約
- 3\le N\le 2\times 10^5
- 1\le Q\le 2\times 10^5
- S は英大文字からなる長さ N の文字列
- 1\le X_i\le N
- C_i は英大文字
入力
入力は以下の形式で標準入力から与えられる。
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
出力
Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
出力例 1
2 1 1 0
各クエリを処理した後の S は次のようになります。
- 1 つ目のクエリを処理後: S=
ABCBABCとなる。この中にABCは部分文字列として 2 回登場する。 - 2 つ目のクエリを処理後: S=
ABABABCとなる。この中にABCは部分文字列として 1 回登場する。 - 3 つ目のクエリを処理後: S=
ABABCBCとなる。この中にABCは部分文字列として 1 回登場する。 - 4 つ目のクエリを処理後: S=
ABAGCBCとなる。この中にABCは部分文字列として 0 回登場する。
入力例 2
3 3 ABC 1 A 2 B 3 C
出力例 2
1 1 1
クエリの処理前と処理後で S が変わらない場合もあります。
入力例 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
出力例 3
0 0 0 0 1 1 2 2 1 1
Score : 350 points
Problem Statement
You are given a string S of length N. You are also given Q queries, which you should process in order.
The i-th query is as follows:
- Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string
ABCappears as a substring in S.
Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.
Constraints
- 3 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- S is a string of length N consisting of uppercase English letters.
- 1 \le X_i \le N
- C_i is an uppercase English letter.
Input
The input is given from Standard Input in the following format:
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
Output
Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.
Sample Input 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
Sample Output 1
2 1 1 0
After processing each query, S becomes as follows.
- After the first query: S=
ABCBABC. In this string,ABCappears twice as a substring. - After the second query: S=
ABABABC. In this string,ABCappears once as a substring. - After the third query: S=
ABABCBC. In this string,ABCappears once as a substring. - After the fourth query: S=
ABAGCBC. In this string,ABCappears zero times as a substring.
Sample Input 2
3 3 ABC 1 A 2 B 3 C
Sample Output 2
1 1 1
There are cases where S does not change through processing a query.
Sample Input 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
Sample Output 3
0 0 0 0 1 1 2 2 1 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
英大文字および英小文字からなる長さ N の文字列 S が与えられます。
これから、文字列 S に対して Q 回の操作を行います。 i 番目 (1\leq i\leq Q) の操作は整数 2 つと文字 1 つからなる組 (t _ i,x _ i,c _ i) で表され、それぞれ次のような操作を表します。
- t _ i=1 のとき、S の x _ i 文字目を c _ i に変更する。
- t _ i=2 のとき、S に含まれる大文字すべてをそれぞれ小文字に変更する(x _ i,c _ i は操作に使用しない)。
- t _ i=3 のとき、S に含まれる小文字すべてをそれぞれ大文字に変更する(x _ i,c _ i は操作に使用しない)。
Q 回の操作がすべて終わったあとの S を出力してください。
制約
- 1\leq N\leq5\times10^5
- S は英大文字および英小文字からなる長さ N の文字列
- 1\leq Q\leq5\times10^5
- 1\leq t _ i\leq3\ (1\leq i\leq Q)
- t _ i=1 ならば 1\leq x _ i\leq N\ (1\leq i\leq Q)
- c _ i は英大文字もしくは英小文字
- t _ i\neq 1 ならば x _ i=0 かつ c _ i=
'a' - N,Q,t _ i,x _ i はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S Q t _ 1 x _ 1 c _ 1 t _ 2 x _ 2 c _ 2 \vdots t _ Q x _ Q c _ Q
出力
答えを 1 行で出力せよ。
入力例 1
7 AtCoder 5 1 4 i 3 0 a 1 5 b 2 0 a 1 4 Y
出力例 1
atcYber
はじめ、文字列 S は AtCoder です。
- 1 番目の操作では、4 文字目を
iに変更します。変更後の S はAtCiderです。 - 2 番目の操作では、すべての小文字を大文字に変更します。変更後の S は
ATCIDERです。 - 3 番目の操作では、5 文字目を
bに変更します。変更後の S はATCIbERです。 - 4 番目の操作では、すべての大文字を小文字に変更します。変更後の S は
atciberです。 - 5 番目の操作では、4 文字目を
Yに変更します。変更後の S はatcYberです。
すべての操作が終わったあとの S は atcYber なので、atcYber と出力してください。
入力例 2
35 TheQuickBrownFoxJumpsOverTheLazyDog 10 2 0 a 1 19 G 1 13 m 1 2 E 1 21 F 2 0 a 1 27 b 3 0 a 3 0 a 1 15 i
出力例 2
TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
Score : 400 points
Problem Statement
You are given a string S of length N consisting of uppercase and lowercase English letters.
Let us perform Q operations on the string S. The i-th operation (1\leq i\leq Q) is represented by a tuple (t _ i,x _ i,c _ i) of two integers and one character, as follows.
- If t _ i=1, change the x _ i-th character of S to c _ i.
- If t _ i=2, convert all uppercase letters in S to lowercase (do not use x _ i,c _ i for this operation).
- If t _ i=3, convert all lowercase letters in S to uppercase (do not use x _ i,c _ i for this operation).
Print the S after the Q operations.
Constraints
- 1\leq N\leq5\times10^5
- S is a string of length N consisting of uppercase and lowercase English letters.
- 1\leq Q\leq5\times10^5
- 1\leq t _ i\leq3\ (1\leq i\leq Q)
- If t _ i=1, then 1\leq x _ i\leq N\ (1\leq i\leq Q).
- c _ i is an uppercase or lowercase English letter.
- If t _ i\neq 1, then x _ i=0 and c _ i=
'a'. - N,Q,t _ i,x _ i are all integers.
Input
The input is given from Standard Input in the following format:
N S Q t _ 1 x _ 1 c _ 1 t _ 2 x _ 2 c _ 2 \vdots t _ Q x _ Q c _ Q
Output
Print the answer in a single line.
Sample Input 1
7 AtCoder 5 1 4 i 3 0 a 1 5 b 2 0 a 1 4 Y
Sample Output 1
atcYber
Initially, the string S is AtCoder.
- The first operation changes the 4-th character to
i, changing S toAtCider. - The second operation converts all lowercase letters to uppercase, changing S to
ATCIDER. - The third operation changes the 5-th character to
b, changing S toATCIbER. - The fourth operation converts all uppercase letters to lowercase, changing S to
atciber. - The fifth operation changes the 4-th character to
Y, changing S toatcYber.
After the operations, the string S is atcYber, so print atcYber.
Sample Input 2
35 TheQuickBrownFoxJumpsOverTheLazyDog 10 2 0 a 1 19 G 1 13 m 1 2 E 1 21 F 2 0 a 1 27 b 3 0 a 3 0 a 1 15 i
Sample Output 2
TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
お店に N 個の商品が並んでおり、それらは商品 1 、商品 2 、\ldots 、商品 N と番号づけられています。
i = 1, 2, \ldots, N について、商品 i の定価は A_i 円です。また、各商品の在庫は 1 つです。
高橋君は、商品 X_1 、商品 X_2 、\ldots 、商品 X_M の M 個の商品が欲しいです。
高橋君は、欲しい商品をすべて手に入れるまで、下記の行動を繰り返します。
現在売れ残っている商品の個数を r とする。 1 \leq j \leq r を満たす整数 j を選び、現在売れ残っている商品のうち番号が j 番目に小さい商品を、その定価に C_j 円だけ加えた金額で購入する。
高橋君が欲しい商品をすべて手に入れるまでにかかる合計費用としてあり得る最小値を出力してください。
なお、高橋君は欲しい商品ではない商品を購入することもできます。
制約
- 1 \leq M \leq N \leq 5000
- 1 \leq A_i \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq X_1 \lt X_2 \lt \cdots \lt X_M \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_M
出力
答えを出力せよ。
入力例 1
5 2 3 1 4 1 5 9 2 6 5 3 3 5
出力例 1
17
高橋君は下記の手順で行動することで、欲しい商品をすべて手に入れるまでにかかる合計費用を最小にすることができます。
- はじめ、商品 1, 2, 3, 4, 5 の 5 個の商品が売れ残っています。 高橋君は j = 5 を選び、売れ残っている商品のうち番号が 5 番目に小さい商品 5 を、A_5 + C_5 = 5 + 3 = 8 円で購入します。
- その後、商品 1, 2, 3, 4 の 4 個の商品が売れ残っています。 高橋君は j = 2 を選び、売れ残っている商品のうち番号が 2 番目に小さい商品 2 を、A_2 + C_2 = 1 + 2 = 3 円で購入します。
- その後、商品 1, 3, 4 の 3 個の商品が売れ残っています。 高橋君は j = 2 を選び、売れ残っている商品のうち番号が 2 番目に小さい商品 3 を、A_3 + C_2 = 4 + 2 = 6 円で購入します。
以上の手順によって、高橋君は欲しい商品である商品 3, 5 のすべて(および、欲しい商品ではない商品 2 )を手に入れることができ、 それまでにかかる合計費用は 8 + 3 + 6 = 17 円です。これが合計費用としてあり得る最小値です。
入力例 2
20 8 29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12 1 3 4 5 8 14 16 20
出力例 2
533
Score : 500 points
Problem Statement
There are N items in a shop, numbered as Item 1, Item 2, \ldots, Item N.
For each i = 1, 2, \ldots, N, the regular price of Item i is A_i yen. For each item, there is only one in stock.
Takahashi wants M items: Item X_1, Item X_2, \ldots, Item X_M.
He repeats the following until he gets all items he wants.
Let r be the number of unsold items now. Choose an integer j such that 1 \leq j \leq r, and buy the item with the j-th smallest item number among the unsold items, for its regular price plus C_j yen.
Print the smallest total amount of money needed to get all items Takahashi wants.
Takahashi may also buy items other than the ones he wants.
Constraints
- 1 \leq M \leq N \leq 5000
- 1 \leq A_i \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq X_1 \lt X_2 \lt \cdots \lt X_M \leq N
- 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 C_1 C_2 \ldots C_N X_1 X_2 \ldots X_M
Output
Print the answer.
Sample Input 1
5 2 3 1 4 1 5 9 2 6 5 3 3 5
Sample Output 1
17
Here is a way for Takahashi to get all items he wants with the smallest total amount of money.
- Initially, five items, Items 1, 2, 3, 4, 5, are remaining. Choose j = 5 to buy the item with the fifth smallest item number among the remaining, Item 5, for A_5 + C_5 = 5 + 3 = 8 yen.
- Then, four items, Items 1, 2, 3, 4, are remaining. Choose j = 2 to buy the item with the second smallest item number among the remaining, Item 2, for A_2 + C_2 = 1 + 2 = 3 yen.
- Then, three items, Items 1, 3, 4, are remaining. Choose j = 2 to buy the item with the second smallest item number among the remaining, Item 3, for A_3 + C_2 = 4 + 2 = 6 yen.
Now, Takahashi has all items he wants, Items 3 and 5, (along with Item 2, which is not wanted) for a total cost of 8 + 3 + 6 = 17 yen, the minimum possible.
Sample Input 2
20 8 29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12 1 3 4 5 8 14 16 20
Sample Output 2
533
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の正整数列 X=(X_1,X_2\ldots,X_N) に対して、f(X) を以下のように定めます。
- N 頂点の木であって、i\ (1 \leq i \leq N) 番目の頂点の次数が X_i であるようなものを良い木と呼ぶ。 良い木が存在するならば、f(X) は良い木の直径の最大値。良い木が存在しないならば、f(X)=0。
ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。
長さ N の正整数列 X としてあり得るもの全てに対する f(X) の総和を 998244353 で割った余りを求めてください。 なお、f(X) の総和は有限値になることが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 2\times 10^5
- 2 \leq N \leq 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_i は i 番目のテストケースを意味する。
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
各テストケースは以下の形式で与えられる。
N
出力
T 行出力せよ。
i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。
入力例 1
10 2 3 5 8 13 21 34 55 89 144
出力例 1
1 6 110 8052 9758476 421903645 377386885 881422708 120024839 351256142
N=3 の場合について、
例えば、
- X=(1,1,1) のとき、次数が 1,1,1 となる 3 頂点の木は存在しないため、f(X)=0 です。
- X=(2,1,1) のとき、良い木は以下の図のものに限られます。この木の直径は 2 であるため、f(X)=2 です。
X=(2,1,1),(1,2,1),(1,1,2) のとき f(X)=2 であり、それ以外の X のとき f(X)=0 であるため、答えは 6 です。
Score : 500 points
Problem Statement
For a sequence X=(X_1,X_2\ldots,X_N) of length N consisting of positive integers, we define f(X) as follows:
- A tree with N vertices is said to be good if and only if the degree of the i-th (1 \leq i \leq N) vertex is X_i. If a good tree exists, f(X) is the maximum diameter of a good tree; if it doesn't, f(X)=0.
Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of a tree is the maximum distance between two vertices.
Find the sum, modulo 998244353, of f(X) over all possible sequences X of length N consisting of positive integers. We can prove that the sum of f(X) is a finite value.
Given T test cases, find the answer for each of them.
Constraints
- 1\leq T \leq 2\times 10^5
- 2 \leq N \leq 10^6
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
Each test case is given in the following format:
N
Output
Print T lines.
The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.
Sample Input 1
10 2 3 5 8 13 21 34 55 89 144
Sample Output 1
1 6 110 8052 9758476 421903645 377386885 881422708 120024839 351256142
If N=3,
for example,
- when X=(1,1,1), there is no tree with three vertices whose degrees are 1,1, and 1, so f(X)=0.
- When X=(2,1,1), the only possible tree is illustrated below. The diameter of this tree is 2, so f(X)=2.
For X=(2,1,1),(1,2,1),(1,1,2), we have f(X)=2; for other X, we have f(X)=0. Thus, the answer is 6.