A - テストの点数比較

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

配点 : 200 点

問題文

青木君のクラスでは先日、数学のテストが行われました。クラスには N 人の生徒がおり、それぞれ 1 から N までの出席番号が付けられています。生徒 i (1 \leq i \leq N) のテストの点数は S_i 点です。

青木君は先生から頼まれて、生徒たちの成績を整理しています。青木君は Q 個の質問に答える必要があります。i 番目の質問では、出席番号 a_i の生徒の点数が出席番号 b_i の生徒の点数より厳密に高いかどうかを判定します。ここで「厳密に高い」とは、等しい場合を含まず、S_{a_i} > S_{b_i} が成り立つことを意味します。なお、a_i = b_i、すなわち同じ生徒同士の比較が行われる場合もあります。

各質問に対して、S_{a_i} > S_{b_i} ならば Yes を、そうでなければ(すなわち S_{a_i} \leq S_{b_i} ならば) No を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 0 \leq S_i \leq 100 (1 \leq i \leq N)
  • 1 \leq a_i \leq N (1 \leq i \leq Q)
  • 1 \leq b_i \leq N (1 \leq i \leq Q)
  • a_i = b_i である場合もある
  • 入力はすべて整数

入力

N Q
S_1 S_2 \ldots S_N
a_1 b_1
a_2 b_2
\vdots
a_Q b_Q
  • 1 行目には、生徒の人数を表す整数 N と、質問の個数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各生徒のテストの点数を表す N 個の整数 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
  • 続く Q 行のうち i 行目 (1 \leq i \leq Q) には、i 番目の質問で比較する 2 人の生徒の出席番号を表す整数 a_i と b_i が、スペース区切りで与えられる。

出力

Q 行にわたって出力してください。i 行目 (1 \leq i \leq Q) には、i 番目の質問に対する答えとして、S_{a_i} > S_{b_i} ならば Yes を、そうでなければ No を出力してください。


入力例 1

3 4
80 65 80
1 2
2 1
1 3
3 1

出力例 1

Yes
No
No
No

入力例 2

5 6
72 85 60 85 90
5 2
2 4
1 3
4 2
3 5
2 1

出力例 2

Yes
No
Yes
No
No
Yes

入力例 3

10 8
45 78 92 65 100 38 72 0 55 100
5 10
10 5
6 8
8 6
3 1
1 2
4 7
9 4

出力例 3

No
No
Yes
No
Yes
No
No
No

Score : 200 pts

Problem Statement

A math test was recently held in Aoki's class. There are N students in the class, each assigned a student number from 1 to N. The test score of student i (1 \leq i \leq N) is S_i points.

Aoki has been asked by the teacher to organize the students' grades. Aoki needs to answer Q questions. For the i-th question, he determines whether the score of the student with student number a_i is strictly higher than the score of the student with student number b_i. Here, "strictly higher" means that equality is not included, i.e., S_{a_i} > S_{b_i} holds. Note that a_i = b_i is possible, meaning a comparison of the same student with themselves may occur.

For each question, output Yes if S_{a_i} > S_{b_i}, and No otherwise (i.e., if S_{a_i} \leq S_{b_i}).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 0 \leq S_i \leq 100 (1 \leq i \leq N)
  • 1 \leq a_i \leq N (1 \leq i \leq Q)
  • 1 \leq b_i \leq N (1 \leq i \leq Q)
  • It is possible that a_i = b_i
  • All input values are integers

Input

N Q
S_1 S_2 \ldots S_N
a_1 b_1
a_2 b_2
\vdots
a_Q b_Q
  • The first line contains an integer N representing the number of students and an integer Q representing the number of questions, separated by a space.
  • The second line contains N integers S_1, S_2, \ldots, S_N representing the test scores of each student, separated by spaces.
  • In the following Q lines, the i-th line (1 \leq i \leq Q) contains integers a_i and b_i representing the student numbers of the two students to compare in the i-th question, separated by a space.

Output

Output Q lines. On the i-th line (1 \leq i \leq Q), output Yes if S_{a_i} > S_{b_i}, and No otherwise, as the answer to the i-th question.


Sample Input 1

3 4
80 65 80
1 2
2 1
1 3
3 1

Sample Output 1

Yes
No
No
No

Sample Input 2

5 6
72 85 60 85 90
5 2
2 4
1 3
4 2
3 5
2 1

Sample Output 2

