E - Kaiten Sushi

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

配点 : 350

問題文

とある回転寿司に、1 から N までの番号が付けられた N 人の人が訪れています。 人 i美食度A_i です。

今からベルトコンベア上を M 個の寿司が流れます。 j 番目に流れる寿司の 美味しさB_j です。 それぞれの寿司は、人 1,2,\dots,N の前をこの順に流れていきます。 それぞれの人は、美味しさが自分の美食度以上である寿司が自分の前に流れてきたときはその寿司を取って食べ、それ以外のときは何もしません。 人 i が取って食べた寿司は、人 j\ (j > i) の前にはもう流れてきません。

M 個の寿司それぞれについて、その寿司を誰が食べるか、あるいは誰も食べないかどうかを求めてください。

制約

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_i \leq 2\times 10^5
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

M 行出力せよ。 j\ (1\leq j \leq M) 行目には、j 番目に流れる寿司を食べる人がいるならばその人の番号を、そうでないならば -1 を出力せよ。


入力例 1

3 3
3 8 2
5 2 1

出力例 1

1
3
-1
  • 1 番目の寿司について、
    • まず人 1 の前を流れます。B_1 \geq A_1 なので、人 1 はこれを取って食べます。
    • 2,3 の前にはこの寿司は流れてきません。
  • 2 番目の寿司について、
    • まず人 1 の前を流れます。B_2 < A_1 なので、人 1 は何もしません。
    • 次に人 2 の前を流れます。B_2 < A_2 なので、人 2 は何もしません。
    • 最後に人 3 の前を流れます。B_2 \geq A_3 なので、人 3 はこれを取って食べます。
  • 3 番目の寿司について、
    • まず人 1 の前を流れます。B_3 < A_1 なので、人 1 は何もしません。
    • 次に人 2 の前を流れます。B_3 < A_2 なので、人 2 は何もしません。
    • 最後に人 3 の前を流れます。B_3 < A_3 なので、人 3 は何もしません。
    • よって、誰もこの寿司を食べません。

入力例 2

3 3
1 1 1
1 1 1

出力例 2

1
1
1

入力例 3

10 5
60 83 76 45 70 91 37 58 94 22
70 39 52 33 18

出力例 3

1
7
4
10
-1

Score: 350 points

Problem Statement

There are N people numbered from 1 to N visiting a conveyor belt sushi restaurant. The gourmet level of person i is A_i.

Now, M pieces of sushi will be placed on the conveyor belt. The deliciousness of the j-th sushi is B_j. Each piece of sushi passes in front of people 1, 2, \dots, N in this order. Each person, when a sushi whose deliciousness is not less than their gourmet level passes in front of them, will take and eat that sushi; otherwise, they do nothing. A sushi that person i takes and eats will no longer pass in front of person j\ (j > i).

For each of the M pieces of sushi, determine who eats that sushi, or if nobody eats it.

Constraints

  • 1 \leq N, M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 2 \times 10^5
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print M lines. The j-th line (1 \leq j \leq M) should contain the number representing the person who eats the j-th sushi, or -1 if nobody eats it.


Sample Input 1

3 3
3 8 2
5 2 1

Sample Output 1

1
3
-1
  • For the 1st sushi:
    • It first passes in front of person 1. Since B_1 \geq A_1, person 1 takes and eats it.
    • It will not pass in front of person 2 and 3.
  • For the 2nd sushi:
    • It first passes in front of person 1. Since B_2 < A_1, person 1 does nothing.
    • Next, it passes in front of person 2. Since B_2 < A_2, person 2 does nothing.
    • Finally, it passes in front of person 3. Since B_2 \geq A_3, person 3 takes and eats it.
  • For the 3rd sushi:
    • It first passes in front of person 1. Since B_3 < A_1, person 1 does nothing.
    • Next, it passes in front of person 2. Since B_3 < A_2, person 2 does nothing.
    • Finally, it passes in front of person 3. Since B_3 < A_3, person 3 does nothing.
    • Therefore, nobody eats this sushi.

Sample Input 2

3 3
1 1 1
1 1 1

Sample Output 2

1
1
1

Sample Input 3

10 5
60 83 76 45 70 91 37 58 94 22
70 39 52 33 18

Sample Output 3

1
7
4
10
-1
F - Find it!

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

配点 : 350

問題文

N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。

注釈

この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。

  • M \ge 2
  • B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
  • B_M から B_1 に辺が伸びている。
  • i \neq j ならば B_i \neq B_j

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

