Time Limit: 2 sec / Memory Limit: 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.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。
グラフに含まれる連結成分の個数を求めてください。
注釈
単純無向グラフ とは、単純で辺に向きの無いグラフのことをいいます。
グラフが 単純 であるとは、グラフが自己ループや多重辺を含まないことをいいます。
あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。
制約
- 1 \leq N \leq 100
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq u_i, v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
5 3 1 2 1 3 4 5
出力例 1
2
与えられるグラフに含まれる連結成分は次の 2 個です。
- 頂点 1, 2, 3 および辺 1, 2 からなる部分グラフ
- 頂点 4, 5 および辺 3 からなる部分グラフ
入力例 2
5 0
出力例 2
5
入力例 3
4 6 1 2 1 3 1 4 2 3 2 4 3 4
出力例 3
1
Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i.
Find the number of connected components in this graph.
Notes
A simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.
A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.
Constraints
- 1 \leq N \leq 100
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq u_i, v_i \leq N
- The given graph is simple.
- All values in the input 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
5 3 1 2 1 3 4 5
Sample Output 1
2
The given graph contains the following two connected components:
- a subgraph formed from vertices 1, 2, 3, and edges 1, 2;
- a subgraph formed from vertices 4, 5, and edge 3.
Sample Input 2
5 0
Sample Output 2
5
Sample Input 3
4 6 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output 3
1
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
数直線上に N+Q 個の点 A_1,\dots,A_N,B_1,\dots,B_Q があり、点 A_i の座標は a_i、点 B_j の座標は b_j です。
j=1,2,\dots,Q それぞれについて、以下の問題に答えてください。
- 点 A_1,A_2,\dots,A_N のうち点 B_j との距離が k_j 番目に近い点を X としたとき、点 X と点 B_j との距離を求めよ。 より厳密には、点 A_i と点 B_j との距離を d_i として、(d_1,d_2,\dots,d_N) を昇順に並び替えてできる列を (d_1',d_2',\dots,d_N') としたとき、d_{k_j}' を求めよ。
制約
- 1\leq N,Q \leq 10^5
- -10^8\leq a_i,b_j \leq 10^8
- 1\leq k_j\leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q a_1 a_2 \dots a_N b_1 k_1 b_2 k_2 \vdots b_Q k_Q
出力
Q 行出力せよ。 l\ (1\leq l \leq Q) 行目には、j=l としたときの問題に対する答えを整数として出力せよ。
入力例 1
4 3 -3 -1 5 6 -2 3 2 1 10 4
出力例 1
7 3 13
1 番目のクエリについて説明します。
点 A_1,A_2,A_3,A_4 と点 B_1 との距離は順に 1,1,7,8 なので、点 B_1 との距離が 3 番目に近いのは点 A_3 です。 よって、点 A_3 と点 B_1 との距離である 7 を出力します。
入力例 2
2 2 0 0 0 1 0 2
出力例 2
0 0
同じ座標に複数の点がある可能性もあります。
入力例 3
10 5 -84 -60 -41 -100 8 -8 -52 -62 -61 -76 -52 5 14 4 -2 6 46 2 26 7
出力例 3
11 66 59 54 88
Score : 425 points
Problem Statement
There are N+Q points A_1,\dots,A_N,B_1,\dots,B_Q on a number line, where point A_i has a coordinate a_i and point B_j has a coordinate b_j.
For each j=1,2,\dots,Q, answer the following question:
- Let X be the point among A_1,A_2,\dots,A_N that is the k_j-th closest to point B_j. Find the distance between points X and B_j. More formally, let d_i be the distance between points A_i and B_j. Sort (d_1,d_2,\dots,d_N) in ascending order to get the sequence (d_1',d_2',\dots,d_N'). Find d_{k_j}'.
Constraints
- 1 \leq N, Q \leq 10^5
- -10^8 \leq a_i, b_j \leq 10^8
- 1 \leq k_j \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q a_1 a_2 \dots a_N b_1 k_1 b_2 k_2 \vdots b_Q k_Q
Output
Print Q lines. The l-th line (1 \leq l \leq Q) should contain the answer to the question for j=l as an integer.
Sample Input 1
4 3 -3 -1 5 6 -2 3 2 1 10 4
Sample Output 1
7 3 13
Let us explain the first query.
The distances from points A_1, A_2, A_3, A_4 to point B_1 are 1, 1, 7, 8, respectively, so the 3rd closest to point B_1 is point A_3. Therefore, print the distance between point A_3 and point B_1, which is 7.
Sample Input 2
2 2 0 0 0 1 0 2
Sample Output 2
0 0
There may be multiple points with the same coordinates.
Sample Input 3
10 5 -84 -60 -41 -100 8 -8 -52 -62 -61 -76 -52 5 14 4 -2 6 46 2 26 7
Sample Output 3
11 66 59 54 88
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の数列 A=(A_0,A_1,\ldots,A_{N-1}) が与えられます。
最初の時点では空の皿があり、高橋君は次の操作を K 回繰り返します。
- 皿の中のアメの個数を X とする。皿に A_{(X\bmod N)} 個のアメを追加する。 ただし、X\bmod N で X を N で割った余りを表す。
K 回の操作の後で、皿の中には何個のアメがあるか求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq K \leq 10^{12}
- 1 \leq A_i\leq 10^6
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N K A_0 A_1 \ldots A_{N-1}
出力
答えを出力せよ。
入力例 1
5 3 2 1 6 3 1
出力例 1
11
皿の中のアメの個数は次のように変化します。
- 1 回目の操作において、X=0 であるので、アメは A_{(0\mod 5)}=A_0=2 個追加されます。
- 2 回目の操作において、X=2 であるので、アメは A_{(2\mod 5)}=A_2=6 個追加されます。
- 3 回目の操作において、X=8 であるので、アメは A_{(8\mod 5)}=A_3=3 個追加されます。
よって、3 回の操作の後で、皿には 11 個のアメがあります。出力する値は N で割った余りではない事に注意してください。
入力例 2
10 1000000000000 260522 914575 436426 979445 648772 690081 933447 190629 703497 47202
出力例 2
826617499998784056
答えは 32 bit 整数型に収まらない場合があります。
Score : 500 points
Problem Statement
You are given a sequence A=(A_0,A_1,\ldots,A_{N-1}) of length N.
There is an initially empty dish. Takahashi is going to repeat the following operation K times.
- Let X be the number of candies on the dish. He puts A_{(X\bmod N)} more candies on the dish. Here, X\bmod N denotes the remainder when X is divided by N.
Find how many candies are on the dish after the K operations.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq K \leq 10^{12}
- 1 \leq A_i\leq 10^6
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_0 A_1 \ldots A_{N-1}
Output
Print the answer.
Sample Input 1
5 3 2 1 6 3 1
Sample Output 1
11
The number of candies on the dish transitions as follows.
- In the 1-st operation, we have X=0, so A_{(0\mod 5)}=A_0=2 more candies will be put on the dish.
- In the 2-nd operation, we have X=2, so A_{(2\mod 5)}=A_2=6 more candies will be put on the dish.
- In the 3-rd operation, we have X=8, so A_{(8\mod 5)}=A_3=3 more candies will be put on the dish.
Thus, after the 3 operations, there will be 11 candies on the dish. Note that you must not print the remainder divided by N.
Sample Input 2
10 1000000000000 260522 914575 436426 979445 648772 690081 933447 190629 703497 47202
Sample Output 2
826617499998784056
The answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 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