E - Collision 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

xy 座標平面上に N 人の人がいます。人 i は (X_i, Y_i) にいます。すべての人は異なる地点にいます。

L, R からなる長さ N の文字列 S があります。
人 i は S_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。

たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

image

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
  • X_i, Y_i はすべて整数である。
  • S は L および R からなる長さ N の文字列である。

入力

入力は以下の形式で標準入力から与えられる。

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

出力

衝突が発生するならば Yes を、発生しないならば No を出力せよ。


入力例 1

3
2 3
1 1
4 1
RRL

出力例 1

Yes

この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。


入力例 2

2
1 1
2 1
RR

出力例 2

No

人 1 と人 2 は同じ向きに歩いているので衝突することはありません。


入力例 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

出力例 3

Yes

Score : 300 points

Problem Statement

There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.

We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.

For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

image

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All X_i and Y_i are integers.
  • S is a string of length N consisting of L and R.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

Output

If there will be a collision, print Yes; otherwise, print No.


Sample Input 1

3
2 3
1 1
4 1
RRL

Sample Output 1

Yes

This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.


Sample Input 2

2
1 1
2 1
RR

Sample Output 2

No

Since Person 1 and Person 2 walk in the same direction, they never collide.


Sample Input 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

Sample Output 3

Yes
F - Flavors

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

N カップのアイスクリームがあります。
i カップ目の味は F_i 、美味しさは S_i ( S_i は偶数 ) です。

あなたは、 N 個のカップの中から 2 つを選んで食べることにしました。
このときの満足度は次のように定義されます。

  • 食べたアイスクリームの美味しさを s,t ( 但し、 s \ge t ) とする。
    • 2 つのカップの味が異なるなら、満足度は \displaystyle s+t である。
    • そうでないなら、満足度は \displaystyle s + \frac{t}{2} である。

満足度として達成可能な最大値を求めてください。

制約

  • 入力は全て整数
  • 2 \le N \le 3 \times 10^5
  • 1 \le F_i \le N
  • 2 \le S_i \le 10^9
  • S_i は偶数

入力

入力は以下の形式で標準入力から与えられる。

N
F_1 S_1
F_2 S_2
\vdots
F_N S_N

出力

答えを整数として出力せよ。


入力例 1

4
1 4
2 10
2 8
3 6

出力例 1

16

2 カップ目と 4 カップ目のアイスを食べることを考えます。

  • 2 カップ目の味は 2 、美味しさは 10 です。
  • 4 カップ目の味は 3 、美味しさは 6 です。
  • 両者の味は異なるので、満足度は 10+6=16 です。

以上より、満足度 16 を達成できます。
満足度を 16 より大きくすることはできません。


入力例 2

4
4 10
3 2
2 4
4 12

出力例 2

17

1 カップ目と 4 カップ目のアイスを食べることを考えます。

  • 1 カップ目の味は 4 、美味しさは 10 です。
  • 4 カップ目の味は 4 、美味しさは 12 です。
  • 両者の味は同じなので、満足度は 12+\frac{10}{2}=17 です。

以上より、満足度 17 を達成できます。
満足度を 17 より大きくすることはできません。

Score : 300 points

Problem Statement

We have N cups of ice cream.
The flavor and deliciousness of the i-th cup are F_i and S_i, respectively (S_i is an even number).

You will choose and eat two of the N cups.
Your satisfaction here is defined as follows.

  • Let s and t (s \ge t) be the deliciousness of the eaten cups.
    • If the two cups have different flavors, your satisfaction is \displaystyle s+t.
    • Otherwise, your satisfaction is \displaystyle s + \frac{t}{2}.

Find the maximum achievable satisfaction.

Constraints

  • All input values are integers.
  • 2 \le N \le 3 \times 10^5
  • 1 \le F_i \le N
  • 2 \le S_i \le 10^9
  • S_i is even.

Input

Input is given from Standard Input in the following format:

N
F_1 S_1
F_2 S_2
\vdots
F_N S_N

Output

Print the answer as an integer.


Sample Input 1

4
1 4
2 10
2 8
3 6

Sample Output 1

16

Consider eating the second and fourth cups.

  • The second cup has a flavor of 2 and deliciousness of 10.
  • The fourth cup has a flavor of 3 and deliciousness of 6.
  • Since they have different flavors, your satisfaction is 10+6=16.

Thus, you can achieve the satisfaction of 16.
You cannot achieve a satisfaction greater than 16.


Sample Input 2

4
4 10
3 2
2 4
4 12

Sample Output 2

17

Consider eating the first and fourth cups.

  • The first cup has a flavor of 4 and deliciousness of 10.
  • The fourth cup has a flavor of 4 and deliciousness of 12.
  • Since they have the same flavor, your satisfaction is 12+\frac{10}{2}=17.

