A - QQ solver

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

3 文字からなる文字列 S が与えられます。S は、1 以上 9 以下の整数 a, b と文字 x を、axb のように順につなげて得られます。

ab の積を求めてください。

制約

  • S の長さは 3
  • S1 文字目および 3 文字目は 1 以上 9 以下の整数を表す
  • S2 文字目は x

入力

入力は以下の形式で標準入力から与えられる。

S

出力

答えを出力せよ。


入力例 1

3x7

出力例 1

21

3 \times 7 = 21 であるので、これを出力します。


入力例 2

9x9

出力例 2

81

入力例 3

1x1

出力例 3

1

Score : 100 points

Problem Statement

You are given a 3-character string S, which is a concatenation of integers a and b between 1 and 9 (inclusive) and the character x in this order: axb.

Find the product of a and b.

Constraints

  • The length of S is 3.
  • The 1-st and 3-rd characters are digits between 1 and 9 (inclusive).
  • The 2-nd character is x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

3x7

Sample Output 1

21

We have 3 \times 7 = 21, which should be printed.


Sample Input 2

9x9

Sample Output 2

81

Sample Input 3

1x1

Sample Output 3

1
B - Move Right

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

横一列に 4 つのマスが並んでいます。

各文字が 0 または 1 である長さ 4 の文字列 S が与えられます。
Si 文字目が 1 であるとき、左から i 番目のマスには 1 人の人がおり、
Si 文字目が 0 であるとき、左から i 番目のマスには人がいません。

全ての人が一斉に、1 つ右隣のマスへ移動します。この移動により、もともと右端のマスにいた人は消えます。

移動後の各マスに人がいるかどうかを、S と同様のルールで文字列として出力してください。

制約

  • S0, 1 のみからなる長さ 4 の文字列

入力

入力は以下の形式で標準入力から与えられる。

S

出力

長さ 4 の文字列であって、移動後、左から i 番目のマスに人がいるならば i 文字目が 1、いないならば 0 であるようなものを出力せよ。


入力例 1

1011

出力例 1

0101

移動により、左から 1 番目のマスにいた人は左から 2 番目のマスに、
左から 3 番目のマスにいた人は左から 4 番目のマスに移動し、
左から 4 番目のマス(右端のマス)にいた人は消えます。


入力例 2

0000

出力例 2

0000

入力例 3

1111

出力例 3

0111

Score : 100 points

Problem Statement

There are 4 squares lined up horizontally.

You are given a string S of length 4 consisting of 0 and 1.
If the i-th character of S is 1, there is a person in the i-th square from the left;
if the i-th character of S is 0, there is no person in the i-th square from the left.

Now, everyone will move to the next square to the right simultaneously. By this move, the person who was originally in the rightmost square will disappear.

Determine if there will be a person in each square after the move. Print the result as a string in the same format as S. (See also Sample Input / Output for clarity.)

Constraints

  • S is a string of length 4 consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string of length 4 such that the i-th character is 1 if there will be a person in the i-th square from the left after the move, and 0 otherwise.


Sample Input 1

1011

Sample Output 1

0101

After the move, the person who was originally in the 1-st square will move to the 2-nd square,
the person in the 3-rd square to the 4-th square,
and the person in the 4-th square will disappear.


Sample Input 2

0000

Sample Output 2

0000

Sample Input 3

1111

Sample Output 3

0111
C - Pentagon

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

以下の図で表される正五角形 P があります。

P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しいか判定してください。

制約

  • S_1,S_2,T_1,T_2A, B, C, D, E のいずれかの文字
  • S_1 \neq S_2
  • T_1 \neq T_2

入力

入力は以下の形式で標準入力から与えられる。

S_1S_2
T_1T_2

出力

P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しい場合 Yes を、等しくない場合 No を出力せよ。


入力例 1

AC
EC

出力例 1

Yes

P の点 A と点 C を結ぶ線分と、P の点 E と点 C を結ぶ線分の長さは等しいです。


入力例 2

DA
EA

出力例 2

No

P の点 D と点 A を結ぶ線分と、P の点 E と点 A を結ぶ線分の長さは等しくありません。


入力例 3