Yes
No
Yes
No
No
Yes

Sample Input 3

10 8
45 78 92 65 100 38 72 0 55 100
5 10
10 5
6 8
8 6
3 1
1 2
4 7
9 4

Sample Output 3

No
No
Yes
No
Yes
No
No
No
B - 噂の広がり

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

配点 : 266 点

問題文

ある学校には N 人の生徒がおり、各生徒には 1 から N までの出席番号が付けられています。

あるとき、面白い噂が発生しました。最初の時点(すべてのグループワークが行われる前)では、出席番号 1, 2, \ldots, K の K 人の生徒だけがこの噂を知っており、それ以外の生徒は噂を知りません。

この学校では、今後 M 回のグループワークが 1 番目から M 番目まで順番に行われます。なお、異なるグループワークで同じペアが再び組まれることもあります。

i 番目 (1 \leq i \leq M) のグループワークでは、出席番号 A_i の生徒と出席番号 B_i の生徒がペアを組んで作業します。そのグループワークの開始時点でペアの2人のうち少なくとも一方が噂を知っていれば、そのグループワークの終了後には2人とも噂を知っている状態になります。どちらも噂を知らない場合は、何も変化しません。

あるグループワークによる噂の伝播は、それ以降のグループワークに影響します。

すべてのグループワークが終わった後に、噂を知っている生徒の人数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq A_i < B_i \leq N (1 \leq i \leq M)
  • 異なるグループワークで同じペアが再び組まれることもある
  • 入力はすべて整数

入力

N M K
A_1 B_1
A_2 B_2
\vdots
A_M B_M
  • 1 行目には、生徒の人数を表す N 、グループワークの回数を表す M 、最初に噂を知っている生徒の人数を表す K が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目では、各グループワークでペアを組む生徒の出席番号が与えられる。M = 0 の場合、この部分は存在しない。
  • 1 + i 行目 (1 \leq i \leq M) では、i 番目のグループワークでペアを組む生徒の出席番号 A_i と B_i がスペース区切りで与えられる。

出力

すべてのグループワークが終わった後に噂を知っている生徒の人数を 1 行で出力してください。


入力例 1

5 3 1
1 2
2 3
4 5

出力例 1

3

入力例 2

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

出力例 2

5

入力例 3

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

出力例 3

9

Score : 266 pts

Problem Statement

A school has N students, each assigned a student ID number from 1 to N.

At some point, an interesting rumor started. At the initial time (before any group work takes place), only the K students with student ID numbers 1, 2, \ldots, K know this rumor, and no other students know it.

From now on, M group work sessions will take place at this school, in order from the 1-st to the M-th. Note that the same pair may be paired together again in different group work sessions.

In the i-th (1 \leq i \leq M) group work session, the student with student ID A_i and the student with student ID B_i form a pair and work together. If at least one of the two paired students knows the rumor at the start of that group work session, then both students will know the rumor after that group work session ends. If neither of them knows the rumor, nothing changes.

The propagation of the rumor caused by a group work session affects all subsequent group work sessions.

Determine the number of students who know the rumor after all group work sessions have been completed.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq A_i < B_i \leq N (1 \leq i \leq M)
  • The same pair may be paired together again in different group work sessions
  • All input values are integers

Input

N M K
A_1 B_1
A_2 B_2
\vdots
A_M B_M
  • The first line contains N representing the number of students, M representing the number of group work sessions, and K representing the number of students who initially know the rumor, separated by spaces.
  • From the 2nd line to the (M + 1)-th line, the student ID numbers of the students who form a pair in each group work session are given. If M = 0, this part does not exist.
  • The (1 + i)-th line (1 \leq i \leq M) contains the student ID numbers A_i and B_i of the students who form a pair in the i-th group work session, separated by a space.

Output

Print in one line the number of students who know the rumor after all group work sessions have been completed.


Sample Input 1

5 3 1
1 2
2 3
4 5

Sample Output 1

3

Sample Input 2

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

Sample Output 2

5

Sample Input 3

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

Sample Output 3

9
C - ユニークな座席

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

配点 : 300 点

問題文

高橋君は、 H 行 W 列の座席表を管理しています。座席表の第 i 行第 j 列の座席を座席 (i, j) と呼びます。各座席にはアルファベット小文字 1 文字で表されるグループ名が割り当てられており、座席 (i, j) のグループ名を G_{i,j} とします。