Thus, you can achieve the satisfaction of 17.
You cannot achieve a satisfaction greater than 17.

G - Max Multiple

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

非負整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。

A の(添え字が相異なる) K 個の項の和として考えられる非負整数の集合を S とします。

S に含まれる D の倍数の最大値を求めてください。ただし、S に D の倍数が含まれない場合、代わりに -1 と出力してください。

制約

  • 1 \leq K \leq N \leq 100
  • 1 \leq D \leq 100
  • 0 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N K D
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

4 2 2
1 2 3 4

出力例 1

6

A から 2 個の項を選ぶ方法を列挙すると

  • a_1 と a_2 を選ぶ。選ばれた項の和は 1+2=3 となる。
  • a_1 と a_3 を選ぶ。選ばれた項の和は 1+3=4 となる。
  • a_1 と a_4 を選ぶ。選ばれた項の和は 1+4=5 となる。
  • a_2 と a_3 を選ぶ。選ばれた項の和は 2+3=5 となる。
  • a_2 と a_4 を選ぶ。選ばれた項の和は 2+4=6 となる。
  • a_3 と a_4 を選ぶ。選ばれた項の和は 3+4=7 となる。

となり、S=\{3,4,5,6,7\} となります。S に含まれる 2 の倍数のうち最大のものは 6 なので、6 と出力します。


入力例 2

3 1 2
1 3 5

出力例 2

-1

この例では S=\{1,3,5\} です。S に含まれる非負整数はいずれも 2 の倍数でないため、-1 と出力します。

Score : 400 points

Problem Statement

You are given a sequence of non-negative integers A=(a_1,a_2,\ldots,a_N).

Let S be the set of non-negative integers that can be the sum of K terms in A (with distinct indices).

Find the greatest multiple of D in S. If there is no multiple of D in S, print -1 instead.

Constraints

  • 1 \leq K \leq N \leq 100
  • 1 \leq D \leq 100
  • 0 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N K D
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

4 2 2
1 2 3 4

Sample Output 1

6

Here are all the ways to choose two terms in A.

  • Choose a_1 and a_2, whose sum is 1+2=3.
  • Choose a_1 and a_3, whose sum is 1+3=4.
  • Choose a_1 and a_4, whose sum is 1+4=5.
  • Choose a_2 and a_3, whose sum is 2+3=5.
  • Choose a_2 and a_4, whose sum is 2+4=6.
  • Choose a_3 and a_4, whose sum is 3+4=7.

Thus, we have S=\{3,4,5,6,7\}. The greatest multiple of 2 in S is 6, so you should print 6.


Sample Input 2

3 1 2
1 3 5

Sample Output 2

-1

In this example, we have S=\{1,3,5\}. Nothing in S is a multiple of 2, so you should print -1.

H - Reachable Set

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450 点

問題文

N 頂点 M 辺からなる無向グラフがあります。 頂点には 1,2,\ldots,N の番号がついており、i 番目 (1\leq i\leq M) の辺は頂点 u _ i と頂点 v _ i を結んでいます。

k=1,2,\ldots,N について、次の問題を解いてください。

次の操作について考える。

  • 頂点を一つ選ぶ。選んだ頂点と、選んだ頂点に接続する全ての辺をグラフから削除する。

上の操作を繰り返すことで、次の条件を満たすことができるか判定せよ。

  • 頂点 1 から辺をたどって到達できる頂点の集合が、頂点 1, 頂点 2,\ldots, 頂点 k のちょうど k 個からなる。

可能な場合、条件を満たすために少なくとも何回操作を行う必要があるか求めよ。

