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: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数 N が与えられるので、以下の条件を全て満たす整数 X の個数を 998244353 で割った余りを求めてください。
- N 桁の正整数である。
- X の各桁を上の位から順に X_1,X_2,\dots,X_N とする。このとき以下の条件を全て満たす。
- 全ての整数 1 \le i \le N に対して、 1 \le X_i \le 9
- 全ての整数 1 \le i \le N-1 に対して、 |X_i-X_{i+1}| \le 1
制約
- N は整数
- 2 \le N \le 10^6
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
4
出力例 1
203
4 桁の整数として、例えば 1111,1234,7878,6545 が問題文中の条件を満たします。
入力例 2
2
出力例 2
25
入力例 3
1000000
出力例 3
248860093
998244353 で割った余りを求めることに注意してください。
Score : 300 points
Problem Statement
Given an integer N, find the number of integers X that satisfy all of the following conditions, modulo 998244353.
- X is an N-digit positive integer.
- Let X_1,X_2,\dots,X_N be the digits of X from top to bottom. They satisfy all of the following:
- 1 \le X_i \le 9 for all integers 1 \le i \le N;
- |X_i-X_{i+1}| \le 1 for all integers 1 \le i \le N-1.
Constraints
- N is an integer.
- 2 \le N \le 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
4
Sample Output 1
203
Some of the 4-digit integers satisfying the conditions are 1111,1234,7878,6545.
Sample Input 2
2
Sample Output 2
25
Sample Input 3
1000000
Sample Output 3
248860093
Be sure to find the count modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
(1, 2, \dots, N) を並び替えて得られる数列 P であって以下の条件を満たすもののうち、辞書順で最小のものを求めてください。
- i = 1, \dots, M に対し、P において A_i は B_i よりも先に現れる。
ただし、そのような P が存在しない場合は -1 と出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
4 3 2 1 3 4 2 4
出力例 1
2 1 3 4
条件を満たす P は (2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1) の 5 つです。これらのうち辞書順で最小のものは (2, 1, 3, 4) です。
入力例 2
2 3 1 2 1 2 2 1
出力例 2
-1
条件を満たす P は存在しません。
Score : 400 points
Problem Statement
Among the sequences P that are permutations of (1, 2, \dots, N) and satisfy the condition below, find the lexicographically smallest sequence.
- For each i = 1, \dots, M, A_i appears earlier than B_i in P.
If there is no such P, print -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Print the answer.
Sample Input 1
4 3 2 1 3 4 2 4
Sample Output 1
2 1 3 4
The following five permutations P satisfy the condition: (2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1). The lexicographically smallest among them is (2, 1, 3, 4).
Sample Input 2
2 3 1 2 1 2 2 1
Sample Output 2
-1
No permutations P satisfy the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純な無向グラフが与えられます。
辺 i は、頂点 A_i と B_i を結んでいます。
頂点 1,2,\ldots,N を順番に消していきます。
なお、頂点 i を消すとは、頂点 i と、頂点 i に接続する全ての辺をグラフから削除することです。
i=1,2,\ldots,N について、頂点 i まで消した時にグラフはいくつの連結成分に分かれていますか?
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min(\frac{N(N-1)}{2} , 2 \times 10^5 )
- 1 \leq A_i \lt B_i \leq N
- i \neq j ならば (A_i,B_i) \neq (A_j,B_j)
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
N 行出力せよ。
i 行目には、頂点 i まで消した時のグラフの連結成分の数を出力せよ。
入力例 1
6 7 1 2 1 4 1 5 2 4 2 3 3 5 3 6
出力例 1
1 2 3 2 1 0

グラフは上図のように変化していきます。
入力例 2
8 7 7 8 3 4 5 6 5 7 5 8 6 7 6 8
出力例 2
3 2 2 1 1 1 1 0
はじめからグラフが非連結なこともあります。
Score : 500 points
Problem Statement
Given is an undirected graph with N vertices and M edges.
Edge i connects Vertices A_i and B_i.
We will erase Vertices 1, 2, \ldots, N one by one.
Here, erasing Vertex i means deleting Vertex i and all edges incident to Vertex i from the graph.
For each i=1, 2, \ldots, N, how many connected components does the graph have when vertices up to Vertex i are deleted?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min(\frac{N(N-1)}{2} , 2 \times 10^5 )
- 1 \leq A_i \lt B_i \leq N
- (A_i,B_i) \neq (A_j,B_j) if i \neq j.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Print N lines.
The i-th line should contain the number of connected components in the graph when vertices up to Vertex i are deleted.
Sample Input 1
6 7 1 2 1 4 1 5 2 4 2 3 3 5 3 6
Sample Output 1
1 2 3 2 1 0