高橋君は、座席表の中から「ユニークな座席」を見つけたいと考えています。座席 (i, j) が「ユニークな座席」であるとは、以下の 2 つの条件を同時に満たすことを意味します。

  • 行における一意性: 第 i 行の W 個の座席の中で、グループ名が G_{i,j} であるものは座席 (i, j) のみである。すなわち、G_{i,k} = G_{i,j} を満たす k (1 \leq k \leq W) は k = j のみである。
  • 列における一意性: 第 j 列の H 個の座席の中で、グループ名が G_{i,j} であるものは座席 (i, j) のみである。すなわち、G_{k,j} = G_{i,j} を満たす k (1 \leq k \leq H) は k = i のみである。

高橋君は、すべてのユニークな座席のグループ名を、行番号の小さい順に、同じ行の中では列番号の小さい順に読み取り、連結した文字列を作りたいと考えています。

座席表の情報が与えられるので、上記の文字列を求めてください。ユニークな座席が 1 つも存在しない場合は、空文字列を求めてください。

制約

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • H, W は整数
  • G_{i,j} (1 \leq i \leq H, 1 \leq j \leq W) はアルファベット小文字(a から z)である。

入力

H W
G_{1,1}G_{1,2}\cdots G_{1,W}
G_{2,1}G_{2,2}\cdots G_{2,W}
\vdots
G_{H,1}G_{H,2}\cdots G_{H,W}
  • 1 行目には、座席表の行数 H と列数 W が、スペース区切りで与えられる。
  • 続く H 行にわたって、座席表の各行の情報が与えられる。その i 番目 (1 \leq i \leq H) の行には、第 i 行の座席のグループ名を連結した W 文字の文字列 G_{i,1}G_{i,2}\cdots G_{i,W} が与えられる。

出力

ユニークな座席のグループ名を、行番号の小さい順に、同じ行の中では列番号の小さい順に連結した文字列を 1 行で出力せよ。ユニークな座席が 1 つも存在しない場合は、空文字列を出力せよ(すなわち、空の行を出力せよ)。


入力例 1

3 3
abc
bca
cab

出力例 1

abcbcacab

入力例 2

2 2
aa
aa

出力例 2



入力例 3

4 6
abcdef
aghijk
almnop
aqrstu

出力例 3

bcdefghijklmnopqrstu

入力例 4

8 10
abcdefghij
abcdefghij
klmnopqrst
klmnopqrst
uvwxabcdef
uvwxabcdef
ghijklmnop
ghijklmnop

出力例 4



入力例 5

1 1
z

出力例 5

z

Score : 300 pts

Problem Statement

Takahashi is managing a seating chart with H rows and W columns. The seat at row i and column j of the seating chart is called seat (i, j). Each seat is assigned a group name represented by a single lowercase letter, and the group name of seat (i, j) is denoted G_{i,j}.

Takahashi wants to find all "unique seats" in the seating chart. A seat (i, j) is a "unique seat" if and only if it satisfies both of the following conditions simultaneously:

  • Uniqueness in the row: Among the W seats in row i, seat (i, j) is the only one with group name G_{i,j}. That is, the only k (1 \leq k \leq W) satisfying G_{i,k} = G_{i,j} is k = j.
  • Uniqueness in the column: Among the H seats in column j, seat (i, j) is the only one with group name G_{i,j}. That is, the only k (1 \leq k \leq H) satisfying G_{k,j} = G_{i,j} is k = i.

Takahashi wants to read the group names of all unique seats in order of increasing row number, and within the same row in order of increasing column number, and concatenate them into a single string.

Given the seating chart information, determine the above string. If no unique seats exist, determine the empty string.

Constraints

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • H, W are integers.
  • G_{i,j} (1 \leq i \leq H, 1 \leq j \leq W) is a lowercase letter (a through z).

Input

H W
G_{1,1}G_{1,2}\cdots G_{1,W}
G_{2,1}G_{2,2}\cdots G_{2,W}
\vdots
G_{H,1}G_{H,2}\cdots G_{H,W}
  • The first line contains the number of rows H and the number of columns W of the seating chart, separated by a space.
  • The following H lines give the information for each row of the seating chart. The i-th (1 \leq i \leq H) of these lines contains a string of W characters G_{i,1}G_{i,2}\cdots G_{i,W}, which is the concatenation of the group names of the seats in row i.