制約

  • 1\leq N\leq2\times10 ^ 5
  • 0\leq M\leq3\times10 ^ 5
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M)
  • (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N M
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ M v _ M

出力

N 行にわたって出力せよ。 i 行目 (1\leq i\leq N) には、k=i としたときの条件を満たすことができないなら -1 を、満たすことができるなら条件を満たすために行う必要がある操作の回数を出力せよ。


入力例 1

6 7
1 2
1 5
2 3
2 4
2 5
3 6
5 6

出力例 1

2
3
3
2
1
0

たとえば、k=2 としたときの問題では、頂点 3, 頂点 4, 頂点 5 の 3 つの頂点を選んで削除することで、頂点 1 から到達できる頂点を頂点 1, 頂点 2 の 2 つだけにすることができます。 頂点を 2 つ以下だけ削除して条件を満たすことはできないため、2 行目には 3 を出力してください。

また、k=6 としたときの問題では、頂点をひとつも削除しないことで頂点 1 から到達できる頂点を頂点 1, 頂点 2,\ldots, 頂点 6 の 6 つにすることができます。 なので、6 行目には 0 を出力してください。


入力例 2

5 4
1 5
2 3
3 4
4 5

出力例 2

1
-1
-1
-1
0

入力例 3

2 0

出力例 3

0
-1

辺が 1 つもない場合もあります。


入力例 4

11 25
6 9
5 9
2 3
1 9
10 11
4 5
9 10
8 9
7 8
3 5
1 7
6 10
4 7
7 9
1 10
4 11
3 8
2 7
3 4
1 8
2 8
3 7
2 10
1 6
6 11

出力例 4

5
-1
-1
-1
-1
-1
4
3
2
1
0

Score : 450 points

Problem Statement

You are given an undirected graph with N vertices and M edges. The vertices are numbered 1,2,\ldots,N, and the i‑th edge (1 \le i \le M) connects vertices u_i and v_i.

For each k = 1,2,\ldots,N, solve the following problem:

Consider the following operation.

  • Choose one vertex, and delete that vertex together with all edges incident to it.

Determine whether one can repeat this operation to satisfy the following condition:

  • The set of vertices reachable from vertex 1 by traversing edges consists exactly of the k vertices 1,2,\ldots,k.

If it is possible, find the minimum number of operations required to do so.

Constraints

  • 1 \le N \le 2 \times 10^{5}
  • 0 \le M \le 3 \times 10^{5}
  • 1 \le u_i < v_i \le N\ (1 \le i \le M)
  • (u_i,v_i) \ne (u_j,v_j)\ (1 \le i < j \le M)
  • 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 N lines. On the i-th line (1 \le i \le N), if one cannot satisfy the condition for k=i, print -1; otherwise, print the minimum number of operations required to satisfy the condition.


Sample Input 1

6 7
1 2
1 5
2 3
2 4
2 5
3 6
5 6

Sample Output 1

2
3
3
2
1
0

For example, for k = 2, deleting the three vertices 3, 4, 5 makes the set of vertices reachable from vertex 1 equal to the two vertices 1, 2. It is impossible with two or fewer deletions, so print 3 on the 2nd line.

For k = 6, deleting zero vertices makes the set of vertices reachable from vertex 1 equal to the six vertices 1,2,\ldots,6, so print 0 on the 6th line.


Sample Input 2

5 4
1 5
2 3
3 4
4 5

Sample Output 2

1
-1
-1
-1
0

Sample Input 3

2 0

Sample Output 3

0
-1

There may be no edges.


Sample Input 4

11 25
6 9
5 9
2 3
1 9
10 11
4 5
9 10
8 9
7 8
3 5
1 7
6 10
4 7
7 9
1 10
4 11
3 8
2 7
3 4
1 8
2 8
3 7
2 10
1 6
6 11

Sample Output 4

5
-1
-1
-1
-1
-1
4
3
2
1
0
I - Cards

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500 点

問題文

1,\ldots,N の番号がついた N 枚のカードがあり、カード i の表には P_i が、裏には Q_i が書かれています。
ここで、P=(P_1,\ldots,P_N) 及び Q=(Q_1,\ldots,Q_N) はそれぞれ (1, 2, \dots, N) の並び替えです。

N 枚のカードから何枚かを選ぶ方法のうち、次の条件を満たすものは何通りありますか? 998244353 で割った余りを求めてください。

条件:1,2,\ldots,N のどの数も選んだカードのいずれかに書かれている

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq P_i,Q_i \leq N
  • P,Q はそれぞれ (1, 2, \dots, N) の並び替えである
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

出力

答えを出力せよ。


入力例 1

3
1 2 3
2 1 3

出力例 1

3

例えばカード 1,3 を選ぶと、1 はカード 1 の表に、2 はカード 1 の裏に、3 はカード 3 の表に書かれているため条件を満たします。

条件を満たすカードの選び方は \{1,3\},\{2,3\},\{1,2,3\} の 3 通りです。


入力例 2

5
2 3 5 4 1
4 2 1 3 5

出力例 2

12

入力例 3

8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8

出力例 3

1

Score : 500 points

Problem Statement

There are N cards numbered 1,\ldots,N. Card i has P_i written on the front and Q_i written on the back.
Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N).

How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353.

Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq P_i,Q_i \leq N
  • P and Q are permutations of (1, 2, \dots, N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

Output

Print the answer.


Sample Input 1

3
1 2 3
2 1 3

Sample Output 1

3

For example, if you choose Cards 1 and 3, then 1 is written on the front of Card 1, 2 on the back of Card 1, and 3 on the front of Card 3, so this combination satisfies the condition.

There are 3 ways to choose cards satisfying the condition: \{1,3\},\{2,3\},\{1,2,3\}.


Sample Input 2

5
2 3 5 4 1
4 2 1 3 5

Sample Output 2

12

Sample Input 3

8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8

Sample Output 3

1