Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は、AtCoder 語を勉強しています。
高橋君は英単語に対して AtCoder 語の単語を対応させて覚えています。
高橋君は英語における red, blue, green は AtCoder 語ではそれぞれ SSS, FFF, MMM に対応していることを覚えており、他の単語は覚えていません。
英小文字からなる文字列 S が与えられます。S が高橋君が AtCoder 語との対応を覚えている英単語の表記と一致しているならば S に対応している AtCoder 語の単語を、そうでないならば文字列 Unknown を出力してください。
制約
- S は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
問題文中の指示に従い、文字列を出力せよ。
入力例 1
red
出力例 1
SSS
英語における red は AtCoder 語における SSS に対応します。
入力例 2
atcoder
出力例 2
Unknown
Score : 100 points
Problem Statement
Takahashi is learning AtCoderish Language.
He memorizes AtCoderish words corresponding to English words.
He knows that red, blue, and green in English respectively correspond to SSS, FFF, and MMM in AtCoderish, and he knows no other words.
You are given a string S consisting of lowercase English letters. If S equals an English word that Takahashi knows corresponds to an AtCoderish word, output the AtCoderish word corresponding to S; otherwise, output the string Unknown.
Constraints
- S is a string of length between 1 and 10, inclusive, consisting of English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output a string according to the instructions in the problem statement.
Sample Input 1
red
Sample Output 1
SSS
red in English corresponds to SSS in AtCoderish.
Sample Input 2
atcoder
Sample Output 2
Unknown
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
以下の式で計算される値を \mathrm{BMI}[\mathrm{kg}/\mathrm{m}^2] と言います。
- \text{体重}[\mathrm{kg}] \div \text{身長}[\mathrm{m}] \div \text{身長}[\mathrm{m}]
日本では、\mathrm{BMI} が 25 \; \mathrm{kg}/\mathrm{m}^2 以上の人は肥満とされます。
身長 H[\mathrm{cm}]、体重 W[\mathrm{kg}] の人が日本で肥満とされるかどうかを判定してください。
制約
- 1 \leq H \leq 300
- 1 \leq W \leq 300
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
出力
身長 H[\mathrm{cm}]、体重 W[\mathrm{kg}] の人が日本で肥満とされるならば Yes を、そうでないならば No を 1 行で出力せよ。
入力例 1
180 60
出力例 1
No
身長が 180 \; \mathrm{cm}=1.8 \; \mathrm{m} なので、\mathrm{BMI} は 60 \; \mathrm{kg} \div 1.8 \; \mathrm{m} \div 1.8 \; \mathrm{m}=18.5\dots \; \mathrm{kg}/\mathrm{m}^2 となり、この人は肥満とはされません。
入力例 2
182 188
出力例 2
Yes
入力例 3
180 81
出力例 3
Yes
Score : 100 points
Problem Statement
The value calculated by the following formula is called \mathrm{BMI}[\mathrm{kg}/\mathrm{m}^2]. (\div denotes division.)
- \text{Weight}[\mathrm{kg}] \div \text{Height}[\mathrm{m}] \div \text{Height}[\mathrm{m}]
In Japan, a person whose \mathrm{BMI} is 25 \; \mathrm{kg}/\mathrm{m}^2 or more is considered obese.
Determine whether a person with height H[\mathrm{cm}] and weight W[\mathrm{kg}] is considered obese in Japan.
Constraints
- 1 \leq H \leq 300
- 1 \leq W \leq 300
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W
Output
Output Yes on one line if a person with height H[\mathrm{cm}] and weight W[\mathrm{kg}] is considered obese in Japan, and No otherwise.
Sample Input 1
180 60
Sample Output 1
No
Since the height is 180 \; \mathrm{cm}=1.8 \; \mathrm{m}, the \mathrm{BMI} is 60 \; \mathrm{kg} \div 1.8 \; \mathrm{m} \div 1.8 \; \mathrm{m}=18.5\dots \; \mathrm{kg}/\mathrm{m}^2, so this person is not considered obese.
Sample Input 2
182 188
Sample Output 2
Yes
Sample Input 3
180 81
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
26 個のキーが数直線上に並んだキーボードがあります。
このキーボードの配列は ABCDEFGHIJKLMNOPQRSTUVWXYZ を並べ替えた文字列 S で表されます。
文字 S_x に対応するキーが座標 x (1 \leq x \leq 26) にあります。 ここで、S_x は S の x 文字目を表します。
あなたはこのキーボードを使って ABCDEFGHIJKLMNOPQRSTUVWXYZ をこの順で右手人差し指で一度だけ入力します。
ある文字を入力するためには、その文字に対応するキーの座標に指を移動させてキーを押す必要があります。
はじめ、指は A に対応するキーの座標にあります。A に対応するキーを押してから、Z に対応するキーを押すまでの指の移動距離の合計として考えられる最小値を求めてください。ただし、
キーを押す動作は移動距離に含まれません。
制約
- S は
ABCDEFGHIJKLMNOPQRSTUVWXYZを並べ替えた文字列である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ABCDEFGHIJKLMNOPQRSTUVWXYZ
出力例 1
25
A のキーを押してから Z のキーを押すまで指を 1 ずつ正の方向に移動させる必要があり、このときの移動距離の合計は 25 です。25 未満の総移動距離でキーを全て入力することはできないため、25 と出力します。
入力例 2
MGJYIZDKSBHPVENFLQURTCWOAX
出力例 2
223
Score : 200 points
Problem Statement
There is a keyboard with 26 keys arranged on a number line.
The arrangement of this keyboard is represented by a string S, which is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ.
The key corresponding to the character S_x is located at coordinate x (1 \leq x \leq 26). Here, S_x denotes the x-th character of S.
You will use this keyboard to input ABCDEFGHIJKLMNOPQRSTUVWXYZ in this order, typing each letter exactly once with your right index finger.
To input a character, you need to move your finger to the coordinate of the key corresponding to that character and press the key.
Initially, your finger is at the coordinate of the key corresponding to A. Find the minimal possible total traveled distance of your finger from pressing the key for A to pressing the key for Z. Here, pressing a key does not contribute to the distance.
Constraints
- S is a permutation of
ABCDEFGHIJKLMNOPQRSTUVWXYZ.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sample Output 1
25
From pressing the key for A to pressing the key for Z, you need to move your finger 1 unit at a time in the positive direction, resulting in a total traveled distance of 25. It is impossible to press all keys with a total traveled distance less than 25, so print 25.
Sample Input 2
MGJYIZDKSBHPVENFLQURTCWOAX
Sample Output 2
223
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数からなる数列が N 個あります。
i \, (1 \leq i \leq N) 番目の数列は L_i 項からなり、i 番目の数列の第 j \, (1 \leq j \leq L_i) 項 は a_{i, j} です。
Q 個のクエリが与えられます。k \, (1 \leq k \leq Q) 番目のクエリでは、整数 s_k, t_k が与えられるので、s_k 番目の数列の第 t_k 項を求めてください。
制約
- 1 \leq N, Q \leq 2 \times 10^5
- L_i \geq 1 \, (1 \leq i \leq N)
- \sum_{i=1}^N L_i \leq 2 \times 10^5
- 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
- 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots
s_Q t_Q
出力
Q 行出力せよ。k \, (1 \leq k \leq Q) 行目には、k 番目のクエリに対する答えを出力せよ。
入力例 1
2 2 3 1 4 7 2 5 9 1 3 2 1
出力例 1
7 5
1 番目の数列は (1, 4, 7)、2 番目の数列は (5, 9) です。
それぞれのクエリに対する答えは次のようになります。
- 1 番目の数列の第 3 項は 7 です。
- 2 番目の数列の第 1 項は 5 です。
入力例 2
3 4 4 128 741 239 901 2 1 1 3 314 159 26535 1 1 2 2 3 3 1 4
出力例 2
128 1 26535 901
Score : 200 points
Problem Statement
There are N sequences of integers.
The i-th (1 \leq i \leq N) sequence has L_i terms; the j-th (1 \leq j \leq L_i) term of the i-th sequence is a_{i, j}.
You are given Q queries. For the k-th (1 \leq k \leq Q) query, given integers s_k and t_k, find the t_k-th term of the s_k-th sequence.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- L_i \geq 1 \, (1 \leq i \leq N)
- \sum_{i=1}^N L_i \leq 2 \times 10^5
- 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
- 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots
s_Q t_Q
Output
Print Q lines. The k-th (1 \leq k \leq Q) line should contain the answer to the k-th query.
Sample Input 1
2 2 3 1 4 7 2 5 9 1 3 2 1
Sample Output 1
7 5
The 1-st sequence is (1, 4, 7) and the 2-nd is (5, 9).
The answer to each query is as follows:
- The 3-rd term of the 1-st sequence is 7.
- The 1-st term of the 2-nd sequence is 5.
Sample Input 2
3 4 4 128 741 239 901 2 1 1 3 314 159 26535 1 1 2 2 3 3 1 4
Sample Output 2
128 1 26535 901
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 =
abcequals the reversal of S_4 =cba, so the second and fourth sticks are considered the same. - S_2 =
abcequals S_6 =abc, so the second and sixth sticks are considered the same. - S_3 =
deequals 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
配点 : 250 点
問題文
長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 1 5 4 9
出力例 1
2
A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。
- 例えば 2 要素目の 1 、 5 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
- このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。
入力例 2
6 5 1 1 1 1 1 1
出力例 2
0
入力例 3
8 3 31 43 26 6 18 36 22 13
出力例 3
18
Score : 250 points
Problem Statement
You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.
Constraints
- All inputs are integers.
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 1 5 4 9
Sample Output 1
2
Consider removing exactly two elements from A=(3,1,5,4,9).
- For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
- In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.
Sample Input 2
6 5 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
8 3 31 43 26 6 18 36 22 13
Sample Output 3
18
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
頂点数 2^{10^{100}}-1 の完全二分木があり、頂点には 1,2,...,2^{10^{100}}-1 の番号がついています。
頂点 1 が根であり、各 1\leq i < 2^{10^{100}-1} について、頂点 i は 頂点 2i を左の子、頂点 2i+1 を右の子として持ちます。
高橋君は最初頂点 X におり、N 回移動を行います。移動は文字列 S により表され、i 回目の移動は次のように行います。
- S の i 番目の文字が
Uのとき、今いる頂点の親に移動する - S の i 番目の文字が
Lのとき、今いる頂点の左の子に移動する - S の i 番目の文字が
Rのとき、今いる頂点の右の子に移動する
N 回の移動後に高橋君がいる頂点の番号を求めてください。なお、答えが 10^{18} 以下になるような入力のみが与えられます。
制約
- 1 \leq N \leq 10^6
- 1 \leq X \leq 10^{18}
- N,X は整数
- S は長さ N であり、
U,L,Rのみからなる - 高橋君が根にいるとき、親に移動しようとすることはない
- 高橋君が葉にいるとき、子に移動しようとすることはない
- 高橋君が N 回の移動後にいる頂点の番号は 10^{18} 以下である
入力
入力は以下の形式で標準入力から与えられる。
N X S
出力
答えを出力せよ。
入力例 1
3 2 URL
出力例 1
6
完全二分木は次のような構造をしています。

