A - Weather Forecast

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

明日からの 7 日間の天気予報を表す文字列 S が与えられます。
i 日後の予報は Si 文字目が o であるとき晴れ、x であるとき雨です。

N 日後の天気予報が晴れかどうかを教えてください。

制約

  • N1 以上 7 以下の整数
  • S は長さ 7 の文字列であり、ox のみからなる

入力

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

N
S

出力

N 日後の天気予報が晴れであるとき Yes を、雨であるとき No を出力せよ。


入力例 1

4
oooxoox

出力例 1

No

明日からの 7 日間の天気予報は順に、晴れ、晴れ、晴れ、雨、晴れ、晴れ、雨です。
特に、今日から 4 日後の天気予報は雨です。


入力例 2

7
ooooooo

出力例 2

Yes

Score : 100 points

Problem Statement

You are given a string S, which represents a weather forecast for the seven days starting tomorrow.
The i-th of those seven days is forecast to be sunny if the i-th character of S is o, and rainy if that character is x.

Tell us whether the N-th day is forecast to be sunny.

Constraints

  • N is an integer between 1 and 7 (inclusive).
  • S is a string of length 7 consisting of o and x.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print Yes if the N-th of the seven days starting tomorrow is forecast to be sunny, and No if that day is forecast to be rainy.


Sample Input 1

4
oooxoox

Sample Output 1

No

The forecast for each of the seven days starting tomorrow is as follows: sunny, sunny, sunny, rainy, sunny, sunny, rainy.
In particular, the fourth day is forecast to be rainy.


Sample Input 2

7
ooooooo

Sample Output 2

Yes
B - qwerty

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1 以上 26 以下の整数からなる長さ 26 の数列 P=(P_1,P_2, \ldots ,P_{26}) が与えられます。ここで、P の要素は相異なることが保証されます。

以下の条件を満たす長さ 26 の文字列 S を出力してください。

  • 任意の i\, (1 \leq i \leq 26) について、Si 文字目は辞書順で小さい方から P_i 番目の英小文字である。

制約

  • 1 \leq P_i \leq 26
  • P_i \neq P_j (i \neq j)
  • 入力は全て整数である。

入力

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

P_1 P_2 \ldots P_{26}

出力

文字列 S を出力せよ。


入力例 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

出力例 1

abcdefghijklmnopqrstuvwxyz

入力例 2

2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

出力例 2

bacdefghijklmnopqrstuvwxyz

入力例 3

5 11 12 16 25 17 18 1 7 10 4 23 20 3 2 24 26 19 14 9 6 22 8 13 15 21

出力例 3

eklpyqragjdwtcbxzsnifvhmou

Score : 200 points

Problem Statement

You are given a sequence of 26 integers P=(P_1,P_2, \ldots ,P_{26}) consisting of integers from 1 through 26. It is guaranteed that all elements in P are distinct.

Print a string S of length 26 that satisfies the following condition.

  • For every i (1 \leq i \leq 26), the i-th character of S is the lowercase English letter that comes P_i-th in alphabetical order.

Constraints

  • 1 \leq P_i \leq 26
  • P_i \neq P_j (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

P_1 P_2 \ldots P_{26}

Output

Print the string S.


Sample Input 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Sample Output 1

abcdefghijklmnopqrstuvwxyz

Sample Input 2

2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Sample Output 2

bacdefghijklmnopqrstuvwxyz

Sample Input 3

5 11 12 16 25 17 18 1 7 10 4 23 20 3 2 24 26 19 14 9 6 22 8 13 15 21

Sample Output 3

eklpyqragjdwtcbxzsnifvhmou
C - Shapes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

2 次元グリッド上に 2 つの図形 ST があります。グリッドは正方形のマスからなります。

SNN 列のグリッド内にあり、S_{i,j}# であるようなマス全体からなります。
TNN 列のグリッド内にあり、T_{i,j}# であるようなマス全体からなります。

ST90 度回転及び平行移動の繰り返しによって一致させることができるか判定してください。

制約

  • 1 \leq N \leq 200
  • S,T#. のみからなる
  • S,T1 つ以上 # を含む

入力

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

N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}

出力

ST90 度回転及び平行移動の繰り返しによって一致させることができるとき Yes を、そうでないとき No を出力せよ。


