Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
空の列と、N 個のボールがあります。i 個目 (1\leq i\leq N) のボールの大きさは 2^{A_i} です。
これから N 回の操作を行います。
i 回目の操作では、i 個目のボールを列の一番右に付け加えた後、次の手順を繰り返します。
- 列にあるボールが 1 つ以下ならば操作を終了する。
- 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 異なる ならば操作を終了する。
- 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 等しい ならば、2 つのボールを取り除き、「取り除かれた 2 つのボールの大きさの和」の大きさのボール 1 つを列の一番右に付け加える。その後、1. に戻り、手順を繰り返す。
N 回の操作の後で、列にあるボールの数を求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 0\leq A_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
N 回の操作の後で、列にあるボールの数を出力せよ。
入力例 1
7 2 1 1 3 5 3 3
出力例 1
3
操作は次のように行われます。
- 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^2 です。
- 2 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^2, 2^1 です。
- 3 回目の操作の後、列にあるボールは 1 つで、大きさは 2^3 です。これは次のようにして得ることができます。
- 3 回目の操作において 3 個目のボールを付け加えたとき、列にあるボールの大きさは順に 2^2,2^1,2^1 となります。
- 右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^1+2^1=2^2 のボールが追加されます。このとき、列にあるボールの大きさは 2^2, 2^2 となります。
- さらに、再び右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^2+2^2=2^3 のボールが追加され、列にあるボールの大きさは 2^3 となります。
- 4 回目の操作の後、列にあるボールは 1 つで、大きさは 2^4 です。
- 5 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^4, 2^5 です。
- 6 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^3 です。
- 7 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^4 です。
よって、最後に列にあるボールの数である 3 を出力します。
入力例 2
5 0 0 0 1 2
出力例 2
4
操作は次のように行われます。
- 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^0 です。
- 2 回目の操作の後、列にあるボールは 1 つで、大きさは 2^1 です。
- 3 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^1, 2^0 です。
- 4 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^1, 2^0, 2^1 です。
- 5 回目の操作の後、列にあるボールは 4 つで、大きさは順に 2^1, 2^0, 2^1, 2^2 です。
よって、最後に列にあるボールの数である 4 を出力します。
Score: 250 points
Problem Statement
You have an empty sequence and N balls. The size of the i-th ball (1 \leq i \leq N) is 2^{A_i}.
You will perform N operations.
In the i-th operation, you add the i-th ball to the right end of the sequence, and repeat the following steps:
- If the sequence has one or fewer balls, end the operation.
- If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation.
- If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process.
Determine the number of balls remaining in the sequence after the N operations.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9
- All input values 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 number of balls in the sequence after the N operations.
Sample Input 1
7 2 1 1 3 5 3 3
Sample Output 1
3
The operations proceed as follows:
- After the first operation, the sequence has one ball, of size 2^2.
- After the second operation, the sequence has two balls, of sizes 2^2 and 2^1 in order.
- After the third operation, the sequence has one ball, of size 2^3. This is obtained as follows:
- When the third ball is added during the third operation, the sequence has balls of sizes 2^2, 2^1, 2^1 in order.
- The first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^1 + 2^1 = 2^2 is added. Now, the sequence has balls of sizes 2^2, 2^2.
- Again, the first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^2 + 2^2 = 2^3 is added, leaving the sequence with a ball of size 2^3.
- After the fourth operation, the sequence has one ball, of size 2^4.
- After the fifth operation, the sequence has two balls, of sizes 2^4 and 2^5 in order.
- After the sixth operation, the sequence has three balls, of sizes 2^4, 2^5, 2^3 in order.
- After the seventh operation, the sequence has three balls, of sizes 2^4, 2^5, 2^4 in order.
Therefore, you should print 3, the final number of balls in the sequence.
Sample Input 2
5 0 0 0 1 2
Sample Output 2
4
The operations proceed as follows:
- After the first operation, the sequence has one ball, of size 2^0.
- After the second operation, the sequence has one ball, of size 2^1.
- After the third operation, the sequence has two balls, of sizes 2^1 and 2^0 in order.
- After the fourth operation, the sequence has three balls, of sizes 2^1, 2^0, 2^1 in order.
- After the fifth operation, the sequence has four balls, of sizes 2^1, 2^0, 2^1, 2^2 in order.
Therefore, you should print 4, the final number of balls in the sequence.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
10^9 階建てのビルがあり、N 本のはしごがかかっています。
ビルの 1 階にいる高橋君ははしごを繰り返し使って(0 回でもよい)できるだけ高い階へ上りたいと考えています。
はしごには 1 から N までの番号がついており、はしご i は A_i 階と B_i 階を結んでいます。はしご i を利用すると A_i 階から B_i 階へ、または B_i 階から A_i 階へ双方向に移動することができますが、それ以外の階の間の移動は行うことはできません。
また、高橋君は同じ階での移動は自由に行うことができますが、はしご以外の方法で他の階へ移動することはできません。
高橋君は最高で何階へ上ることができますか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- A_i \neq B_i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \ldots A_N B_N
出力
答えを出力せよ。
入力例 1
4 1 4 4 3 4 10 8 3
出力例 1
10
はしご 1 で 4 階に進み、はしご 3 で 10 階に進むことにより、10 階にたどり着くことができます。
入力例 2
6 1 3 1 5 1 12 3 5 3 12 5 12
出力例 2
12
入力例 3
3 500000000 600000000 600000000 700000000 700000000 800000000
出力例 3
1
他の階への移動ができない場合もあります。
Score : 300 points
Problem Statement
There is a 10^9-story building with N ladders.
Takahashi, who is on the 1-st (lowest) floor, wants to reach the highest floor possible by using ladders (possibly none).
The ladders are numbered from 1 to N, and ladder i connects the A_i-th and B_i-th floors. One can use ladder i in either direction to move from the A_i-th floor to the B_i-th floor or vice versa, but not between other floors.
Takahashi can freely move within the same floor, but cannot move between floors without using ladders.
What is the highest floor Takahashi can reach?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- A_i \neq B_i
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \ldots A_N B_N
Output
Print an integer representing the answer.
Sample Input 1
4 1 4 4 3 4 10 8 3
Sample Output 1
10
He can reach the 10-th floor by using ladder 1 to get to the 4-th floor and then ladder 3 to get to the 10-th floor.
Sample Input 2
6 1 3 1 5 1 12 3 5 3 12 5 12
Sample Output 2
12
Sample Input 3
3 500000000 600000000 600000000 700000000 700000000 800000000
Sample Output 3
1
He may be unable to move between floors.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
英大小文字からなる文字列 S が与えられます。
S に以下の操作を 10^{100} 回繰り返します。
- まず、 S の大文字を小文字に、小文字を大文字に書き換えた文字列を T とする。
- その後、 S と T とをこの順に連結した文字列を新たな S とする。
Q 個の質問に答えて下さい。 そのうち i 個目は次の通りです。
- 全ての操作を終えた後の S の先頭から K_i 文字目を求めよ。
制約
- S は英大小文字からなる長さ 1 以上 2 \times 10^5 以下の文字列
- Q,K_i は整数
- 1 \le Q \le 2 \times 10^5
- 1 \le K_i \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
S Q K_1 K_2 \dots K_Q
出力
i 個目の質問の答えを C_i とする時、以下の形式で 1 行に空白区切りで出力せよ。
C_1 C_2 \dots C_Q
入力例 1
aB 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
出力例 1
a B A b A b a B A b a B a B A b
操作前の S = aB です。
aBに 1 回操作を行うとaBAbとなります。aBに 2 回操作を行うとaBAbAbaBとなります。- \dots
10^{100} 回の操作を終えた後の S = aBAbAbaBAbaBaBAb... です。
入力例 2
qWeRtYuIoP 8 1 1 2 3 5 8 13 21
出力例 2
q q W e t I E Q
入力例 3
AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay 5 1000000000000000000 123456789 1 987654321 999999999999999999
出力例 3
K a A Z L
Score : 350 points
Problem Statement
You are given a string S consisting of uppercase and lowercase English letters.
We perform the following operation on S 10^{100} times:
- First, create a string T by changing uppercase letters in S to lowercase, and lowercase letters to uppercase.
- Then, concatenate S and T in this order to form a new S.
Answer Q queries. The i-th query is as follows:
- Find the K_i-th character from the beginning of S after all operations are completed.
Constraints
- S is a string consisting of uppercase and lowercase English letters, with length between 1 and 2 \times 10^5, inclusive.
- Q and K_i are integers.
- 1 \le Q \le 2 \times 10^5
- 1 \le K_i \le 10^{18}
Input
The input is given from Standard Input in the following format:
S Q K_1 K_2 \dots K_Q
Output
Let C_i be the answer to the i-th query. Print them in a single line, separated by spaces, in the following format:
C_1 C_2 \dots C_Q
Sample Input 1
aB 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Sample Output 1
a B A b A b a B A b a B a B A b
Before the operations, S = aB.
- After performing the operation once on
aB, it becomesaBAb. - After performing the operation twice on
aB, it becomesaBAbAbaB. - \dots
After performing the operation 10^{100} times, S = aBAbAbaBAbaBaBAb...
Sample Input 2
qWeRtYuIoP 8 1 1 2 3 5 8 13 21
Sample Output 2
q q W e t I E Q
Sample Input 3
AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay 5 1000000000000000000 123456789 1 987654321 999999999999999999
Sample Output 3
K a A Z L
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
高橋くんは秘密の整数列 a を持っており、現時点で、a の長さが N であることは分かっています。
a の中身を当てたいあなたに対し、高橋くんは以下の Q 個の情報を追加で与えてくれることを約束しました。
- i\ (1 \leq i \leq Q) 個目の情報: a_{l_i}+a_{l_i+1}+\cdots+a_{r_i} の値
高橋くんが約束を守り、Q 個の情報すべてが与えられた場合、a に含まれる全要素の総和 a_1+a_2+\cdots+a_N を特定することは可能ですか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})
- 1 \leq l_i \leq r_i \leq N
- (l_i,r_i) \neq (l_j,r_j)\ (i \neq j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
l_1 r_1
l_2 r_2
\hspace{0.4cm}\vdots
l_Q r_Q
出力
a に含まれる全要素の総和を特定することが可能なら Yes を、そうでないなら No を出力せよ。
入力例 1
3 3 1 2 2 3 2 2
出力例 1
Yes
1 個目の情報と 2 個目の情報から、a_1+a_2+a_2+a_3 の値が分かります。そこから 3 個目の情報によって得られる a_2 の値を引くと、a_1+a_2+a_3 の値を特定可能です。
入力例 2
4 3 1 3 1 2 2 3
出力例 2
No
a の先頭 3 項の総和を特定することは可能ですが、全要素の総和を特定することは不可能です。
入力例 3
4 4 1 1 2 2 3 3 1 4
出力例 3
Yes
4 個目の情報によって全要素の総和が直接与えられています。
Score : 500 points
Problem Statement
Takahashi has a secret integer sequence a. You know that the length of a is N.
You want to guess the contents of a. He has promised to give you the following Q additional pieces of information.
- The i-th information: the value a_{l_i}+a_{l_i+1}+\cdots+a_{r_i}.
Is it possible to determine the sum of all elements in a, a_1+a_2+\cdots+a_N, if the Q pieces of promised information are given?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})
- 1 \leq l_i \leq r_i \leq N
- (l_i,r_i) \neq (l_j,r_j)\ (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
l_1 r_1
l_2 r_2
\hspace{0.4cm}\vdots
l_Q r_Q
Output
If it is possible to determine the sum of all elements in a, print Yes; otherwise, print No.
Sample Input 1
3 3 1 2 2 3 2 2
Sample Output 1
Yes
From the first and second information, we can find the value a_1+a_2+a_2+a_3. By subtracting the value of a_2 from it, we can determine the value a_1+a_2+a_3.
Sample Input 2
4 3 1 3 1 2 2 3
Sample Output 2
No
We can determine the sum of the first 3 elements of a, but not the sum of all elements.
Sample Input 3
4 4 1 1 2 2 3 3 1 4
Sample Output 3
Yes
The fourth information directly gives us the sum of all elements.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
N\times N のマス目があり、上から i 行目、左から j 列目 (1\leq i,j\leq N) のマスには整数 A _ {i,j} が書かれています。
整数 M が与えられます。 M\times M のマス目を重ならないように 3 つ選ぶときの、選んだマス目に書かれている整数の総和としてありえる最大値を求めてください。
問題の厳密な定義
整数の 6 つ組 (i _ 1,j _ 1,i _ 2,j _ 2,i _ 3,j _ 3) が次の 3 つの条件を満たしているとき、良い 6 つ組ということにします。- 1\leq i _ k\leq N-M+1\ (k=1,2,3)
- 1\leq j _ k\leq N-M+1\ (k=1,2,3)
- k\neq l\ (k,l\in\lbrace1,2,3\rbrace) ならば、\lbrace(i,j)\mid i _ k\leq i\lt i _ k+M\wedge j _ k\leq j\lt j _ k+M\rbrace と \lbrace(i,j)\mid i _ l\leq i\lt i _ l+M\wedge j _ l\leq j\lt j _ l+M\rbrace に共通部分はない
制約
- 2\leq N\leq 1000
- 1\leq M\leq N/2
- 0\leq A _ {i,j}\leq10 ^ 9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A _ {1,1} A _ {1,2} \ldots A _ {1,N}
A _ {2,1} A _ {2,2} \ldots A _ {2,N}
\vdots \ \vdots \ddots \vdots
A _ {N,1} A _ {N,2} \ldots A _ {N,N}
出力
答えを出力せよ。
入力例 1
7 3 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5
出力例 1
154
与えられたグリッドから、以下の図のように 3\times3 のマス目を 3 つ選ぶと(これは (i _ 1,j _ 1,i _ 2,j _ 2,i _ 3,j _ 3)=(1,5,2,1,5,2) とすることに対応します)、選んだマス目に書かれている数の合計は 154 となります。

問題文の条件を満たす選び方であって選んだマス目に書かれている数の合計が 155 以上であるものは存在しないため、154 を出力してください。
入力例 2
7 1 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5
出力例 2
27
以下のように選ぶのが最適です。

入力例 3
16 4 74 16 58 32 97 52 43 51 40 58 13 24 65 11 63 29 98 75 40 77 15 50 83 85 35 46 38 37 56 38 63 55 95 42 10 70 53 40 25 10 70 32 33 19 52 79 74 58 33 91 53 11 65 63 78 77 81 46 81 63 11 82 55 62 39 95 92 69 77 89 14 84 53 78 71 81 66 39 96 29 74 26 60 55 89 35 32 64 17 26 74 92 84 33 59 82 23 69 10 95 94 14 58 58 97 95 62 58 72 55 71 43 93 77 27 87 74 72 91 37 53 80 51 71 37 35 97 46 81 88 26 79 78 30 53 68 83 28 59 28 74 55 20 86 93 13 25 19 53 53 17 24 69 14 67 81 10 19 69 90 88 83 62 92 22 31 27 34 67 48 42 32 68 14 96 87 44 69 25 48 68 42 53 82 44 42 96 31 13 56 68 83 63 87 24 75 16 70 63 99 95 10 63 26 56 12 77 49 94 83 69 95 48 41 40 97 45 61 26 38 83 91 44 31 43 69 54 64 20 60 17 15 62 25 58 50 59 63 88 70 72 95 21 28 41 14 77 22 64 78 33 55 67 51 78 40
出力例 3
3295
以下のように選ぶのが最適です。

Score: 525 points
Problem Statement
There is an N\times N grid, and the cell at the i-th row from the top and the j-th column from the left (1\leq i,j\leq N) contains the integer A _ {i,j}.
You are given an integer M. When choosing three non-overlapping M\times M grids, find the maximum possible sum of the integers written in the chosen grids.
Formal definition of the problem
A 6-tuple of integers (i _ 1,j _ 1,i _ 2,j _ 2,i _ 3,j _ 3) is called a good 6-tuple when it satisfies the following three conditions:- 1\leq i _ k\leq N-M+1\ (k=1,2,3)
- 1\leq j _ k\leq N-M+1\ (k=1,2,3)
- If k\neq l\ (k,l\in\lbrace1,2,3\rbrace), the sets \lbrace(i,j)\mid i _ k\leq i\lt i _ k+M\wedge j _ k\leq j\lt j _ k+M\rbrace and \lbrace(i,j)\mid i _ l\leq i\lt i _ l+M\wedge j _ l\leq j\lt j _ l+M\rbrace do not intersect.
Constraints
- 2\leq N\leq 1000
- 1\leq M\leq N/2
- 0\leq A _ {i,j}\leq10 ^ 9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A _ {1,1} A _ {1,2} \ldots A _ {1,N}
A _ {2,1} A _ {2,2} \ldots A _ {2,N}
\vdots \ \vdots \ddots \vdots
A _ {N,1} A _ {N,2} \ldots A _ {N,N}
Output
Print the answer.
Sample Input 1
7 3 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5
Sample Output 1
154
From the given grid, if we choose three 3\times3 grids as shown in the figure below (this corresponds to setting (i _ 1,j _ 1,i _ 2,j _ 2,i _ 3,j _ 3)=(1,5,2,1,5,2)), the sum of the numbers written in the chosen grids will be 154.

There is no way to make the sum 155 or greater while satisfying the conditions in the problem statement, so print 154.
Sample Input 2
7 1 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5
Sample Output 2
27
The following choice is optimal.

Sample Input 3
16 4 74 16 58 32 97 52 43 51 40 58 13 24 65 11 63 29 98 75 40 77 15 50 83 85 35 46 38 37 56 38 63 55 95 42 10 70 53 40 25 10 70 32 33 19 52 79 74 58 33 91 53 11 65 63 78 77 81 46 81 63 11 82 55 62 39 95 92 69 77 89 14 84 53 78 71 81 66 39 96 29 74 26 60 55 89 35 32 64 17 26 74 92 84 33 59 82 23 69 10 95 94 14 58 58 97 95 62 58 72 55 71 43 93 77 27 87 74 72 91 37 53 80 51 71 37 35 97 46 81 88 26 79 78 30 53 68 83 28 59 28 74 55 20 86 93 13 25 19 53 53 17 24 69 14 67 81 10 19 69 90 88 83 62 92 22 31 27 34 67 48 42 32 68 14 96 87 44 69 25 48 68 42 53 82 44 42 96 31 13 56 68 83 63 87 24 75 16 70 63 99 95 10 63 26 56 12 77 49 94 83 69 95 48 41 40 97 45 61 26 38 83 91 44 31 43 69 54 64 20 60 17 15 62 25 58 50 59 63 88 70 72 95 21 28 41 14 77 22 64 78 33 55 67 51 78 40
Sample Output 3
3295
The following choice is optimal.