Output

Output in a single line the string obtained by concatenating the group names of the unique seats in order of increasing row number, and within the same row in order of increasing column number. If no unique seats exist, output the empty string (that is, output an empty line).


Sample Input 1

3 3
abc
bca
cab

Sample Output 1

abcbcacab

Sample Input 2

2 2
aa
aa

Sample Output 2



Sample Input 3

4 6
abcdef
aghijk
almnop
aqrstu

Sample Output 3

bcdefghijklmnopqrstu

Sample Input 4

8 10
abcdefghij
abcdefghij
klmnopqrst
klmnopqrst
uvwxabcdef
uvwxabcdef
ghijklmnop
ghijklmnop

Sample Output 4



Sample Input 5

1 1
z

Sample Output 5

z
D - 蛍光ペンでマーキング

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

配点 : 366 点

問題文

高橋君は、試験勉強のために教科書の重要な部分に蛍光ペンでマーキングをしています。

教科書のある行に注目します。この行には N 文字が横一列に並んでおり、左から順に位置 1, 2, \ldots, N と番号が付いています。

高橋君が使っている蛍光ペンは、一度ペンを引くと連続する W 文字分をマーキングします。高橋君はこの蛍光ペンを合計 K 回使用しました。i 回目 (1 \leq i \leq K) のマーキングでは、開始位置 L_i を選び、位置 L_i から位置 L_i + W - 1 までの連続する W 文字をマーキングしました。ただし、マーキング範囲が行からはみ出すことはありません(すなわち 1 \leq L_i \leq N - W + 1 が成り立ちます)。

異なる回のマーキングで同じ開始位置が選ばれることもあります。また、開始位置が異なっていてもマーキング範囲が重なることがあります。各位置の文字がマーキングされた回数は、その位置を含むマーキング操作の回数の合計です。

すべてのマーキング作業が終わった後、各位置の文字が合計で何回マーキングされたかを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq W \leq N
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq L_i \leq N - W + 1 (1 \leq i \leq K)
  • 入力はすべて整数

入力

N W K
L_1
L_2
\vdots
L_K
  • 1 行目には、文字の総数 N 、蛍光ペンで一度にマーキングされる文字数 W 、マーキングの回数 K がスペース区切りで与えられる。
  • 続く K 行のうち i 行目 (1 \leq i \leq K) には、 i 回目のマーキングの開始位置 L_i が与えられる。

出力

N 個の整数をスペース区切りで 1 行に出力せよ。 j 番目 (1 \leq j \leq N) の整数は、位置 j の文字がマーキングされた回数を表す。


入力例 1

10 3 2
2
5

出力例 1

0 1 1 1 1 1 1 0 0 0

入力例 2

8 4 3
1
3
2

出力例 2

1 2 3 3 2 1 0 0

入力例 3

15 5 6
1
3
7
11
2
7

出力例 3

1 2 3 3 3 2 3 2 2 2 3 1 1 1 1

Score : 366 pts

Problem Statement

Takahashi is highlighting important parts of his textbook with a fluorescent marker while studying for exams.

Consider a particular line in the textbook. This line contains N characters arranged in a horizontal row, numbered from left to right as positions 1, 2, \ldots, N.

The fluorescent marker Takahashi uses marks W consecutive characters each time it is used. Takahashi used this fluorescent marker a total of K times. In the i-th marking (1 \leq i \leq K), he chose a starting position L_i and marked the W consecutive characters from position L_i to position L_i + W - 1. The marking range never extends beyond the line (that is, 1 \leq L_i \leq N - W + 1 holds).

The same starting position may be chosen in different markings. Also, even if starting positions differ, their marking ranges may overlap. The number of times a character at each position is marked is the total number of marking operations that include that position.

After all marking operations are completed, determine how many times the character at each position was marked in total.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq W \leq N
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq L_i \leq N - W + 1 (1 \leq i \leq K)
  • All inputs are integers

Input

N W K
L_1
L_2
\vdots
L_K
  • The first line contains the total number of characters N, the number of characters marked at once by the fluorescent marker W, and the number of markings K, separated by spaces.
  • The i-th (1 \leq i \leq K) of the following K lines contains the starting position L_i of the i-th marking.

Output

Output N integers separated by spaces on a single line. The j-th (1 \leq j \leq N) integer represents the number of times the character at position j was marked.


Sample Input 1

