C - Rectangle Detection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋くんは、以下の方法で 10 個の文字列 S_1,S_2,\dots,S_{10} を生成しました。

  • まず、 S_i (1 \le i \le 10)= .......... ( . が 10 個並んだ文字列) とする。
  • 次に、以下の条件を全て満たす 4 つの整数 A,B,C,D を選ぶ。
    • 1 \le A \le B \le 10
    • 1 \le C \le D \le 10
  • その後、以下の条件を全て満たす全ての整数組 (i,j) について、 S_i の j 文字目を # に書き換える。
    • A \le i \le B
    • C \le j \le D

以上の方法で生成された S_1,S_2,\dots,S_{10} が与えられるので、高橋くんが選んだ整数 A,B,C,D を求めてください。
なお、制約より A,B,C,D は一意に定まる (答えはただひとつ存在する) ことが証明できます。

制約

  • S_1,S_2,\dots,S_{10} は問題文中の方法で生成されうるそれぞれ長さ 10 の文字列

入力

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

S_1
S_2
\vdots
S_{10}

出力

答えを以下の形式で出力せよ。

A B
C D

入力例 1

..........
..........
..........
..........
...######.
...######.
...######.
...######.
..........
..........

出力例 1

5 8
4 9

高橋くんが選んだ整数は A=5,B=8,C=4,D=9 です。
このように選ぶことにより、 S_5,S_6,S_7,S_8 の 4 文字目から 9 文字目が # であり他の文字が . である 10 個の長さ 10 の文字列 S_1,S_2,\dots,S_{10} が生成されます。
これは入力で与えられた 10 個の文字列と一致します。


入力例 2

..........
..#.......
..........
..........
..........
..........
..........
..........
..........
..........

出力例 2

2 2
3 3

入力例 3

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########

出力例 3

1 10
1 10

Score : 200 points

Problem Statement

Takahashi generated 10 strings S_1,S_2,\dots,S_{10} as follows.

  • First, let S_i (1 \le i \le 10)= .......... (10 .s in a row).
  • Next, choose four integers A, B, C, and D satisfying all of the following.
    • 1 \le A \le B \le 10.
    • 1 \le C \le D \le 10.
  • Then, for every pair of integers (i,j) satisfying all of the following, replace the j-th character of S_i with #.
    • A \le i \le B.
    • C \le j \le D.

You are given S_1,S_2,\dots,S_{10} generated as above. Find the integers A, B, C, and D Takahashi chose.
It can be proved that such integers A, B, C, and D uniquely exist (there is just one answer) under the Constraints.

Constraints

  • S_1,S_2,\dots,S_{10} are strings, each of length 10, that can be generated according to the Problem Statement.

Input

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

S_1
S_2
\vdots
S_{10}

Output

Print the answer in the following format:

A B
C D

Sample Input 1

..........
..........
..........
..........
...######.
...######.
...######.
...######.
..........
..........

Sample Output 1

5 8
4 9

Here, Takahashi chose A=5, B=8, C=4, D=9.
This choice generates 10 strings S_1,S_2,\dots,S_{10}, each of length 10, where the 4-th through 9-th characters of S_5,S_6,S_7,S_8 are #, and the other characters are ..
These are equal to the strings given in the input.


Sample Input 2

..........
..#.......
..........
..........
..........
..........
..........
..........
..........
..........

Sample Output 2

2 2
3 3

Sample Input 3

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########

Sample Output 3

1 10
1 10
D - Robot Weight

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

ロボットがあり、はじめロボットの重さは X です。 このロボットには、同時に取り付けられる部品が種類 1, 種類 2,\ldots, 種類 N の N 種類あります。 種類 i\ (1\le i\le N) の部品の重さは W _ i です。 はじめ、ロボットには N 種類のうちのどの部品もついていません。

次の Q 個のクエリを順に処理してください。 i 番目 (1\le i\le Q) のクエリは、整数 P _ i で表される次のようなクエリです。

  • 現在、ロボットに種類 P _ i の部品がついていない場合は取り付け、ついている場合は取り外す。その後、現在のロボットの重さを出力する。