各移動により、高橋君がいる頂点の番号は 2 \to 1 \to 3 \to 6 と変化します。
入力例 2
4 500000000000000000 RRUU
出力例 2
500000000000000000
移動の途中過程において、高橋君がいる頂点の番号が 10^{18} を超えることもあります。
入力例 3
30 123456789 LRULURLURLULULRURRLRULRRRUURRU
出力例 3
126419752371
Score : 400 points
Problem Statement
There is a perfect binary tree with 2^{10^{100}}-1 vertices, numbered 1,2,...,2^{10^{100}}-1.
Vertex 1 is the root. For each 1\leq i < 2^{10^{100}-1}, Vertex i has two children: Vertex 2i to the left and Vertex 2i+1 to the right.
Takahashi starts at Vertex X and performs N moves, represented by a string S. The i-th move is as follows.
- If the i-th character of S is
U, go to the parent of the vertex he is on now. - If the i-th character of S is
L, go to the left child of the vertex he is on now. - If the i-th character of S is
R, go to the right child of the vertex he is on now.
Find the index of the vertex Takahashi will be on after N moves. In the given cases, it is guaranteed that the answer is at most 10^{18}.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq X \leq 10^{18}
- N and X are integers.
- S has a length of N and consists of
U,L, andR. - When Takahashi is at the root, he never attempts to go to the parent.
- When Takahashi is at a leaf, he never attempts to go to a child.
- The index of the vertex Takahashi is on after N moves is at most 10^{18}.
Input
Input is given from Standard Input in the following format:
N X S
Output
Print the answer.
Sample Input 1
3 2 URL
Sample Output 1
6
The perfect binary tree has the following structure.