10 3 2
2
5

Sample Output 1

0 1 1 1 1 1 1 0 0 0

Sample Input 2

8 4 3
1
3
2

Sample Output 2

1 2 3 3 2 1 0 0

Sample Input 3

15 5 6
1
3
7
11
2
7

Sample Output 3

1 2 3 3 3 2 3 2 2 2 3 1 1 1 1
E - 材料を使ってロープを作る

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

配点 : 433 点

問題文

高橋君は、ちょうど W センチメートルの長さのロープを 1 本作る必要があります。

高橋君は N 種類の紐を持っています。i 番目の種類の紐は長さが L_i センチメートルで、C_i 本の在庫があります。異なる種類の紐の長さが同じこともあります。

高橋君は、これらの紐の中から 1 本以上を選び、切断せずにそのままの長さで一直線に繋げてロープを作ります。繋げたロープの長さは、選んだ紐の長さの合計に等しくなります(結び目による長さの減少はありません)。同じ種類の紐を複数本使うこともできますが、i 番目の種類の紐は在庫の C_i 本までしか使えません。

高橋君は、選んだ紐の長さの合計がちょうど W センチメートルになるようにしつつ、使用する紐の本数をできるだけ少なくしたいと考えています。

ちょうど W センチメートルのロープを作れるとき、使用する紐の最小本数を求めてください。どのように紐を選んでもちょうど W センチメートルにできない場合は、-1 を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq W \leq 50000
  • 1 \leq L_i \leq W
  • 1 \leq C_i \leq 10000
  • 入力はすべて整数

入力

N W
L_1 C_1
L_2 C_2
\vdots
L_N C_N
  • 1 行目には、紐の種類数を表す N と、作りたいロープの長さを表す W が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各紐の情報が与えられる。
  • 1 + i 行目では、i 番目の種類の紐の長さ L_i と在庫数 C_i が、スペース区切りで与えられる。

出力

ちょうど W センチメートルのロープを作るために必要な紐の最小本数を 1 行で出力してください。ロープを作ることが不可能な場合は、-1 を出力してください。


入力例 1

3 10
3 2
5 3
7 1

出力例 1

2

入力例 2

2 7
3 2
5 1

出力例 2

-1

入力例 3

5 80
7 5
13 4
23 3
31 2
50 2

出力例 3

3

入力例 4

10 5000
3 100
7 200
11 150
19 80
29 60
53 40
97 30
181 20
337 15
631 8

出力例 4

12

入力例 5

1 1
1 1

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi needs to make exactly one rope of exactly W centimeters in length.

Takahashi has N types of strings. The i-th type of string has a length of L_i centimeters, and he has C_i of them in stock. Different types of strings may have the same length.

Takahashi will select one or more of these strings and connect them in a straight line without cutting them, keeping their original lengths, to make a rope. The length of the resulting rope equals the sum of the lengths of the selected strings (there is no length reduction due to knots). He may use multiple strings of the same type, but he can use at most C_i strings of the i-th type.

Takahashi wants to minimize the number of strings used while ensuring that the total length of the selected strings is exactly W centimeters.

If it is possible to make a rope of exactly W centimeters, find the minimum number of strings needed. If it is impossible to make a rope of exactly W centimeters regardless of how the strings are chosen, output -1.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq W \leq 50000
  • 1 \leq L_i \leq W
  • 1 \leq C_i \leq 10000
  • All inputs are integers

Input

N W
L_1 C_1
L_2 C_2
\vdots
L_N C_N
  • The first line contains N, the number of types of strings, and W, the desired length of the rope, separated by a space.
  • From the 2nd line to the (N + 1)-th line, information about each string is given.
  • The (1 + i)-th line contains the length L_i and the stock count C_i of the i-th type of string, separated by a space.

Output

Output in one line the minimum number of strings needed to make a rope of exactly W centimeters. If it is impossible to make the rope, output -1.


Sample Input 1

3 10
3 2
5 3
7 1

Sample Output 1

2

Sample Input 2

2 7
3 2
5 1

Sample Output 2

-1

Sample Input 3

5 80
7 5
13 4
23 3
31 2
50 2

Sample Output 3

3

Sample Input 4

10 5000
3 100
7 200
11 150
19 80
29 60
53 40
97 30
181 20
337 15
631 8

Sample Output 4

12

Sample Input 5

1 1
1 1

Sample Output 5

1