BD
BD

出力例 3

Yes

Score : 200 points

Problem Statement

A regular pentagon P is shown in the figure below.

Determine whether the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2.

Constraints

  • Each of S_1, S_2, T_1, and T_2 is one of the characters A, B, C, D, and E.
  • S_1 \neq S_2
  • T_1 \neq T_2

Input

The input is given from Standard Input in the following format:

S_1S_2
T_1T_2

Output

If the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2, print Yes; otherwise, print No.


Sample Input 1

AC
EC

Sample Output 1

Yes

The length of the line segment connecting point A and point C of P equals the length of the line segment connecting point E and point C.


Sample Input 2

DA
EA

Sample Output 2

No

The length of the line segment connecting point D and point A of P does not equal the length of the line segment connecting point E and point A.


Sample Input 3

BD
BD

Sample Output 3

Yes
D - Longest Palindrome

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

文字列 S が与えられます。 S の連続する部分文字列のうち、回文であるものの長さの最大値を求めてください。
ただし、S の連続する部分文字列であって回文であるものは常に存在します。

制約

  • S は長さ 2 以上 100 以下の英大文字からなる文字列

入力

入力は以下の形式で標準入力から与えられる。

S

出力

答えを出力せよ。


入力例 1

TOYOTA

出力例 1

5

TOYOTA の連続する部分文字列 TOYOT は長さ 5 の回文です。
TOYOTA の唯一の長さ 6 の連続する部分文字列 TOYOTA は回文でないので、5 を出力します。


入力例 2

ABCDEFG

出力例 2

1

すべての長さ 1 の連続する部分文字列は回文です。


入力例 3

AAAAAAAAAA

出力例 3

10

Score : 200 points

Problem Statement

You are given a string S. Find the maximum length of a contiguous substring of S that is a palindrome.
Note that there is always a contiguous substring of S that is a palindrome.

Constraints

  • S is a string of length between 2 and 100, 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

TOYOTA

Sample Output 1

5

TOYOT, a contiguous substring of TOYOTA, is a palindrome of length 5.
TOYOTA, the only length-6 contiguous substring of TOYOTA, is not a palindrome, so print 5.


Sample Input 2

ABCDEFG

Sample Output 2

1

Every contiguous substring of length 1 is a palindrome.


Sample Input 3

AAAAAAAAAA

Sample Output 3

10
E - Sowing Stones

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

1 から N まで番号がつけられた N 個のマスが一列に並んでいます。最初、 M 個のマスに石が入っており、マス X_i には A_i(1 \leq i \leq M) の石が入っています。

あなたは以下の操作を好きな回数( 0 回でもよい)行うことができます。

  • マス i (1 \leq i \leq N-1) に石があるとき、マス i から石を 1 つマス i+1 に移動させる。

N 個のマスすべてに石がちょうど 1 個ずつ入っている状態にするために必要な操作回数の最小値を求めてください。ただし、不可能な場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

出力

答えを出力せよ。


入力例 1

5 2
1 4
3 2

出力例 1

4

以下の 4 回の操作で、5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることができます。

  • マス 1 の石を 1 つマス 2 に移動させる。
  • マス 2 の石を 1 つマス 3 に移動させる。
  • マス 4 の石を 1 つマス 5 に移動させる。
  • マス 1 の石を 1 つマス 2 に移動させる。

また、3 回以下の操作では 5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、4 を出力します。


入力例 2

10 3
1 4 8
4 2 4

出力例 2

-1

どのように操作を行っても 10 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、-1 を出力します。

Score : 300 points

Problem Statement

There are N cells numbered from 1 to N in a row. Initially, M cells contain stones, and cell X_i contains A_i stones (1 \leq i \leq M).

You can perform the following operation any number of times (possibly zero):

  • If cell i (1 \leq i \leq N-1) contains a stone, move one stone from cell i to cell i+1.

Find the minimum number of operations required to reach a state where each of the N cells contains exactly one stone. If it is impossible, print -1.

Constraints

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

Output

Print the answer.


Sample Input 1

5 2
1 4
3 2

Sample Output 1

4