入力例 1

5
.....
..#..
.###.
.....
.....
.....
.....
....#
...##
....#

出力例 1

Yes

S を左回りに 90 度回転させ、平行移動することで T に一致させることができます。


入力例 2

5
#####
##..#
#..##
#####
.....
#####
#..##
##..#
#####
.....

出力例 2

No

90 度回転と平行移動の繰り返しによって一致させることはできません。


入力例 3

4
#...
..#.
..#.
....
#...
#...
..#.
....

出力例 3

Yes

S 及び T は連結とは限りません。


入力例 4

4
#...
.##.
..#.
....
##..
#...
..#.
....

出力例 4

No

回転や移動の操作は連結成分ごとにできるわけではなく、S,T 全体に対して行うことに注意してください。

Score : 300 points

Problem Statement

We have two figures S and T on a two-dimensional grid with square cells.

S lies within a grid with N rows and N columns, and consists of the cells where S_{i,j} is #.
T lies within the same grid with N rows and N columns, and consists of the cells where T_{i,j} is #.

Determine whether it is possible to exactly match S and T by 90-degree rotations and translations.

Constraints

  • 1 \leq N \leq 200
  • Each of S and T consists of # and ..
  • Each of S and T contains at least one #.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}

Output

Print Yes if it is possible to exactly match S and T by 90-degree rotations and translations, and No otherwise.


Sample Input 1

5
.....
..#..
.###.
.....
.....
.....
.....
....#
...##
....#

Sample Output 1

Yes

We can match S to T by rotating it 90-degrees counter-clockwise and translating it.


Sample Input 2

5
#####
##..#
#..##
#####
.....
#####
#..##
##..#
#####
.....

Sample Output 2

No

It is impossible to match them by 90-degree rotations and translations.


Sample Input 3

4
#...
..#.
..#.
....
#...
#...
..#.
....

Sample Output 3

Yes

Each of S and T may not be connected.


Sample Input 4

4
#...
.##.
..#.
....
##..
#...
..#.
....

Sample Output 4

No

Note that it is not allowed to rotate or translate just a part of a figure; it is only allowed to rotate or translate a whole figure.

D - Rectangles

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 400

問題文

2 次元平面上に N 個の相異なる点があり、1,2,\ldots ,N の番号がついています。点 i\,(1 \leq i \leq N) の座標は (x_i,y_i) です。

これらの点のうち 4 つを頂点とし、全ての辺が x 軸または y 軸に平行であるような長方形はいくつありますか?

制約

  • 4 \leq N \leq 2000
  • 0 \leq x_i, y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j) (i \neq j)
  • 入力は全て整数である。

入力

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

N
x_1 y_1
x_2 y_2
 \vdots 
x_N y_N

出力

答えを出力せよ。


入力例 1

6
0 0
0 1
1 0
1 1
2 0
2 1

出力例 1

3

1 、点 2 、点 3 、点 4 を頂点とする長方形、

1 、点 2 、点 5 、点 6 を頂点とする長方形、

3 、点 4 、点 5 、点 6 を頂点とする長方形

の合計 3 つです。


入力例 2

4
0 1
1 2
2 3
3 4

出力例 2

0

入力例 3

7
0 1
1 0
2 0
2 1
2 2
3 0
3 2

出力例 3

1

Score : 400 points

Problem Statement

We have N distinct points on a two-dimensional plane, numbered 1,2,\ldots,N. Point i (1 \leq i \leq N) has the coordinates (x_i,y_i).

How many rectangles are there whose vertices are among the given points and whose edges are parallel to the x- or y-axis?

Constraints

  • 4 \leq N \leq 2000
  • 0 \leq x_i, y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
 \vdots 
x_N y_N

Output

Print the answer.


Sample Input 1

6
0 0
0 1
1 0
1 1
2 0
2 1

Sample Output 1

3

There are three such rectangles:

the rectangle whose vertices are Points 1, 2, 3, 4,

the rectangle whose vertices are Points 1, 2, 5, 6,

and the rectangle whose vertices are Points 3, 4, 5, 6.


Sample Input 2

4
0 1
1 2
2 3
3 4

Sample Output 2

0

Sample Input 3

7
0 1
1 0
2 0
2 1
2 2
3 0
3 2

