Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
長さ M の A の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。
注記
数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。
例えば (2, 3) や (1, 2, 3) は (1, 2, 3, 4) の連続部分列ですが、(1, 3) や (3,2,1) は (1, 2, 3, 4) の連続部分列ではありません。
制約
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 2 5 4 -1 8
出力例 1
15
B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。
B=(A_1,A_4) などを選ぶことができないことに注意してください。
入力例 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
出力例 2
31
Score : 300 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.
Notes
A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.
For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).
Constraints
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- 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
Output
Print the answer.
Sample Input 1
4 2 5 4 -1 8
Sample Output 1
15
When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.
Note that you are not allowed to choose, for instance, B=(A_1,A_4).
Sample Input 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
Sample Output 2
31
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。
i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。
2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_i が S_j と一致するか、S_i が S_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。
N 本の棒の中に、何種類の異なる棒があるかを出力してください。
制約
- N は整数
- 2 \leq N \leq 2 \times 10^5
- S_i は英小文字のみからなる文字列
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
6 a abc de cba de abc
出力例 1
3
- S_2 =
abc
が S_4 =cba
を前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。 - S_2 =
abc
が S_6 =abc
と一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。 - S_3 =
de
が S_5 =de
と一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。
よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。
Score : 300 points
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
6 a abc de cba de abc
Sample Output 1
3
- S_2 =
abc
equals the reversal of S_4 =cba
, so the second and fourth sticks are considered the same. - S_2 =
abc
equals S_6 =abc
, so the second and sixth sticks are considered the same. - S_3 =
de
equals S_5 =de
, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
atcoder
の並べ替えである文字列 S が与えられます。
この文字列 S に対して以下の操作を 0 回以上行います。
- S 中の隣接する 2 文字を選び、入れ替える。
S を atcoder
にするために必要な最小の操作回数を求めてください。
制約
- S は
atcoder
の並べ替えである文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
catredo
出力例 1
8
catredo
\rightarrow [ac]tredo
\rightarrow actre[od]
\rightarrow actr[oe]d
\rightarrow actro[de]
\rightarrow act[or]de
\rightarrow acto[dr]e
\rightarrow a[tc]odre
\rightarrow atcod[er]
という流れで操作を行うと、 8 回で S を atcoder
にすることができ、これが達成可能な最小の操作回数です。
入力例 2
atcoder
出力例 2
0
この場合、文字列 S は元から atcoder
です。
入力例 3
redocta
出力例 3
21
Score : 400 points
Problem Statement
You are given a string S that is a permutation of atcoder
.
On this string S, you will perform the following operation 0 or more times:
- Choose two adjacent characters of S and swap them.
Find the minimum number of operations required to make S equal atcoder
.
Constraints
- S is a string that is a permutation of
atcoder
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
catredo
Sample Output 1
8
You can make S equal atcoder
in 8 operations as follows:
catredo
\rightarrow [ac]tredo
\rightarrow actre[od]
\rightarrow actr[oe]d
\rightarrow actro[de]
\rightarrow act[or]de
\rightarrow acto[dr]e
\rightarrow a[tc]odre
\rightarrow atcod[er]
This is the minimum number of operations achievable.
Sample Input 2
atcoder
Sample Output 2
0
In this case, the string S is already atcoder
.
Sample Input 3
redocta
Sample Output 3
21
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
変数 X と、X の値を変更する N 種類の操作があります。操作 i は整数の組 (T_i,A_i) で表され、意味は次の通りです。
- T_i=1 のとき、X の値を X\ {\rm and}\ A_i に置き換える。
- T_i=2 のとき、X の値を X\ {\rm or}\ A_i に置き換える。
- T_i=3 のとき、X の値を X\ {\rm xor}\ A_i に置き換える。
変数 X を値 C で初期化した状態から、以下の処理を順に実行してください。
- 操作 1 を行い、操作後の X の値を出力する。
- 続けて、操作 1,2 を順に行い、操作後の X の値を出力する。
- 続けて、操作 1,2,3 を順に行い、操作後の X の値を出力する。
- \vdots
- 続けて、操作 1,2,\ldots,N を順に行い、操作後の X の値を出力する。
{\rm and}, {\rm or}, {\rm xor} とは
非負整数 A, B の {\rm and}, {\rm or}, {\rm xor} は、以下のように定義されます。- A\ {\rm and}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
- A\ {\rm or}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも一方が 1 であれば 1、そうでなければ 0 である。
- A\ {\rm xor}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうちちょうど一方が 1 であれば 1、そうでなければ 0 である。
制約
- 1 \leq N \leq 2\times 10^5
- 1\leq T_i \leq 3
- 0\leq A_i \lt 2^{30}
- 0\leq C \lt 2^{30}
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N C T_1 A_1 T_2 A_2 \vdots T_N A_N
出力
問題文中の指示に従って N 行出力せよ。
入力例 1
3 10 3 3 2 5 1 12
出力例 1
9 15 12
最初、X の値は 10 です。
- 操作 1 を行うと X の値は 9 になります。
- 続けて操作 1 を行うと X の値は 10 になり、さらに操作 2 を行うと 15 になります。
- 続けて操作 1 を行うと X の値は 12 になり、さらに操作 2 を行うと 13 に、さらに続けて操作 3 を行うと 12 になります。
入力例 2
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9
出力例 2
0 2 1 0 5 3 3 11 2
Score : 500 points
Problem Statement
We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (T_i,A_i), and is the following operation:
- if T_i=1, it replaces the value of X with X\ {\rm and}\ A_i;
- if T_i=2, it replaces the value of X with X\ {\rm or}\ A_i;
- if T_i=3, it replaces the value of X with X\ {\rm xor}\ A_i.
Initialize X with the value of C and execute the following procedures in order:
- Perform Operation 1, and then print the resulting value of X.
- Next, perform Operation 1, 2 in this order, and then print the value of X.
- Next, perform Operation 1, 2, 3 in this order, and then print the value of X.
- \vdots
- Next, perform Operation 1, 2, \ldots, N in this order, and then print the value of X.
What are {\rm and}, {\rm or}, {\rm xor}?
The {\rm and}, {\rm or}, {\rm xor} of non-negative integers A and B are defined as follows:
- When A\ {\rm and}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
- When A\ {\rm or}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if at least one of the digits in that place of A and B is 1, and 0 otherwise.
- When A\ {\rm xor}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1\leq T_i \leq 3
- 0\leq A_i \lt 2^{30}
- 0\leq C \lt 2^{30}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N C T_1 A_1 T_2 A_2 \vdots T_N A_N
Output
Print N lines, as specified in the Problem Statement.
Sample Input 1
3 10 3 3 2 5 1 12
Sample Output 1
9 15 12
The initial value of X is 10.
- Operation 1 changes X to 9.
- Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
- Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.
Sample Input 2
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9
Sample Output 2
0 2 1 0 5 3 3 11 2
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder 国には N 個の島があり、 最初、どの島にも空港・港はなく、どの島の間にも道路はありません。 王である高橋君はこれらの島の間に交通手段を用意することにしました。 具体的には、高橋君は次の操作のうち 1 つを選んで行うことを好きなだけ繰り返す事ができます。
- 1\leq i\leq N をみたす i を選び、コスト X_i を払って、島 i に空港を建設する。
- 1\leq i\leq N をみたす i を選び、コスト Y_i を払って、島 i に港を建設する。
- 1\leq i\leq M をみたす i を選び、コスト Z_i を払って、島 A_i と島 B_i の間を双方向に結ぶ道路を建設する。
高橋君の目標は、任意の相異なる 2 つの島 U, V について、 島 U からはじめて次の行動のうち 1 つを選んで行うことを好きなだけ繰り返す事で、 島 V に到達することができるようにする事です。
- 島 S,T の両方に空港がある時、島 S から島 T まで移動する。
- 島 S,T の両方に港がある時、島 S から島 T まで移動する。
- 島 S,T を結ぶ道路が存在する時、島 S から島 T まで移動する。
高橋君が目標を達成するのに必要な最小コストを求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1\leq X_i\leq 10^9
- 1\leq Y_i\leq 10^9
- 1\leq A_i<B_i\leq N
- 1\leq Z_i\leq 10^9
- i\neq j ならば (A_i,B_i)\neq (A_j,B_j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 X_2 \ldots X_N Y_1 Y_2 \ldots Y_N A_1 B_1 Z_1 A_2 B_2 Z_2 \vdots A_M B_M Z_M
出力
高橋君が目標を達成するのに必要な最小コストを出力せよ。
入力例 1
4 2 1 20 4 7 20 2 20 3 1 3 5 1 4 6
出力例 1
16
高橋君は次のように交通手段を建設します。
- コスト X_1=1 を払って、島 1 に空港を建設する。
- コスト X_3=4 を払って、島 3 に空港を建設する。
- コスト Y_2=2 を払って、島 2 に港を建設する。
- コスト Y_4=3 を払って、島 4 に港を建設する。
- コスト Z_2=6 を払って、島 1 と島 4 の間を結ぶ道路を建設する。
このとき、目標は達成されており、かかったコストは 16 となります。 コスト 15 以下で目標を達成する方法はないため、16 を出力します。
入力例 2
3 1 1 1 1 10 10 10 1 2 100
出力例 2
3
空港・港・道路のうち、一度も建設されないものがあっても構いません。
入力例 3
7 8 35 29 36 88 58 15 25 99 7 49 61 67 4 57 2 3 3 2 5 36 2 6 89 1 6 24 5 7 55 1 3 71 3 4 94 5 6 21
出力例 3
160
Score : 500 points
Problem Statement
The Republic of AtCoder has N islands. Initially, none of the islands has an airport or harbor, and there is no road between any two of them. Takahashi, the king, will provide some means of transportation between these islands. Specifically, he can do the following operations any number of times in any order.
- Choose an integer i such that 1\leq i\leq N and pay a cost of X_i to build an airport on island i.
- Choose an integer i such that 1\leq i\leq N and pay a cost of Y_i to build a harbor on island i.
- Choose an integer i such that 1\leq i\leq M and pay a cost of Z_i to build a road that bidirectionally connects island A_i and island B_i.
Takahashi's objective is to make it possible, for every pair of different islands U and V, to reach island V from island U when one can perform the following actions any number of times in any order.
- When islands S and T both have airports, go from island S to island T.
- When islands S and T both have harbors, go from island S to island T.
- When islands S and T are connected by a road, go from island S to island T.
Find the minimum total cost Takahashi needs to pay to achieve his objective.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1\leq X_i\leq 10^9
- 1\leq Y_i\leq 10^9
- 1\leq A_i<B_i\leq N
- 1\leq Z_i\leq 10^9
- (A_i,B_i)\neq (A_j,B_j), if i\neq j.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 X_2 \ldots X_N Y_1 Y_2 \ldots Y_N A_1 B_1 Z_1 A_2 B_2 Z_2 \vdots A_M B_M Z_M
Output
Print the minimum total cost Takahashi needs to pay to achieve his objective.
Sample Input 1
4 2 1 20 4 7 20 2 20 3 1 3 5 1 4 6
Sample Output 1
16
Takahashi will build the following infrastructure.
- Pay a cost of X_1=1 to build an airport on island 1.
- Pay a cost of X_3=4 to build an airport on island 3.
- Pay a cost of Y_2=2 to build a harbor on island 2.
- Pay a cost of Y_4=3 to build a harbor on island 4.
- Pay a cost of Z_2=6 to build a road connecting island 1 and island 4.
Then, the objective will be achieved at a cost of 16. There is no way to achieve the objective for a cost of 15 or less, so 16 should be printed.
Sample Input 2
3 1 1 1 1 10 10 10 1 2 100
Sample Output 2
3
It is not mandatory to build all three types of facilities at least once.
Sample Input 3
7 8 35 29 36 88 58 15 25 99 7 49 61 67 4 57 2 3 3 2 5 36 2 6 89 1 6 24 5 7 55 1 3 71 3 4 94 5 6 21
Sample Output 3
160