Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoderレストランでは 1 から N までの番号がつけられている N 種類の食材を扱っています。
また、AtCoderレストランでは 1 から M までの番号がつけられている M 個の料理を提供しています。料理 i には K_i 種類の食材が使われており、食材 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} が使われています。
すぬけくんは現在 N 種類の食材がすべて苦手です。また、すぬけ君は苦手な食材が 1 種類でも使われている料理を食べることができず、苦手な食材が 1 種類も使われていない料理を食べることができます。
すぬけ君はこれから N 日間かけて苦手な食材を克服しようとしています。 すぬけ君は i 日目に食材 B_i を克服し、それ以降苦手な食材でなくなります。
i=1,2,\ldots,N について以下の値を求めてください。
- i 日目にすぬけ君が食材 B_i を克服した直後、すぬけ君が食べることができるAtCoderレストランの料理の個数
制約
- 1 \leq N \leq 3 \times 10^{5}
- 1 \leq M \leq 3 \times 10^{5}
- 1 \leq K_i \leq N (1 \leq i \leq M)
- K_i の総和は 3 \times 10^{5} 以下
- 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
- A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
- 1 \leq B_i \leq N (1 \leq i \leq N)
- B_i \neq B_j (i \neq j )
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1} K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2} \vdots K_M A_{M,1} A_{M,2} \ldots A_{M,K_M} B_1 B_2 \ldots B_N
出力
N 行出力せよ。k 行目には、i=k のときの値を出力せよ。
入力例 1
5 4 2 1 2 3 3 4 5 3 1 2 5 1 3 1 3 2 5 4
出力例 1
0 1 2 3 4
以下のようにすぬけ君は苦手な食材を克服します。
- 1 日目: 食材 1 を克服する。このとき、どの料理にも苦手な食材が使われているため 0 を出力する。
- 2 日目: 食材 3 を克服する。このとき、料理 4 は苦手な食材が使われなくなるため食べられるようになる。料理 4 以外の料理は苦手な食材が使われているため、 1 を出力する。
- 3 日目: 食材 2 を克服する。このとき、料理 1 は苦手な食材が使われなくなるため食べられるようになる。料理 1,4 以外の料理は苦手な食材が使われているため、 2 を出力する。
- 4 日目: 食材 5 を克服する。このとき、料理 3 は苦手な食材が使われなくなるため食べられるようになる。料理 1,3,4 以外の料理は苦手な食材が使われているため、 3 を出力する。
- 5 日目: 食材 4 を克服する。このとき、料理 2 は苦手な食材が使われなくなるため食べられるようになる。全ての料理に苦手な食材が使われていないため、 4 を出力する。
入力例 2
9 8 1 4 5 6 9 7 4 3 4 2 4 1 3 1 1 5 7 9 8 1 5 2 9 8 1 2 1 1 6 5 2 7 8 4 1 9 3
出力例 2
0 0 1 1 1 2 4 6 8
Score : 300 points
Problem Statement
The AtCoder Restaurant uses N types of ingredients numbered from 1 to N.
The restaurant offers M dishes numbered from 1 to M. Dish i uses K_i types of ingredients, namely A_{i,1}, A_{i,2}, \ldots, A_{i,K_i}.
Snuke currently dislikes all N ingredients. He cannot eat any dish that uses one or more ingredients he dislikes, and he can eat a dish that uses none of the disliked ingredients.
Over the next N days, he will overcome his dislikes one ingredient per day. On day i, he overcomes ingredient B_i, and from then on he no longer dislikes it.
For each i=1,2,\ldots,N, find:
- the number of dishes at the AtCoder Restaurant that he can eat immediately after overcoming ingredient B_i on day i.
Constraints
- 1 \leq N \leq 3 \times 10^{5}
- 1 \leq M \leq 3 \times 10^{5}
- 1 \leq K_i \leq N (1 \leq i \leq M)
- The sum of K_i is at most 3 \times 10^{5}.
- 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
- A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
- 1 \leq B_i \leq N (1 \leq i \leq N)
- B_i \neq B_j (i \neq j )
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1} K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2} \vdots K_M A_{M,1} A_{M,2} \ldots A_{M,K_M} B_1 B_2 \ldots B_N
Output
Print N lines. The k-th line should contain the answer for i=k.
Sample Input 1
5 4 2 1 2 3 3 4 5 3 1 2 5 1 3 1 3 2 5 4
Sample Output 1
0 1 2 3 4
Snuke overcomes his disliked ingredients as follows:
- Day 1: He overcomes ingredient 1. At this time, every dish still uses a disliked ingredient, so print 0.
- Day 2: He overcomes ingredient 3. Dish 4 no longer uses any disliked ingredient and becomes edible; all other dishes still use disliked ingredients, so print 1.
- Day 3: He overcomes ingredient 2. Dish 1 no longer uses any disliked ingredient and becomes edible; all dishes except 1 and 4 still use disliked ingredients, so print 2.
- Day 4: He overcomes ingredient 5. Dish 3 no longer uses any disliked ingredient and becomes edible; all dishes except 1, 3, and 4 still use disliked ingredients, so print 3.
- Day 5: He overcomes ingredient 4. Dish 2 no longer uses any disliked ingredient and becomes edible; now all dishes have no disliked ingredients, so print 4.
Sample Input 2
9 8 1 4 5 6 9 7 4 3 4 2 4 1 3 1 1 5 7 9 8 1 5 2 9 8 1 2 1 1 6 5 2 7 8 4 1 9 3
Sample Output 2
0 0 1 1 1 2 4 6 8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。
ここで、S_i は 0
, 1
, \ldots, 9
がちょうど 1 回ずつ現れる長さ 10 の文字列です。
それぞれのリールには対応するボタンがついており、高橋君は各非負整数 t について、
スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す(または何もしない)ことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、
i 番目のリールは S_i の (t\bmod{10})+1 文字目を表示して止まります。
ただし、t\bmod{10} で t を 10 で割ったあまりを表します。
高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
制約
- 2\leq N\leq 100
- N は整数
- S_i は
0
,1
, \ldots,9
がちょうど 1 回ずつ現れる長さ 10 の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを出力せよ。
入力例 1
3 1937458062 8124690357 2385760149
出力例 1
6
高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8
で揃えることができます。
- スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2 の (0\bmod{10})+1=1 文字目である
8
を表示して止まります。 - スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3 の (2\bmod{10})+1=3 文字目である
8
を表示して止まります。 - スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1 の (6\bmod{10})+1=7 文字目である
8
を表示して止まります。
5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。
入力例 2
5 0123456789 0123456789 0123456789 0123456789 0123456789
出力例 2
40
全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。
Score : 300 points
Problem Statement
There is a slot machine with N reels.
The placement of symbols on the i-th reel is represented by a string S_i of length 10 containing each of 0
, 1
, \ldots, 9
exactly once.
Each reel has a corresponding button. For each non-negative integer t, Takahashi can press one of the buttons of his choice (or do nothing) t seconds after the reels start spinning.
If the button for the i-th reel is pressed t seconds after the start of the spin, the i-th reel will stop to display the ((t\bmod{10})+1)-th character of S_i.
Here, t\bmod{10} denotes the remainder when t is divided by 10.
Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.
Constraints
- 2\leq N\leq 100
- N is an integer.
- S_i is a string of length 10 containing each of
0
,1
, \ldots,9
exactly once.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.
Sample Input 1
3 1937458062 8124690357 2385760149
Sample Output 1
6
Takahashi can make all reels display 8
in 6 seconds after the start of the spin by stopping them as follows.
- 0 seconds after the start of the spin, press the button for the 2-nd reel, making it stop to display the ((0\bmod{10})+1=1)-st character of S_2,
8
. - 2 seconds after the start of the spin, press the button for the 3-rd reel, making it stop to display the ((2\bmod{10})+1=3)-rd character of S_3,
8
. - 6 seconds after the start of the spin, press the button for the 1-st reel, making it stop to display the ((6\bmod{10})+1=7)-th character of S_1,
8
.
There is no way to make all reels display the same character in five seconds or less, so the answer is 6.
Sample Input 2
5 0123456789 0123456789 0123456789 0123456789 0123456789
Sample Output 2
40
Note that he must stop all reels to make them display the same character.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N 個の頂点と M 本の辺からなる単純な(すなわち、自己ループも多重辺も含まない)無向グラフ G が与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。
1 \leq u \lt v \leq N を満たす整数の組 (u, v) であって、下記の 2 つの条件をともに満たすものの個数を出力してください。
- グラフ G において、頂点 u と頂点 v を結ぶ辺は存在しない。
- グラフ G に、頂点 u と頂点 v を結ぶ辺を追加して得られるグラフは、二部グラフである。
二部グラフとは?
無向グラフが二部グラフであるとは、下記の条件を満たすように各頂点を黒または白のどちらかの色で塗ることができることを言います。
- 同じ色に塗られた頂点どうしを結ぶ辺は存在しない。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace
- 1 \leq u_i, v_i \leq N
- グラフ G は単純
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
5 4 4 2 3 1 5 2 3 2
出力例 1
2
問題文中の条件を満たす整数の組 (u, v) は、(1, 4) と (1, 5) の 2 つです。よって、2 を出力します。
他の組については、例えば、(1, 3) はグラフ G において頂点 1 と頂点 3 を結ぶ辺が存在することから、
(4, 5) はグラフ G に頂点 4 と頂点 5 を結ぶ辺を追加して得られるグラフが二部グラフではないことから、
それぞれ問題文中の条件を満たしません。
入力例 2
4 3 3 1 3 2 1 2
出力例 2
0
与えられるグラフが二部グラフであったり連結であるとは限らないことに注意してください。
入力例 3
9 11 4 9 9 1 8 2 8 3 9 2 8 4 6 7 4 6 7 5 4 5 7 8
出力例 3
9
Score : 400 points
Problem Statement
You are given a simple undirected graph G with N vertices and M edges (a simple graph does not contain self-loops or multi-edges). For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i.
Print the number of pairs of integers (u, v) that satisfy 1 \leq u \lt v \leq N and both of the following conditions.
- The graph G does not have an edge connecting vertex u and vertex v.
- Adding an edge connecting vertex u and vertex v in the graph G results in a bipartite graph.
What is a bipartite graph?
An undirected graph is said to be bipartite if and only if one can paint each vertex black or white to satisfy the following condition.
- No edge connects vertices painted in the same color.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace
- 1 \leq u_i, v_i \leq N
- The graph G 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 4 4 2 3 1 5 2 3 2
Sample Output 1
2
We have two pairs of integers (u, v) that satisfy the conditions in the problem statement: (1, 4) and (1, 5). Thus, you should print 2.
The other pairs do not satisfy the conditions. For instance, for (1, 3), the graph G has an edge connecting vertex 1 and vertex 3; for (4, 5), adding an edge connecting vertex 4 and vertex 5 in the graph G does not result in a bipartite graph.
Sample Input 2
4 3 3 1 3 2 1 2
Sample Output 2
0
Note that the given graph may not be bipartite or connected.
Sample Input 3
9 11 4 9 9 1 8 2 8 3 9 2 8 4 6 7 4 6 7 5 4 5 7 8
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
長さ N の整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を求めてください。
ビット単位 xor とは
非負整数 A, B のビット単位 xor 、A \oplus B は、以下のように定義されます。- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
一般に k 個の整数 p_1, \dots, p_k の排他的論理和は (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) と定義され、これは p_1, \dots, p_k の順番によらないことが証明できます。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq A_i \leq 10^8
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_{N}
出力
答えを出力せよ。
入力例 1
3 1 3 2
出力例 1
3
A_1\oplus A_2 = 2, A_1 \oplus A_2\oplus A_3 = 0, A_2\oplus A_3 = 1 なので答えは 2+0+1=3 です。
入力例 2
7 2 5 6 5 2 1 7
出力例 2
83
Score : 450 points
Problem Statement
You are given an integer sequence A=(A_1,\ldots,A_N) of length N. Find the value of the following expression:
Notes on bitwise XOR
The bitwise XOR of non-negative integers A and B, denoted as A \oplus B, is defined as follows:- In the binary representation of A \oplus B, the digit at the 2^k (k \geq 0) position is 1 if and only if exactly one of the digits at the 2^k position in the binary representations of A and B is 1; otherwise, it is 0.
In general, the bitwise XOR of k integers p_1, \dots, p_k is defined as (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k). It can be proved that this is independent of the order of p_1, \dots, p_k.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_{N}
Output
Print the answer.
Sample Input 1
3 1 3 2
Sample Output 1
3
A_1 \oplus A_2 = 2, A_1 \oplus A_2 \oplus A_3 = 0, and A_2 \oplus A_3 = 1, so the answer is 2 + 0 + 1 = 3.
Sample Input 2
7 2 5 6 5 2 1 7
Sample Output 2
83
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N),C=(C_1,C_2,\ldots,C_N) および整数 K が与えられます。
1\leq i,j,k\leq N を満たす整数 i,j,k の選び方 N^3 通りそれぞれに対して A_iB_j+B_jC_k+C_kA_i の値を計算したとき、その中で大きい方から K 番目の値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq K\leq \min(N^3,5\times 10^5)
- 1\leq A_i,B_i,C_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N C_1 C_2 \ldots C_N
出力
答えを出力せよ。
入力例 1
2 5 1 2 3 4 5 6
出力例 1
31
N^3=8 個の整数の値は以下の通りです。
- (i,j,k)=(1,1,1) : A_1B_1+B_1C_1+C_1A_1=1\times 3+3\times 5+5\times 1=23
- (i,j,k)=(1,1,2) : A_1B_1+B_1C_2+C_2A_1=1\times 3+3\times 6+6\times 1=27
- (i,j,k)=(1,2,1) : A_1B_2+B_2C_1+C_1A_1=1\times 4+4\times 5+5\times 1=29
- (i,j,k)=(1,2,2) : A_1B_2+B_2C_2+C_2A_1=1\times 4+4\times 6+6\times 1=34
- (i,j,k)=(2,1,1) : A_2B_1+B_1C_1+C_1A_2=2\times 3+3\times 5+5\times 2=31
- (i,j,k)=(2,1,2) : A_2B_1+B_1C_2+C_2A_2=2\times 3+3\times 6+6\times 2=36
- (i,j,k)=(2,2,1) : A_2B_2+B_2C_1+C_1A_2=2\times 4+4\times 5+5\times 2=38
- (i,j,k)=(2,2,2) : A_2B_2+B_2C_2+C_2A_2=2\times 4+4\times 6+6\times 2=44
これらを値の降順に並べ替えると (44,38,36,34,31,29,27,23) となるため、 大きい方から 5 番目の値は 31 です。
入力例 2
3 10 100 100 100 100 100 100 100 100 100
出力例 2
30000
入力例 3
5 54 800516877 573289179 26509423 168629803 696409999 656737335 915059758 201458890 931198638 185928366 140174496 254538849 830992027 305186313 322164559
出力例 3
689589940713840351
Score : 500 points
Problem Statement
You are given three integer sequences of length N, namely A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N), and C=(C_1,C_2,\ldots,C_N), and an integer K.
For each of the N^3 choices of integers i,j,k (1\leq i,j,k\leq N), compute the value A_iB_j + B_jC_k + C_kA_i. Among all these values, find the K-th largest value.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq K \leq \min(N^3,5\times 10^5)
- 1\leq A_i,B_i,C_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 A_2 \ldots A_N B_1 B_2 \ldots B_N C_1 C_2 \ldots C_N
Output
Print the answer.
Sample Input 1
2 5 1 2 3 4 5 6
Sample Output 1
31
The N^3=8 values are computed as follows:
- For (i,j,k)=(1,1,1): A_1B_1+B_1C_1+C_1A_1=1\times 3+3\times 5+5\times 1=23
- For (i,j,k)=(1,1,2): A_1B_1+B_1C_2+C_2A_1=1\times 3+3\times 6+6\times 1=27
- For (i,j,k)=(1,2,1): A_1B_2+B_2C_1+C_1A_1=1\times 4+4\times 5+5\times 1=29
- For (i,j,k)=(1,2,2): A_1B_2+B_2C_2+C_2A_1=1\times 4+4\times 6+6\times 1=34
- For (i,j,k)=(2,1,1): A_2B_1+B_1C_1+C_1A_2=2\times 3+3\times 5+5\times 2=31
- For (i,j,k)=(2,1,2): A_2B_1+B_1C_2+C_2A_2=2\times 3+3\times 6+6\times 2=36
- For (i,j,k)=(2,2,1): A_2B_2+B_2C_1+C_1A_2=2\times 4+4\times 5+5\times 2=38
- For (i,j,k)=(2,2,2): A_2B_2+B_2C_2+C_2A_2=2\times 4+4\times 6+6\times 2=44
Sorting these values in descending order, we have (44,38,36,34,31,29,27,23), so the 5th largest value is 31.
Sample Input 2
3 10 100 100 100 100 100 100 100 100 100
Sample Output 2
30000
Sample Input 3
5 54 800516877 573289179 26509423 168629803 696409999 656737335 915059758 201458890 931198638 185928366 140174496 254538849 830992027 305186313 322164559
Sample Output 3
689589940713840351