Sample Output 3

1
E - Destruction

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の連結無向グラフがあります。
頂点には 1 から N の番号が、辺には 1 から M の番号がついており、辺 i は頂点 A_iB_i を結んでいます。

高橋君は、このグラフから 0 個以上の辺を取り除こうとしています。

i を取り除くと、C_i \geq 0 のとき C_i の報酬を得、C_i<0 のとき |C_i| の罰金を払います。

辺を取り除いたあともグラフが連結でなければならないとき、高橋君が得られる報酬の最大値を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • -10^9 \leq C_i \leq 10^9
  • 与えられるグラフは連結である
  • 入力に含まれる値は全て整数である

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

答えを出力せよ。


入力例 1

4 5
1 2 1
1 3 1
1 4 1
3 2 2
4 2 2

出力例 1

4

4,5 を取り除くことで合計 4 の報酬を得られます。これより多くの報酬を得ることはできないため、答えは 4 となります。


入力例 2

3 3
1 2 1
2 3 0
3 1 -1

出力例 2

1

報酬が負であるような辺が存在することもあります。


入力例 3

2 3
1 2 -1
1 2 2
1 1 3

出力例 3

5

多重辺や自己ループが存在することもあります。

Score : 500 points

Problem Statement

We have a connected undirected graph with N vertices and M edges.
The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects Vertices A_i and B_i.

Takahashi is going to remove zero or more edges from this graph.

When removing Edge i, a reward of C_i is given if C_i \geq 0, and a fine of |C_i| is incurred if C_i<0.

Find the maximum total reward that Takahashi can get when the graph must be connected after removing edges.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • -10^9 \leq C_i \leq 10^9
  • The given graph is connected.
  • All values in input are integers.

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
\vdots
A_M B_M C_M

Output

Print the answer.


Sample Input 1

4 5
1 2 1
1 3 1
1 4 1
3 2 2
4 2 2

Sample Output 1

4

Removing Edges 4 and 5 yields a total reward of 4. You cannot get any more, so the answer is 4.


Sample Input 2

3 3
1 2 1
2 3 0
3 1 -1

Sample Output 2

1

There may be edges that give a negative reward when removed.


Sample Input 3

2 3
1 2 -1
1 2 2
1 1 3

Sample Output 3

5

There may be multi-edges and self-loops.

F - Blocked Roads

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の有向グラフが与えられます。頂点には 1 から N の番号、辺には 1 から M の番号がついています。辺 i\,(1 \leq i \leq M) は頂点 s_i から頂点 t_i に向かう長さ 1 の辺です。

i\,(1 \leq i \leq M) について、辺 i のみ通れないときの頂点 1 から頂点 N までの最短距離を求めてください。ただし、頂点 1 から頂点 N にたどり着けない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 400
  • 1 \leq M \leq N(N-1)
  • 1 \leq s_i,t_i \leq N
  • s_i \neq t_i
  • (s_i,t_i) \neq (s_j,t_j) (i \neq j)
  • 入力は全て整数である。

入力

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

N M
s_1 t_1
s_2 t_2
\vdots
s_M t_M

出力

M 行出力せよ。

i 行目には、辺 i のみ通れないときの頂点 1 から頂点 N までの最短距離を出力せよ。ただし、頂点 1 から頂点 N にたどり着けない場合は -1 を出力せよ。


入力例 1

3 3
1 2
1 3
2 3

出力例 1

1
2
1

入力例 2

4 4
1 2
2 3
2 4
3 4

出力例 2

-1
2
3
2

1 のみ通れないとき、頂点 1 から頂点 N にたどり着けないので -1 を出力します。


入力例 3

5 10
1 2
1 4
1 5
2 1
2 3
3 1
3 2
3 5
4 2
4 3

出力例 3

1
1
3
1
1
1
1
1
1
1

入力例 4

4 1
1 2

出力例 4

-1

Score : 500 points

Problem Statement

You are given a directed graph with N vertices and M edges. The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i (1 \leq i \leq M) goes from Vertex s_i to Vertex t_i and has a length of 1.

For each i (1 \leq i \leq M), find the shortest distance from Vertex 1 to Vertex N when all edges except Edge i are passable, or print -1 if Vertex N is unreachable from Vertex 1.

