実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
xy 平面上に長方形があります。この長方形の各辺は x 軸または y 軸に平行であり、面積は 0 ではありません。
この長方形の 4 つの頂点のうち異なる 3 つの頂点の座標 (x_1, y_1), (x_2, y_2), (x_3, y_3) が与えられるので、残る 1 つの頂点の座標を求めてください。
制約
- -100 \leq x_i, y_i \leq 100
- (x_1, y_1), (x_2, y_2), (x_3, y_3) のすべてを頂点に持つ長方形がただ一つ存在し、その各辺は x 軸または y 軸に平行であり、面積は 0 ではない。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
x_1 y_1 x_2 y_2 x_3 y_3
出力
答えとなる頂点の座標 (x, y) を下記の形式にしたがい空白区切りで出力せよ。
x y
入力例 1
-1 -1 -1 2 3 2
出力例 1
3 -1
(-1, -1), (-1, 2), (3, 2) を頂点とする長方形の残る 1 つの頂点は (3, -1) です。
入力例 2
-60 -40 -60 -80 -20 -80
出力例 2
-20 -40
Score : 100 points
Problem Statement
There is a rectangle in the xy-plane. Each edge of this rectangle is parallel to the x- or y-axis, and its area is not zero.
Given the coordinates of three of the four vertices of this rectangle, (x_1, y_1), (x_2, y_2), and (x_3, y_3), find the coordinates of the other vertex.
Constraints
- -100 \leq x_i, y_i \leq 100
- There uniquely exists a rectangle with all of (x_1, y_1), (x_2, y_2), (x_3, y_3) as vertices, edges parallel to the x- or y-axis, and a non-zero area.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
x_1 y_1 x_2 y_2 x_3 y_3
Output
Print the sought coordinates (x, y) separated by a space in the following format:
x y
Sample Input 1
-1 -1 -1 2 3 2
Sample Output 1
3 -1
The other vertex of the rectangle with vertices (-1, -1), (-1, 2), (3, 2) is (3, -1).
Sample Input 2
-60 -40 -60 -80 -20 -80
Sample Output 2
-20 -40
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の英小文字からなる文字列 S が与えられます。
S の i 文字目を S_i と記します。
S_1,S_1,S_2,S_2,\dots,S_N,S_N をこの順に連結した長さ 2N の文字列を出力してください。
例えば、 S が beginner のときは bbeeggiinnnneerr と出力してください。
制約
- N は 1 \le N \le 50 を満たす整数
- S は長さ N の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
8 beginner
出力例 1
bbeeggiinnnneerr
問題文中の例と同じです。
入力例 2
3 aaa
出力例 2
aaaaaa
Score : 100 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
We denote the i-th character of S by S_i.
Print the string of length 2N obtained by concatenating S_1,S_1,S_2,S_2,\dots,S_N, and S_N in this order.
For example, if S is beginner, print bbeeggiinnnneerr.
Constraints
- N is an integer such that 1 \le N \le 50.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
8 beginner
Sample Output 1
bbeeggiinnnneerr
It is the same as the example described in the problem statement.
Sample Input 2
3 aaa
Sample Output 2
aaaaaa
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。
長さ 8 の文字列 S が与えられます。S には K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。
-
S において左から x,y\ (x < y) 文字目が
Bであるとする。このとき x と y の偶奇が異なる。 -
Kは 2 つのRの間にある。より形式的には、S において左から x,y\ (x < y) 文字目がRであり、 z 文字目がKであるとする。このとき、 x< z < y が成り立つ。
制約
- S は 長さ 8 の文字列であり、
K,Qがちょうど 1 文字ずつ、R,B,Nがちょうど 2 文字ずつ含まれる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が条件を満たす場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
RNBQKBNR
出力例 1
Yes
B は左から 3 番目、6 番目にあり、3 と 6 は偶奇が異なります。
また、K は 2 つの R の間にあります。よって条件を満たします。
入力例 2
KRRBBNNQ
出力例 2
No
K は 2 つの R の間にありません。
入力例 3
BRKRBQNN
出力例 3
No
Score : 200 points
Problem Statement
Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.
You are given a string S of length eight. S has exactly one K and Q, and exactly two R's, B's , and N's. Determine if S satisfies all of the following conditions.
-
Suppose that the x-th and y-th (x < y) characters from the left of S are
B; then, x and y have different parities. -
Kis between twoR's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S areRand the z-th isK; then x< z < y.
Constraints
- S is a string of length 8 that contains exactly one
KandQ, and exactly twoR's,B's , andN's.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S satisfies the conditions; print No otherwise.
Sample Input 1
RNBQKBNR
Sample Output 1
Yes
The 3-rd and 6-th characters are B, and 3 and 6 have different parities.
Also, K is between the two R's. Thus, the conditions are fulfilled.
Sample Input 2
KRRBBNNQ
Sample Output 2
No
K is not between the two R's.
Sample Input 3
BRKRBQNN
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。 S の x 文字目 (1 \le x \le N) は S_x です。
i=1,2,\dots,N-1 それぞれについて、以下の条件を全て満たす最大の非負整数 l を求めてください。
- l+i \le N である。
- 全ての 1 \le k \le l を満たす整数 k について、 S_{k} \neq S_{k+i} を満たす。
なお、 l=0 は常に条件を満たすことに注意してください。
制約
- N は 2 \le N \le 5000 を満たす整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N-1 行にわたって出力せよ。そのうち x 行目 (1 \le x < N) には i=x とした場合の答えを整数として出力せよ。
入力例 1
6 abcbac
出力例 1
5 1 2 0 1
この入力では、 S= abcbac です。
- i=1 のとき、 S_1 \neq S_2, S_2 \neq S_3, \dots ,S_5 \neq S_6 であるため、 最大値は l=5 です。
- i=2 のとき、 S_1 \neq S_3 ですが S_2 = S_4 であるため、 最大値は l=1 です。
- i=3 のとき、 S_1 \neq S_4, S_2 \neq S_5 ですが S_3 = S_6 であるため、 最大値は l=2 です。
- i=4 のとき、 S_1 = S_5 であるため、 最大値は l=0 です。
- i=5 のとき、 S_1 \neq S_6 であるため、 最大値は l=1 です。
Score : 200 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. The x-th (1 \le x \le N) character of S is S_x.
For each i=1,2,\dots,N-1, find the maximum non-negative integer l that satisfies all of the following conditions:
- l+i \le N, and
- for all integers k such that 1 \le k \le l, it holds that S_{k} \neq S_{k+i}.
Note that l=0 always satisfies the conditions.
Constraints
- N is an integer such that 2 \le N \le 5000.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Print (N-1) lines. The x-th (1 \le x < N) line should contain the answer as an integer when i=x.
Sample Input 1
6 abcbac
Sample Output 1
5 1 2 0 1
In this input, S= abcbac.
- When i=1, we have S_1 \neq S_2, S_2 \neq S_3, \dots, and S_5 \neq S_6, so the maximum value is l=5.
- When i=2, we have S_1 \neq S_3 but S_2 = S_4, so the maximum value is l=1.
- When i=3, we have S_1 \neq S_4 and S_2 \neq S_5 but S_3 = S_6, so the maximum value is l=2.
- When i=4, we have S_1 = S_5, so the maximum value is l=0.
- When i=5, we have S_1 \neq S_6, so the maximum value is l=1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 本の導火線を一直線に接着したものがあります。左から i 本目の導火線は長さが A_i cm で、 1 秒あたり B_i cm の一定の速さで燃えます。
この導火線の左端と右端から同時に火をつけるとき、 2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i,B_i \leq 1000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か(単位を除いて)出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。
入力例 1
3 1 1 2 1 3 1
出力例 1
3.000000000000000
着火前の導火線の左端から 3 cm の地点で 2 つの火がぶつかります。
入力例 2
3 1 3 2 2 3 1
出力例 2
3.833333333333333
入力例 3
5 3 9 1 2 4 6 1 5 5 3
出力例 3
8.916666666666668
Score : 300 points
Problem Statement
We have N fuses connected in series. The i-th fuse from the left has a length of A_i centimeters and burns at a constant speed of B_i centimeters per second.
Consider igniting this object from left and right ends simultaneously. Find the distance between the position where the two flames will meet and the left end of the object.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i,B_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the distance between the position where the two flames will meet and the left end of the object, in centimeters (print just the number).
Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-5}.
Sample Input 1
3 1 1 2 1 3 1
Sample Output 1
3.000000000000000
The two flames will meet at 3 centimeters from the left end of the object.
Sample Input 2
3 1 3 2 2 3 1
Sample Output 2
3.833333333333333
Sample Input 3
5 3 9 1 2 4 6 1 5 5 3
Sample Output 3
8.916666666666668
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
二次元平面があります。1 以上 9 以下の整数 r,c について、S_{r} の c 番目の文字が # であるとき座標 (r,c) にポーンが置いてあり、S_{r} の c 番目の文字が . であるとき座標 (r,c) に何も置かれていません。
この平面上の正方形であって、4 頂点全てにポーンが置いてあるものの個数を求めてください。
制約
- S_1,\ldots,S_9 はそれぞれ
#と.からなる長さ 9 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2 \vdots S_9
出力
答えを出力せよ。
入力例 1
##....... ##....... ......... .......#. .....#... ........# ......#.. ......... .........
出力例 1
2
座標 (1,1),(1,2),(2,2),(2,1) を頂点とする正方形は、4 頂点全てにポーンが置かれています。
座標 (4,8),(5,6),(7,7),(6,9) を頂点とする正方形も、4 頂点全てにポーンが置かれています。
よって答えは 2 です。
入力例 2
.#....... #.#...... .#....... ......... ....#.#.# ......... ....#.#.# ........# .........
出力例 2
3
Score : 300 points
Problem Statement
There is a two-dimensional plane. For integers r and c between 1 and 9, there is a pawn at the coordinates (r,c) if the c-th character of S_{r} is #, and nothing if the c-th character of S_{r} is ..
Find the number of squares in this plane with pawns placed at all four vertices.
Constraints
- Each of S_1,\ldots,S_9 is a string of length 9 consisting of
#and..
Input
The input is given from Standard Input in the following format:
S_1 S_2 \vdots S_9
Output
Print the answer.
Sample Input 1
##....... ##....... ......... .......#. .....#... ........# ......#.. ......... .........
Sample Output 1
2
The square with vertices (1,1), (1,2), (2,2), and (2,1) have pawns placed at all four vertices.
The square with vertices (4,8), (5,6), (7,7), and (6,9) also have pawns placed at all four vertices.
Thus, the answer is 2.
Sample Input 2
.#....... #.#...... .#....... ......... ....#.#.# ......... ....#.#.# ........# .........
Sample Output 2
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 頂点の木があり、頂点は 1, 2, \dots, N と番号付けられています。
i \, (1 \leq i \leq N - 1) 番目の辺は頂点 u_i と頂点 v_i を結び、重みは w_i です。
異なる頂点 u, v に対し、頂点 u から頂点 v までの最短パスに含まれる辺の重みの最大値を f(u, v) とおきます。
\displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j) を求めてください。
制約
- 2 \leq N \leq 10^5
- 1 \leq u_i, v_i \leq N
- 1 \leq w_i \leq 10^7
- 与えられるグラフは木である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1 w_1
\vdots
u_{N - 1} v_{N - 1} w_{N - 1}
出力
答えを出力せよ。
入力例 1
3 1 2 10 2 3 20
出力例 1
50
f(1, 2) = 10, f(2, 3) = 20, f(1, 3) = 20 であるので、これらの和である 50 を出力します。
入力例 2
5 1 2 1 2 3 2 4 2 5 3 5 14
出力例 2
76
Score : 400 points
Problem Statement
We have a tree with N vertices numbered 1, 2, \dots, N.
The i-th edge (1 \leq i \leq N - 1) connects Vertex u_i and Vertex v_i and has a weight w_i.
For different vertices u and v, let f(u, v) be the greatest weight of an edge contained in the shortest path from Vertex u to Vertex v.
Find \displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j).
Constraints
- 2 \leq N \leq 10^5
- 1 \leq u_i, v_i \leq N
- 1 \leq w_i \leq 10^7
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1 w_1
\vdots
u_{N - 1} v_{N - 1} w_{N - 1}
Output
Print the answer.
Sample Input 1
3 1 2 10 2 3 20
Sample Output 1
50
We have f(1, 2) = 10, f(2, 3) = 20, and f(1, 3) = 20, so we should print their sum, or 50.
Sample Input 2
5 1 2 1 2 3 2 4 2 5 3 5 14
Sample Output 2
76
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
高橋くんは秘密の整数列 a を持っており、現時点で、a の長さが N であることは分かっています。
a の中身を当てたいあなたに対し、高橋くんは以下の Q 個の情報を追加で与えてくれることを約束しました。
- i\ (1 \leq i \leq Q) 個目の情報: a_{l_i}+a_{l_i+1}+\cdots+a_{r_i} の値
高橋くんが約束を守り、Q 個の情報すべてが与えられた場合、a に含まれる全要素の総和 a_1+a_2+\cdots+a_N を特定することは可能ですか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})
- 1 \leq l_i \leq r_i \leq N
- (l_i,r_i) \neq (l_j,r_j)\ (i \neq j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
l_1 r_1
l_2 r_2
\hspace{0.4cm}\vdots
l_Q r_Q
出力
a に含まれる全要素の総和を特定することが可能なら Yes を、そうでないなら No を出力せよ。
入力例 1
3 3 1 2 2 3 2 2
出力例 1
Yes
1 個目の情報と 2 個目の情報から、a_1+a_2+a_2+a_3 の値が分かります。そこから 3 個目の情報によって得られる a_2 の値を引くと、a_1+a_2+a_3 の値を特定可能です。
入力例 2
4 3 1 3 1 2 2 3
出力例 2
No
a の先頭 3 項の総和を特定することは可能ですが、全要素の総和を特定することは不可能です。
入力例 3
4 4 1 1 2 2 3 3 1 4
出力例 3
Yes
4 個目の情報によって全要素の総和が直接与えられています。
Score : 500 points
Problem Statement
Takahashi has a secret integer sequence a. You know that the length of a is N.
You want to guess the contents of a. He has promised to give you the following Q additional pieces of information.
- The i-th information: the value a_{l_i}+a_{l_i+1}+\cdots+a_{r_i}.
Is it possible to determine the sum of all elements in a, a_1+a_2+\cdots+a_N, if the Q pieces of promised information are given?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})
- 1 \leq l_i \leq r_i \leq N
- (l_i,r_i) \neq (l_j,r_j)\ (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
l_1 r_1
l_2 r_2
\hspace{0.4cm}\vdots
l_Q r_Q
Output
If it is possible to determine the sum of all elements in a, print Yes; otherwise, print No.
Sample Input 1
3 3 1 2 2 3 2 2
Sample Output 1
Yes
From the first and second information, we can find the value a_1+a_2+a_2+a_3. By subtracting the value of a_2 from it, we can determine the value a_1+a_2+a_3.
Sample Input 2
4 3 1 3 1 2 2 3
Sample Output 2
No
We can determine the sum of the first 3 elements of a, but not the sum of all elements.
Sample Input 3
4 4 1 1 2 2 3 3 1 4
Sample Output 3
Yes
The fourth information directly gives us the sum of all elements.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1,\ldots,N の番号がついた N 枚のカードがあり、カード i の表には P_i が、裏には Q_i が書かれています。
ここで、P=(P_1,\ldots,P_N) 及び Q=(Q_1,\ldots,Q_N) はそれぞれ (1, 2, \dots, N) の並び替えです。
N 枚のカードから何枚かを選ぶ方法のうち、次の条件を満たすものは何通りありますか? 998244353 で割った余りを求めてください。
条件:1,2,\ldots,N のどの数も選んだカードのいずれかに書かれている
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq P_i,Q_i \leq N
- P,Q はそれぞれ (1, 2, \dots, N) の並び替えである
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
出力
答えを出力せよ。
入力例 1
3 1 2 3 2 1 3
出力例 1
3
例えばカード 1,3 を選ぶと、1 はカード 1 の表に、2 はカード 1 の裏に、3 はカード 3 の表に書かれているため条件を満たします。
条件を満たすカードの選び方は \{1,3\},\{2,3\},\{1,2,3\} の 3 通りです。
入力例 2
5 2 3 5 4 1 4 2 1 3 5
出力例 2
12
入力例 3
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
出力例 3
1
Score : 500 points
Problem Statement
There are N cards numbered 1,\ldots,N. Card i has P_i written on the front and Q_i written on the back.
Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N).
How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353.
Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq P_i,Q_i \leq N
- P and Q are permutations of (1, 2, \dots, N).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
Output
Print the answer.
Sample Input 1
3 1 2 3 2 1 3
Sample Output 1
3
For example, if you choose Cards 1 and 3, then 1 is written on the front of Card 1, 2 on the back of Card 1, and 3 on the front of Card 3, so this combination satisfies the condition.
There are 3 ways to choose cards satisfying the condition: \{1,3\},\{2,3\},\{1,2,3\}.
Sample Input 2
5 2 3 5 4 1 4 2 1 3 5
Sample Output 2
12
Sample Input 3
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Sample Output 3
1