Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S が与えられます。
ここで、英小文字に含まれるドットの個数を以下のようにして定めます。
- 英小文字が
iまたはjであるとき : 1 個 - 英小文字が
i,jのいずれでもないとき : 0 個
S のすべての文字に対するドットの個数の和を求めてください。
制約
- S は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
aiiaj
出力例 1
3
a, i, i, a, j に含まれるドットの個数はそれぞれ 0, 1, 1, 0, 1 です。
0 + 1 + 1 + 0 + 1 = 3 であるため、3 を出力します。
入力例 2
abcedfgh
出力例 2
0
入力例 3
jjjjjj
出力例 3
6
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Here, we define the number of dots in a lowercase English letter as follows:
- If the lowercase English letter is
iorj: 1 dot - If the lowercase English letter is neither
inorj: 0 dots
Find the sum of the numbers of dots over all characters in S.
Constraints
- S is a string consisting of lowercase English letters with length between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
aiiaj
Sample Output 1
3
The numbers of dots in a, i, i, a, j are 0, 1, 1, 0, 1, respectively.
We have 0 + 1 + 1 + 0 + 1 = 3, so print 3.
Sample Input 2
abcedfgh
Sample Output 2
0
Sample Input 3
jjjjjj
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
実数 X.Y が与えられます。ただし Y はちょうど 1 桁です。
- 0 \leq Y \leq 2 ならば、X-
- 3 \leq Y \leq 6 ならば、X
- 7 \leq Y \leq 9 ならば、X+
と出力してください。
制約
- 1 \leq X \leq 15
- 0 \leq Y \leq 9
- X と Y は整数
入力
入力は以下の形式で標準入力から与えられる。
X.Y
出力
答えを出力せよ。
入力例 1
15.8
出力例 1
15+
15 + のような出力は不正解とみなされます。
X と + の間や、X と - の間には空白を入れずに出力してください。
入力例 2
1.0
出力例 2
1-
1.00 や 1 などの値が入力として与えられることはありません。
入力例 3
12.5
出力例 3
12
Score : 100 points
Problem Statement
You are given a real number X.Y, where Y is a single digit.
Print the following: (quotes for clarity)
- X-, if 0 \leq Y \leq 2;
- X, if 3 \leq Y \leq 6;
- X+, if 7 \leq Y \leq 9.
Constraints
- 1 \leq X \leq 15
- 0 \leq Y \leq 9
- X and Y are integers.
Input
Input is given from Standard Input in the following format:
X.Y
Output
Print the answer.
Sample Input 1
15.8
Sample Output 1
15+
Here, printing 15 + will not be accepted: do not print a space between X and +, or between X and -.
Sample Input 2
1.0
Sample Output 2
1-
You will not get inputs such as 1.00 and 1.
Sample Input 3
12.5
Sample Output 3
12
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 人の人がおり、i 人目 (1\leq i\leq N) の現在の髪の長さは L_i です。
すべての人は 1 日経つごとに髪の長さが 1 ずつ増えます。
髪の長さが T 以上の人が初めて P 人以上になるのは現在から何日後か出力してください。
ただし、現在の時点ですでに髪の長さが T 以上の人が P 人以上にいる場合は 0 を出力してください。
制約
- 1\leq N\leq 100
- 1\leq L_i\leq 100
- 1\leq T\leq 100
- 1\leq P\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N T P L_1 L_2 \ldots L_N
出力
髪の長さが T 以上の人が初めて P 人以上になるのは現在から何日後か出力せよ。 ただし、現在の時点ですでに条件をみたしている場合は 0 を出力せよ。
入力例 1
5 10 3 3 11 1 6 2
出力例 1
7
5 人の人がおり、現在の時点で髪の長さはそれぞれ 3,11,1,6,2 であるため、髪の長さが 10 以上の人は 1 人です。
現在から 7 日後にはそれぞれの人の髪の長さは順に 10,18,8,13,9 となり、髪の長さが 10 以上の人は 3 人となります。
現在から 6 日後の時点では髪の長さが 10 以上の人は 2 人であるため条件をみたしておらず、よって 7 を出力します。
入力例 2
2 5 2 10 10
出力例 2
0
現在の時点ですでに髪の長さが 5 以上の人が 2 人いるため条件をみたしており、0 を出力します。
入力例 3
3 10 1 1 2 3
出力例 3
7
Score : 200 points
Problem Statement
There are N people, and the current hair length of the i-th person (1 \leq i \leq N) is L_i.
Each person's hair grows by 1 per day.
Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time.
If there are already P or more people whose hair length is at least T now, print 0.
Constraints
- 1 \leq N \leq 100
- 1 \leq L_i \leq 100
- 1 \leq T \leq 100
- 1 \leq P \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T P L_1 L_2 \ldots L_N
Output
Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time. If this condition is already satisfied now, print 0.
Sample Input 1
5 10 3 3 11 1 6 2
Sample Output 1
7
There are five people, and their current hair lengths are 3, 11, 1, 6, 2, so there is one person whose hair length is at least 10.
After seven days, the hair lengths of the people will be 10, 18, 8, 13, 9, respectively, and there will be three people whose hair length is at least 10.
After six days, there are only two people whose hair length is at least 10, not satisfying the condition, so print 7.
Sample Input 2
2 5 2 10 10
Sample Output 2
0
Since there are already two people whose hair length is at least 5 now, satisfying the condition, so print 0.
Sample Input 3
3 10 1 1 2 3
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
N 頂点の単純無向グラフ G があり、グラフの頂点には 1,2,\ldots, N の番号が付けられています。
G の隣接行列 (A_{i,j}) が与えられます。すなわち、G は A_{i,j} = 1 であるとき、またそのときに限り頂点 i と頂点 j を結ぶ辺を持ちます。
i = 1, 2, \ldots, N について、頂点 i と直接結ばれている頂点の番号を昇順に出力してください。
ただし、頂点 i と頂点 j が直接結ばれているとは、頂点 i と頂点 j を結ぶ辺が存在することをいいます。
制約
- 2 \leq N \leq 100
- A_{i,j} \in \lbrace 0,1 \rbrace
- A_{i,i} = 0
- A_{i,j} = A_{j,i}
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
出力
N 行出力せよ。 i 行目には頂点 i と直接結ばれている頂点の番号を昇順に空白区切りで出力せよ。
入力例 1
4 0 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0
出力例 1
2 3 1 4 1 2
頂点 1 と直接結ばれている頂点は頂点 2, 3 です。したがって、1 行目には 2, 3 をこの順で出力します。
同様に、2 行目には 1, 4 をこの順に、3 行目には 1 を、4 行目には 2 を出力します。
入力例 2
2 0 0 0 0
出力例 2
G に辺が存在しないこともあります。
入力例 3
5 0 1 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 1 0 1 1 0
出力例 3
2 4 5 1 4 5 1 2 5 1 3 4
Score: 150 points
Problem Statement
There is a simple undirected graph G with N vertices labeled with numbers 1, 2, \ldots, N.
You are given the adjacency matrix (A_{i,j}) of G. That is, G has an edge connecting vertices i and j if and only if A_{i,j} = 1.
For each i = 1, 2, \ldots, N, print the numbers of the vertices directly connected to vertex i in ascending order.
Here, vertices i and j are said to be directly connected if and only if there is an edge connecting vertices i and j.
Constraints
- 2 \leq N \leq 100
- A_{i,j} \in \lbrace 0,1 \rbrace
- A_{i,i} = 0
- A_{i,j} = A_{j,i}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
Output
Print N lines. The i-th line should contain the numbers of the vertices directly connected to vertex i in ascending order, separated by a space.
Sample Input 1
4 0 1 1 0 1 0 0 1 1 0 0 0 0 1 0 0
Sample Output 1
2 3 1 4 1 2
Vertex 1 is directly connected to vertices 2 and 3. Thus, the first line should contain 2 and 3 in this order.
Similarly, the second line should contain 1 and 4 in this order, the third line should contain 1, and the fourth line should contain 2.
Sample Input 2
2 0 0 0 0
Sample Output 2
G may have no edges.
Sample Input 3
5 0 1 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 1 0 1 1 0
Sample Output 3
2 4 5 1 4 5 1 2 5 1 3 4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1,2,\dots,N の番号が、辺には 1,2,\dots,M の番号がつけられており、辺 i は頂点 A_i と頂点 B_i を結んでいます。
このグラフがサイクルグラフであるか判定してください。
単純無向グラフとは
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
サイクルグラフとは
頂点に 1, 2, \dots, N の番号が付けられた N 頂点のグラフがサイクルグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
制約
- 3\leq N \leq 2\times 10^5
- 0 \leq M \leq 2\times 10^5
- 1 \leq A_i, B_i \leq N
- 与えられるグラフは単純
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
与えられたグラフがサイクルグラフであるなら Yes、そうでないなら No と出力せよ。
入力例 1
4 4 2 4 3 1 4 1 2 3
出力例 1
Yes
与えられたグラフは以下の通りであり、これはサイクルグラフです。

