A - Rotate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

長さ 3 の文字列 S が与えられます。
S の先頭の文字を S の末尾に移動して得られる文字列 S' を出力してください。

制約

  • S は英小文字のみからなる長さ 3 の文字列である

入力

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

S

出力

S' を出力せよ。


入力例 1

abc

出力例 1

bca

abc の先頭の文字 a を末尾に移動すると bca となります。


入力例 2

aab

出力例 2

aba

Score : 100 points

Problem Statement

Given is a string S of length 3.
Move the first character of S to the end of S and print the resulting string S'.

Constraints

  • S is a string of length 3 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print S'.


Sample Input 1

abc

Sample Output 1

bca

Moving the first character a of the string abc results in bca.


Sample Input 2

aab

Sample Output 2

aba
B - Visibility

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

H 行、横 W 列のマス目があり、いくつかのマスには障害物が置かれています。
上から i 番目、左から j 番目のマスをマス (i, j) と表すことにします。
H 個の文字列 S_1, S_2, S_3, \dots, S_H が与えられます。S_ij 文字目はマス (i, j) の状態を表し、# なら障害物が置かれていることを、. なら障害物が置かれていないことを表します。
このマス目上のあるマスからあるマスが見えるとは、2 つのマスが同じ行または列にあり、2 つのマスの間 (2 つのマス自身を含む) に障害物が 1 つも置かれていないことを意味します。
このマス目上のマスであって、マス (X, Y) から見えるもの (マス (X, Y) 自身を含む) の数を求めてください。

制約

  • 1 \le H \le 100
  • 1 \le W \le 100
  • 1 \le X \le H
  • 1 \le Y \le W
  • S_i. および # のみからなる長さ W の文字列
  • マス (X, Y) に障害物は置かれていない

入力

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

H W X Y
S_1
S_2
S_3
\hspace{3pt} \vdots
S_H

出力

答えを出力せよ。


入力例 1

4 4 2 2
##..
...#
#.#.
.#.#

出力例 1

4

以下がマス (2, 2) から見えるマスです。

  • マス (2, 1)
  • マス (2, 2)
  • マス (2, 3)
  • マス (3, 2)

入力例 2

3 5 1 4
#....
#####
....#

出力例 2

4

行または列が同じでも、間に障害物があるようなマスは見えません。


入力例 3

5 5 4 2
.#..#
#.###
##...
#..#.
#.###

出力例 3

3

Score : 200 points

Problem Statement

We have a grid of H horizontal rows and W vertical columns, where some of the squares contain obstacles.
Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
You are given H strings S_1, S_2, S_3, \dots, S_H. The j-th character of S_i describes the square (i, j); # means the square contains an obstacle, and . means it does not.
We say a square is visible from another when it is on the same row or the same column, and there is no obstacle between them (including themselves).
Print the number of squares visible from the square (X, Y) (including (X, Y) itself).

Constraints

  • 1 \le H \le 100
  • 1 \le W \le 100
  • 1 \le X \le H
  • 1 \le Y \le W
  • S_i is a string of length W consisting of . and #.
  • The square (X, Y) does not contain an obstacle.

Input

Input is given from Standard Input in the following format:

H W X Y
S_1
S_2
S_3
\hspace{3pt} \vdots
S_H

Output

Print the answer.


Sample Input 1

4 4 2 2
##..
...#
#.#.
.#.#

Sample Output 1

4

The squares visible from the square (2, 2) are:

  • (2, 1)
  • (2, 2)
  • (2, 3)
  • (3, 2)

Sample Input 2

3 5 1 4
#....
#####
....#

Sample Output 2

4

Even if two squares are on the same row or the same column, they are not visible from each other when there are obstacles between them.


Sample Input 3

5 5 4 2
.#..#
#.###
##...
#..#.
#.###

Sample Output 3

3
C - ORXOR

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の数列 A が与えられます。
この数列を、1 つ以上の空でない連続した区間に分けます。
その後、分けた各区間で、区間内の数のビット単位 \mathrm{OR} を計算します。
こうして得られた全ての値のビット単位 \mathrm{XOR} として考えられる最小値を求めてください。

ビット単位 \mathrm{OR} 演算とは