In the three moves, Takahashi goes 2 \to 1 \to 3 \to 6.
Sample Input 2
4 500000000000000000 RRUU
Sample Output 2
500000000000000000
During the process, Takahashi may be at a vertex whose index exceeds 10^{18}.
Sample Input 3
30 123456789 LRULURLURLULULRURRLRULRRRUURRU
Sample Output 3
126419752371
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2 行 L 列のマス目があります。 上から i 行目 (i\in\lbrace1,2\rbrace)、左から j 列目 (1\leq j\leq L)のマス目を (i,j) で表します。 (i,j) には整数 x _ {i,j} が書かれています。
x _ {1,j}=x _ {2,j} であるような整数 j の個数を求めてください。
ただし、x _ {i,j} の情報は (x _ {1,1},x _ {1,2},\ldots,x _ {1,L}) と (x _ {2,1},x _ {2,2},\ldots,x _ {2,L}) をそれぞれ連長圧縮した、長さ N _ 1 の列 ((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1})) と長さ N _ 2 の列 ((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2})) として与えられます。
ここで、列 A の連長圧縮とは、A の要素 v _ i と正整数 l _ i の組 (v _ i,l _ i) の列であって、次の操作で得られるものです。
- A を異なる要素が隣り合っている部分で分割する。
- 分割した各列 B _ 1,B _ 2,\ldots,B _ k に対して、v _ i を B _ i の要素、l _ i を B _ i の長さとする。
制約
- 1\leq L\leq 10 ^ {12}
- 1\leq N _ 1,N _ 2\leq 10 ^ 5
- 1\leq v _ {i,j}\leq 10 ^ 9\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- 1\leq l _ {i,j}\leq L\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- v _ {i,j}\neq v _ {i,j+1}\ (i\in\lbrace1,2\rbrace,1\leq j\lt N _ i)
- l _ {i,1}+l _ {i,2}+\cdots+l _ {i,N _ i}=L\ (i\in\lbrace1,2\rbrace)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
L N _ 1 N _ 2
v _ {1,1} l _ {1,1}
v _ {1,2} l _ {1,2}
\vdots
v _ {1,N _ 1} l _ {1,N _ 1}
v _ {2,1} l _ {2,1}
v _ {2,2} l _ {2,2}
\vdots
v _ {2,N _ 2} l _ {2,N _ 2}
出力
答えを 1 行で出力せよ。
入力例 1
8 4 3 1 2 3 2 2 3 3 1 1 4 2 1 3 3
出力例 1
4
マス目は以下の図のようになっています。