Constraints

  • 2 \leq N \leq 400
  • 1 \leq M \leq N(N-1)
  • 1 \leq s_i,t_i \leq N
  • s_i \neq t_i
  • (s_i,t_i) \neq (s_j,t_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
s_1 t_1
s_2 t_2
\vdots
s_M t_M

Output

Print M lines.

The i-th line should contain the shortest distance from Vertex 1 to Vertex N when all edges except Edge i are passable, or -1 if Vertex N is unreachable from Vertex 1.


Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

1
2
1

Sample Input 2

4 4
1 2
2 3
2 4
3 4

Sample Output 2

-1
2
3
2

Vertex N is unreachable from Vertex 1 when all edges except Edge 1 are passable, so the corresponding line contains -1.


Sample Input 3

5 10
1 2
1 4
1 5
2 1
2 3
3 1
3 2
3 5
4 2
4 3

Sample Output 3

1
1
3
1
1
1
1
1
1
1

Sample Input 4

4 1
1 2

Sample Output 4

-1
G - Game on Tree 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点の木があり、各頂点には 1 から N までの番号が振られています。また、i\,(1 \leq i \leq N-1) 本目の辺は頂点 u_i と頂点 v_i を結んでおり、頂点 i\,(1 \leq i \leq N) には偶数 A_i が書かれています。太郎君と次郎君がこの木と 1 つの駒を使ってゲームをします。

はじめ、駒は頂点 1 に置かれています。二人は太郎君から始めて交互に、駒を現在置かれている頂点と直接辺で結ばれた頂点に移動させます。ただし、駒が一度訪れた頂点に移動させることはできません。駒を移動させることができなくなった時点でゲームが終了します。

太郎君はゲームが終了するまでに駒が訪れた頂点(頂点 1 を含む)に書かれた数の(多重)集合の中央値を最大化、次郎君は最小化したいです。両者が最適に行動するとき、駒が訪れた頂点に書かれた数の集合の中央値を求めてください。

中央値とは

大きさ K の数の(多重)集合の中央値は以下のように定義されます。

  • K が奇数のとき、小さい方から \frac{K+1}{2} 番目の値
  • K が偶数のとき、小さい方から \frac{K}{2} 番目の値と \frac{K}{2}+1 番目の値の平均値
例えば、\{ 2,2,4 \} の中央値は 2\{ 2,4,6,6\} の中央値は 5 です。

制約

  • 2 \leq N \leq 10^5
  • 2 \leq A_i \leq 10^9
  • A_i は偶数
  • 1 \leq u_i < v_i \leq N
  • 与えられるグラフは木
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

両者が最適に行動するとき、駒が訪れた頂点に書かれた数の(多重)集合の中央値を出力せよ。


入力例 1

5
2 4 6 8 10
4 5
3 4
1 5
2 4

出力例 1

7

両者が最適に行動するとき、ゲームは以下のように進行します。

  • 太郎君が駒を頂点 1 から頂点 5 に移動させる
  • 次郎君が駒を頂点 5 から頂点 4 に移動させる
  • 太郎君が駒を頂点 4 から頂点 3 に移動させる
  • 次郎君は駒を動かせないのでゲームが終了する

駒が訪れた頂点に書かれた数の集合は \{2,10,8,6\} となるので、中央値である 7 を出力します。


入力例 2

5
6 4 6 10 8
1 4
1 2
1 5
1 3

出力例 2

8

両者が最適に行動するとき、ゲームは以下のように進行します。

  • 太郎君が駒を頂点 1 から頂点 4 に移動させる
  • 次郎君は駒を動かせないのでゲームが終了する

駒が訪れた頂点に書かれた数の集合は \{6,10\} となるので、中央値である 8 を出力します。


入力例 3

6
2 2 6 4 6 6
1 2
2 3
4 6
2 5
2 6

出力例 3

2

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 through N. The i-th edge (1 \leq i \leq N-1) connects Vertex u_i and Vertex v_i, and Vertex i (1 \leq i \leq N) has an even number A_i written on it. Taro and Jiro will play a game using this tree and a piece.

Initially, the piece is on Vertex 1. With Taro going first, the players alternately move the piece to a vertex directly connected to the vertex on which the piece lies. However, the piece must not revisit a vertex it has already visited. The game ends when the piece gets unable to move.

Taro wants to maximize the median of the (multi-)set of numbers written on the vertices visited by the piece before the end of the game (including Vertex 1), and Jiro wants to minimize this value. Find the median of this set when both players play optimally.

What is median?

The median of a (multi-)set of K numbers is defined as follows:

  • the \frac{K+1}{2}-th smallest number, if K is odd;
  • the average of the \frac{K}{2}-th and (\frac{K}{2}+1)-th smallest numbers, if K is even.
For example, the median of \{ 2,2,4 \} is 2, and the median of \{ 2,4,6,6\} is 5.

Constraints

  • 2 \leq N \leq 10^5
  • 2 \leq A_i \leq 10^9
  • A_i is even.
  • 1 \leq u_i < v_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print the median of the (multi-)set of numbers written on the vertices visited by the piece when both players play optimally.


Sample Input 1

5
2 4 6 8 10
4 5
3 4
1 5
2 4

Sample Output 1

7

When both players play optimally, the game goes as follows.

  • Taro moves the piece from Vertex 1 to Piece 5.
  • Jiro moves the piece from Vertex 5 to Piece 4.
  • Taro moves the piece from Vertex 4 to Piece 3.
  • Jiro cannot move the piece, so the game ends.

Here, the set of numbers written on the vertices visited by the piece is \{2,10,8,6\}. The median of this set is 7, which should be printed.


Sample Input 2

5
6 4 6 10 8
1 4
1 2
1 5
1 3

Sample Output 2

8

When both players play optimally, the game goes as follows.

  • Taro moves the piece from Vertex 1 to Piece 4.
  • Jiro cannot move the piece, so the game ends.

Here, the set of numbers written on the vertices visited by the piece is \{6,10\}. The median of this set is 8, which should be printed.


Sample Input 3

6
2 2 6 4 6 6
1 2
2 3
4 6
2 5
2 6

Sample Output 3

2
H - Red and Blue Lamps

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N の番号がついた N 個のランプが一列に並べられています。あなたはこのうち R 個を赤く、N-R 個を青く光らせようとしています。

i=1,\ldots,N-1 について、ランプ i とランプ i+1 が異なる色で光っているとき、あなたは A_i の報酬を得ます。

ランプの色を適切に定めたときに得られる報酬の合計の最大値を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq R \leq N-1
  • 1 \leq A_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

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

N R
A_1 A_2 \ldots A_{N-1}

出力

答えを出力せよ。


入力例 1

6 2
3 1 4 1 5

出力例 1

11

ランプ 3,5 を赤く、ランプ 1,2,4,6 を青くすることで、A_2+A_3+A_4+A_5=11 の報酬を得ます。

これ以上の報酬を得ることはできないため、答えは 11 です。


入力例 2

7 6
2 7 1 8 2 8

出力例 2

10

ランプ 1,2,3,4,5,7 を赤く、ランプ 6 を青くすることで、A_5+A_6=10 の報酬を得ます。


入力例 3

11 7
12345 678 90123 45678901 234567 89012 3456 78901 23456 7890

出力例 3

46207983

Score : 600 points

Problem Statement

There are N lamps numbered 1 through N arranged in a row. You are going to light R of them in red and N-R in blue.

For each i=1,\ldots,N-1, a reward of A_i is given if Lamp i and Lamp i+1 light up in different colors.

Find the maximum total reward that can be obtained by efficiently deciding the colors of the lamps.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq R \leq N-1
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N R
A_1 A_2 \ldots A_{N-1}

Output

Print the answer.


Sample Input 1

6 2
3 1 4 1 5

Sample Output 1

11

Lighting up Lamps 3, 5 in red and Lamps 1, 2, 4, 6 in blue yields a total reward of A_2+A_3+A_4+A_5=11.

You cannot get any more, so the answer is 11.


Sample Input 2

7 6
2 7 1 8 2 8

Sample Output 2

10

Lighting up Lamps 1, 2, 3, 4, 5, 7 in red and Lamp 6 in blue yields a total reward of A_5+A_6=10.


Sample Input 3

11 7
12345 678 90123 45678901 234567 89012 3456 78901 23456 7890

Sample Output 3

46207983