実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
文字列 S に対して操作を Q 回行います。 i 回目 (1\leq i\leq Q) の操作は文字の組 (c _ i,d _ i) で表され、次のような操作に対応します。
- S に含まれる文字 c _ i をすべて文字 d _ i で置き換える。
すべての操作が終わったあとの文字列 S を出力してください。
制約
- 1\leq N\leq2\times10^5
- S は英小文字からなる長さ N の文字列
- 1\leq Q\leq2\times10^5
- c _ i,d _ i は英小文字 (1\leq i\leq Q)
- N,Q は整数
入力
入力は以下の形式で標準入力から与えられる。
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
出力
すべての操作が終わったあとの S を出力せよ。
入力例 1
7 atcoder 4 r a t e d v a r
出力例 1
recover
S は atcoder → atcodea → aecodea → aecovea → recover と変化します。
たとえば、4 番目の操作では S={}aecovea に含まれる a (1 文字目と 7 文字目)をすべて r に置き換えるので S={}recover となります。
すべての操作が終わったときには S={}recover となっているため、recover を出力してください。
入力例 2
3 abc 4 a a s k n n z b
出力例 2
abc
c _ i=d _ i であるような操作や S に c _ i が含まれないような操作もあります。
入力例 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
出力例 3
laklimamriiamrmrllrmlrkramrjimrial
Score: 350 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
You will perform an operation Q times on the string S. The i-th operation (1\leq i\leq Q) is represented by a pair of characters (c _ i,d _ i), which corresponds to the following operation:
- Replace all occurrences of the character c _ i in S with the character d _ i.
Print the string S after all operations are completed.
Constraints
- 1\leq N\leq2\times10^5
- S is a string of length N consisting of lowercase English letters.
- 1\leq Q\leq2\times10^5
- c _ i and d _ i are lowercase English letters (1\leq i\leq Q).
- N and Q are integers.
Input
The input is given from Standard Input in the following format:
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
Output
Print the string S after all operations are completed.
Sample Input 1
7 atcoder 4 r a t e d v a r
Sample Output 1
recover
S changes as follows: atcoder → atcodea → aecodea → aecovea → recover.
For example, in the fourth operation, all occurrences of a in S={}aecovea (the first and seventh characters) are replaced with r, resulting in S={}recover.
After all operations are completed, S={}recover, so print recover.
Sample Input 2
3 abc 4 a a s k n n z b
Sample Output 2
abc
There may be operations where c _ i=d _ i or S does not contain c _ i.
Sample Input 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
Sample Output 3
laklimamriiamrmrllrmlrkramrjimrial
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
あなたは以下の連続する操作をちょうど一度だけ行います。
-
整数 x\ (0\leq x \leq N) を選ぶ。x として 0 を選んだ場合何もしない。 x として 1 以上の整数を選んだ場合、A_1,A_2,\ldots,A_x をそれぞれ L で置き換える。
-
整数 y\ (0\leq y \leq N) を選ぶ。y として 0 を選んだ場合何もしない。 y として 1 以上の整数を選んだ場合、A_{N},A_{N-1},\ldots,A_{N-y+1} をそれぞれ R で置き換える。
操作後の A の要素の総和として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq L, R\leq 10^9
- -10^9 \leq A_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 4 3 5 5 0 6 3
出力例 1
14
x=2,y=2 として操作をすると、数列 A = (4,4,0,3,3) となり、要素の総和は 14 になります。
これが達成可能な最小値です。
入力例 2
4 10 10 1 2 3 4
出力例 2
10
x=0,y=0 として操作をすると、数列 A = (1,2,3,4) となり、要素の総和は 10 になります。
これが達成可能な最小値です。
入力例 3
10 -5 -3 9 -6 10 -1 2 10 -1 7 -15 5
出力例 3
-58
L,R,A_i は負であることがあります。
Score : 400 points
Problem Statement
You are given an integer sequence of length N: A=(A_1,A_2,\ldots,A_N).
You will perform the following consecutive operations just once:
-
Choose an integer x (0\leq x \leq N). If x is 0, do nothing. If x is 1 or greater, replace each of A_1,A_2,\ldots,A_x with L.
-
Choose an integer y (0\leq y \leq N). If y is 0, do nothing. If y is 1 or greater, replace each of A_{N},A_{N-1},\ldots,A_{N-y+1} with R.
Print the minimum possible sum of the elements of A after the operations.
Constraints
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq L, R\leq 10^9
- -10^9 \leq A_i\leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L R A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 4 3 5 5 0 6 3
Sample Output 1
14
If you choose x=2 and y=2, you will get A = (4,4,0,3,3), for the sum of 14, which is the minimum sum achievable.
Sample Input 2
4 10 10 1 2 3 4
Sample Output 2
10
If you choose x=0 and y=0, you will get A = (1,2,3,4), for the sum of 10, which is the minimum sum achievable.
Sample Input 3
10 -5 -3 9 -6 10 -1 2 10 -1 7 -15 5
Sample Output 3
-58
L, R, and A_i may be negative.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の重み付き無向連結グラフ G が与えられます。G には自己ループや多重辺が含まれている可能性があります。
頂点には頂点 1, 頂点 2, \dots, 頂点 N と番号がついています。
辺には辺 1, 辺 2, \dots, 辺 M と番号がついています。辺 i は頂点 a_i と頂点 b_i を結ぶ重み c_i の辺です。ここで、1 \leq i \lt j \leq M を満たすすべての整数の組 (i, j) について c_i \neq c_j が成り立ちます。
以下で説明される Q 個のクエリに答えてください。
i 番目のクエリでは整数の組 (u_i, v_i, w_i) が与えられます。ここで、1 \leq j \leq M を満たすすべての整数 j について w_i \neq c_j が成り立ちます。
頂点 u_i と頂点 v_i を結ぶ重み w_i の無向辺を e_i として、G に e_i を追加してできるグラフ G_i を考えます。
このとき G_i の最小全域木 T_i は一意に定まることが証明できますが、T_i に e_i は含まれるでしょうか?答えを Yes あるいは No で出力してください。
ここで、クエリの前後で G は変化しないことに注意してください。言い換えると、クエリ i で G に e_i を追加したグラフを考えたとしても、他のクエリで出てくる G に e_i が追加されていることはありません。
最小全域木とは?
G の 全域木 とは、G に含まれるすべての頂点と G に含まれる辺の一部からなる木のことを言います。G の 最小全域木 とは、G の全域木の中で辺の重みの和が最小である木のことを言います。
制約
- 2 \leq N \leq 2 \times 10^5
- N - 1 \leq M \leq 2 \times 10^5
- 1 \leq a_i \leq N (1 \leq i \leq M)
- 1 \leq b_i \leq N (1 \leq i \leq M)
- 1 \leq c_i \leq 10^9 (1 \leq i \leq M)
- c_i \neq c_j (1 \leq i \lt j \leq M)
- グラフ G は連結である。
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq u_i \leq N (1 \leq i \leq Q)
- 1 \leq v_i \leq N (1 \leq i \leq Q)
- 1 \leq w_i \leq 10^9 (1 \leq i \leq Q)
- w_i \neq c_j (1 \leq i \leq Q, 1 \leq j \leq M)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M Q a_1 b_1 c_1 a_2 b_2 c_2 \vdots a_M b_M c_M u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_Q v_Q w_Q
出力
Q 行出力せよ。i 行目ではクエリ i への答えを Yes または No で出力せよ。
入力例 1
5 6 3 1 2 2 2 3 3 1 3 6 2 4 5 4 5 9 3 5 8 1 3 1 3 4 7 3 5 7
出力例 1
Yes No Yes
以下では頂点 u と頂点 v を結ぶ重み w の無向辺を (u,v,w) と表します。 G を図に表したものを以下に挙げます。