入力

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

N
A_1 A_2 \dots A_N

出力

以下の形式で出力せよ。

M
B_1 B_2 \dots B_M

M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

答えとして考えられるものが複数ある場合、どれを出力しても正解となる。


入力例 1

7
6 7 2 1 3 4 5

出力例 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。

4
2 7 5 3
3
4 1 6

グラフが連結であるとは限らないことに注意してください。


入力例 2

2
2 1

出力例 2

2
1 2

1 \rightarrow 22 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 21 \rightarrow 22 \rightarrow 1 の辺が双方あることを表現しています。


入力例 3

8
3 7 4 7 3 3 8 2

出力例 3

3
2 7 8

この入力に対応するグラフは以下の通りです。

Score : 350 points

Problem Statement

There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.

Notes

The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:

  • M \geq 2
  • The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
  • The edge from vertex B_M to vertex B_1 exists.
  • If i \neq j, then B_i \neq B_j.

Constraints

  • All input values are integers.
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

Input

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

N
A_1 A_2 \dots A_N

Output

Print a solution in the following format:

M
B_1 B_2 \dots B_M

M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

If multiple solutions exist, any of them will be accepted.


Sample Input 1

7
6 7 2 1 3 4 5

Sample Output 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.

Here is the graph corresponding to this input:

Here are other acceptable outputs:

4
2 7 5 3
3
4 1 6

Note that the graph may not be connected.


Sample Input 2

2
2 1

Sample Output 2

2
1 2

This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.

Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:


Sample Input 3

8
3 7 4 7 3 3 8 2

Sample Output 3

3
2 7 8

Here is the graph corresponding to this input:

G - Cross Explosion

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

配点 : 400

問題文

H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と表します。
はじめ、全てのマスには壁が 1 個ずつ立てられています。
以下で説明されるクエリを Q 個与えられる順に処理した後に、残っている壁の個数を求めてください。

q 番目のクエリでは整数 R_q, C_q が与えられます。
あなたは (R_q, C_q) に爆弾を置いて壁を爆破します。その結果、以下の処理が行われます。

  • (R_q, C_q) に壁が存在する場合は、その壁を破壊して処理を終了する。
  • (R_q, C_q) に壁が存在しない場合は、(R_q, C_q) から上下左右に見て最初に現れる壁を破壊する。厳密に述べると、以下の 4 個の処理が同時に行われる。
    • (i, C_q) に壁が存在して (k, C_q) (i \lt k \lt R_q) に壁が存在しないような i \lt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
    • (i, C_q) に壁が存在して (k, C_q) (R_q \lt k \lt i) に壁が存在しないような i \gt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
    • (R_q, j) に壁が存在して (R_q, k) (j \lt k \lt C_q) に壁が存在しないような j \lt C_q が存在する時、(R_q, j) に存在する壁を破壊する。
    • (R_q, j) に壁が存在して (R_q, k) (C_q \lt k \lt j) に壁が存在しないような j \gt C_q が存在する時、(R_q, j) に存在する壁を破壊する。

制約

  • 1 \leq H, W
  • H \times W \leq 4 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq R_q \leq H
  • 1 \leq C_q \leq W
  • 入力される値は全て整数

入力

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

H W Q
R_1 C_1
R_2 C_2
\vdots
R_Q C_Q

出力

クエリを全て処理した後に、残っている壁の個数を出力せよ。


入力例 1

2 4 3
1 2
1 2
1 3

出力例 1

2

クエリを処理する手順を説明すると次のようになります。

  • 1 番目のクエリでは (R_1, C_1) = (1, 2) である。 (1, 2) に壁は存在するので (1, 2) の壁が爆破される。
  • 2 番目のクエリでは (R_2, C_2) = (1, 2) である。 (1, 2) に壁は存在しないので、(1, 2) から上下左右に見て最初に現れる壁である (2,2),(1,1),(1,3) が爆破される。
  • 3 番目のクエリでは (R_2, C_2) = (1, 3) である。 (1, 3) に壁は存在しないので、(1, 3) から上下左右に見て最初に現れる壁である (2,3),(1,4) が爆破される。

全てのクエリを処理した後に残っている壁は (2, 1), (2, 4)2 個です。


入力例 2

5 5 5
3 3
3 3
3 2
2 2
1 2

出力例 2

10

入力例 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

出力例 3

2

Score : 400 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Initially, there is one wall in each cell.
After processing Q queries explained below in the order they are given, find the number of remaining walls.

