実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
整数の多重集合 S があります。はじめ S は空です。
Q 個のクエリが与えられるので順に処理してください。 クエリは次の 3 種類のいずれかです。
-
1 x: S に x を 1 個追加する。 -
2 x c: S から x を \mathrm{min}(c, (S に含まれる x の個数 )) 個削除する。 -
3: (S の最大値 )- (S の最小値 ) を出力する。このクエリを処理するとき、 S が空でないことが保証される。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
3のクエリを処理するとき、S は空でない。- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
i 番目のクエリを表す \mathrm{query}_i は以下の 3 種類のいずれかである。
1 x
2 x c
3
出力
3 のクエリに対する答えを順に改行区切りで出力せよ。
入力例 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
出力例 1
1 5 4
多重集合 S は以下のように変化します。
- 1 番目のクエリ : S に 3 を追加する。S は \lbrace3 \rbrace となる。
- 2 番目のクエリ : S に 2 を追加する。S は \lbrace2, 3\rbrace となる。
- 3 番目のクエリ : S = \lbrace 2, 3\rbrace の最大値は 3 、最小値は 2 なので、 3-2=1 を出力する。
- 4 番目のクエリ : S に 2 を追加する。S は \lbrace2,2,3 \rbrace となる。
- 5 番目のクエリ : S に 7 を追加する。S は \lbrace2, 2,3, 7\rbrace となる。
- 6 番目のクエリ : S = \lbrace2,2,3, 7\rbrace の最大値は 7 、最小値は 2 なので、 7-2=5 を出力する。
- 7 番目のクエリ : S に含まれる 2 の個数は 2 個なので、 \mathrm{min(2,3)} = 2 個の 2 を S から削除する。S は \lbrace3, 7\rbrace となる。
- 8 番目のクエリ : S = \lbrace3, 7\rbrace の最大値は 7 、最小値は 3 なので、 7-3=4 を出力する。
入力例 2
4 1 10000 1 1000 2 100 3 1 10
出力例 2
クエリ 3 が含まれない場合、何も出力してはいけません。
Score : 300 points
Problem Statement
We have a multiset of integers S, which is initially empty.
Given Q queries, process them in order. Each query is of one of the following types.
-
1 x: Insert an x into S. -
2 x c: Remove an x from S m times, where m = \mathrm{min}(c,( the number of x's contained in S)). -
3: Print ( maximum value of S)-( minimum value of S). It is guaranteed that S is not empty when this query is given.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
- When a query of type
3is given, S is not empty. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
\mathrm{query}_i, which denotes the i-th query, is in one of the following formats:
1 x
2 x c
3
Output
Print the answers for the queries of type 3 in the given order, separated by newlines.
Sample Input 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
Sample Output 1
1 5 4
The multiset S transitions as follows.
- 1-st query: insert 3 into S. S is now \lbrace 3 \rbrace.
- 2-nd query: insert 2 into S. S is now \lbrace 2, 3 \rbrace.
- 3-rd query: the maximum value of S = \lbrace 2, 3\rbrace is 3 and its minimum value is 2, so print 3-2=1.
- 4-th query: insert 2 into S. S is now \lbrace 2,2,3 \rbrace.
- 5-th query: insert 7 into S. S is now \lbrace 2, 2,3, 7\rbrace.
- 6-th query: the maximum value of S = \lbrace 2,2,3, 7\rbrace is 7 and its minimum value is 2, so print 7-2=5.
- 7-th query: since there are two 2's in S and \mathrm{min(2,3)} = 2, remove 2 from S twice. S is now \lbrace 3, 7\rbrace.
- 8-th query: the maximum value of S = \lbrace 3, 7\rbrace is 7 and its minimum value is 3, so print 7-3=4.
Sample Input 2
4 1 10000 1 1000 2 100 3 1 10
Sample Output 2
If the given queries do not contain that of type 3, nothing should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
頂点に 1 から N の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。
グラフを森にするためには辺を最低何本削除する必要がありますか?
森とは
単純無向グラフ F が森であるとは、F が閉路を含まないことを言います。制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5\right)
- 1 \leq u_i \lt v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
4 4 1 2 1 3 2 4 3 4
出力例 1
1
例えば 1 番目の辺を削除すると、グラフは森になります。
入力例 2
5 0
出力例 2
0
入力例 3
10 10 7 9 4 6 6 10 2 5 5 6 5 9 6 8 4 8 1 5 1 4
出力例 3
2
Score : 350 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges, where the vertices are labeled 1 to N. The i-th edge connects vertices u_i and v_i.
What is the minimum number of edges that need to be deleted from this graph so that the graph becomes a forest?
What is a forest?
A simple undirected graph F is called a forest if and only if F does not contain any cycle.Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5\right)
- 1 \leq u_i < v_i \leq 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 u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
4 4 1 2 1 3 2 4 3 4
Sample Output 1
1
For example, if you delete the first edge, the graph becomes a forest.
Sample Input 2
5 0
Sample Output 2
0
Sample Input 3
10 10 7 9 4 6 6 10 2 5 5 6 5 9 6 8 4 8 1 5 1 4
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
0 および 1 からなる長さ N の文字列 S が与えられます。
ここで、S には 1 が 1 つ以上含まれることが保証されます。
あなたは、以下の操作を繰り返し(0 回でも良い)行うことができます。
- 1\leq i\leq N-1 を満たす整数 i を選び、S の i 文字目と i+1 文字目を入れ替える。
すべての 1 がひとかたまりになった状態にするために必要な操作回数の最小値を求めてください。
なお、すべての 1 がひとかたまりになっているとは、ある整数 l,r\ (1\leq l\leq r\leq N) が存在し、
S の i 文字目は l\leq i\leq r ならば 1、そうでないならば 0 であることをいいます。
制約
- 2\leq N\leq 5\times 10^5
- N は整数
- S は
0および1からなる長さ N の文字列 - S には
1が 1 つ以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
7 0101001
出力例 1
3
例えば、以下のように操作を 3 回行うと、すべての 1 がひとかたまりになります。
- i=2 を選び、S の 2 文字目と 3 文字目を入れ替える。S=
0011001になる。 - i=6 を選び、S の 6 文字目と 7 文字目を入れ替える。S=
0011010になる。 - i=5 を選び、S の 5 文字目と 6 文字目を入れ替える。S=
0011100になる。
2 回以下の操作ですべての 1 をひとかたまりにすることはできないため、答えは 3 です。
入力例 2
3 100
出力例 2
0
既にすべての 1 がひとかたまりになっているため、操作は必要ありません。
入力例 3
10 0101001001
出力例 3
7
Score : 425 points
Problem Statement
You are given a string S of length N consisting of 0 and 1. It is guaranteed that S contains at least one 1.
You may perform the following operation any number of times (possibly zero):
- Choose an integer i (1 \leq i \leq N-1) and swap the i-th and (i+1)-th characters of S.
Find the minimum number of operations needed so that all 1s are contiguous.
Here, all 1s are said to be contiguous if and only if there exist integers l and r (1 \leq l \leq r \leq N) such that the i-th character of S is 1 if and only if l \leq i \leq r, and 0 otherwise.
Constraints
- 2 \leq N \leq 5 \times 10^5
- N is an integer.
- S is a length N string of
0and1. - S contains at least one
1.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
7 0101001
Sample Output 1
3
For example, the following three operations make all 1s contiguous:
- Choose i=2 and swap the 2nd and 3rd characters. Then, S=
0011001. - Choose i=6 and swap the 6th and 7th characters. Then, S=
0011010. - Choose i=5 and swap the 5th and 6th characters. Then, S=
0011100.
It is impossible to do this in two or fewer swaps, so the answer is 3.
Sample Input 2
3 100
Sample Output 2
0
All 1s are already contiguous, so no swaps are needed.
Sample Input 3
10 0101001001
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
英大文字からなる長さ N の文字列 S と、英大文字からなる長さ M\ (\leq N) の文字列 T が与えられます。
# のみからなる長さ N の文字列 X があります。
以下の操作を好きな回数行うことで、X を S に一致させることができるか判定してください。
- X の中から連続する M 文字を選び、T で置き換える。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq \min(N, 5)
- S は英大文字からなる長さ N の文字列
- T は英大文字からなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S T
出力
X を S に一致させることができるならば Yes を、できないならば No を出力せよ。
入力例 1
7 3 ABCBABC ABC
出力例 1
Yes
以下、X の l 文字目から r 文字目までの部分を X[l:r] と表記します。
次のように操作を行うことで、X を S に一致させることができます。
- X[3:5] を T で置き換える。X=
##ABC##になる。 - X[1:3] を T で置き換える。X=
ABCBC##になる。 - X[5:7] を T で置き換える。X=
ABCBABCになる。
入力例 2
7 3 ABBCABC ABC
出力例 2
No
どのように操作を行っても、X を S に一致させることはできません。
入力例 3
12 2 XYXXYXXYYYXY XY
出力例 3
Yes
Score : 475 points
Problem Statement
You are given two strings: S, which consists of uppercase English letters and has length N, and T, which also consists of uppercase English letters and has length M\ (\leq N).
There is a string X of length N consisting only of the character #. Determine whether it is possible to make X match S by performing the following operation any number of times:
- Choose M consecutive characters in X and replace them with T.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq \min(N, 5)
- S is a string consisting of uppercase English letters with length N.
- T is a string consisting of uppercase English letters with length M.
Input
The input is given from Standard Input in the following format:
N M S T
Output
Print Yes if it is possible to make X match S; print No otherwise.
Sample Input 1
7 3 ABCBABC ABC
Sample Output 1
Yes
Below, let X[l:r] denote the part from the l-th through the r-th character of X.
You can make X match S by operating as follows.
- Replace X[3:5] with T. X becomes
##ABC##. - Replace X[1:3] with T. X becomes
ABCBC##. - Replace X[5:7] with T. X becomes
ABCBABC.
Sample Input 2
7 3 ABBCABC ABC
Sample Output 2
No
No matter how you operate, it is impossible to make X match S.
Sample Input 3
12 2 XYXXYXXYYYXY XY
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
N 個の頂点と M 本の辺からなる単純な無向グラフが与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。 また、i = 1, 2, \ldots, N について、頂点 i には正整数 W_i が割り当てられており、A_i 個のコマが置かれています。
グラフ上にコマが存在する限り、下記の操作を繰り返します。
- まず、グラフ上のコマを 1 個選んで取り除き、そのコマが置かれていた頂点を x とおく。
- x に隣接するいくつかの頂点からなる(空でも良い)集合 S であって、\sum_{y \in S} W_y \lt W_x であるものを選び、S に含まれる頂点それぞれに 1 個ずつコマを置く。
操作を行う回数としてあり得る最大値を出力してください。
なお、どのように操作を行っても、有限回の操作の後にはグラフ上にコマが存在しない状態に至ることが証明出来ます。
制約
- 入力される値はすべて整数
- 2 \leq N \leq 5000
- 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
- 1 \leq W_i \leq 5000
- 0 \leq A_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M W_1 W_2 \ldots W_N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 6 1 2 2 3 3 1 3 4 1 5 5 6 9 2 3 1 4 4 1 0 0 0 0 1
出力例 1
5
以下の説明では、各頂点にあるコマの個数を、数列 A = (A_1, A_2, \ldots, A_N) として表します。 はじめ、A = (1, 0, 0, 0, 0, 1) です。
下記の手順で操作を行うことを考えます。
- 頂点 1 にあるコマを 1 個取り除き、頂点 2, 3 にコマを 1 個ずつ置く。その結果、A = (0, 1, 1, 0, 0, 1) となる。
- 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 1) となる。
- 頂点 6 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 0) となる。
- 頂点 3 にあるコマを 1 個取り除き、頂点 2 にコマを 1 個置く。その結果、A = (0, 1, 0, 0, 0, 0) となる。
- 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 0, 0, 0, 0) となる。
上記の手順で操作を行う回数は 5 回であり、これが操作を行う回数としてあり得る最大値です。
入力例 2
2 1 1 2 1 2 0 0
出力例 2
0
この入力例では、はじめからグラフ上にコマが存在しません。
入力例 3
10 20 4 8 1 10 1 7 5 9 9 10 8 10 7 5 1 4 7 3 8 7 2 8 5 8 4 2 5 1 7 2 8 3 3 4 8 9 7 10 2 3 25 5 1 1 16 5 98 3 21 1 35 39 32 11 35 37 14 29 36 1
出力例 3
1380
Score: 475 points
Problem Statement
You are given a simple undirected graph consisting of N vertices and M edges. For i = 1, 2, \ldots, M, the i-th edge connects vertices u_i and v_i. Also, for i = 1, 2, \ldots, N, vertex i is assigned a positive integer W_i, and there are A_i pieces placed on it.
As long as there are pieces on the graph, repeat the following operation:
- First, choose and remove one piece from the graph, and let x be the vertex on which the piece was placed.
- Choose a (possibly empty) set S of vertices adjacent to x such that \sum_{y \in S} W_y \lt W_x, and place one piece on each vertex in S.
Print the maximum number of times the operation can be performed.
It can be proved that, regardless of how the operation is performed, there will be no pieces on the graph after a finite number of iterations.
Constraints
- All input values are integers.
- 2 \leq N \leq 5000
- 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
- 1 \leq W_i \leq 5000
- 0 \leq A_i \leq 10^9
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 W_1 W_2 \ldots W_N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
6 6 1 2 2 3 3 1 3 4 1 5 5 6 9 2 3 1 4 4 1 0 0 0 0 1
Sample Output 1
5
In the following explanation, let A = (A_1, A_2, \ldots, A_N) represent the numbers of pieces on the vertices. Initially, A = (1, 0, 0, 0, 0, 1).
Consider performing the operation as follows:
- Remove one piece from vertex 1 and place one piece each on vertices 2 and 3. Now, A = (0, 1, 1, 0, 0, 1).
- Remove one piece from vertex 2. Now, A = (0, 0, 1, 0, 0, 1).
- Remove one piece from vertex 6. Now, A = (0, 0, 1, 0, 0, 0).
- Remove one piece from vertex 3 and place one piece on vertex 2. Now, A = (0, 1, 0, 0, 0, 0).
- Remove one piece from vertex 2. Now, A = (0, 0, 0, 0, 0, 0).
In this procedure, the operation is performed five times, which is the maximum possible number of times.
Sample Input 2
2 1 1 2 1 2 0 0
Sample Output 2
0
In this sample input, there are no pieces on the graph from the beginning.
Sample Input 3
10 20 4 8 1 10 1 7 5 9 9 10 8 10 7 5 1 4 7 3 8 7 2 8 5 8 4 2 5 1 7 2 8 3 3 4 8 9 7 10 2 3 25 5 1 1 16 5 98 3 21 1 35 39 32 11 35 37 14 29 36 1
Sample Output 3
1380