たとえばクエリ 1 では G に e_1 = (1,3,1) を追加したグラフ G_1 を考えます。G_1 の最小全域木 T_1 の辺集合は \lbrace (1,2,2),(1,3,1),(2,4,5),(3,5,8) \rbrace であり e_1 を含みます。よって Yes を出力します。
入力例 2
2 3 2 1 2 100 1 2 1000000000 1 1 1 1 2 2 1 1 5
出力例 2
Yes No
Score : 500 points
Problem Statement
Given is a weighted undirected connected graph G with N vertices and M edges, which may contain self-loops and multi-edges.
The vertices are labeled as Vertex 1, Vertex 2, \dots, Vertex N.
The edges are labeled as Edge 1, Edge 2, \ldots, Edge M. Edge i connects Vertex a_i and Vertex b_i and has a weight of c_i. Here, for every pair of integers (i, j) such that 1 \leq i \lt j \leq M, c_i \neq c_j holds.
Process the Q queries explained below.
The i-th query gives a triple of integers (u_i, v_i, w_i). Here, for every integer j such that 1 \leq j \leq M, w_i \neq c_j holds.
Let e_i be an undirected edge that connects Vertex u_i and Vertex v_i and has a weight of w_i. Consider the graph G_i obtained by adding e_i to G.
It can be proved that the minimum spanning tree T_i of G_i is uniquely determined. Does T_i contain e_i? Print the answer as Yes or No.
Note that the queries do not change T. In other words, even though Query i considers the graph obtained by adding e_i to G, the G in other queries does not have e_i.
What is minimum spanning tree?
The spanning tree of G is a tree with all of the vertices in G and some of the edges in G.The minimum spanning tree of G is the tree with the minimum total weight of edges among the spanning trees of G.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N - 1 \leq M \leq 2 \times 10^5
- 1 \leq a_i \leq N (1 \leq i \leq M)
- 1 \leq b_i \leq N (1 \leq i \leq M)
- 1 \leq c_i \leq 10^9 (1 \leq i \leq M)
- c_i \neq c_j (1 \leq i \lt j \leq M)
- The graph G is connected.
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq u_i \leq N (1 \leq i \leq Q)
- 1 \leq v_i \leq N (1 \leq i \leq Q)
- 1 \leq w_i \leq 10^9 (1 \leq i \leq Q)
- w_i \neq c_j (1 \leq i \leq Q, 1 \leq j \leq M)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M Q a_1 b_1 c_1 a_2 b_2 c_2 \vdots a_M b_M c_M u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_Q v_Q w_Q
Output
Print Q lines. The i-th line should contain the answer to Query i: Yes or No.
Sample Input 1
5 6 3 1 2 2 2 3 3 1 3 6 2 4 5 4 5 9 3 5 8 1 3 1 3 4 7 3 5 7
Sample Output 1
Yes No Yes
Below, let (u,v,w) denote an undirected edge that connects Vertex u and Vertex v and has the weight of w. Here is an illustration of G:

For example, Query 1 considers the graph G_1 obtained by adding e_1 = (1,3,1) to G. The minimum spanning tree T_1 of G_1 has the edge set \lbrace (1,2,2),(1,3,1),(2,4,5),(3,5,8) \rbrace, which contains e_1, so Yes should be printed.
Sample Input 2
2 3 2 1 2 100 1 2 1000000000 1 1 1 1 2 2 1 1 5
Sample Output 2
Yes No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ L のパンが 1 つあり、これを N 人の子供たちに切り分けます。 i 番目 (1\leq i\leq N) の子供は長さ A_i のパンを欲しがっています。
そこで、高橋君は次の操作を繰り返して、長さ A_1,A_2,\ldots,A_N のパンを切り出して配ることにしました。
長さ k のパン 1 つと 1 以上 k-1 以下の整数 x を選ぶ。選んだパンを長さ x のパンと 長さ k-x のパンに切り分ける。
このとき、x の値によらずコストが k かかる。
それぞれの子供に配るパンは長さがちょうど A_i のパン 1 つである必要がありますが、誰にも配られずに余るパンがいくつあっても構いません。
このとき、全ての子供たちにパンを配るために必要な最小のコストを求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1\leq A_i\leq 10^9
- A_1+A_2+\cdots+A_N\leq L\leq 10^{15}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L A_1 A_2 \ldots A_N
出力
全ての子供たちにパンを配るために必要な最小のコストを出力せよ。
入力例 1
5 7 1 2 1 2 1
出力例 1
16
高橋君は次のようにしてパンを切り分けて配ることができます。
- 長さ 7 のパンと整数 x=3 を選ぶ。パンは長さ 3 のパンと長さ 4 のパンに切り分けられる。 (コスト 7 )
- 長さ 3 のパンと整数 x=1 を選ぶ。パンは長さ 1 のパンと長さ 2 のパンに切り分けられる。前者を 1 番目の子供に配る。 (コスト 3 )
- 長さ 2 のパンと整数 x=1 を選ぶ。パンは長さ 1 のパン 2 つに切り分けられる。これを 3,5 番目の子供に配る。 (コスト 2 )
- 長さ 4 のパンと整数 x=2 を選ぶ。パンは長さ 2 のパン 2 つに切り分けられる。これを 2,4 番目の子供に配る。 (コスト 4 )
このとき、コストは 7+3+2+4=16 かかり、これが最小です。 また、余るパンはありません。
入力例 2
3 1000000000000000 1000000000 1000000000 1000000000
出力例 2
1000005000000000
それぞれの子供に配るパンの長さはちょうど A_i でなければならない事に注意してください。
Score : 500 points
Problem Statement
We have a loaf of bread of length L, which we will cut and distribute to N children.
The i-th child (1\leq i\leq N) wants a loaf of length A_i.
Now, Takahashi will repeat the operation below to cut the loaf into lengths A_1, A_2, \ldots, A_N for the children.
Choose a loaf of length k and an integer x between 1 and k-1 (inclusive). Cut the loaf into two loaves of length x and k-x.
This operation incurs a cost of k regardless of the value of x.
Each child i must receive a loaf of length exactly A_i, but it is allowed to leave some loaves undistributed.
Find the minimum cost needed to distribute loaves to all children.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1\leq A_i\leq 10^9
- A_1+A_2+\cdots+A_N\leq L\leq 10^{15}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L A_1 A_2 \ldots A_N
Output
Print the minimum cost needed to distribute loaves to all children.
Sample Input 1
5 7 1 2 1 2 1
Sample Output 1
16
Takahashi can cut the loaf for the children as follows.
- Choose the loaf of length 7 and x=3, cutting it into loaves of length 3 and 4 for a cost of 7.
- Choose the loaf of length 3 and x=1, cutting it into loaves of length 1 and 2 for a cost of 3. Give the former to the 1-st child.
- Choose the loaf of length 2 and x=1, cutting it into two loaves of length 1 for a cost of 2. Give them to the 3-rd and 5-th children.
- Choose the loaf of length 4 and x=2, cutting it into two loaves of length 2 for a cost of 4. Give them to the 2-nd and 4-th children.
This incurs a cost of 7+3+2+4=16, which is the minimum possible. There will be no leftover loaves.
Sample Input 2
3 1000000000000000 1000000000 1000000000 1000000000
Sample Output 2
1000005000000000
Note that each child i must receive a loaf of length exactly A_i.