The figure above shows the transition of the graph.
Sample Input 2
8 7 7 8 3 4 5 6 5 7 5 8 6 7 6 8
Sample Output 2
3 2 2 1 1 1 1 0
The graph may be disconnected from the beginning.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
(1,2,\ldots,N) を並び替えた長さ N の順列 P=(P_1,P_2,\ldots,P_N) があります。
あなたが可能な操作は M 種類あり、操作 i は「 P の a_i 番目の要素と P の b_i 番目の要素を入れ替える」というものです。
操作を好きな順序で、合計 5\times 10^5 回以下行うことによって、P を昇順に並び替えることはできますか?
できるならば、操作手順を 1 つ教えてください。できないならばその旨を伝えてください。
制約
- 2\leq N \leq 1000
- P は (1,2,\ldots,N) を並び替えた順列
- 1\leq M \leq \min(2\times 10^5, \frac{N(N-1)}{2})
- 1\leq a_i \lt b_i\leq N
- i\neq j ならば (a_i,b_i)\neq (a_j,b_j)
- 入力に含まれる値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N M a_1 b_1 a_2 b_2 \vdots a_M b_M
出力
P を昇順に並び替えることができるならば以下の形式で出力せよ。
K c_1 c_2 \ldots c_K
ここで、K は操作の回数を表し、c_i(1\leq i \leq K) は i 回目に行う操作が操作 c_i であることを表す。
0\leq K \leq 5\times 10^5 を満たさなければならないことに注意せよ。
P を昇順に並び替えることができないならば、-1 と出力せよ。
入力例 1
6 5 3 2 4 6 1 4 1 5 5 6 1 2 2 3
出力例 1
3 4 2 1
P は、(5,3,2,4,6,1)\to (5,2,3,4,6,1)\to (5,2,3,4,1,6)\to (1,2,3,4,5,6) と変化します。
入力例 2
5 3 4 1 2 5 2 1 3 2 5
出力例 2
-1
P を昇順に並び替えることはできません。
入力例 3
4 1 2 3 4 6 1 2 1 3 1 4 2 3 2 4 3 4
出力例 3
0
初めから P が昇順に並んでいることもあります。
また、以下のような答えも正解になります。
4 5 5 5 5
操作の回数を最小化する必要はないことに注意してください。
Score : 500 points
Problem Statement
We have a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N).
There are M kinds of operations available to you. Operation i swaps the a_i-th and b_i-th elements of P.
Is it possible to sort P in ascending order by doing at most 5\times 10^5 operations in total in any order?
If it is possible, give one such sequence of operations. Otherwise, report so.
Constraints
- 2\leq N \leq 1000
- P is a permutation of (1,2,\ldots,N).
- 1\leq M \leq \min(2\times 10^5, \frac{N(N-1)}{2})
- 1\leq a_i \lt b_i\leq N
- (a_i,b_i)\neq (a_j,b_j) if i\neq j.
- 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 M a_1 b_1 a_2 b_2 \vdots a_M b_M
Output
If it is possible to sort P in ascending order, print the following:
K c_1 c_2 \ldots c_K
Here, K represents the number of operations to do, and c_i (1\leq i \leq K) means the i-th operation to do is Operation c_i.
Note that 0\leq K \leq 5\times 10^5 must hold.
If it is impossible to sort P in ascending order, print -1.
Sample Input 1
6 5 3 2 4 6 1 4 1 5 5 6 1 2 2 3
Sample Output 1
3 4 2 1
P changes as follows: (5,3,2,4,6,1)\to (5,2,3,4,6,1)\to (5,2,3,4,1,6)\to (1,2,3,4,5,6).
Sample Input 2
5 3 4 1 2 5 2 1 3 2 5
Sample Output 2
-1
We cannot sort P in ascending order.
Sample Input 3
4 1 2 3 4 6 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output 3
0
P may already be sorted in ascending order.
Additionally, here is another accepted output:
4 5 5 5 5
Note that it is not required to minimize the number of operations.