In the q-th query, you are given two integers R_q and C_q.
You place a bomb at (R_q, C_q) to destroy walls. As a result, the following process occurs.

  • If there is a wall at (R_q, C_q), destroy that wall and end the process.
  • If there is no wall at (R_q, C_q), destroy the first walls that appear when looking up, down, left, and right from (R_q, C_q). More precisely, the following four processes occur simultaneously:
    • If there exists an i \lt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all i \lt k \lt R_q, destroy the wall at (i, C_q).
    • If there exists an i \gt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all R_q \lt k \lt i, destroy the wall at (i, C_q).
    • If there exists a j \lt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all j \lt k \lt C_q, destroy the wall at (R_q, j).
    • If there exists a j \gt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all C_q \lt k \lt j, destroy the wall at (R_q, j).

Constraints

  • 1 \leq H, W
  • H \times W \leq 4 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq R_q \leq H
  • 1 \leq C_q \leq W
  • All input values are integers.

Input

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

H W Q
R_1 C_1
R_2 C_2
\vdots
R_Q C_Q

Output

Print the number of remaining walls after processing all queries.


Sample Input 1

2 4 3
1 2
1 2
1 3

Sample Output 1

2

The process of handling the queries can be explained as follows:

  • In the 1st query, (R_1, C_1) = (1, 2). There is a wall at (1, 2), so the wall at (1, 2) is destroyed.
  • In the 2nd query, (R_2, C_2) = (1, 2). There is no wall at (1, 2), so the walls at (2,2),(1,1),(1,3), which are the first walls that appear when looking up, down, left, and right from (1, 2), are destroyed.
  • In the 3rd query, (R_3, C_3) = (1, 3). There is no wall at (1, 3), so the walls at (2,3),(1,4), which are the first walls that appear when looking up, down, left, and right from (1, 3), are destroyed.

After processing all queries, there are two remaining walls, at (2, 1) and (2, 4).


Sample Input 2

5 5 5
3 3
3 3
3 2
2 2
1 2

Sample Output 2

10

Sample Input 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

Sample Output 3

2
H - (∀x∀)

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

配点 : 500

問題文

T 個のテストケースについて、次の問題を解いてください。

整数 N と文字列 S が与えられるので、以下の条件を全て満たす文字列 X の数を 998244353 で割った余りを求めてください。

  • X は英大文字のみからなる長さ N の文字列
  • X は回文
  • 辞書順で X \le S
    • すなわち、 X=S であるか、辞書順で XS より前に来る

制約

  • 1 \le T \le 250000
  • N1 以上 10^6 以下の整数
  • ひとつの入力について、含まれるテストケースの N の総和は 10^6 を超えない
  • S は英大文字のみからなる長さ N の文字列

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

ただし、 \mathrm{case}_ii 個目のテストケースを表す。

各テストケースは以下の形式で与えられる。

N
S

出力

全体で T 行出力せよ。
i 行目には i 個目のテストケースに対する答えを整数として出力せよ。


入力例 1

5
3
AXA
6
ABCZAZ
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW

出力例 1

24
29
212370247
36523399
231364016

この入力には 5 個のテストケースが含まれます。

1 個目のテストケース:
問題文中の条件を満たす文字列は AAA, ABA, ACA,..., AXA24 個です。

2 個目のテストケース:
S が回文であるとは限りません。

3 個目のテストケース:
998244353 で割った余りを求めることに注意してください。

Score : 500 points

Problem Statement

Solve the following problem for T test cases.

Given an integer N and a string S, find the number of strings X that satisfy all of the conditions below, modulo 998244353.

  • X is a string of length N consisting of uppercase English letters.
  • X is a palindrome.
  • X \le S in lexicographical order.
    • That is, X=S or X is lexicographically smaller than S.

Constraints

  • 1 \le T \le 250000
  • N is an integer between 1 and 10^6 (inclusive).
  • In a single input, the sum of N over the test cases is at most 10^6.
  • S is a string of length N consisting of uppercase English letters.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Here, \mathrm{case}_i represents the i-th test case.

Each test case is in the following format:

N
S

Output

Print T lines. The i-th line should contain the answer for the i-th test case as an integer.


Sample Input 1

5
3
AXA
6
ABCZAZ
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW

Sample Output 1

24
29
212370247
36523399
231364016

This input contains five test cases.

Test case #1:
The 24 strings satisfying the conditions are AAA, ABA, ACA,..., AXA.

Test case #2:
S may not be a palindrome.

