Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
ここに円形のピザが 1 枚あります。
高橋くんは長さ N の数列 A を使ってこのピザを以下の手順で切り分けます。
- 最初に、円の中心から 12 時の方向に切れ込みをひとつ入れます。
- 次に、以下の操作を N 回繰り返します。 i 回目の操作では以下を行います。
- まず、ピザを時計回りに A_i 度回転させる。
- 次に、円の中心から 12 時の方向に切れ込みをひとつ入れる。
例えば、A=(90,180,45,195) として手順を行うと、下図のようになります。
このとき、最も大きなピザの中心角が何度であるか求めてください。
制約
- 入力は全て整数
- 1 \le N \le 359
- 1 \le A_i \le 359
- 同じ場所に複数回切れ込みが入ることはない。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 90 180 45 195
出力例 1
120
この入力は問題文中の例と一致します。
最も大きなピザの中心角は 120 度です。
入力例 2
1 1
出力例 2
359
入力例 3
10 215 137 320 339 341 41 44 18 241 149
出力例 3
170
Score : 200 points
Problem Statement
We have a circular pizza.
Takahashi will cut this pizza using a sequence A of length N, according to the following procedure.
- First, make a cut from the center in the 12 o'clock direction.
- Next, do N operations. The i-th operation is as follows.
- Rotate the pizza A_i degrees clockwise.
- Then, make a cut from the center in the 12 o'clock direction.
For example, if A=(90,180,45,195), the procedure cuts the pizza as follows.
Find the center angle of the largest pizza after the procedure.
Constraints
- All values in input are integers.
- 1 \le N \le 359
- 1 \le A_i \le 359
- There will be no multiple cuts at the same position.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 90 180 45 195
Sample Output 1
120
This input coincides with the example in the Problem Statement.
The center angle of the largest pizza is 120 degrees.
Sample Input 2
1 1
Sample Output 2
359
Sample Input 3
10 215 137 320 339 341 41 44 18 241 149
Sample Output 3
170
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数からなる長さ N の数列 A=(A_1,\ldots,A_N) があります。どの隣接する 2 項の値も相異なります。
この数列に対し、次の操作によりいくつか数を挿入します。
- 数列 A のどの隣接する 2 項の差の絶対値も 1 であるとき、操作を終了する。
- 数列 A の先頭から見て、隣接する 2 項の差の絶対値が 1 でない最初の箇所を A_i,A_{i+1} とする。
- A_i < A_{i+1} ならば、A_i と A_{i+1} の間に、A_i+1,A_i+2,\ldots,A_{i+1}-1 を挿入する。
- A_i > A_{i+1} ならば、A_i と A_{i+1} の間に、A_i-1,A_i-2,\ldots,A_{i+1}+1 を挿入する。
- 手順 1 に戻る。
操作が終了したときの数列を出力してください。
制約
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- A_i \neq A_{i+1}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
操作が終了したときの数列の各要素を空白区切りで出力せよ。
入力例 1
4 2 5 1 2
出力例 1
2 3 4 5 4 3 2 1 2
最初、数列は (2,5,1,2) です。操作は以下のように行われます。
- 1 項目の 2 と 2 項目の 5 の間に 3,4 を挿入する。数列は (2,3,4,5,1,2) となる。
- 4 項目の 5 と 5 項目の 1 の間に 4,3,2 を挿入する。数列は (2,3,4,5,4,3,2,1,2) となる。
入力例 2
6 3 4 5 6 5 4
出力例 2
3 4 5 6 5 4
一度も挿入が行われないこともあります。
Score : 200 points
Problem Statement
We have a sequence of length N consisting of positive integers: A=(A_1,\ldots,A_N). Any two adjacent terms have different values.
Let us insert some numbers into this sequence by the following procedure.
- If every pair of adjacent terms in A has an absolute difference of 1, terminate the procedure.
- Let A_i, A_{i+1} be the pair of adjacent terms nearest to the beginning of A whose absolute difference is not 1.
- If A_i < A_{i+1}, insert A_i+1,A_i+2,\ldots,A_{i+1}-1 between A_i and A_{i+1}.
- If A_i > A_{i+1}, insert A_i-1,A_i-2,\ldots,A_{i+1}+1 between A_i and A_{i+1}.
- Return to step 1.
Print the sequence when the procedure ends.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- A_i \neq A_{i+1}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the terms in the sequence when the procedure ends, separated by spaces.
Sample Input 1
4 2 5 1 2
Sample Output 1
2 3 4 5 4 3 2 1 2
The initial sequence is (2,5,1,2). The procedure goes as follows.
- Insert 3,4 between the first term 2 and the second term 5, making the sequence (2,3,4,5,1,2).
- Insert 4,3,2 between the fourth term 5 and the fifth term 1, making the sequence (2,3,4,5,4,3,2,1,2).
Sample Input 2
6 3 4 5 6 5 4
Sample Output 2
3 4 5 6 5 4
No insertions may be performed.
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
配点 : 300 点
問題文
長さ N の文字列 S が与えられます。S_i\ (1\leq i \leq N) を S の左から i 番目の文字とします。
あなたは以下の 2 種類の操作を好きな順番で 0 回以上好きな回数行うことができます。
-
A 円払う。 S の左端の文字を右端に移動する。すなわち、S_1S_2\ldots S_N を S_2\ldots S_NS_1 に変える。
-
B 円払う。 1 以上 N 以下の整数 i を選び、 S_i を好きな英小文字で置き換える。
S を回文にするためには最低で何円必要ですか?
回文とは
ある文字列 T について、 T の長さを |T| として、全ての整数 i (1 \le i \le |T|) について、 T の前から i 文字目と後ろから i 文字目が同じであるとき、またそのときに限って、 T は回文です。制約
- 1\leq N \leq 5000
- 1\leq A,B\leq 10^9
- S は英小文字からなる長さ N の文字列
- S 以外の入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A B S
出力
答えを整数として出力せよ。
入力例 1
5 1 2 rrefa
出力例 1
3
最初に 2 番目の操作を 1 回行います。2 円払い、i=5 として S_5 を e
で置き換えます。 S は rrefe
となります。
次に 1 番目の操作を 1 回行います。1 円払い、S は refer
となります。これは回文です。
よって 3 円払うことで S を回文にすることができました。 2 円以下払うことで S を回文にすることは不可能なので、これが答えです。
入力例 2
8 1000000000 1000000000 bcdfcgaa
出力例 2
4000000000
答えは 32 bit 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
You are given a string S of length N. Let S_i\ (1\leq i \leq N) be the i-th character of S from the left.
You may perform the following two kinds of operations zero or more times in any order:
-
Pay A yen (the currency in Japan). Move the leftmost character of S to the right end. In other words, change S_1S_2\ldots S_N to S_2\ldots S_NS_1.
-
Pay B yen. Choose an integer i between 1 and N, and replace S_i with any lowercase English letter.
How many yen do you need to pay to make S a palindrome?
What is a palindrome?
A string T is a palindrome if and only if the i-th character from the left and the i-th character from the right are the same for all integers i (1 \le i \le |T|), where |T| is the length of T.Constraints
- 1\leq N \leq 5000
- 1\leq A,B\leq 10^9
- S is a string of length N consisting of lowercase English letters.
- All values in the input except for S are integers.
Input
The input is given from Standard Input in the following format:
N A B S
Output
Print the answer as an integer.
Sample Input 1
5 1 2 rrefa
Sample Output 1
3
First, pay 2 yen to perform the operation of the second kind once: let i=5 to replace S_5 with e
. S is now rrefe
.
Then, pay 1 yen to perform the operation of the first kind once. S is now refer
, which is a palindrome.
Thus, you can make S a palindrome for 3 yen. Since you cannot make S a palindrome for 2 yen or less, 3 is the answer.
Sample Input 2
8 1000000000 1000000000 bcdfcgaa
Sample Output 2
4000000000
Note that the answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ 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 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。
例えば、(10,30) は (10,20,30) の部分列ですが、(20,10) は (10,20,30) の部分列ではありません。
制約
- 1 \le M \le N \le 2000
- - 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
21
B=(A_1,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times 5 + 2 \times 8 = 21 となります。22 以上の値を達成することはできないため、解は 21 です。
入力例 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
出力例 2
54
Score : 400 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 (not necessarily contiguous) subsequence B=(B_1,B_2,\dots,B_M) of length M of A.
Notes
A subsequence of a number sequence is a sequence that is obtained by removing 0 or more elements from the original number sequence and concatenating the remaining elements without changing the order.
For example, (10,30) is a subsequence of (10,20,30), but (20,10) is not a subsequence of (10,20,30).
Constraints
- 1 \le M \le N \le 2000
- - 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
21
When B=(A_1,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times 5 + 2 \times 8 = 21. Since it is impossible to achieve 22 or a larger value, the solution is 21.
Sample Input 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
Sample Output 2
54