You can reach a state where each of the five cells contains exactly one stone with four operations as follows:

  • Move one stone from cell 1 to cell 2.
  • Move one stone from cell 2 to cell 3.
  • Move one stone from cell 4 to cell 5.
  • Move one stone from cell 1 to cell 2.

It is impossible to achieve the goal in three or fewer operations. Therefore, print 4.


Sample Input 2

10 3
1 4 8
4 2 4

Sample Output 2

-1

No matter how you perform the operations, you cannot reach a state where all ten cells contain exactly one stone. Therefore, print -1.

F - One More aab aba baa

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

文字列 S の各文字を並べ替えて作ることが可能な文字列を辞書順にすべて列挙したとき、前から K 番目にくる文字列を求めてください。

「各文字を並べ替えて作ることが可能な文字列」とは? 「文字列 A が文字列 B の各文字を並べ替えて作ることが可能な文字列である」とは、任意の文字が文字列 A と文字列 B に同数含まれるということを指します。

制約

  • 1 \le |S| \le 8
  • S は英小文字のみからなる
  • S の各文字を並べ替えてできる文字列は K 種類以上存在する

入力

入力は以下の形式で標準入力から与えられる。

S K

出力

答えを出力せよ。


入力例 1

aab 2

出力例 1

aba

文字列 aab の各文字を並べ替えて作ることが可能な文字列は \{ aab, aba, baa \}3 つですが、このうち辞書順で前から 2 番目にくるものは aba です。


入力例 2

baba 4

出力例 2

baab

入力例 3

ydxwacbz 40320

出力例 3

zyxwdcba

Score : 300 points

Problem Statement

Find the K-th lexicographically smallest string among the strings that are permutations of a string S.

What is a permutation of a string?A string A is said to be a permutation of a string B when any character occurs the same number of times in A and B.

Constraints

  • 1 \le |S| \le 8
  • S consists of lowercase English letters.
  • There are at least K distinct strings that are permutations of S.

Input

Input is given from Standard Input in the following format:

S K

Output

Print the answer.


Sample Input 1

aab 2

Sample Output 1

aba

There are three permutations of a string aab: \{ aab, aba, baa \}. The 2-nd lexicographically smallest of them is aba.


Sample Input 2

baba 4

Sample Output 2

baab

Sample Input 3

ydxwacbz 40320

Sample Output 3

zyxwdcba
G - Logical Filling

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

., o, ? のみからなる長さ N の文字列 S が与えられます。 全ての ? をそれぞれ . または o で置き換えて得られる文字列のうち、以下の条件を全て満たすものの集合を X とします。

  • o の個数がちょうど K
  • o が連続しない

X が空集合でないことは保証されます。

以下を満たす、長さ N の文字列 T を出力して下さい。ここで、T の左から i 番目の文字を T_i と表記します。

  • X に含まれる全ての文字列の i 文字目が . である場合: T_i= .
  • X に含まれる全ての文字列の i 文字目が o である場合: T_i= o
  • i 文字目が . である文字列も o である文字列も X に含まれている場合: T_i=?

制約

  • 1\leq N\leq 2\times 10^5
  • 0\leq K
  • S., o, ? のみからなる長さ N の文字列
  • X は空集合ではない
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N K
S

出力

答えを出力せよ。


入力例 1

4 2
o???

出力例 1

o.??

o.o.o..o の二つの文字列から X はなります。

X に含まれる全ての文字列の 1 文字目は o なので、 T_1o です。

X に含まれる全ての文字列の 2 文字目は . なので、 T_2. です。

X に含まれる文字列の 3 文字目としては .o も考えられるので、 T_3? です。


入力例 2

5 2
?????

出力例 2

?????

入力例 3

7 3
.o???o.

出力例 3

.o.o.o.

Score : 400 points

Problem Statement

You are given a string S of length N consisting of the characters ., o, and ?. Among the strings that can be obtained by replacing every ? in S independently with either . or o, let X be the set of strings that satisfy all of the following conditions:

  • The number of os is exactly K.
  • No two os are adjacent.

It is guaranteed that X is non‑empty.

