Time Limit: 2 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は N 日間の鉄道旅行を計画しています。
高橋君はそれぞれの日について、運賃の通常料金を払うか、1 日周遊パスを 1 枚使用するか選ぶことができます。
ここで、1\leq i\leq N について、i 日目の旅行にかかる運賃の通常料金は F_i 円です。
一方、1 日周遊パスは D 枚セットで P 円で発売されており、何セットでも購入することが可能ですが、D 枚単位でしか購入することができません。
また、購入したパスは 1 枚ずつ好きな日に使うことができ、旅行が終了した時点で余っていても構いません。
N 日間の旅行でかかる金額、すなわち 1 日周遊パスの購入にかかった代金と、1 日周遊パスを利用しなかった日における運賃の通常料金の合計金額の和としてあり得る最小値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq D\leq 2\times 10^5
- 1\leq P\leq 10^9
- 1\leq F_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D P F_1 F_2 \ldots F_N
出力
N 日間の旅行でかかる金額としてあり得る最小値を出力せよ。
入力例 1
5 2 10 7 1 6 3 6
出力例 1
20
1 日周遊パスを 1 セットだけ購入し、1 日目と 3 日目に使用すると、合計金額は (10\times 1)+(0+1+0+3+6)=20 となり、このときかかる金額が最小となります。
よって、20 を出力します。
入力例 2
3 1 10 1 2 3
出力例 2
6
3 日間すべてにおいて運賃の通常料金を支払ったときに最小となります。
入力例 3
8 3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 3
3000000000
1 日周遊パスを 3 セット購入し、8 日間すべてにおいて 1 日周遊パスを利用したときに最小となります。
答えが 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
Takahashi is planning an N-day train trip.
For each day, he can pay the regular fare or use a one-day pass.
Here, for 1\leq i\leq N, the regular fare for the i-th day of the trip is F_i yen.
On the other hand, a batch of D one-day passes is sold for P yen. You can buy as many passes as you want, but only in units of D.
Each purchased pass can be used on any day, and it is fine to have some leftovers at the end of the trip.
Find the minimum possible total cost for the N-day trip, that is, the cost of purchasing one-day passes plus the total regular fare for the days not covered by one-day passes.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq D\leq 2\times 10^5
- 1\leq P\leq 10^9
- 1\leq F_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D P F_1 F_2 \ldots F_N
Output
Print the minimum possible total cost for the N-day trip.
Sample Input 1
5 2 10 7 1 6 3 6
Sample Output 1
20
If he buys just one batch of one-day passes and uses them for the first and third days, the total cost will be (10\times 1)+(0+1+0+3+6)=20, which is the minimum cost needed.
Thus, print 20.
Sample Input 2
3 1 10 1 2 3
Sample Output 2
6
The minimum cost is achieved by paying the regular fare for all three days.
Sample Input 3
8 3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
3000000000
The minimum cost is achieved by buying three batches of one-day passes and using them for all eight days.
Note that the answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
英大文字からなる文字列 S が与えられます。
整数の組 (i, j, k) であって、以下の条件をともに満たすものの個数を求めてください。
- 1 \leq i < j < k \leq |S|
- S_i, S_j, S_k をこの順に結合して得られる長さ 3 の文字列が回文となる
ただし、|S| は文字列 S の長さ、S_x は S の x 番目の文字を指します。
制約
- S は長さ 1 以上 2 \times 10^5 以下の英大文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ABCACC
出力例 1
5
(i, j, k) = (1, 2, 4), (1, 3, 4), (3, 4, 5), (3, 4, 6), (3, 5, 6) が条件を満たします。
入力例 2
OOOOOOOO
出力例 2
56
入力例 3
XYYXYYXYXXX
出力例 3
75
Score : 400 points
Problem Statement
You are given a string S consisting of uppercase English letters.
Find the number of integer triples (i, j, k) satisfying both of the following conditions:
- 1 \leq i < j < k \leq |S|
- The length-3 string formed by concatenating S_i, S_j, and S_k in this order is a palindrome.
Here, |S| denotes the length of S, and S_x denotes the x-th character of S.
Constraints
- S is a string of length between 1 and 2 \times 10^5, inclusive, consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ABCACC
Sample Output 1
5
The triples satisfying the conditions are (i, j, k) = (1, 2, 4), (1, 3, 4), (3, 4, 5), (3, 4, 6), (3, 5, 6).
Sample Input 2
OOOOOOOO
Sample Output 2
56
Sample Input 3
XYYXYYXYXXX
Sample Output 3
75
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純無向グラフ(すなわち、自己辺も多重辺もない無向グラフ)が与えられます。i 本目の辺は頂点 U_i と頂点 V_i を結んでいます。頂点 i には正整数 A_i が書かれています。
あなたは、以下の操作を N 回繰り返します。
- まだ削除されていない頂点 x を選び、「頂点 x 」と「頂点 x を端点に持つ辺全て」を削除する。この操作のコストは、頂点 x と辺で直接結ばれていて、かつまだ削除されていない頂点に書かれている整数の総和である。
N 回の操作全体のコストを、1 回ごとの操作におけるコストのうちの最大値として定めます。操作全体のコストとして取り得る値の最小値を求めてください。
制約
- 1 \le N \le 2 \times 10^5
- 0 \le M \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 1 \le U_i,V_i \le N
- 与えられるグラフは単純。
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
答えを出力せよ。
入力例 1
4 3 3 1 4 2 1 2 1 3 4 1
出力例 1
3
以下のように操作を行うことにより、N 回の操作のコストのうちの最大値を 3 にすることができます。
- 頂点 3 を選ぶ。コストは A_1=3 である。
- 頂点 1 を選ぶ。コストは A_2+A_4=3 である。
- 頂点 2 を選ぶ。コストは 0 である。
- 頂点 4 を選ぶ。コストは 0 である。
N 回の操作のコストのうちの最大値を 2 以下にすることはできないため、解は 3 です。
入力例 2
7 13 464 661 847 514 74 200 188 5 1 7 1 5 7 4 1 4 5 2 4 5 2 1 3 1 6 3 5 1 2 4 6 2 7
出力例 2
1199
Score : 500 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The i-th edge connects Vertices U_i and V_i. Vertex i has a positive integer A_i written on it.
You will repeat the following operation N times.
- Choose a Vertex x that is not removed yet, and remove Vertex x and all edges incident to Vertex x. The cost of this operation is the sum of the integers written on the vertices directly connected by an edge with Vertex x that are not removed yet.
We define the cost of the entire N operations as the maximum of the costs of the individual operations. Find the minimum possible cost of the entire operations.
Constraints
- 1 \le N \le 2 \times 10^5
- 0 \le M \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 1 \le U_i,V_i \le N
- The given graph is simple.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
Print the answer.
Sample Input 1
4 3 3 1 4 2 1 2 1 3 4 1
Sample Output 1
3
By performing the operations as follows, the maximum of the costs of the N operations can be 3.
- Choose Vertex 3. The cost is A_1=3.
- Choose Vertex 1. The cost is A_2+A_4=3.
- Choose Vertex 2. The cost is 0.
- Choose Vertex 4. The cost is 0.
The maximum of the costs of the N operations cannot be 2 or less, so the solution is 3.
Sample Input 2
7 13 464 661 847 514 74 200 188 5 1 7 1 5 7 4 1 4 5 2 4 5 2 1 3 1 6 3 5 1 2 4 6 2 7
Sample Output 2
1199
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
AtCoder 社のオンラインショップでは現在 N 個の商品を取り扱っており、商品 i の在庫は残り A_i 個です。
以下の Q 件の注文を順に処理してください。そのうち i 件目は次の通りです。
- 商品 l_i,l_i+1,\dots,r_i を k_i 個ずつ買う。但し、 k_i 個未満の商品はあるだけ全て買う。この注文で買われた商品の個数の合計を答えよ。
i<Q について、 i 件目の注文で買われた商品の在庫を減らした状態で i+1 件目の注文に進むことに注意してください。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^{15}
- 1 \le Q \le 3 \times 10^5
- 1 \le l_i \le r_i \le N
- 1 \le k_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N Q l_1 r_1 k_1 l_2 r_2 k_2 \vdots l_Q r_Q k_Q
出力
Q 行出力せよ。
そのうち i 行目には、 i 件目の注文で買われた商品の個数の合計を答えよ。
入力例 1
6 2 6 4 5 7 5 5 1 6 1 3 5 4 4 4 1 2 5 1 1 6 100
出力例 1
6 11 0 2 10
この入力には 5 件の注文が含まれます。
- はじめ、各商品の在庫は (商品 1 から順に) 2,6,4,5,7,5 個です。
- 1 件目の注文は l_1 = 1, r_1 = 6, k_1 = 1 です。
- この注文で各商品は 1,1,1,1,1,1 個、合計 6 個買われます。
- その後、各商品の在庫は 1,5,3,4,6,4 個になります。
- 2 件目の注文は l_2 = 3, r_2 = 5, k_2 = 4 です。
- この注文で各商品は 0,0,3,4,4,0 個、合計 11 個買われます。
- その後、各商品の在庫は 1,5,0,0,2,4 個になります。
- 3 件目の注文は l_3 = 4, r_3 = 4, k_3 = 1 です。
- この注文で各商品は 0,0,0,0,0,0 個、合計 0 個買われます。
- その後、各商品の在庫は 1,5,0,0,2,4 個になります。
- 4 件目の注文は l_4 = 2, r_4 = 5, k_4 = 1 です。
- この注文で各商品は 0,1,0,0,1,0 個、合計 2 個買われます。
- その後、各商品の在庫は 1,4,0,0,1,4 個になります。
- 5 件目の注文は l_5 = 1, r_5 = 6, k_5 = 100 です。
- この注文で各商品は 1,4,0,0,1,4 個、合計 10 個買われます。
- その後、各商品の在庫は 0,0,0,0,0,0 個になります。
Score : 525 points
Problem Statement
AtCoder Inc.'s online shop currently handles N products, and the stock of product i is A_i units remaining.
Process the following Q orders in order. The i-th order is as follows:
- Buy k_i units each of products l_i,l_i+1,\dots,r_i. For products with fewer than k_i units, buy all available units. Report the total number of products bought in this order.
Note that for i<Q, the stock of products bought in the i-th order is reduced before proceeding to the (i+1)-th order.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^{15}
- 1 \le Q \le 3 \times 10^5
- 1 \le l_i \le r_i \le N
- 1 \le k_i \le 10^9
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N Q l_1 r_1 k_1 l_2 r_2 k_2 \vdots l_Q r_Q k_Q
Output
Output Q lines.
The i-th line should contain the total number of products bought in the i-th order.
Sample Input 1
6 2 6 4 5 7 5 5 1 6 1 3 5 4 4 4 1 2 5 1 1 6 100
Sample Output 1
6 11 0 2 10
This input contains 5 orders.
- Initially, the stocks of the products are (from product 1 onward) 2,6,4,5,7,5 units.
- The first order is l_1 = 1, r_1 = 6, k_1 = 1.
- In this order, 1,1,1,1,1,1 units of the products are bought, for a total of 6 units.
- After this, the stocks of the products become 1,5,3,4,6,4 units.
- The second order is l_2 = 3, r_2 = 5, k_2 = 4.
- In this order, 0,0,3,4,4,0 units of the products are bought, for a total of 11 units.
- After this, the stocks of the products become 1,5,0,0,2,4 units.
- The third order is l_3 = 4, r_3 = 4, k_3 = 1.
- In this order, 0,0,0,0,0,0 units of the products are bought, for a total of 0 units.
- After this, the stocks of the products become 1,5,0,0,2,4 units.
- The fourth order is l_4 = 2, r_4 = 5, k_4 = 1.
- In this order, 0,1,0,0,1,0 units of the products are bought, for a total of 2 units.
- After this, the stocks of the products become 1,4,0,0,1,4 units.
- The fifth order is l_5 = 1, r_5 = 6, k_5 = 100.
- In this order, 1,4,0,0,1,4 units of the products are bought, for a total of 10 units.
- After this, the stocks of the products become 0,0,0,0,0,0 units.