実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
AtCoder 社はロゴ入りの T シャツを販売しています。
高橋君の N 日間の予定が 0, 1, 2 のみからなる長さ N の文字列 S で与えられます。
具体的には、1\leq i\leq N をみたす整数 i について、
- S の i 文字目が
0のとき、i 日目に何の予定も入っていません。 - S の i 文字目が
1のとき、i 日目に高橋君は食事に行く予定があります。 - S の i 文字目が
2のとき、i 日目に高橋君は競技プログラミングのイベントに行く予定が入っています。
高橋君は無地の T シャツを M 枚持っており、1 日目の直前の時点ですべて洗濯済みの状態となっています。
これに加えて、次の条件をみたすように行動できるように、高橋君は AtCoder のロゴ入りの T シャツを何枚か購入する事にしました。
- 食事に行く日には、無地の T シャツ 1 枚またはロゴ入りの T シャツ 1 枚を着用する。
- 競技プログラミングのイベントに行く日にはロゴ入りの T シャツ 1 枚を着用する。
- 何の予定もない日には T シャツを着用しない。加えて、その時点で着用済みの T シャツを全て洗濯する。 洗濯した T シャツは翌日から着用できる。
- 一度着用した T シャツは次に洗濯するまで着用できない。
N 日間のうち予定が入っている日すべてについて、条件をみたす T シャツを着用できるようにするために、高橋君は最低何枚のTシャツを購入する必要があるか求めてください。
新しく T シャツを購入する必要がないならば 0 を出力してください。
ただし、購入した T シャツも 1 日目の直前の時点ですべて洗濯済みの状態で存在するものとします。
制約
- 1\leq M\leq N\leq 1000
- S は
0,1,2のみからなる長さ N の文字列 - N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M S
出力
問題文の条件をみたすように行動するために
高橋君が購入する必要のある T シャツの枚数の最小値を出力せよ。
新しく購入する必要がないならば 0 を出力せよ。
入力例 1
6 1 112022
出力例 1
2
高橋君がロゴ入りの T シャツを 2 枚購入したとき、次のようにして高橋君は T シャツを着用することができます。
- 1 日目、高橋君はロゴ入りの T シャツを着用して食事に行きます。
- 2 日目、高橋君は無地の T シャツを着用して食事に行きます。
- 3 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
- 4 日目、高橋君は予定がないため、着用した T シャツをすべて洗濯します。これにより、1,2,3 日目に着用した T シャツを再び着用することが可能になります。
- 5 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
- 6 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
高橋君がロゴ入りの T シャツを 1 枚以下しか購入しなかった場合には、
どのようにしても条件をみたすように T シャツを着用することができません。
よって、2 を出力します。
入力例 2
3 1 222
出力例 2
3
入力例 3
2 1 01
出力例 3
0
高橋君は新しく T シャツを購入する必要はありません。
Score : 300 points
Problem Statement
AtCoder Inc. sells T-shirts with its logo.
You are given Takahashi's schedule for N days as a string S of length N consisting of 0, 1, and 2.
Specifically, for an integer i satisfying 1\leq i\leq N,
- if the i-th character of S is
0, he has no plan scheduled for the i-th day; - if the i-th character of S is
1, he plans to go out for a meal on the i-th day; - if the i-th character of S is
2, he plans to attend a competitive programming event on the i-th day.
Takahashi has M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.
- On days he goes out for a meal, he will wear a plain or logo T-shirt.
- On days he attends a competitive programming event, he will wear a logo T-shirt.
- On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
- Once he wears a T-shirt, he cannot wear it again until he washes it.
Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N days. If he does not need to buy new T-shirts, print 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.
Constraints
- 1\leq M\leq N\leq 1000
- S is a string of length N consisting of
0,1, and2. - N and M are integers.
Input
The input is given from Standard Input in the following format:
N M S
Output
Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print 0.
Sample Input 1
6 1 112022
Sample Output 1
2
If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:
- On the first day, he wears a logo T-shirt to go out for a meal.
- On the second day, he wears a plain T-shirt to go out for a meal.
- On the third day, he wears a logo T-shirt to attend a competitive programming event.
- On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
- On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
- On the sixth day, he wears a logo T-shirt to attend a competitive programming event.
If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2.
Sample Input 2
3 1 222
Sample Output 2
3
Sample Input 3
2 1 01
Sample Output 3
0
He does not need to buy new T-shirts.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 1 5 4 9
出力例 1
2
A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。
- 例えば 2 要素目の 1 、 5 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
- このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。
入力例 2
6 5 1 1 1 1 1 1
出力例 2
0
入力例 3
8 3 31 43 26 6 18 36 22 13
出力例 3
18
Score : 250 points
Problem Statement
You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.
Constraints
- All inputs are integers.
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 1 5 4 9
Sample Output 1
2
Consider removing exactly two elements from A=(3,1,5,4,9).
- For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
- In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.
Sample Input 2
6 5 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
8 3 31 43 26 6 18 36 22 13
Sample Output 3
18
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
キーエンスには N 個の部署があり、i\,(1 \leq i \leq N) 番目の部署には A_i 人の社員が所属しています。異なる部署に同じ社員が所属していることはありません。
キーエンスは、部署をまたいだ全社横断プロジェクトを計画しています。1 つのプロジェクトは K 個の相異なる部署から 1 人ずつ選出して作り、ちょうど K 人から構成されるようにします。
プロジェクトは最大でいくつ作れますか?ただし、1 人が複数のプロジェクトに参加することはできません。
制約
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^{12}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
プロジェクトの個数の最大値を出力せよ。
入力例 1
3 3 2 3 4
出力例 1
2
3 個の部署それぞれから 1 人ずつ選出したプロジェクトを 2 つ作ることができます。
入力例 2
4 2 1 1 3 4
出力例 2
4
入力例 3
4 3 1 1 3 4
出力例 3
2
Score : 400 points
Problem Statement
KEYENCE has N departments, where A_i employees belong to the i-th department (1 \leq i \leq N). No employee belongs to multiple departments.
The company is planning cross-departmental projects. Each project will be composed of exactly K employees chosen from K distinct departments.
At most how many projects can be made? No employee can participate in multiple projects.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^{12}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Print the maximum possible number of projects.
Sample Input 1
3 3 2 3 4
Sample Output 1
2
There can be two projects, each composed of three employees from distinct departments.
Sample Input 2
4 2 1 1 3 4
Sample Output 2
4
Sample Input 3
4 3 1 1 3 4
Sample Output 3
2
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A = (a_1,\ldots,a_N) と B = (b_1,\ldots,b_N) が与えられます。
i=1,...,Q に対し、次の形式のクエリに答えてください。
- A の先頭 x_i 項 (a_1,\ldots,a_{x_i}) に含まれる値の集合と B の先頭 y_i 項 (b_1,\ldots,b_{y_i}) に含まれる値の集合が等しいならば
Yesと、そうでなければNoと出力せよ。
制約
- 1 \leq N,Q \leq 2 \times 10^5
- 1 \leq a_i,b_i \leq 10^9
- 1 \leq x_i,y_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N b_1 \ldots b_N Q x_1 y_1 \vdots x_Q y_Q
出力
Q 行出力せよ。 i 行目には、i 番目のクエリに対する出力をせよ。
入力例 1
5 1 2 3 4 5 1 2 2 4 3 7 1 1 2 2 2 3 3 3 4 4 4 5 5 5
出力例 1
Yes Yes Yes No No Yes No
集合は各値が含まれるかどうかのみに注目した概念であることに気を付けてください。
3 番目のクエリにおいて、A の先頭 2 項には 1 と 2 が 1 個ずつ、B の先頭 3 項には 1 が 1 個と 2 が 2 個含まれます。しかし、それぞれに含まれる値の集合はどちらも \{ 1,2 \} となり、一致します。
また、6 番目のクエリにおいては各値が現れる順番が異なりますが、やはり集合としては一致します。
Score : 500 points
Problem Statement
You are given integer sequences A = (a_1,\ldots,a_N) and B = (b_1,\ldots,b_N), each of length N.
For i=1,...,Q, answer the query in the following format.
- If the set of values contained in the first x_i terms of A, (a_1,\ldots,a_{x_i}), and the set of values contained in the first y_i terms of B, (b_1,\ldots,b_{y_i}), are equal, then print
Yes; otherwise, printNo.
Constraints
- 1 \leq N,Q \leq 2 \times 10^5
- 1 \leq a_i,b_i \leq 10^9
- 1 \leq x_i,y_i \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N a_1 \ldots a_N b_1 \ldots b_N Q x_1 y_1 \vdots x_Q y_Q
Output
Print Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
5 1 2 3 4 5 1 2 2 4 3 7 1 1 2 2 2 3 3 3 4 4 4 5 5 5
Sample Output 1
Yes Yes Yes No No Yes No
Note that sets are a concept where it matters only whether each value is contained or not.
For the 3-rd query, the first 2 terms of A contain one 1 and one 2, while the first 3 terms of B contain one 1 and two 2's. However, the sets of values contained in the segments are both \{ 1,2 \}, which are equal.
Also, for the 6-th query, the values appear in different orders, but they are still equal as sets.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
整数 K が与えられます。はじめ空である集合 S に対して、次の 2 種類のクエリを順に Q 個処理してください。
1 x:整数 x が与えられる。S に x が含まれているならば、S から x を取り除く。そうでないならば、S に x を追加する。2 x:S に含まれる整数 x が与えられる。S に含まれる数を頂点とし、差の絶対値が K 以下であるような数の間に辺を張ったグラフにおいて、x が属する連結成分の頂点数を出力する。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq K \leq 10^{18}
- 各クエリにおいて、1\leq x \leq 10^{18}
- 2 種類目のクエリにおいて与えられる x はその時点で S に含まれる。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
Q K
\mathrm{query}_1
\vdots
\mathrm{query}_Q
各クエリはそれぞれ次の形式で与えられる。
1 x
2 x
出力
クエリを処理せよ。
入力例 1
7 5 1 3 1 10 2 3 1 7 2 3 1 10 2 3
出力例 1
1 3 2
クエリの処理は次のように進行します。
- 1 番目のクエリでは、S に 3 が追加され、S=\{3\} となります。
- 2 番目のクエリでは、S に 10 が追加され、S=\{3,10\} となります。
- 3 番目のクエリでは、3,10 の 2 頂点からなる辺のないグラフを考え、3 が属する連結成分のサイズである 1 を出力します。
- 4 番目のクエリでは、S に 7 が追加され、S=\{3,7,10\} となります。
- 5 番目のクエリでは、3,7,10 の 3 頂点からなり、3 と 7、7 と 10 の間に辺のあるグラフを考え、3 が属する連結成分のサイズである 3 を出力します。
- 6 番目のクエリでは、S から 10 が削除され、S=\{3,7\} となります。
- 7 番目のクエリでは、3,7 の 2 頂点からなり、3 と 7 の間に辺のあるグラフを考え、3 が属する連結成分のサイズである 2 を出力します。
入力例 2
11 1000000000000000000 1 1 1 100 1 10000 1 1000000 1 100000000 1 10000000000 1 1000000000000 1 100000000000000 1 10000000000000000 1 1000000000000000000 2 1
出力例 2
10
入力例 3
8 0 1 1 1 2 2 1 1 1 1 2 1 1 1 2 2 1
出力例 3
1 1
Score : 525 points
Problem Statement
You are given an integer K. For a set S that is initially empty, process Q queries of the following two types in order:
1 x: An integer x is given. If x is in S, remove x from S. Otherwise, add x to S.2 x: An integer x that is in S is given. Consider a graph where the vertices are the numbers in S, and there is an edge between two numbers if and only if the absolute difference between them is at most K. Print the count of vertices in the connected component that contains x.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq K \leq 10^{18}
- For each query, 1 \leq x \leq 10^{18}.
- For each query of the second type, the given x is in S at that point.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q K
\mathrm{query}_1
\vdots
\mathrm{query}_Q
Each query is given in the following format:
1 x
2 x
Output
Process the queries.
Sample Input 1
7 5 1 3 1 10 2 3 1 7 2 3 1 10 2 3
Sample Output 1
1 3 2
The query processing proceeds as follows:
- In the first query, 3 is added to S, resulting in S=\{3\}.
- In the second query, 10 is added to S, resulting in S=\{3,10\}.
- In the third query, consider a graph with vertices 3 and 10 and no edges. The connected component containing 3 has a size of 1, so print 1.
- In the fourth query, 7 is added to S, resulting in S=\{3,7,10\}.
- In the fifth query, consider a graph with vertices 3, 7, and 10, with edges between 3 and 7, and 7 and 10. The connected component containing 3 has a size of 3, so print 3.
- In the sixth query, 10 is removed from S, resulting in S=\{3,7\}.
- In the seventh query, consider a graph with vertices 3 and 7, with an edge between them. The connected component containing 3 has a size of 2, so print 2.
Sample Input 2
11 1000000000000000000 1 1 1 100 1 10000 1 1000000 1 100000000 1 10000000000 1 1000000000000 1 100000000000000 1 10000000000000000 1 1000000000000000000 2 1
Sample Output 2
10
Sample Input 3
8 0 1 1 1 2 2 1 1 1 1 2 1 1 1 2 2 1
Sample Output 3
1 1