Print a string T of length N that satisfies the following (let T_i denote the i‑th character of T):

  • If the i‑th character of every string in X is ., then T_i= ..
  • If the i‑th character of every string in X is o, then T_i= o.
  • If X contains both a string whose i‑th character is . and a string whose i‑th character is o, then T_i= ?.

Constraints

  • 1 \le N \le 2 \times 10^{5}
  • 0 \le K
  • S is a string of length N consisting of ., o, ?.
  • X is non‑empty.
  • All given numerical values are integers.

Input

The input is given from Standard Input in the following format:

N K
S

Output

Print the answer.


Sample Input 1

4 2
o???

Sample Output 1

o.??

The set X consists of the two strings o.o. and o..o.

  • The 1st character of every string in X is o, so T_1 is o.
  • The 2nd character of every string in X is ., so T_2 is ..
  • The 3rd character of a string in X can be either . or o, so T_3 is ?.

Sample Input 2

5 2
?????

Sample Output 2

?????

Sample Input 3

7 3
.o???o.

Sample Output 3

.o.o.o.
H - Set Add Query

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

全ての要素が 0 で初期化された長さ N の整数列 A=(A_1,A_2,\ldots,A_N) があります。また、集合 S があります。はじめ S は空です。

以下の Q 個のクエリを順に行います。Q 個のクエリを全て処理した後の数列 A の各要素の値を求めてください。 i 番目のクエリは以下の形式です。

  • 整数 x_i が与えられる。整数 x_iS に含まれる場合、S から x_i を削除する。そうでない場合、Sx_i を追加する。次に、j=1,2,\ldots,N について、j\in S ならば A_j|S| を加算する。

なお、|S| は集合 S の要素数を意味します。例えば S=\lbrace 3,4,7\rbrace のとき、|S|=3 です。

制約

  • 1\leq N,Q\leq 2\times10^5
  • 1\leq x_i\leq N
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
x_1 x_2 \ldots x_Q

出力

クエリを全て処理した後の数列 A を以下の形式で出力せよ。

A_1 A_2 \ldots A_N

入力例 1

3 4
1 3 3 2

出力例 1

6 2 2

1 番目のクエリでは、S1 を追加し、S=\lbrace 1\rbrace となります。その後、A_1|S|=1 を加算します。A=(1,0,0) となります。

2 番目のクエリでは、S3 を追加し、S=\lbrace 1,3\rbrace となります。その後、A_1,A_3|S|=2 を加算します。A=(3,0,2) となります。

3 番目のクエリでは、S から 3 を削除し、S=\lbrace 1\rbrace となります。その後、A_1|S|=1 を加算します。A=(4,0,2) となります。

4 番目のクエリでは、S2 を追加し、S=\lbrace 1,2\rbrace となります。その後、A_1,A_2|S|=2 を加算します。A=(6,2,2) となります。

最終的に、A=(6,2,2) となります。


入力例 2

4 6
1 2 3 2 4 2

出力例 2

15 9 12 7

Score: 500 points

Problem Statement

There is an integer sequence A=(A_1,A_2,\ldots,A_N) of length N, where all elements are initially set to 0. Also, there is a set S, which is initially empty.

Perform the following Q queries in order. Find the value of each element in the sequence A after processing all Q queries. The i-th query is in the following format:

  • An integer x_i is given. If the integer x_i is contained in S, remove x_i from S. Otherwise, insert x_i to S. Then, for each j=1,2,\ldots,N, add |S| to A_j if j\in S.

Here, |S| denotes the number of elements in the set S. For example, if S=\lbrace 3,4,7\rbrace, then |S|=3.

Constraints

  • 1\leq N,Q\leq 2\times10^5
  • 1\leq x_i\leq N
  • All given numbers are integers.

Input

The input is given from Standard Input in the following format:

N Q
x_1 x_2 \ldots x_Q

Output

Print the sequence A after processing all queries in the following format:

A_1 A_2 \ldots A_N

Sample Input 1

3 4
1 3 3 2

Sample Output 1

6 2 2

In the first query, 1 is inserted to S, making S=\lbrace 1\rbrace. Then, |S|=1 is added to A_1. The sequence becomes A=(1,0,0).