制約

  • 1\le X\le100
  • 1\le N\le100
  • 1\le W _ i\le100\ (1\le i\le N)
  • 1\le Q\le100
  • 1\le P _ i\le N\ (1\le i\le Q)
  • 入力はすべて整数

入力

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

X
N
W _ 1 W _ 2 \ldots W _ N
Q
P _ 1
P _ 2
\vdots
P _ Q

出力

Q 行にわたって出力せよ。 i 行目 (1\le i\le Q) には、i 番目のクエリを処理した結果を出力せよ。


入力例 1

31
4
15 92 65 35
4
3
1
4
1

出力例 1

96
111
146
131

はじめ、ロボットの重さは 31 です。 それぞれのクエリで、ロボットの重さは次のようになります。

  • 1 番目のクエリでは、種類 3 のパーツをロボットに取り付ける。ロボットの重さは 31+65=96 になる。
  • 2 番目のクエリでは、種類 1 のパーツをロボットに取り付ける。ロボットの重さは 96+15=111 になる。
  • 3 番目のクエリでは、種類 4 のパーツをロボットに取り付ける。ロボットの重さは 111+35=146 になる。
  • 4 番目のクエリでは、種類 1 のパーツをロボットから取り外す。ロボットの重さは 146-15=131 になる。

入力例 2

41
10
73 8 55 26 97 48 37 47 35 55
15
1
2
7
1
6
3
10
8
4
8
1
5
9
9
3

出力例 2

114
122
159
86
134
189
244
291
317
270
343
440
475
440
385

Score : 200 points

Problem Statement

There is a robot, and initially the weight of the robot is X. This robot has N types of parts that can be attached simultaneously: type 1, type 2,\ldots, type N. The weight of the type i\ (1\le i\le N) part is W _ i. Initially, none of the N types of parts are attached to the robot.

Process the following Q queries in order. The i-th query (1\le i\le Q) is represented by an integer P _ i and is as follows:

  • If the type P _ i part is not currently attached to the robot, attach it; if it is attached, remove it. Then, print the current weight of the robot.

Constraints

  • 1\le X\le100
  • 1\le N\le100
  • 1\le W _ i\le100\ (1\le i\le N)
  • 1\le Q\le100
  • 1\le P _ i\le N\ (1\le i\le Q)
  • All input values are integers.

Input

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

X
N
W _ 1 W _ 2 \ldots W _ N
Q
P _ 1
P _ 2
\vdots
P _ Q

Output

Output Q lines. The i-th line (1\le i\le Q) should contain the result of processing the i-th query.


Sample Input 1

31
4
15 92 65 35
4
3
1
4
1

Sample Output 1

96
111
146
131

Initially, the weight of the robot is 31. For each query, the weight of the robot becomes as follows:

  • In the 1-st query, attach the type 3 part to the robot. The weight of the robot becomes 31+65=96.
  • In the 2-nd query, attach the type 1 part to the robot. The weight of the robot becomes 96+15=111.
  • In the 3-rd query, attach the type 4 part to the robot. The weight of the robot becomes 111+35=146.
  • In the 4-th query, remove the type 1 part from the robot. The weight of the robot becomes 146-15=131.

Sample Input 2

41
10
73 8 55 26 97 48 37 47 35 55
15
1
2
7
1
6
3
10
8
4
8
1
5
9
9
3

Sample Output 2

114
122
159
86
134
189
244
291
317
270
343
440
475
440
385
E - Connect 6

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 行 N 列のマス目があり、それぞれのマスは白または黒で塗られています。
マス目の状態は N 個の文字列 S_i で表され、 S_i の j 文字目が # であることはマス目の上から i 行目、左から j 列目のマスが黒く塗られていることを、 . であることは白く塗られていることをさします。

高橋君はこのマス目のうち高々 2 つの白く塗られているマスを選び、黒く塗ることができます。
マス目の中に、黒く塗られたマスが縦、横、ななめのいずれかの向きに 6 つ以上連続するようにできるか判定してください。
ただし、黒く塗られたマスがななめに 6 つ以上連続するとは、N 行 N 列のマス目に完全に含まれる 6 行 6 列のマス目であって、その少なくとも一方の対角線上のマスがすべて黒く塗られているようなものが存在する事をさします。