整数 A, B のビット単位 \mathrm{OR}A\ \mathrm{OR}\ B は以下のように定義されます。

  • A\ \mathrm{OR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも片方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{OR}\ 5 = 7 となります (二進表記すると: 011\ \mathrm{OR}\ 101 = 111)。
一般に k 個の整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{OR}(\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

ビット単位 \mathrm{XOR} 演算とは

整数 A, B のビット単位 \mathrm{XOR}A\ \mathrm{XOR}\ B は、以下のように定義されます。

  • A\ \mathrm{XOR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{XOR}\ 5 = 6 となります (二進表記すると: 011\ \mathrm{XOR}\ 101 = 110)。
一般に k 個以上の整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 1 \le N \le 20
  • 0 \le A_i \lt 2^{30}
  • 入力に含まれる値は全て整数である

入力

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

N
A_1 A_2 A_3 \dots A_N

出力

答えを出力せよ。


入力例 1

3
1 5 7

出力例 1

2

[1, 5, 7][1, 5][7]2 つの区間に分けると、それぞれの区間内の数のビット単位 \mathrm{OR}5, 7 となり、その \mathrm{XOR}2 です。
これより小さくすることはできないので、2 を出力します。


入力例 2

3
10 10 10

出力例 2

0

[10][10, 10] に分けるとよいです。


入力例 3

4
1 3 3 1

出力例 3

0

[1, 3][3, 1] に分けるとよいです。

Score : 300 points

Problem Statement

Given is a number sequence A of length N.
Let us divide this sequence into one or more non-empty contiguous intervals.
Then, for each of these intervals, let us compute the bitwise \mathrm{OR} of the numbers in it.
Find the minimum possible value of the bitwise \mathrm{XOR} of the values obtained in this way.

What is bitwise \mathrm{OR}?

The bitwise \mathrm{OR} of integers A and B, A\ \mathrm{OR}\ B, is defined as follows:

  • When A\ \mathrm{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 A and B is 1, and 0 otherwise.
For example, we have 3\ \mathrm{OR}\ 5 = 7 (in base two: 011\ \mathrm{OR}\ 101 = 111).
Generally, the bitwise \mathrm{OR} of k integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots p_k.

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of integers A and B, A\ \mathrm{XOR}\ B, is defined as follows:

  • When A\ \mathrm{XOR}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3\ \mathrm{XOR}\ 5 = 6 (in base two: 011\ \mathrm{XOR}\ 101 = 110).
Generally, the bitwise \mathrm{XOR} of k integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots p_k.

Constraints

  • 1 \le N \le 20
  • 0 \le A_i \lt 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_N

Output

Print the answer.


Sample Input 1

3
1 5 7

Sample Output 1

2

If we divide [1, 5, 7] into [1, 5] and [7], their bitwise \mathrm{OR}s are 5 and 7, whose \mathrm{XOR} is 2.
It is impossible to get a smaller result, so we print 2.


Sample Input 2

3
10 10 10

Sample Output 2

0

We should divide this sequence into [10] and [10, 10].


Sample Input 3

4
1 3 3 1

Sample Output 3

0

We should divide this sequence into [1, 3] and [3, 1].

D - Opposite

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

\mathrm{x} 軸の正の向きを右、\mathrm{y} 軸の正の向きを上とする 2 次元座標平面上に、p_0, p_1, p_2, \dots, p_{N - 1}N 個の頂点からなる正 N 角形があります。
ここで N は偶数であることが保証され、頂点 p_0, p_1, p_2, \dots, p_{N - 1} はこの順に反時計回りに並んでいます。
p_i の座標を (x_i, y_i) とします。
x_0, y_0, x_{\frac{N}{2}}, y_{\frac{N}{2}} が与えられるので、x_1, y_1 を求めてください。

制約

  • 4 \le N \le 100
  • N は偶数
  • 0 \le x_0, y_0 \le 100
  • 0 \le x_{\frac{N}{2}}, y_{\frac{N}{2}} \le 100
  • (x_0, y_0) \neq (x_{\frac{N}{2}}, y_{\frac{N}{2}})
  • 入力に含まれる値は全て整数である

入力

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

N
x_0 y_0
x_{\frac{N}{2}} y_{\frac{N}{2}}

出力

x_1, y_1 をこの順に空白区切りで出力せよ。
出力されたそれぞれの値について、想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解と判定される。


入力例 1

4
1 1
2 2

出力例 1

2.00000000000 1.00000000000

p_0 = (1, 1), p_2 = (2, 2) という情報が与えられています。
p_0, p_1, p_2, p_3 が正方形をなし、反時計回りに並んでいるという情報から残りの頂点の座標は一意に定まり、以下のようになります。

  • p_1 = (2, 1)
  • p_3 = (1, 2)

入力例 2

6
5 3
7 4

出力例 2

5.93301270189 2.38397459622

Score : 400 points

Problem Statement

On a two-dimensional coordinate plane where the \mathrm{x} axis points to the right and the \mathrm{y} axis points up, we have a regular N-gon with N vertices p_0, p_1, p_2, \dots, p_{N - 1}.
Here, N is guaranteed to be even, and the vertices p_0, p_1, p_2, \dots, p_{N - 1} are in counter-clockwise order.
Let (x_i, y_i) denotes the coordinates of p_i.
Given x_0, y_0, x_{\frac{N}{2}}, and y_{\frac{N}{2}}, find x_1 and y_1.

Constraints

  • 4 \le N \le 100
  • N is even.
  • 0 \le x_0, y_0 \le 100
  • 0 \le x_{\frac{N}{2}}, y_{\frac{N}{2}} \le 100
  • (x_0, y_0) \neq (x_{\frac{N}{2}}, y_{\frac{N}{2}})
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_0 y_0
x_{\frac{N}{2}} y_{\frac{N}{2}}

Output

Print x_1 and y_1 in this order, with a space in between.
Your output is considered correct when, for each value printed, the absolute or relative error from our answer is at most 10^{-5}.


Sample Input 1

4
1 1
2 2

Sample Output 1

2.00000000000 1.00000000000

We are given p_0 = (1, 1) and p_2 = (2, 2).
The fact that p_0, p_1, p_2, and p_3 form a square and they are in counter-clockwise order uniquely determines the coordinates of the other vertices, as follows:

  • p_1 = (2, 1)
  • p_3 = (1, 2)

Sample Input 2

6
5 3
7 4

Sample Output 2

5.93301270189 2.38397459622
E - Traveler

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

数直線上にボール 1 からボール N までの N 個のボールがあります。
ボール i は座標 X_i にあります。
各ボールには 1 以上 N 以下の整数で表される色がついていて、ボール i の色は整数 C_i で表されます。
今座標 0 にいるあなたは、毎秒 1 の速さで数直線上を動き、全てのボールを回収してから再び座標 0 に戻ります。
このとき、ボールの色を表す整数を回収順に並べた時に広義単調増加となっている必要があります。
ボールを回収するにはボールと同じ座標にいる必要がありますが、ボールを回収できる時に必ずしも回収する必要はありません。
座標 0 を出発してから、全てのボールを回収して再び座標 0 に戻るまでにかかる時間の最小値を求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • |X_i| \le 10^9
  • X_i \neq X_j (i \neq j)
  • X_i \neq 0
  • 1 \le C_i \le N
  • 入力に含まれる値は全て整数である

入力

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

N
X_1 C_1
X_2 C_2
X_3 C_3
\hspace{15pt} \vdots
X_N C_N

出力

答え [秒] を出力せよ。


入力例 1

5
2 2
3 1
1 3
4 2
5 3

出力例 1

12

以下のように行動するのが最適です。

  • 3 秒かけて座標 3 に移動し、ボール 2 を回収する
  • 1 秒かけて座標 2 に移動し、ボール 1 を回収する
  • 2 秒かけて座標 4 に移動し、ボール 4 を回収する
  • 1 秒かけて座標 5 に移動し、ボール 5 を回収する
  • 4 秒かけて座標 1 に移動し、ボール 3 を回収する
  • 1 秒かけて座標 0 に戻る

ボールの色を表す整数を回収順に並べると 1, 2, 2, 3, 3 と広義単調増加になっています。


入力例 2

9
5 5
-4 4
4 3
6 3
-5 5
-3 2
2 2
3 3
1 4

出力例 2

38

Score : 500 points

Problem Statement

There are N balls, called Ball 1 through N, on a number line.
Ball i is at the coordinate X_i.
Each ball has a color represented by an integer ID between 1 and N (inclusive); the ID of the color of Ball i is C_i.
You are now at the coordinate 0. You will collect all the balls by moving along the line at the speed of 1 per second, and then return to the coordinate 0.
Here, you have to collect the balls in a non-descending order of their IDs.
When collecting a ball, you have to be at the coordinate of that ball, but it is not mandatory to collect it when you are there.
Find the minimum time needed to start at the coordinate 0, collect all the balls, and return to the coordinate 0.

Constraints

  • 1 \le N \le 2 \times 10^5
  • |X_i| \le 10^9
  • X_i \neq X_j (i \neq j)
  • X_i \neq 0
  • 1 \le C_i \le N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 C_1
X_2 C_2
X_3 C_3
\hspace{15pt} \vdots
X_N C_N

Output

Print the number of seconds needed.


Sample Input 1

5
2 2
3 1
1 3
4 2
5 3

Sample Output 1

12

The optimal strategy is:

  • spend 3 seconds to reach the coordinate 3 and collect Ball 2;
  • spend 1 second to reach the coordinate 2 and collect Ball 1;
  • spend 2 seconds to reach the coordinate 4 and collect Ball 4;
  • spend 1 second to reach the coordinate 5 and collect Ball 5;
  • spend 4 seconds to reach the coordinate 1 and collect Ball 3;
  • spend 1 second to return to the coordinate 0.

Here, we collected the balls in a non-descending order of their IDs: 1, 2, 2, 3, 3.


Sample Input 2

9
5 5
-4 4
4 3
6 3
-5 5
-3 2
2 2
3 3
1 4

Sample Output 2

38
F - Construct a Palindrome

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の、単純とは限らない連結な無向グラフがあります。
i は頂点 A_i と頂点 B_i を結んでおり、文字 C_i が書かれています。
頂点 1 から頂点 N へのパス (同じ辺や頂点を複数回通っても構わない) を 1 つ選び、通る辺に書かれている文字を順に並べて文字列を作ります。
この文字列が回文になることはあるかを判定し、あるならばそのような回文の長さとして考えられる最小値を求めてください。

制約

  • 2 \le N \le 1000
  • 1 \le M \le 1000
  • 1 \le A_i \le N
  • 1 \le B_i \le N
  • C_i は英小文字
  • 与えられるグラフは連結である

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
A_3 B_3 C_3
\hspace{25pt} \vdots
A_M B_M C_M

出力

作る文字列が回文になることがあるならば、そのような回文の長さの最小値を、ないならば -1 を出力せよ。


入力例 1

8 8
1 2 a
2 3 b
1 3 c
3 4 b
4 5 a
5 6 c
6 7 b
7 8 a

出力例 1

10

1, 2, 3, 1, 2, 4, 5, 6, 7, 8 の順に通ると、出来上がる文字列は abcabbacba となり、回文となります。
これより短い回文を作ることはできないので、答えは 10 です。


入力例 2

4 5
1 1 a
1 2 a
2 3 a
3 4 b
4 4 a

出力例 2

5

2, 3, 4, 5, 5 の順に通ると aabaa という文字列を作ることができ、これは回文です。
同じ辺や頂点を複数回通っても構わないことに注意してください。


入力例 3

3 4
1 1 a
1 2 a
2 3 b
3 3 b

出力例 3

-1

出来上がる文字列が回文となることはありません。

Score : 600 points

Problem Statement

We have a connected undirected graph with N vertices and M edges, which is not necessarily simple.
Edge i connects Vertex A_i and Vertex B_i, and has a character C_i written on it.
Let us make a string by choosing a path from Vertex 1 to Vertex N (possibly with repetitions of the same edge and vertex) and arrange the characters written on the edges in that path.
Determine whether this string can be a palindrome. If it can, find the minimum possible length of such a palindrome.

Constraints

  • 2 \le N \le 1000
  • 1 \le M \le 1000
  • 1 \le A_i \le N
  • 1 \le B_i \le N
  • C_i is a lowercase English letter.
  • The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
A_2 B_2 C_2
A_3 B_3 C_3
\hspace{25pt} \vdots
A_M B_M C_M

Output

If a palindrome can be made, print the minimum possible length of such a palindrome; otherwise, print -1.


Sample Input 1

8 8
1 2 a
2 3 b
1 3 c
3 4 b
4 5 a
5 6 c
6 7 b
7 8 a

Sample Output 1

10

By traversing the edges in the order 1, 2, 3, 1, 2, 4, 5, 6, 7, 8, we can make a string abcabbacba, which is a palindrome.
It is impossible to make a shorter palindrome, so the answer is 10.


Sample Input 2

4 5
1 1 a
1 2 a
2 3 a
3 4 b
4 4 a

Sample Output 2

5

By traversing the edges in the order 2, 3, 4, 5, 5, we can make a string aabaa, which is a palindrome.
Note that repetitions of the same edge and vertex are allowed.


Sample Input 3

3 4
1 1 a
1 2 a
2 3 b
3 3 b

Sample Output 3

-1

We cannot make a palindrome.