x _ {1,j}=x _ {2,j} となるような整数 j は、j=1,2,5,8 の 4 つなので、出力すべき値は 4 です。
入力例 2
10000000000 1 1 1 10000000000 1 10000000000
出力例 2
10000000000
答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。
入力例 3
1000 4 7 19 79 33 463 19 178 33 280 19 255 33 92 34 25 19 96 12 11 19 490 33 31
出力例 3
380
Score : 500 points
Problem Statement
We have a grid with 2 rows and L columns. Let (i,j) denote the square at the i-th row from the top (i\in\lbrace1,2\rbrace) and j-th column from the left (1\leq j\leq L). (i,j) has an integer x _ {i,j} written on it.
Find the number of integers j such that x _ {1,j}=x _ {2,j}.
Here, the description of x _ {i,j} is given to you as the run-length compressions of (x _ {1,1},x _ {1,2},\ldots,x _ {1,L}) and (x _ {2,1},x _ {2,2},\ldots,x _ {2,L}) into sequences of lengths N _ 1 and N _ 2, respectively: ((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1})) and ((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2})).
Here, the run-length compression of a sequence A is a sequence of pairs (v _ i,l _ i) of an element v _ i of A and a positive integer l _ i obtained as follows.
- Split A between each pair of different adjacent elements.
- For each sequence B _ 1,B _ 2,\ldots,B _ k after the split, let v _ i be the element of B _ i and l _ i be the length of B _ i.
Constraints
- 1\leq L\leq 10 ^ {12}
- 1\leq N _ 1,N _ 2\leq 10 ^ 5
- 1\leq v _ {i,j}\leq 10 ^ 9\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- 1\leq l _ {i,j}\leq L\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- v _ {i,j}\neq v _ {i,j+1}\ (i\in\lbrace1,2\rbrace,1\leq j\lt N _ i)
- l _ {i,1}+l _ {i,2}+\cdots+l _ {i,N _ i}=L\ (i\in\lbrace1,2\rbrace)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
L N _ 1 N _ 2
v _ {1,1} l _ {1,1}
v _ {1,2} l _ {1,2}
\vdots
v _ {1,N _ 1} l _ {1,N _ 1}
v _ {2,1} l _ {2,1}
v _ {2,2} l _ {2,2}
\vdots
v _ {2,N _ 2} l _ {2,N _ 2}
Output
Print a single line containing the answer.
Sample Input 1
8 4 3 1 2 3 2 2 3 3 1 1 4 2 1 3 3
Sample Output 1
4
The grid is shown below.

