実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。
注釈
この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。
- M \ge 2
- B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
- B_M から B_1 に辺が伸びている。
- i \neq j ならば B_i \neq B_j
制約
- 入力は全て整数
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le N
- A_i \neq i
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
以下の形式で出力せよ。
M B_1 B_2 \dots B_M
M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。
- 2 \le M
- B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
- B_{1} = A_{B_M}
- B_i \neq B_j ( i \neq j )
答えとして考えられるものが複数ある場合、どれを出力しても正解となる。
入力例 1
7 6 7 2 1 3 4 5
出力例 1
4 7 5 3 2
7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。
この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。
4 2 7 5 3
3 4 1 6
グラフが連結であるとは限らないことに注意してください。
入力例 2
2 2 1
出力例 2
2 1 2
1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。
この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 2 で 1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方あることを表現しています。

入力例 3
8 3 7 4 7 3 3 8 2
出力例 3
3 2 7 8
この入力に対応するグラフは以下の通りです。

Score : 350 points
Problem Statement
There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.
Notes
The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:
- M \geq 2
- The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
- The edge from vertex B_M to vertex B_1 exists.
- If i \neq j, then B_i \neq B_j.
Constraints
- All input values are integers.
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le N
- A_i \neq i
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print a solution in the following format:
M B_1 B_2 \dots B_M
M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:
- 2 \le M
- B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
- B_{1} = A_{B_M}
- B_i \neq B_j ( i \neq j )
If multiple solutions exist, any of them will be accepted.
Sample Input 1
7 6 7 2 1 3 4 5
Sample Output 1
4 7 5 3 2
7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.
Here is the graph corresponding to this input:

Here are other acceptable outputs:
4 2 7 5 3
3 4 1 6
Note that the graph may not be connected.
Sample Input 2
2 2 1
Sample Output 2
2 1 2
This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.
Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:

Sample Input 3
8 3 7 4 7 3 3 8 2
Sample Output 3
3 2 7 8
Here is the graph corresponding to this input:

実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
高橋君は医者のすぬけ君から N 種類の薬を処方されました。i 種類目の薬は(処方された日を含めて) a_i 日間、毎日 b_i 錠ずつ飲む必要があります。また、高橋君はこれ以外の薬を飲む必要がありません。
薬を処方された日を 1 日目とします。1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのは何日目かを求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq a_i,b_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K a_1 b_1 \vdots a_N b_N
出力
1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのが X 日目の時、 X を出力せよ。
入力例 1
4 8 6 3 2 5 1 9 4 2
出力例 1
3
1 日目には、高橋君は 1,2,3,4 種類目の薬をそれぞれ 3,5,9,2 錠飲む必要があります。よってこの日は 19 錠飲む必要があり、K(=8) 錠以下ではありません。
2 日目には、高橋君は 1,2,4 種類目の薬をそれぞれ 3,5,2 錠飲む必要があります。よってこの日は 10 錠飲む必要があり、K(=8) 錠以下ではありません。
3 日目には、高橋君は 1,4 種類目の薬をそれぞれ 3,2 錠飲む必要があります。よってこの日は 5 錠飲む必要があり、初めて K(=8) 錠以下になります。
以上より、3 が答えです。
入力例 2
4 100 6 3 2 5 1 9 4 2
出力例 2
1
入力例 3
15 158260522 877914575 2436426 24979445 61648772 623690081 33933447 476190629 62703497 211047202 71407775 628894325 31963982 822804784 50968417 430302156 82631932 161735902 80895728 923078537 7723857 189330739 10286918 802329211 4539679 303238506 17063340 492686568 73361868 125660016 50287940
出力例 3
492686569
Score : 350 points
Problem Statement
Snuke the doctor prescribed N kinds of medicine for Takahashi. For the next a_i days (including the day of the prescription), he has to take b_i pills of the i-th medicine. He does not have to take any other medicine.
Let the day of the prescription be day 1. On or after day 1, when is the first day on which he has to take K pills or less?
Constraints
- 1 \leq N \leq 3 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq a_i,b_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K a_1 b_1 \vdots a_N b_N
Output
If Takahashi has to take K pills or less on day X for the first time on or after day 1, print X.
Sample Input 1
4 8 6 3 2 5 1 9 4 2
Sample Output 1
3
On day 1, he has to take 3,5,9, and 2 pills of the 1-st, 2-nd, 3-rd, and 4-th medicine, respectively. In total, he has to take 19 pills on this day, which is not K(=8) pills or less.
On day 2, he has to take 3,5, and 2 pills of the 1-st, 2-nd, and 4-th medicine, respectively. In total, he has to take 10 pills on this day, which is not K(=8) pills or less.
On day 3, he has to take 3 and 2 pills of the 1-st and 4-th medicine, respectively. In total, he has to take 5 pills on this day, which is K(=8) pills or less for the first time.
Thus, the answer is 3.
Sample Input 2
4 100 6 3 2 5 1 9 4 2
Sample Output 2
1
Sample Input 3
15 158260522 877914575 2436426 24979445 61648772 623690081 33933447 476190629 62703497 211047202 71407775 628894325 31963982 822804784 50968417 430302156 82631932 161735902 80895728 923078537 7723857 189330739 10286918 802329211 4539679 303238506 17063340 492686568 73361868 125660016 50287940
Sample Output 3
492686569
実行時間制限: 2 sec / メモリ制限: 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.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
AtCoder 王国には都市 1,2,\ldots,N の N 個の都市と、道路 1,2,\ldots,M の M 本の道路があります。
道路 i は都市 A_i と B_i を双方向に結び、距離は C_i です。
どの都市間もいくつかの道路を通って行き来することができます。
財政難である王国は、どの都市間もいくつかの道路を通って行き来できるという条件を満たすように N-1 本の道路を保守し、それ以外の道路を廃道にすることにしました。
保守する道路のみを通って都市 1 から都市 i へ移動するときの距離を d_i とするとき、保守する道路の選び方であって、d_2+d_3+\ldots+d_N を最小化するようなものを 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- i\neq j のとき、(A_i,B_i)\neq(A_j,B_j)
- 1\leq C_i \leq 10^9
- どの都市間もいくつかの道路を通って行き来することができる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
保守するような道路の番号を空白区切りで出力せよ。出力の順序は問わない。
答えが複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
3 3 1 2 1 2 3 2 1 3 10
出力例 1
1 2
保守する道路の選び方と d_i の値は次のようになります。
- 道路 1,2 を保守するとき、d_2=1, d_3=3
- 道路 1,3 を保守するとき、d_2=1, d_3=10
- 道路 2,3 を保守するとき、d_2=12, d_3=10
よって、道路 1,2 を保守するときに d_2+d_3 が最小になります。
入力例 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
出力例 2
3 1 2
Score : 500 points
Problem Statement
The Kingdom of AtCoder has N cities called City 1,2,\ldots,N and M roads called Road 1,2,\ldots,M.
Road i connects Cities A_i and B_i bidirectionally and has a length of C_i.
One can travel between any two cities using some roads.
Under financial difficulties, the kingdom has decided to maintain only N-1 roads so that one can still travel between any two cities using those roads and abandon the rest.
Let d_i be the total length of the roads one must use when going from City 1 to City i using only maintained roads. Print a choice of roads to maintain that minimizes d_2+d_3+\ldots+d_N.
Constraints
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- (A_i,B_i)\neq(A_j,B_j) if i\neq j.
- 1\leq C_i \leq 10^9
- One can travel between any two cities using some roads.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the indices of roads to maintain, in arbitrary order, with spaces in between.
If multiple solutions exist, you may print any of them.
Sample Input 1
3 3 1 2 1 2 3 2 1 3 10
Sample Output 1
1 2
Here are the possible choices of roads to maintain and the corresponding values of d_i.
- Maintain Road 1 and 2: d_2=1, d_3=3.
- Maintain Road 1 and 3: d_2=1, d_3=10.
- Maintain Road 2 and 3: d_2=12, d_3=10.
Thus, maintaining Road 1 and 2 minimizes d_2+d_3.
Sample Input 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
Sample Output 2
3 1 2
実行時間制限: 3 sec / メモリ制限: 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.