Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 300 点
問題文
数列 a_1,a_2,... ,a_n が以下の条件を満たすとき、 /\/\/\/ と呼ぶことにします。
- 各 i = 1,2,..., n-2 について、a_i = a_{i+2}
- 数列に現れる数はちょうど 2 種類
偶数長の数列 v_1,v_2,...,v_n が与えられます。 要素をいくつか書き換えることでこの数列を /\/\/\/ にしたいです。 書き換える要素の数は最小でいくつになるか求めてください。
制約
- 2 \leq n \leq 10^5
- n は偶数
- 1 \leq v_i \leq 10^5
- v_i は整数
入力
入力は以下の形式で標準入力から与えられる。
n v_1 v_2 ... v_n
出力
書き換える要素数の最小値を出力してください。
入力例 1
4 3 1 3 2
出力例 1
1
数列 3,1,3,2 は /\/\/\/ ではありませんが、1 要素書き換えることで /\/\/\/ にすることができます。 例えば、4 要素目を書き換えて 3,1,3,1 とすればよいです。
入力例 2
6 105 119 105 119 105 119
出力例 2
0
数列 105,119,105,119,105,119 は /\/\/\/ です。
入力例 3
4 1 1 1 1
出力例 3
2
数列 1,1,1,1 は 1 種類の数からなる数列であるため、 /\/\/\/ ではありません。
Score : 300 points
Problem Statement
A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:
- For each i = 1,2,..., n-2, a_i = a_{i+2}.
- Exactly two different numbers appear in the sequence.
You are given a sequence v_1,v_2,...,v_n whose length is even. We would like to make this sequence /\/\/\/ by replacing some of its elements. Find the minimum number of elements that needs to be replaced.
Constraints
- 2 \leq n \leq 10^5
- n is even.
- 1 \leq v_i \leq 10^5
- v_i is an integer.
Input
Input is given from Standard Input in the following format:
n v_1 v_2 ... v_n
Output
Print the minimum number of elements that needs to be replaced.
Sample Input 1
4 3 1 3 2
Sample Output 1
1
The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.
Sample Input 2
6 105 119 105 119 105 119
Sample Output 2
0
The sequence 105,119,105,119,105,119 is /\/\/\/.
Sample Input 3
4 1 1 1 1
Sample Output 3
2
The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 600 点
問題文
すぬけ君の工場では,次のような特徴を持つ ロボットアーム を導入することになりました:
- ロボットアームは,m 個の 腕 と m+1 個の 関節 からなる.腕には 1, 2, ..., m で,関節には 0, 1, ..., m で番号が付けられている.腕 i は関節 i-1 と関節 i をつなぐ.腕 i の長さは d_i である.
- 各腕には,それぞれ独立に モード を指定することができる.モードは
L
,R
,D
,U
のいずれかであり,腕のモードはその腕の向きを指定する.工場を座標平面とみなすと,関節 i の座標 (x_i, y_i) は次のように定まる:- (x_0, y_0) = (0, 0).
- 腕 i のモードが
L
のとき,(x_i, y_i) = (x_{i-1} - d_{i}, y_{i-1}). - 腕 i のモードが
R
のとき,(x_i, y_i) = (x_{i-1} + d_{i}, y_{i-1}). - 腕 i のモードが
D
のとき,(x_i, y_i) = (x_{i-1}, y_{i-1} - d_{i}). - 腕 i のモードが
U
のとき,(x_i, y_i) = (x_{i-1}, y_{i-1} + d_{i}).
すぬけ君は,腕のモードをうまく指定することにより,N 個の点 (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) のいずれにもロボットアームの関節 m をちょうど一致させられるようなロボットアームを導入したいです. このようなことは可能でしょうか? 可能ならば,条件を満たすロボットアームおよび,各点 (X_j, Y_j) にそのロボットアームの関節 m を到達させるためのモードの指定方法を求めてください.
制約
- 入力はすべて整数である
- 1 \leq N \leq 1000
- -10^9 \leq X_i \leq 10^9
- -10^9 \leq Y_i \leq 10^9
部分点
- 300 点分のテストケースでは,-10 \leq X_i \leq 10 および -10 \leq Y_i \leq 10 が成り立つ.
入力
入力は以下の形式で標準入力から与えられる.
N X_1 Y_1 X_2 Y_2 : X_N Y_N
出力
条件を満たすことが可能なら,次の形式で出力せよ.不可能な場合は -1
を出力せよ.
m d_1 d_2 ... d_m w_1 w_2 : w_N
m および d_i はロボットアームの情報を表す.それぞれの変数の意味は問題文を参照せよ. ここで,1 \leq m \leq 40, 1 \leq d_i \leq 10^{12} かつ m, d_i はすべて整数でなければならない.
w_j は m 文字からなる文字列であり,点 (X_j, Y_j) にロボットアームの関節 m を到達させる方法を表す.
w_j の i 文字目は L
, R
, D
, U
のいずれかの文字であり,腕 i のモードを表す.
入力例 1
3 -1 0 0 3 2 -1
出力例 1
2 1 2 RL UU DR
それぞれの (X_j, Y_j) にロボットアームの関節 m を到達させる方法において,各関節の位置は次のようになります.
- (X_1, Y_1) = (-1, 0) に到達させる方法では,まず関節 0 の位置は (x_0, y_0) = (0, 0) です.腕 1 のモードが
R
なので,関節 1 の位置は (x_1, y_1) = (1, 0) です.腕 2 のモードがL
なので,関節 m=2 の位置は (x_2, y_2) = (-1, 0) です. - (X_2, Y_2) = (0, 3) に到達させる方法では,(x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3) です.
- (X_3, Y_3) = (2, -1) に到達させる方法では,(x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1) です.
入力例 2
5 0 0 1 0 2 0 3 0 4 0
出力例 2
-1
入力例 3
2 1 1 1 1
出力例 3
2 1 1 RU UR
(X_j, Y_j) の中に同じ点が含まれることもあります.
入力例 4
3 -7 -3 7 3 -3 -7
出力例 4
5 3 1 4 1 5 LRDUL RDULR DULRD
Score : 600 points
Problem Statement
Snuke is introducing a robot arm with the following properties to his factory:
- The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.
- For each section, its mode can be specified individually. There are four modes:
L
,R
,D
andU
. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):- (x_0, y_0) = (0, 0).
- If the mode of Section i is
L
, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}). - If the mode of Section i is
R
, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}). - If the mode of Section i is
D
, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}). - If the mode of Section i is
U
, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).
Snuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections. Is this possible? If so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).
Constraints
- All values in input are integers.
- 1 \leq N \leq 1000
- -10^9 \leq X_i \leq 10^9
- -10^9 \leq Y_i \leq 10^9
Partial Score
- In the test cases worth 300 points, -10 \leq X_i \leq 10 and -10 \leq Y_i \leq 10 hold.
Input
Input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 : X_N Y_N
Output
If the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1
.
m d_1 d_2 ... d_m w_1 w_2 : w_N
m and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means. Here, 1 \leq m \leq 40 and 1 \leq d_i \leq 10^{12} must hold. Also, m and d_i must all be integers.
w_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).
The i-th character of w_j should be one of the letters L
, R
, D
and U
, representing the mode of Section i.
Sample Input 1
3 -1 0 0 3 2 -1
Sample Output 1
2 1 2 RL UU DR
In the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:
- To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is
R
, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 isL
, the position of Joint 2 is (x_2, y_2) = (-1, 0). - To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).
- To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1).
Sample Input 2
5 0 0 1 0 2 0 3 0 4 0
Sample Output 2
-1
Sample Input 3
2 1 1 1 1
Sample Output 3
2 1 1 RU UR
There may be duplicated points among (X_j, Y_j).
Sample Input 4
3 -7 -3 7 3 -3 -7
Sample Output 4
5 3 1 4 1 5 LRDUL RDULR DULRD
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 700 点
問題文
長さ n の文字列 s が与えられます。 以下の条件を満たす n 頂点の木は存在するでしょうか?
- 各頂点には 1,2,..., n の番号が付けられている
- 各辺には 1,2,..., n-1 の番号が付けられていて、i 番目の辺は 頂点 u_i と v_i をつないでいる
- s の i 番目の文字が
1
であるとき、 木からある辺を 1 つ取り除くことで、サイズ i の連結成分が作れる - s の i 番目の文字が
0
であるとき、 木からどのように辺を 1 つ取り除いてもサイズ i の連結成分が作れない
条件を満たす木が存在する場合、1 つ構築してください。
制約
- 2\leq n \leq 10^5
- s は
0
と1
からなる長さ n の文字列
入力
入力は以下の形式で標準入力から与えられる。
s
出力
条件を満たす n 頂点の木が存在しない場合、-1
と出力してください。
条件を満たす n 頂点の木が存在する場合、 n-1 行出力してください。 i 行目には u_i,v_i を空白区切りで出力してください。 複数の木が条件を満たす場合、どれを出力しても構いません。
入力例 1
1111
出力例 1
-1
n 頂点の木から辺を 1 つ取り除いてサイズ n の連結成分を作ることはできません。
入力例 2
1110
出力例 2
1 2 2 3 3 4
1 番目の辺または 3 番目の辺を取り除くと、サイズ 1 の連結成分とサイズ 3 の連結成分ができます。 2 番目の辺を取り除くと、サイズ 2 の連結成分が 2 つできます。
入力例 3
1010
出力例 3
1 2 1 3 1 4
どの辺を取り除いても、サイズ 1 の連結成分とサイズ 3 の連結成分ができます。
Score : 700 points
Problem Statement
You are given a string s of length n. Does a tree with n vertices that satisfies the following conditions exist?
- The vertices are numbered 1,2,..., n.
- The edges are numbered 1,2,..., n-1, and Edge i connects Vertex u_i and v_i.
- If the i-th character in s is
1
, we can have a connected component of size i by removing one edge from the tree. - If the i-th character in s is
0
, we cannot have a connected component of size i by removing any one edge from the tree.
If such a tree exists, construct one such tree.
Constraints
- 2 \leq n \leq 10^5
- s is a string of length n consisting of
0
and1
.
Input
Input is given from Standard Input in the following format:
s
Output
If a tree with n vertices that satisfies the conditions does not exist, print -1
.
If a tree with n vertices that satisfies the conditions exist, print n-1 lines. The i-th line should contain u_i and v_i with a space in between. If there are multiple trees that satisfy the conditions, any such tree will be accepted.
Sample Input 1
1111
Sample Output 1
-1
It is impossible to have a connected component of size n after removing one edge from a tree with n vertices.
Sample Input 2
1110
Sample Output 2
1 2 2 3 3 4
If Edge 1 or Edge 3 is removed, we will have a connected component of size 1 and another of size 3. If Edge 2 is removed, we will have two connected components, each of size 2.
Sample Input 3
1010
Sample Output 3
1 2 1 3 1 4
Removing any edge will result in a connected component of size 1 and another of size 3.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 900 点
問題文
長さ N の数列 D_1, D_2, ..., D_N が与えられます。 D_i の値はすべて異なります。 以下の条件を満たす N 頂点の木は存在するでしょうか?
- 各頂点には 1,2,..., N の番号が付けられている
- 各辺には 1,2,..., N-1 の番号が付けられていて、i 番目の辺は頂点 u_i と v_i をつないでいる
- 各頂点 i に対して、i から他の頂点までの距離の和は D_i である。ただし、各辺の長さは 1 であるものとする。
条件を満たす木が存在する場合、1 つ構築してください。
制約
- 2 \leq N \leq 100000
- 1 \leq D_i \leq 10^{12}
- D_i はすべて異なる
入力
入力は以下の形式で標準入力から与えられる.
N D_1 D_2 : D_N
出力
条件を満たす N 頂点の木が存在しない場合、-1
と出力してください。
条件を満たす N 頂点の木が存在する場合、N-1 行出力してください。 i 行目には u_i,v_i を空白区切りで出力してください。 複数の木が条件を満たす場合、どれを出力しても構いません。
入力例 1
7 10 15 13 18 11 14 19
出力例 1
1 2 1 3 1 5 3 4 5 6 6 7
次のような木が条件を満たします。
入力例 2
2 1 2
出力例 2
-1
入力例 3
15 57 62 47 45 42 74 90 75 54 50 66 63 77 87 51
出力例 3
1 10 1 11 2 8 2 15 3 5 3 9 4 5 4 10 5 15 6 12 6 14 7 13 9 12 11 13
Score : 900 points
Problem Statement
You are given a sequence D_1, D_2, ..., D_N of length N. The values of D_i are all distinct. Does a tree with N vertices that satisfies the following conditions exist?
- The vertices are numbered 1,2,..., N.
- The edges are numbered 1,2,..., N-1, and Edge i connects Vertex u_i and v_i.
- For each vertex i, the sum of the distances from i to the other vertices is D_i, assuming that the length of each edge is 1.
If such a tree exists, construct one such tree.
Constraints
- 2 \leq N \leq 100000
- 1 \leq D_i \leq 10^{12}
- D_i are all distinct.
Input
Input is given from Standard Input in the following format:
N D_1 D_2 : D_N
Output
If a tree with n vertices that satisfies the conditions does not exist, print -1
.
If a tree with n vertices that satisfies the conditions exist, print n-1 lines. The i-th line should contain u_i and v_i with a space in between. If there are multiple trees that satisfy the conditions, any such tree will be accepted.
Sample Input 1
7 10 15 13 18 11 14 19
Sample Output 1
1 2 1 3 1 5 3 4 5 6 6 7
The tree shown below satisfies the conditions.
Sample Input 2
2 1 2
Sample Output 2
-1
Sample Input 3
15 57 62 47 45 42 74 90 75 54 50 66 63 77 87 51
Sample Output 3
1 10 1 11 2 8 2 15 3 5 3 9 4 5 4 10 5 15 6 12 6 14 7 13 9 12 11 13