制約

  • 6 \leq N \leq 1000
  • \lvert S_i\rvert =N
  • S_i は # と . のみからなる。

入力

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

N
S_1
S_2
\vdots
S_N

出力

高々 2 つのマス目を黒く塗ることで条件をみたすようにできるなら Yes を、そうでないならば No を出力せよ。


入力例 1

8
........
........
.#.##.#.
........
........
........
........
........

出力例 1

Yes

上から 3 行目の左から 3, 6 番目のマスを塗ることで横方向に 6 つの黒く塗られたマスを連続させることができます。


入力例 2

6
######
######
######
######
######
######

出力例 2

Yes

高橋君はマス目を新たに黒く塗ることはできませんが、すでにこのマス目は条件をみたしています。


入力例 3

10
..........
#..##.....
..........
..........
....#.....
....#.....
.#...#..#.
..........
..........
..........

出力例 3

No

Score : 300 points

Problem Statement

There is a grid with N horizontal rows and N vertical columns, where each square is painted white or black.
The state of the grid is represented by N strings, S_i. If the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is painted black. If the character is ., then the square is painted white.

Takahashi can choose at most two of these squares that are painted white, and paint them black.
Determine if it is possible to make the grid contain 6 or more consecutive squares painted black aligned either vertically, horizontally, or diagonally.
Here, the grid is said to be containing 6 or more consecutive squares painted black aligned diagonally if the grid with N rows and N columns completely contains a subgrid with 6 rows and 6 columns such that all the squares on at least one of its diagonals are painted black.

Constraints

  • 6 \leq N \leq 1000
  • \lvert S_i\rvert =N
  • S_i consists of # and ..

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

If it is possible to fulfill the condition by painting at most two squares, then print Yes; otherwise, print No.


Sample Input 1

8
........
........
.#.##.#.
........
........
........
........
........

Sample Output 1

Yes

By painting the 3-rd and the 6-th squares from the left in the 3-rd row from the top, there will be 6 squares painted black aligned horizontally.


Sample Input 2

6
######
######
######
######
######
######

Sample Output 2

Yes

While Takahashi cannot choose a square to paint black, the grid already satisfies the condition.


Sample Input 3

10
..........
#..##.....
..........
..........
....#.....
....#.....
.#...#..#.
..........
..........
..........

Sample Output 3

No
F - One Time Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

文字列 S が与えられます。次の操作を ちょうど 1 回 行った後の文字列としてあり得るものがいくつあるか求めてください。

  • S の長さを N とする。 1\leq i<j\leq N をみたす整数の組 (i,j) を選び、S の i 文字目と j 文字目を入れ替える。

なお、この問題の制約下で操作を必ず行うことができることが証明できます。

制約

  • S は英小文字からなる長さ 2 以上 10^6 以下の文字列

入力

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

S

出力

S に対して問題文中の操作をちょうど 1 回行った後の文字列としてあり得るものの個数を出力せよ。


入力例 1

abc

出力例 1

3

S の長さは 3 であるため、1\leq i<j\leq 3 をみたす整数の組 (i,j) としては、 (1,2), (1,3), (2,3) の 3 通りが考えられます。

  • S の 1 文字目と 2 文字目を入れ替えた時、S は bac となります。
  • S の 1 文字目と 3 文字目を入れ替えた時、S は cba となります。
  • S の 2 文字目と 3 文字目を入れ替えた時、S は acb となります。

よって、abc に対して操作を行った後の文字列としては、bac, cba, acb の 3 つがあり得るため、3 を出力します。


入力例 2

aaaaa

出力例 2

1

どの 2 つの文字を入れ替えても S は aaaaa のままです。よって、操作後の文字列としてあり得るものは 1 つです。

Points: 350 points

Problem Statement

You are given a string S. Find the number of strings that can result from performing the following operation exactly once.

  • Let N be the length of S. Choose a pair of integers (i,j) such that 1\leq i<j\leq N and swap the i-th and j-th characters of S.

It can be proved that you can always perform it under the constraints of this problem.