Test case #3:
Be sure to find the count modulo 998244353.

I - Sensor Optimization Dilemma

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

配点 : 500

問題文

キーエンスの工場長であるあなたは、ベルトコンベア上のいくつかの区間をセンサーによって監視したいと考えています。 あなたが監視したい区間は全部で N 個あり、i 個目の区間の長さは D_i メートルです。

センサーには 2 種類の候補があり、それぞれのセンサーに関する情報は以下の通りです。

  • センサー j\ (1\leq j \leq 2) : 長さ L_j メートルの区間を監視できる。 価格は 1 個あたり C_j であり、全体で最大 K_j 個まで使用することができる。

1 つの区間をいくつかの区間に分割して監視することもできます。 また、センサーが監視する区間が重なっていたり、監視したい区間の長さより余分に監視していたりしても問題はありません。 例えば、L_1=4,L_2=2 であるとき、センサー 11 つ使って長さ 3 メートルの区間を監視したり、センサー 1,21 つずつ使って長さ 5 メートルの区間を監視したりすることが可能です。

N 個の区画をすべて監視することが可能であるか判定し、可能ならば必要なセンサーの価格の総和の最小値を求めてください。

制約

  • 1\leq N \leq 100
  • 1\leq D_i,L_j \leq 10^5
  • 1\leq C_j \leq 10^9
  • 1\leq K_j \leq 10^3
  • 入力は全て整数

入力

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

N
D_1 D_2 \dots D_N
L_1 C_1 K_1
L_2 C_2 K_2

出力

N 個の区画をすべて監視することが不可能ならば -1 を、可能ならば必要なセンサーの価格の総和の最小値を出力せよ。


入力例 1

3
3 5 10
4 3 3
2 2 6

出力例 1

17

以下のようにすることで、センサー 13 つ、センサー 24 つ使ってすべての区間を監視できます。

  • センサー 11 つ使って 1 個目の区間を監視する。
  • センサー 1,21 つずつ使って 2 個目の区間を監視する。
  • センサー 11 つ、センサー 23 つ使って 3 個目の区間を監視する。

このとき、必要なセンサーの価格の総和は 3\times 3 + 2\times 4 = 17 であり、これが最小です。


入力例 2

3
3 5 10
4 3 3
2 2 3

出力例 2

-1

入力例 3

2
4 8
3 1 100
4 10000 100

出力例 3

5

1 つも使わない種類のセンサーがあっても構いません。

Score : 500 points

Problem Statement

As the factory manager of Keyence, you want to monitor several sections on a conveyor belt. There are a total of N sections you want to monitor, and the length of the i-th section is D_i meters.

There are two types of sensors to choose from, and below is some information about each sensor.

  • Type-j sensor (1\leq j \leq 2): Can monitor a section of length L_j meters. The price is C_j per sensor, and you can use at most K_j sensors of this type in total.

You can divide one section into several sections for monitoring. It is fine if the sections monitored by the sensors overlap, or if they monitor more than the length of the section you want to monitor. For example, when L_1=4 and L_2=2, you can use one type-1 sensor to monitor a section of length 3 meters, or use one type-1 and one type-2 sensor to monitor a section of length 5 meters.

Determine whether it is possible to monitor all N sections, and if it is possible, find the minimum total cost of the necessary sensors.

Constraints

  • 1\leq N \leq 100
  • 1\leq D_i,L_j \leq 10^5
  • 1\leq C_j \leq 10^9
  • 1\leq K_j \leq 10^3
  • All input values are integers.

Input

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

N
D_1 D_2 \dots D_N
L_1 C_1 K_1
L_2 C_2 K_2

Output

If it is impossible to monitor all N sections, print -1. Otherwise, print the minimum total cost of the necessary sensors.


Sample Input 1

3
3 5 10
4 3 3
2 2 6

Sample Output 1

17

You can monitor all sections by using three type-1 sensors and four type-2 sensors as follows.

  • Use one type-1 sensor to monitor the first section.
  • Use one type-1 and one type-2 sensor to monitor the second section.
  • Use one type-1 and three type-2 sensors to monitor the third section.

In this case, the total cost of the necessary sensors is 3\times 3 + 2\times 4 = 17, which is the minimum.


Sample Input 2

3
3 5 10
4 3 3
2 2 3

Sample Output 2

-1

Sample Input 3

2
4 8
3 1 100
4 10000 100

Sample Output 3

5

It is fine if one type of sensor is not used at all.