In the second query, 3 is inserted to S, making S=\lbrace 1,3\rbrace. Then, |S|=2 is added to A_1 and A_3. The sequence becomes A=(3,0,2).

In the third query, 3 is removed from S, making S=\lbrace 1\rbrace. Then, |S|=1 is added to A_1. The sequence becomes A=(4,0,2).

In the fourth query, 2 is inserted to S, making S=\lbrace 1,2\rbrace. Then, |S|=2 is added to A_1 and A_2. The sequence becomes A=(6,2,2).

Eventually, the sequence becomes A=(6,2,2).


Sample Input 2

4 6
1 2 3 2 4 2

Sample Output 2

15 9 12 7
I - Skate

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

HW 列のグリッド型のスケート場があります。上から i 行目、左から j 行目のマスを (i,j) で表します。

スケート場には N 個の障害物があり、i 個目の障害物は (X_i,Y_i) に置かれています。

高橋君は 1 回の移動において、上下左右いずれかの方向を選んで、障害物に当たるまで進み続けます。
障害物に当たるとその 1 つ手前のマスで停止します。 なお、スケート場の周りは崖になっており、障害物に当たらないような移動は禁止とします。

高橋君ははじめ (s_x,s_y) にいて、何回か移動することで (g_x,g_y) で停止したいと考えています。

(g_x,g_y) へ辿り着くには最小で何回の移動が必要ですか?辿り着けないときはその旨を伝えてください。

制約

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • i\neq j ならば、(X_i,Y_i)\neq (X_j,Y_j)
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

(g_x,g_y) へ辿り着くには最小で何回の移動が必要か出力せよ。
ただし、辿り着けないならば -1 と出力せよ。


入力例 1

7 8 7
3 4
5 6
1 4
2 1
2 8
4 5
5 7
6 2
6 6

出力例 1

4

図は、(s_x,s_y)S で、(g_x,g_y)G で表しています。
(3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6) と移動すると、4 回の移動で (g_x,g_y) に辿り着くことができます。


入力例 2

4 6 2
3 2
3 5
4 5
2 5

出力例 2

-1

(g_x,g_y) で停止する必要があります。
通過しただけでは (g_x,g_y) へ辿り着いたとみなされないことに注意してください。


入力例 3

1 10 1
1 5
1 1
1 7

出力例 3

-1


左を選んで進むと、高橋君は (g_x,g_y) を通過したのちに崖に落ちてしまいます。
スケート場の周りは崖になっており、障害物に当たらないような移動は禁止されていることに注意してください。

Score : 500 points

Problem Statement

There is a skating rink represented by a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

The skating rink has N obstacles. The i-th obstacle is placed at (X_i,Y_i).

In a single move, Takahashi chooses one of the four directions, up, down, left, or right, and keeps moving until he hits an obstacle.
When he hits an obstacle, he stops at the square right before the obstacle. Since the skating rink is surrounded by cliffs, it is prohibited to start a move in which he will never hit an obstacle.

Takahashi is initially at (s_x,s_y). He wants to make some number of moves to stop at (g_x,g_y).

Find the minimum number of moves required to end up at (g_x, g_y). If it is not possible, report the fact.

Constraints

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • If i\neq j, then (X_i,Y_i)\neq (X_j,Y_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum number of moves required to end up at (g_x,g_y).
If it is impossible to end up there, print -1.


Sample Input 1

7 8 7
3 4
5 6
1 4
2 1
2 8
4 5
5 7
6 2
6 6

Sample Output 1

4

In the figure, (s_x,s_y) is denoted by S and (g_x,g_y) is denoted by G.
By moving as (3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6), he can end up at (g_x,g_y) with 4 moves.


Sample Input 2

4 6 2
3 2
3 5
4 5
2 5

Sample Output 2

-1

He must stop at (g_x,g_y).
Note that just passing through (g_x,g_y) is not considered to be ending up at the goal.


Sample Input 3

1 10 1
1 5
1 1
1 7

Sample Output 3

-1


If he chooses to move to the left, Takahashi will fall down the cliff after passing through (g_x,g_y).
Note that it is prohibited to start a move in which he will never hit an obstacle, as the skating rink is surrounded by cliffs.