Constraints

  • S is a string of length between 2 and 10^6, inclusive, consisting of lowercase English letters.

Input

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

S

Output

Print the number of strings that can result from performing the operation in the problem statement exactly once on S.


Sample Input 1

abc

Sample Output 1

3

The length of S is 3, so 1\leq i<j\leq 3 is satisfied by three pairs of integers (i,j): (1,2), (1,3), and (2,3).

  • Swapping the 1-st and 2-nd characters of S results in S being bac.
  • Swapping the 1-st and 3-rd characters of S results in S being cba.
  • Swapping the 2-nd and 3-rd characters of S results in S being acb.

Therefore, the operation on abc results in one of the three strings: bac, cba, and acb, so print 3.


Sample Input 2

aaaaa

Sample Output 2

1

Swapping any two characters results in S remaining aaaaa. Thus, only one string can result from the operation.

G - Circumferences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

xy -平面上の N 個の円が与えられます。 i = 1, 2, \ldots, N について、i 番目の円は点 (x_i, y_i) を中心とする半径 r_i の円です。

N 個の円のうち少なくとも 1 つ以上の円の円周上にある点のみを通って、点 (s_x, s_y) から点 (t_x, t_y) に行くことができるかどうかを判定してください。

制約

  • 1 \leq N \leq 3000
  • -10^9 \leq x_i, y_i \leq 10^9
  • 1 \leq r_i \leq 10^9
  • (s_x, s_y) は N 個の円のうち少なくとも 1 つ以上の円の円周上にある
  • (t_x, t_y) は N 個の円のうち少なくとも 1 つ以上の円の円周上にある
  • 入力はすべて整数

入力

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

N
s_x s_y t_x t_y
x_1 y_1 r_1
x_2 y_2 r_2
\vdots
x_N y_N r_N

出力

点 (s_x, s_y) から点 (t_x, t_y) に行くことができる場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

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

出力例 1

Yes

例えば、下記の経路で点 (0, -2) から点 (3, 3) へ行くことができます。

  • 点 (0, -2) から 1 つ目の円の円周上を反時計回りに通って点 (1, -\sqrt{3}) へ行く。
  • 点 (1, -\sqrt{3}) から 2 つ目の円の円周上を時計回りに通って点 (2, 2) へ行く。
  • 点 (2, 2) から 3 つ目の円の円周上を反時計回りに通って点 (3, 3) へ行く。

よって、Yes を出力します。


入力例 2

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

出力例 2

No

少なくとも 1 つ以上の円の円周上にある点のみを通って点 (0, 1) から点 (0, 3) に行くことはできないので No を出力します。

Score : 400 points

Problem Statement

You are given N circles on the xy-coordinate plane. For each i = 1, 2, \ldots, N, the i-th circle is centered at (x_i, y_i) and has a radius of r_i.

Determine whether it is possible to get from (s_x, s_y) to (t_x, t_y) by only passing through points that lie on the circumference of at least one of the N circles.

Constraints

  • 1 \leq N \leq 3000
  • -10^9 \leq x_i, y_i \leq 10^9
  • 1 \leq r_i \leq 10^9
  • (s_x, s_y) lies on the circumference of at least one of the N circles.
  • (t_x, t_y) lies on the circumference of at least one of the N circles.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_x s_y t_x t_y
x_1 y_1 r_1
x_2 y_2 r_2
\vdots
x_N y_N r_N

Output

If it is possible to get from (s_x, s_y) to (t_x, t_y), print Yes; otherwise, print No. Note that the judge is case-sensitive.


Sample Input 1

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

Sample Output 1

Yes

Here is one way to get from (0, -2) to (3, 3).

  • From (0, -2), pass through the circumference of the 1-st circle counterclockwise to reach (1, -\sqrt{3}).
  • From (1, -\sqrt{3}), pass through the circumference of the 2-nd circle clockwise to reach (2, 2).
  • From (2, 2), pass through the circumference of the 3-rd circle counterclockwise to reach (3, 3).

Thus, Yes should be printed.


Sample Input 2

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

Sample Output 2

No

It is impossible to get from (0, 1) to (0, 3) by only passing through points on the circumference of at least one of the circles, so No should be printed.