入力例 2
4 6 1 2 1 3 1 4 2 3 2 4 3 4
出力例 2
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 connects vertices A_i and B_i.
Determine whether this graph is a cycle graph.
Definition of simple undirected graph
A simple undirected graph is a graph with undirected edges without self-loops or multi-edges.
Definition of cycle graph
An N-vertex graph with vertices labeled 1,2,\dots,N is a cycle graph when there exists a permutation (v_1,v_2,\dots,v_N) of (1,2,\dots,N) such that:
Constraints
- 3 \le N \le 2\times 10^5
- 0 \le M \le 2\times 10^5
- 1 \le A_i, B_i \le N
- The given graph is simple.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Output Yes if the given graph is a cycle graph; otherwise, print No.
Sample Input 1
4 4 2 4 3 1 4 1 2 3
Sample Output 1
Yes
The given graph is as follows, and this is a cycle graph.

Sample Input 2
4 6 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output 2
No
The given graph is as follows, and this is not a cycle graph.

Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 枚の靴下があります。i 枚目の靴下の色は A_i です。
あなたは以下の操作をできるだけ多い回数行いたいです。最大で何回行うことができますか?
- まだペアになっていない靴下の中から同じ色の靴下を 2 枚選んでペアにする。
制約
- 1\leq N \leq 5\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
6 4 1 7 4 1 4
出力例 1
2
以下のようにして、2 回の操作を行うことができます。
- 色が 1 である靴下を 2 枚選んでペアにする。
- 色が 4 である靴下を 2 枚選んでペアにする。
このとき、色が 4 である靴下と 7 である靴下が 1 枚ずつ残るため、これ以上操作はできません。 また、どのように操作をしても 3 回以上操作を行うことはできないため、2 を出力します。
入力例 2
1 158260522
出力例 2
0
入力例 3
10 295 2 29 295 29 2 29 295 2 29
出力例 3
4
Score : 300 points
Problem Statement
You have N socks. The color of the i-th sock is A_i.
You want to perform the following operation as many times as possible. How many times can it be performed at most?
- Choose two same-colored socks that are not paired yet, and pair them.
Constraints
- 1\leq N \leq 5\times 10^5
- 1\leq A_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print an integer representing the answer.
Sample Input 1
6 4 1 7 4 1 4
Sample Output 1
2
You can do the operation twice as follows.
- Choose two socks with the color 1 and pair them.
- Choose two socks with the color 4 and pair them.
Then, you will be left with one sock with the color 4 and another with the color 7, so you can no longer do the operation. There is no way to do the operation three or more times, so you should print 2.
Sample Input 2
1 158260522
Sample Output 2
0
Sample Input 3
10 295 2 29 295 29 2 29 295 2 29
Sample Output 3
4
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。上から i 行目、左から j 列目にあるマスをマス (i, j) で表します。
N 個のマス (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) は壁になっています。
はじめ、高橋君はマス (r_\mathrm{s}, c_\mathrm{s}) にいます。
高橋君に Q 個の指示が与えられます。
i = 1, 2, \ldots, Q について、i 番目の指示は文字 d_i と正整数 l_i の組で表されます。d_i は L 、R 、U 、D のいずれかの文字であり、それぞれ左、右、上、下の方向を表します。
i 番目の指示に対して高橋君は下記の行動を l_i 回繰り返します。
現在いるマスに対して、d_i が表す向きに壁のないマスが隣接しているなら、そのマスに移動する。 そのようなマスが存在しない場合は、何もしない。
i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマスを出力してください。
制約
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- すべての i = 1, 2, \ldots, Nについて、(r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i)
- 1 \leq Q \leq 2 \times 10^5
- d_i は
L、R、U、Dのいずれかの文字 - 1 \leq l_i \leq 10^9
- d_i 以外の入力値は整数
入力
入力は以下の形式で標準入力から与えられる。
H W r_\mathrm{s} c_\mathrm{s}
N
r_1 c_1
r_2 c_2
\vdots
r_N c_N
Q
d_1 l_1
d_2 l_2
\vdots
d_Q l_Q
出力
Q 行出力せよ。 下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマス (R_i, C_i) を i 行目に出力せよ。
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
入力例 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
出力例 1
4 2 3 2 3 1 3 5
与えられるグリッドと高橋君の初期位置は下記の通りです。
ここで、# は壁のマスを、T は高橋君がいるマスを表し、. がその他のマスを表します。
...#. .#... ..... ...T. ..#..
1 つ目の指示に対して高橋君は、左に 2 マス移動し、高橋君の位置は下記の通り、マス (4, 2) になります。
...#. .#... ..... .T... ..#..
2 つ目の指示に対して高橋君は、上に 1 マスに移動した後、次の移動先が壁であるために「何もしない」を 2 回行います。その結果、高橋君の位置は下記の通り、マス (3, 2) になります。
...#. .#... .T... ..... ..#..
3 つ目の指示に対して高橋君は、左に 1 マス移動した後、次の移動先となるマスが存在しないために「何もしない」を 1 回行います。その結果、高橋君の位置は下記の通り、マス (3, 1) になります。
...#. .#... T.... ..... ..#..
4 つ目の指示に対して高橋君は、右に 4 マス移動し、高橋君の位置は下記の通り、マス (3, 5) になります。
...#. .#... ....T ..... ..#..
入力例 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
出力例 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Score : 400 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
N squares, (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N), have walls.
Takahashi is initially at square (r_\mathrm{s}, c_\mathrm{s}).
Q instructions are given to Takahashi.
For i = 1, 2, \ldots, Q, the i-th instruction is represented by a pair of a character d_i and a positive integer l_i. d_i is one of L, R, U, and D, representing the directions of left, right, up, and down, respectively.
Given the i-th direction, Takahashi repeats the following action l_i times:
If a square without a wall is adjacent to the current square in the direction represented by d_i, move to that square; otherwise, do nothing.
For i = 1, 2, \ldots, Q, print the square where Takahashi will be after he follows the first i instructions.
Constraints
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- (r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i) for all i = 1, 2, \ldots, N.
- 1 \leq Q \leq 2 \times 10^5
- d_i is one of the characters
L,R,U, andD. - 1 \leq l_i \leq 10^9
- All values in the input other than d_i are integers.
Input
The input is given from Standard Input in the following format:
H W r_\mathrm{s} c_\mathrm{s}
N
r_1 c_1
r_2 c_2
\vdots
r_N c_N
Q
d_1 l_1
d_2 l_2
\vdots
d_Q l_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the square (R_i, C_i) where Takahashi will be after he follows the first i instructions, in the following format:
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
Sample Input 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
Sample Output 1
4 2 3 2 3 1 3 5
The given grid and the initial position of Takahashi are as follows, where # denotes a square with a wall, T a square where Takahashi is, and . the other squares:
...#. .#... ..... ...T. ..#..
Given the 1-st instruction, Takahashi moves 2 squares to the left, ending up in square (4, 2) as follows:
...#. .#... ..... .T... ..#..
Given the 2-nd instruction, Takahashi first moves 1 square upward, then he "does nothing" twice because the adjacent square in his direction has a wall. As a result, he ends up in square (3, 2) as follows:
...#. .#... .T... ..... ..#..
Given the 3-rd instruction, Takahashi first moves 1 square to the left, then he "does nothing" once because there is no square in his direction. As a result, he ends up in square (3, 1) as follows:
...#. .#... T.... ..... ..#..
Given the 4-th instruction, Takahashi moves 4 squares to the right, ending up in square (3, 5) as follows:
...#. .#... ....T ..... ..#..
Sample Input 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
Sample Output 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 頂点 0 辺の無向グラフが与えられます。
頂点には 1,2,\dots,N の番号が付けられており、最初は全ての頂点が白色です。
以下の 3 種類のクエリを合計 Q 個処理して下さい。
- タイプ 1 : 頂点 u,v を結ぶ無向辺を追加する。
- タイプ 2 : 頂点 v が白色なら黒色に、黒色なら白色に変更する。
- タイプ 3 : 頂点 v から 0 本以上の辺を辿って黒色の頂点に到達できるか判定し、到達できるなら
Yes、到達できないならNoと答える。
制約
- 入力は全て整数
- 1 \le N \le 2 \times 10^5
- 1 \le Q \le 6 \times 10^5
- タイプ 1 のクエリは以下の制約を満たす。
- 1 \le u < v \le N
- 各クエリについて、そのクエリより前に u,v を結ぶ辺は追加されていない。
- タイプ 2,3 のクエリは以下の制約を満たす。
- 1 \le v \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
但し、 \rm{Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 u v
タイプ 2 のクエリは以下の形式で与えられる。
2 v
タイプ 3 のクエリは以下の形式で与えられる。
3 v
出力
タイプ 3 のクエリが現れる度に、その解答を次の通りに出力せよ。
- 頂点 v から 0 本以上の辺を辿って黒色の頂点に到達できるなら
Yes - 頂点 v から 0 本以上の辺を辿って黒色の頂点に到達できないなら
No
入力例 1
5 12 3 2 2 2 3 2 1 2 5 1 3 4 3 4 3 5 1 4 5 1 1 3 3 1 2 2 3 1
出力例 1
No Yes No Yes Yes No
この入力では、グラフは最初 5 頂点 0 辺です。
この入力には 12 個のクエリが含まれます。
- 1 個目のクエリは
3 2です。- この時点で頂点 2 から 0 本以上の辺を辿って黒色の頂点に到達できないので、
Noと解答します。
- この時点で頂点 2 から 0 本以上の辺を辿って黒色の頂点に到達できないので、
- 2 個目のクエリは
2 2です。- 頂点 2 は白色なので、黒色に変更します。
- 3 個目のクエリは
3 2です。- この時点で頂点 2 から 0 本以上の辺を辿って黒色の頂点 2 に到達できます。よって、
Yesと解答します。
- この時点で頂点 2 から 0 本以上の辺を辿って黒色の頂点 2 に到達できます。よって、
- 4 個目のクエリは
1 2 5です。- 頂点 2,5 を結ぶ辺を追加します。
- 5 個目のクエリは
1 3 4です。- 頂点 3,4 を結ぶ辺を追加します。
- 6 個目のクエリは
3 4です。- この時点で頂点 4 から 0 本以上の辺を辿って黒色の頂点に到達できないので、
Noと解答します。
- この時点で頂点 4 から 0 本以上の辺を辿って黒色の頂点に到達できないので、
- 7 個目のクエリは
3 5です。- この時点で頂点 5 から 0 本以上の辺を辿って黒色の頂点 2 に到達できます。よって、
Yesと解答します。
- この時点で頂点 5 から 0 本以上の辺を辿って黒色の頂点 2 に到達できます。よって、
- 8 個目のクエリは
1 4 5です。- 頂点 4,5 を結ぶ辺を追加します。
- 9 個目のクエリは
1 1 3です。- 頂点 1,3 を結ぶ辺を追加します。
- 10 個目のクエリは
3 1です。- この時点で頂点 1 から 0 本以上の辺を辿って黒色の頂点 2 に到達できます。よって、
Yesと解答します。
- この時点で頂点 1 から 0 本以上の辺を辿って黒色の頂点 2 に到達できます。よって、
- 11 個目のクエリは
2 2です。- 頂点 2 は黒色なので、白色に変更します。
- 12 個目のクエリは
3 1です。- この時点で頂点 1 から 0 本以上の辺を辿って黒色の頂点に到達できないので、
Noと解答します。
- この時点で頂点 1 から 0 本以上の辺を辿って黒色の頂点に到達できないので、
Score : 450 points
Problem Statement
You are given an undirected graph with N vertices and zero edges.
The vertices are numbered 1,2,\dots,N, and initially all vertices are white.
Process a total of Q queries of the following three types:
- Type 1 : Add an undirected edge connecting vertices u and v.
- Type 2 : If vertex v is white, change it to black; if it is black, change it to white.
- Type 3 : Determine whether a black vertex can be reached from vertex v by traversing zero or more edges; report
Yesif reachable, andNootherwise.
Constraints
- All input values are integers.
- 1 \le N \le 2 \times 10^5
- 1 \le Q \le 6 \times 10^5
- Type 1 queries satisfy the following constraints:
- 1 \le u < v \le N
- For each query, no edge connecting u and v has been added before that query.
- Type 2,3 queries satisfy the following constraints:
- 1 \le v \le N
Input
The input is given from Standard Input in the following format:
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
where \rm{Query}_i represents the i-th query.
Type 1 queries are given in the following format:
1 u v
Type 2 queries are given in the following format:
2 v
Type 3 queries are given in the following format:
3 v
Output
For each type 3 query, output the answer as follows:
Yesif a black vertex can be reached from vertex v by traversing zero or more edges;Noif no black vertex can be reached from vertex v by traversing zero or more edges.
Sample Input 1
5 12 3 2 2 2 3 2 1 2 5 1 3 4 3 4 3 5 1 4 5 1 1 3 3 1 2 2 3 1
Sample Output 1
No Yes No Yes Yes No
In this input, the graph initially has five vertices and zero edges.
This input contains 12 queries.
- The 1st query is
3 2.- At this point, no black vertex can be reached from vertex 2 by traversing zero or more edges, so report
No.
- At this point, no black vertex can be reached from vertex 2 by traversing zero or more edges, so report
- The 2nd query is
2 2.- Vertex 2 is white, so change it to black.
- The 3rd query is
3 2.- At this point, black vertex 2 can be reached from vertex 2 by traversing zero or more edges. Therefore, report
Yes.
- At this point, black vertex 2 can be reached from vertex 2 by traversing zero or more edges. Therefore, report
- The 4th query is
1 2 5.- Add an edge connecting vertices 2,5.
- The 5th query is
1 3 4.- Add an edge connecting vertices 3,4.
- The 6th query is
3 4.- At this point, no black vertex can be reached from vertex 4 by traversing zero or more edges, so report
No.
- At this point, no black vertex can be reached from vertex 4 by traversing zero or more edges, so report
- The 7th query is
3 5.- At this point, black vertex 2 can be reached from vertex 5 by traversing zero or more edges. Therefore, report
Yes.
- At this point, black vertex 2 can be reached from vertex 5 by traversing zero or more edges. Therefore, report
- The 8th query is
1 4 5.- Add an edge connecting vertices 4,5.
- The 9th query is
1 1 3.- Add an edge connecting vertices 1,3.
- The 10th query is
3 1.- At this point, black vertex 2 can be reached from vertex 1 by traversing zero or more edges. Therefore, report
Yes.
- At this point, black vertex 2 can be reached from vertex 1 by traversing zero or more edges. Therefore, report
- The 11th query is
2 2.- Vertex 2 is black, so change it to white.
- The 12th query is
3 1.- At this point, no black vertex can be reached from vertex 1 by traversing zero or more edges, so report
No.
- At this point, no black vertex can be reached from vertex 1 by traversing zero or more edges, so report
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の球が左右一列に並んでいます。 左から i 番目の球の色は色 C_i であり、整数 X_i が書かれています。
高橋君の目標は球を左から右に見た時に書かれている数が非減少になるように球を並べ替えることです。 言い換えれば、高橋君の目標は、任意の 1\leq i\leq N-1 について、左から i+1 番目の球に書かれている数 が左から i 番目に書かれている数以上であるようにすることです。
高橋君は目標を達成するために、次の操作を好きなだけ( 0 回でも良い )繰り返すことができます。
1\leq i\leq N-1 をみたす整数 i を選ぶ。
左から i 番目の球と i+1 番目の球の色が異なっているならば、コストを 1 支払う。 (色が等しいならばコストを支払う必要は無い。)
左から i 番目の球と i+1 番目の球を入れ替える。
高橋君が目標を達成するために支払う必要のあるコストの最小値を求めてください。
制約
- 2 \leq N \leq 3\times 10^5
- 1\leq C_i\leq N
- 1\leq X_i\leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_N
出力
高橋君が支払う必要のあるコストの最小値を整数で出力せよ。
入力例 1
5 1 5 2 2 1 3 2 1 2 1
出力例 1
6
球の情報を (色, 整数) で表すとします。 最初の状態は (1,3), (5,2), (2,1), (2,2), (1,1) です。 例えば、高橋君は次のように操作を行うことができます。
- 左から 1 番目の球 (色 1) と 2 番目の球 (色 5) を入れ替える。 球は左から (5,2), (1,3), (2,1), (2,2), (1,1) となる。
- 左から 2 番目の球 (色 1) と 3 番目の球 (色 2) を入れ替える。 球は左から (5,2), (2,1), (1,3), (2,2), (1,1) となる。
- 左から 3 番目の球 (色 1) と 4 番目の球 (色 2) を入れ替える。 球は左から (5,2), (2,1), (2,2), (1,3), (1,1) となる。
- 左から 4 番目の球 (色 1) と 5 番目の球 (色 1) を入れ替える。 球は左から (5,2), (2,1), (2,2), (1,1), (1,3) となる。
- 左から 3 番目の球 (色 2) と 4 番目の球 (色 1) を入れ替える。 球は左から (5,2), (2,1), (1,1), (2,2), (1,3) となる。
- 左から 1 番目の球 (色 5) と 2 番目の球 (色 2) を入れ替える。 球は左から (2,1), (5,2), (1,1), (2,2), (1,3) となる。
- 左から 2 番目の球 (色 5) と 3 番目の球 (色 1) を入れ替える。 球は左から (2,1), (1,1), (5,2), (2,2), (1,3) となる。
最後の操作の後で球に書かれている数は左から順に 1,1,2,2,3 となっているため、高橋君は目的を達成しています。
1,2,3,5,6,7 回目の操作にコストが 1 ずつかかるため、
このとき合計でコストは 6 かかり、このときが最小となります。
4 回目の操作では、入れ替えた球の色がともに色 1 であるためコストがかからないことに注意してください。
入力例 2
3 1 1 1 3 2 1
出力例 2
0
すべての球の色は同じであるため、球の入れ替えにコストがかかることはありません。
入力例 3
3 3 1 2 1 1 2
出力例 3
0
高橋君は一度も操作を行わずとも、目的を達成できています。
Score : 500 points
Problem Statement
There are N balls arranged from left to right. The color of the i-th ball from the left is Color C_i, and an integer X_i is written on it.
Takahashi wants to rearrange the balls so that the integers written on the balls are non-decreasing from left to right. In other words, his objective is to reach a situation where, for every 1\leq i\leq N-1, the number written on the (i+1)-th ball from the left is greater than or equal to the number written on the i-th ball from the left.
For this, Takahashi can repeat the following operation any number of times (possibly zero):
Choose an integer i such that 1\leq i\leq N-1.
If the colors of the i-th and (i+1)-th balls from the left are different, pay a cost of 1. (No cost is incurred if the colors are the same).
Swap the i-th and (i+1)-th balls from the left.
Find the minimum total cost Takahashi needs to pay to achieve his objective.
Constraints
- 2 \leq N \leq 3\times 10^5
- 1\leq C_i\leq N
- 1\leq X_i\leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_N
Output
Print the minimum total cost Takahashi needs to pay to achieve his objective, as an integer.
Sample Input 1
5 1 5 2 2 1 3 2 1 2 1
Sample Output 1
6
Let us represent a ball as (Color, Integer). The initial situation is (1,3), (5,2), (2,1), (2,2), (1,1). Here is a possible sequence of operations for Takahashi:
- Swap the 1-st ball (Color 1) and 2-nd ball (Color 5). Now the balls are arranged in the order (5,2), (1,3), (2,1), (2,2), (1,1).
- Swap the 2-nd ball (Color 1) and 3-rd ball (Color 2). Now the balls are arranged in the order (5,2), (2,1), (1,3), (2,2), (1,1).
- Swap the 3-rd ball (Color 1) and 4-th ball (Color 2). Now the balls are in the order (5,2), (2,1), (2,2), (1,3), (1,1).
- Swap the 4-th ball (Color 1) and 5-th ball (Color 1). Now the balls are in the order (5,2), (2,1), (2,2), (1,1), (1,3).
- Swap the 3-rd ball (Color 2) and 4-th ball (Color 1). Now the balls are in the order(5,2), (2,1), (1,1), (2,2), (1,3).
- Swap the 1-st ball (Color 5) and 2-nd ball (Color 2). Now the balls are in the order (2,1), (5,2), (1,1), (2,2), (1,3).
- Swap the 2-nd ball (Color 5) and 3-rd ball (Color 1). Now the balls are in the order (2,1), (1,1), (5,2), (2,2), (1,3).
After the last operation, the numbers written on the balls are 1,1,2,2,3 from left to right, which achieves Takahashi's objective.
The 1-st, 2-nd, 3-rd, 5-th, 6-th, and 7-th operations incur a cost of 1 each, for a total of 6, which is the minimum. Note that the 4-th operation does not incur a cost since the balls are both in Color 1.
Sample Input 2
3 1 1 1 3 2 1
Sample Output 2
0
All balls are in the same color, so no cost is incurred in swapping balls.
Sample Input 3
3 3 1 2 1 1 2
Sample Output 3
0
Takahashi's objective is already achieved without any operation.