We have four integers j such that x _ {1,j}=x _ {2,j}: j=1,2,5,8. Thus, you should print 4.
Sample Input 2
10000000000 1 1 1 10000000000 1 10000000000
Sample Output 2
10000000000
Note that the answer may not fit into a 32-bit integer.
Sample Input 3
1000 4 7 19 79 33 463 19 178 33 280 19 255 33 92 34 25 19 96 12 11 19 490 33 31
Sample Output 3
380
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
あなたは以下の操作を A に対して 0 回以上好きな回数行うことができます:
- 1\le i \le N - 1 を満たす整数 i を選び、A_i を 1 減らし、A_{i+1} を 1 増やす。
A を狭義単調増加列にするために必要な操作回数の最小値を求めてください。
ただし、答えは 2^{63} 未満になることが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 3\times 10^5
- 1\le N\le 2\times 10^5
- 0\le A_i\le 10^9
- 全てのテストケースにおける N の総和は 6\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。
N A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
4 3 0 1 0 4 4 6 3 5 7 1 2 3 4 5 6 7 10 11 9 1 3 17 19 10 19 17 3
出力例 1
3 5 0 78
1 番目のテストケースについて考えます。
以下のように操作することで 3 回の操作で A を狭義単調増加列にすることができます:
- i=1 を選ぶ。A=(-1,2,0) となる。
- i=2 を選ぶ。A=(-1,1,1) となる。
- i=2 を選ぶ。A=(-1,0,2) となる。
3 回未満の操作で A を狭義単調増加列にすることはできないので、3 を出力してください。
Score : 525 points
Problem Statement
You are given a sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) of length N.
You can perform the following operation on A zero or more times:
- Choose an integer i with 1 \le i \le N - 1, decrease A_i by 1, and increase A_{i+1} by 1.
Find the minimum number of operations required to make A strictly increasing.
It can be proved that the answer is less than 2^{63}.
You are given T test cases; solve each.
Constraints
- 1 \le T \le 3 \times 10^5
- 1 \le N \le 2 \times 10^5
- 0 \le A_i \le 10^9
- The sum of N across all test cases is at most 6 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
The i-th (1 \le i \le T) test case \text{case}_i is given in the following format:
N A_1 A_2 \ldots A_N
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
4 3 0 1 0 4 4 6 3 5 7 1 2 3 4 5 6 7 10 11 9 1 3 17 19 10 19 17 3
Sample Output 1
3 5 0 78
Consider the first test case.
By performing the following operations, A can be made strictly increasing in three operations:
- Choose i=1. A becomes (-1, 2, 0).
- Choose i=2. A becomes (-1, 1, 1).
- Choose i=2. A becomes (-1, 0, 2).
It is impossible to make A strictly increasing in fewer than three operations, so output 3.