実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, 2, \dots, N の番号が、辺には 1, 2, \dots, M の番号が付けられています。
辺 i \, (i = 1, 2, \dots, M) は頂点 u_i, v_i を結んでいます。
このグラフがパスグラフであるか判定してください。
単純無向グラフとは
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
パスグラフとは
頂点に 1, 2, \dots, N の番号が付けられたN 頂点のグラフがパスグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
- 入力される値は全て整数
- 入力で与えられるグラフは単純
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
与えられたグラフがパスグラフなら Yes
、そうでないなら No
と出力せよ。
入力例 1
4 3 1 3 4 2 3 2
出力例 1
Yes
与えらえたグラフは下図のようであり、これはパスグラフです。
入力例 2
2 0
出力例 2
No
与えらえたグラフは下図のようであり、これはパスグラフではありません。
入力例 3
5 5 1 2 2 3 3 4 4 5 5 1
出力例 3
No
与えらえたグラフは下図のようであり、これはパスグラフではありません。
Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, 2, \dots, N, and the edges are numbered 1, 2, \dots, M.
Edge i \, (i = 1, 2, \dots, M) connects vertices u_i and v_i.
Determine if this graph is a path graph.
What is a simple undirected graph?
A simple undirected graph is a graph without self-loops or multiple edges whose edges do not have a direction.
What is a path graph?
A graph with N vertices numbered 1, 2, \dots, N is said to be a path graph if and only if there is a sequence (v_1, v_2, \dots, v_N) that is a permutation of (1, 2, \dots, N) and satisfies the following conditions:
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
- All values in the input are integers.
- The graph given in the input is simple.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print Yes
if the given graph is a path graph; print No
otherwise.
Sample Input 1
4 3 1 3 4 2 3 2
Sample Output 1
Yes
Illustrated below is the given graph, which is a path graph.
Sample Input 2
2 0
Sample Output 2
No
Illustrated below is the given graph, which is not a path graph.
Sample Input 3
5 5 1 2 2 3 3 4 4 5 5 1
Sample Output 3
No
Illustrated below is the given graph, which is not a path graph.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる 2 つの文字列 S, T が与えられます。 次の操作を好きな回数( 0 回でも良い)行うことで、S を T と一致させることができるかを判定してください。
S において同じ文字が 2 文字連続しているところの間に、その文字と同じ文字を 1 つ挿入する。 すなわち、下記の 3 つの手順からなる操作を行う。
- 現在の S の長さを N とし、S = S_1S_2\ldots S_N とする。
- 1 以上 N-1 以下の整数 i であって、S_i = S_{i+1} を満たすものを 1 つ選択する。(ただし、そのような i が存在しない場合は、何もせずに手順 3.をスキップして操作を終了する。)
- S の i 文字目と i+1 文字目の間に文字 S_i(= S_{i+1}) を 1 つ挿入する。その結果、S は長さ N+1 の文字列 S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N となる。
制約
- S と T はそれぞれ英小文字からなる長さ 2 以上 2 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S を T と一致させることができる場合は Yes
を、そうでない場合は No
を出力せよ。
ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。
入力例 1
abbaac abbbbaaac
出力例 1
Yes
下記の 3 回の操作によって、S = abbaac
を T = abbbbaaac
に一致させることができます。
- まず、S の 2 文字目と 3 文字目の間に
b
を挿入する。その結果、S =abbbaac
となる。 - 次に、再び S の 2 文字目と 3 文字目の間に
b
を挿入する。その結果、S =abbbbaac
となる。 - 最後に、S の 6 文字目と 7 文字目の間に
a
を挿入する。その結果、S =abbbbaaac
となる。
よって、Yes
を出力します。
入力例 2
xyzz xyyzz
出力例 2
No
どのように操作を行っても、 S = xyzz
を T = xyyzz
に一致させることはできません。
よって、No
を出力します。
Score : 300 points
Problem Statement
You are given two strings S and T. Determine whether it is possible to make S equal T by performing the following operation some number of times (possibly zero).
Between two consecutive equal characters in S, insert a character equal to these characters. That is, take the following three steps.
- Let N be the current length of S, and S = S_1S_2\ldots S_N.
- Choose an integer i between 1 and N-1 (inclusive) such that S_i = S_{i+1}. (If there is no such i, do nothing and terminate the operation now, skipping step 3.)
- Insert a single copy of the character S_i(= S_{i+1}) between the i-th and (i+1)-th characters of S. Now, S is a string of length N+1: S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N.
Constraints
- Each of S and T is a string of length between 2 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S T
Output
If it is possible to make S equal T, print Yes
; otherwise, print No
.
Note that the judge is case-sensitive.
Sample Input 1
abbaac abbbbaaac
Sample Output 1
Yes
You can make S = abbaac
equal T = abbbbaaac
by the following three operations.
- First, insert
b
between the 2-nd and 3-rd characters of S. Now, S =abbbaac
. - Next, insert
b
again between the 2-nd and 3-rd characters of S. Now, S =abbbbaac
. - Lastly, insert
a
between the 6-th and 7-th characters of S. Now, S =abbbbaaac
.
Thus, Yes
should be printed.
Sample Input 2
xyzz xyyzz
Sample Output 2
No
No sequence of operations makes S = xyzz
equal T = xyyzz
.
Thus, No
should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
縦 N 行 横 N 列のマスからなるグリッドがあります。ここで、N は 45 以下の奇数です。
上から i 行目、左から j 列目のマスをマス (i,j) と表します。
このグリッドに、以下の条件を満たすように高橋君と 1 から N^2-1 までの番号がついた N^2-1 個のパーツからなる 1 匹の龍を配置します。
- 高橋君はグリッドの中央、すなわちマス (\frac{N+1}{2},\frac{N+1}{2}) に配置しなければならない。
- 高橋君がいるマスを除く各マスには龍のパーツをちょうど 1 つ配置しなければならない。
- 2 \leq x \leq N^2-1 を満たす全ての整数 x について、龍のパーツ x はパーツ x-1 があるマスと辺で隣接するマスに配置しなければならない。
- マス (i,j) とマス (k,l) が辺で隣接するとは、|i-k|+|j-l|=1 であることを意味します。
条件を満たす配置方法を 1 つ出力してください。なお、条件を満たす配置は必ず存在します。
制約
- 3 \leq N \leq 45
- N は奇数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N 行出力せよ。
X_{i,j} を、マス (i,j) に高橋君を配置するとき T
、パーツ x を配置するとき x とし、i 行目には X_{i,1},\ldots,X_{i,N} を空白区切りで出力せよ。
入力例 1
5
出力例 1
1 2 3 4 5 16 17 18 19 6 15 24 T 20 7 14 23 22 21 8 13 12 11 10 9
この他、以下の出力も条件をすべて満たすため正解となります。
9 10 11 14 15 8 7 12 13 16 5 6 T 18 17 4 3 24 19 20 1 2 23 22 21
一方、以下の出力はそれぞれ不正解となります。
高橋君が中央にいない。
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 T
パーツ 23 とパーツ 24 のあるマスが辺で隣接していない。
1 2 3 4 5 10 9 8 7 6 11 12 24 22 23 14 13 T 21 20 15 16 17 18 19
Score : 350 points
Problem Statement
There is a grid with N rows and N columns, where N is an odd number at most 45.
Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.
In this grid, you will place Takahashi and a dragon consisting of N^2-1 parts numbered 1 to N^2-1 in such a way that satisfies the following conditions:
- Takahashi must be placed at the center of the grid, that is, in cell (\frac{N+1}{2},\frac{N+1}{2}).
- Except for the cell where Takahashi is, exactly one dragon part must be placed in each cell.
- For every integer x satisfying 2 \leq x \leq N^2-1, the dragon part x must be placed in a cell adjacent by an edge to the cell containing part x-1.
- Cells (i,j) and (k,l) are said to be adjacent by an edge if and only if |i-k|+|j-l|=1.
Print one way to arrange the parts to satisfy the conditions. It is guaranteed that there is at least one arrangement that satisfies the conditions.
Constraints
- 3 \leq N \leq 45
- N is odd.
Input
The input is given from Standard Input in the following format:
N
Output
Print N lines.
The i-th line should contain X_{i,1},\ldots,X_{i,N} separated by spaces, where X_{i,j} is T
when placing Takahashi in cell (i,j) and x when placing part x there.
Sample Input 1
5
Sample Output 1
1 2 3 4 5 16 17 18 19 6 15 24 T 20 7 14 23 22 21 8 13 12 11 10 9
The following output also satisfies all the conditions and is correct.
9 10 11 14 15 8 7 12 13 16 5 6 T 18 17 4 3 24 19 20 1 2 23 22 21
On the other hand, the following outputs are incorrect for the reasons given.
Takahashi is not at the center.
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 T
The cells containing parts 23 and 24 are not adjacent by an edge.
1 2 3 4 5 10 9 8 7 6 11 12 24 22 23 14 13 T 21 20 15 16 17 18 19
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
座標平面上に N 個の点 P_1,P_2,\ldots,P_N があり、点 P_i の座標は (X_i,Y_i) です。
2 つの点 A,B の距離 \text{dist}(A,B) を次のように定義します。
最初、ウサギが点 A にいる。
(x,y) にいるウサギは (x+1,y+1), (x+1,y-1), (x-1,y+1), (x-1,y-1) のいずれかに 1 回のジャンプで移動することができる。
点 A から点 B まで移動するために必要なジャンプの回数の最小値を \text{dist}(A,B) として定義する。
ただし、何度ジャンプを繰り返しても点 A から点 B まで移動できないとき、\text{dist}(A,B)=0 とする。
\displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i,P_j) を求めてください。
制約
- 2\leq N\leq 2\times 10^5
- 0\leq X_i,Y_i\leq 10^8
- i\neq j ならば (X_i,Y_i)\neq (X_j,Y_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
\displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i,P_j) の値を整数で出力せよ。
入力例 1
3 0 0 1 3 5 6
出力例 1
3
P_1,P_2,P_3 の座標はそれぞれ (0,0), (1,3), (5,6) です。
P_1 から P_2 へはウサギは (0,0)\to (1,1)\to (0,2)\to (1,3) と 3 回で移動でき、2 回以下では P_1 から P_2 まで移動できないため、
\text{dist}(P_1,P_2)=3 です。
P_1 から P_3 および P_2 から P_3 へはウサギは移動できないため、\text{dist}(P_1,P_3)=\text{dist}(P_2,P_3)=0 となります。
よって、答えは \displaystyle\sum_{i=1}^{2}\displaystyle\sum_{j=i+1}^3\text{dist}(P_i,P_j)=\text{dist}(P_1,P_2)+\text{dist}(P_1,P_3)+\text{dist}(P_2,P_3)=3+0+0=3 となります。
入力例 2
5 0 5 1 7 2 9 3 8 4 6
出力例 2
11
Score: 500 points
Problem Statement
On a coordinate plane, there are N points P_1, P_2, \ldots, P_N, where point P_i has coordinates (X_i, Y_i).
The distance \text{dist}(A, B) between two points A and B is defined as follows:
A rabbit is initially at point A.
A rabbit at position (x, y) can jump to (x+1, y+1), (x+1, y-1), (x-1, y+1), or (x-1, y-1) in one jump.
\text{dist}(A, B) is defined as the minimum number of jumps required to get from point A to point B.
If it is impossible to get from point A to point B after any number of jumps, let \text{dist}(A, B) = 0.
Calculate the sum \displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i, P_j).
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i, Y_i \leq 10^8
- For i \neq j, (X_i, Y_i) \neq (X_j, Y_j)
- All input values are integers.
Input
The 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 value of \displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i, P_j) as an integer.
Sample Input 1
3 0 0 1 3 5 6
Sample Output 1
3
P_1, P_2, and P_3 have coordinates (0,0), (1,3), and (5,6), respectively.
The rabbit can get from P_1 to P_2 in three jumps via (0,0) \to (1,1) \to (0,2) \to (1,3), but not in two or fewer jumps,
so \text{dist}(P_1, P_2) = 3.
The rabbit cannot get from P_1 to P_3 or from P_2 to P_3, so \text{dist}(P_1, P_3) = \text{dist}(P_2, P_3) = 0.
Therefore, the answer is \displaystyle\sum_{i=1}^{2}\displaystyle\sum_{j=i+1}^3\text{dist}(P_i, P_j)=\text{dist}(P_1, P_2)+\text{dist}(P_1, P_3)+\text{dist}(P_2, P_3)=3+0+0=3.
Sample Input 2
5 0 5 1 7 2 9 3 8 4 6
Sample Output 2
11
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
キーエンスの工場長であるあなたは、ベルトコンベア上のいくつかの区間をセンサーによって監視したいと考えています。 あなたが監視したい区間は全部で N 個あり、i 個目の区間の長さは D_i メートルです。
センサーには 2 種類の候補があり、それぞれのセンサーに関する情報は以下の通りです。
- センサー j\ (1\leq j \leq 2) : 長さ L_j メートルの区間を監視できる。 価格は 1 個あたり C_j であり、全体で最大 K_j 個まで使用することができる。
1 つの区間をいくつかの区間に分割して監視することもできます。 また、センサーが監視する区間が重なっていたり、監視したい区間の長さより余分に監視していたりしても問題はありません。 例えば、L_1=4,L_2=2 であるとき、センサー 1 を 1 つ使って長さ 3 メートルの区間を監視したり、センサー 1,2 を 1 つずつ使って長さ 5 メートルの区間を監視したりすることが可能です。
N 個の区画をすべて監視することが可能であるか判定し、可能ならば必要なセンサーの価格の総和の最小値を求めてください。
制約
- 1\leq N \leq 100
- 1\leq D_i,L_j \leq 10^5
- 1\leq C_j \leq 10^9
- 1\leq K_j \leq 10^3
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N D_1 D_2 \dots D_N L_1 C_1 K_1 L_2 C_2 K_2
出力
N 個の区画をすべて監視することが不可能ならば -1
を、可能ならば必要なセンサーの価格の総和の最小値を出力せよ。
入力例 1
3 3 5 10 4 3 3 2 2 6
出力例 1
17
以下のようにすることで、センサー 1 を 3 つ、センサー 2 を 4 つ使ってすべての区間を監視できます。
- センサー 1 を 1 つ使って 1 個目の区間を監視する。
- センサー 1,2 を 1 つずつ使って 2 個目の区間を監視する。
- センサー 1 を 1 つ、センサー 2 を 3 つ使って 3 個目の区間を監視する。
このとき、必要なセンサーの価格の総和は 3\times 3 + 2\times 4 = 17 であり、これが最小です。
入力例 2
3 3 5 10 4 3 3 2 2 3
出力例 2
-1
入力例 3
2 4 8 3 1 100 4 10000 100
出力例 3
5
1 つも使わない種類のセンサーがあっても構いません。
Score : 500 points
Problem Statement
As the factory manager of Keyence, you want to monitor several sections on a conveyor belt. There are a total of N sections you want to monitor, and the length of the i-th section is D_i meters.
There are two types of sensors to choose from, and below is some information about each sensor.
- Type-j sensor (1\leq j \leq 2): Can monitor a section of length L_j meters. The price is C_j per sensor, and you can use at most K_j sensors of this type in total.
You can divide one section into several sections for monitoring. It is fine if the sections monitored by the sensors overlap, or if they monitor more than the length of the section you want to monitor. For example, when L_1=4 and L_2=2, you can use one type-1 sensor to monitor a section of length 3 meters, or use one type-1 and one type-2 sensor to monitor a section of length 5 meters.
Determine whether it is possible to monitor all N sections, and if it is possible, find the minimum total cost of the necessary sensors.
Constraints
- 1\leq N \leq 100
- 1\leq D_i,L_j \leq 10^5
- 1\leq C_j \leq 10^9
- 1\leq K_j \leq 10^3
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D_1 D_2 \dots D_N L_1 C_1 K_1 L_2 C_2 K_2
Output
If it is impossible to monitor all N sections, print -1
. Otherwise, print the minimum total cost of the necessary sensors.
Sample Input 1
3 3 5 10 4 3 3 2 2 6
Sample Output 1
17
You can monitor all sections by using three type-1 sensors and four type-2 sensors as follows.
- Use one type-1 sensor to monitor the first section.
- Use one type-1 and one type-2 sensor to monitor the second section.
- Use one type-1 and three type-2 sensors to monitor the third section.
In this case, the total cost of the necessary sensors is 3\times 3 + 2\times 4 = 17, which is the minimum.
Sample Input 2
3 3 5 10 4 3 3 2 2 3
Sample Output 2
-1
Sample Input 3
2 4 8 3 1 100 4 10000 100
Sample Output 3
5
It is fine if one type of sensor is not used at all.