Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。
高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。
- 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。
操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q x_1 \vdots x_Q
出力
a_1,\ldots,a_N を空白区切りで出力せよ。
入力例 1
5 5 1 2 3 4 5
出力例 1
1 2 3 5 4
操作は以下のように行われます。
- 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
- 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
- 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。
入力例 2
7 7 7 7 7 7 7 7 7
出力例 2
1 2 3 4 5 7 6
入力例 3
10 6 1 5 2 9 6 6
出力例 3
1 2 3 4 5 7 6 8 10 9
Score : 300 points
Problem Statement
N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.
Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.
- Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.
Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q x_1 \vdots x_Q
Output
Print a_1,\ldots,a_N, with spaces in between.
Sample Input 1
5 5 1 2 3 4 5
Sample Output 1
1 2 3 5 4
The operations are performed as follows.
- Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
- Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
- Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.
Sample Input 2
7 7 7 7 7 7 7 7 7
Sample Output 2
1 2 3 4 5 7 6
Sample Input 3
10 6 1 5 2 9 6 6
Sample Output 3
1 2 3 4 5 7 6 8 10 9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
9\times 9 のマス目 A があり、各マスには 1 以上 9 以下の整数が書き込まれています。
具体的には、 A の上から i 行目、左から j 列目のマスには A_{i,j} が書き込まれています。
A が次の条件をすべてみたしているならば Yes
を、そうでないならば No
を出力してください。
- A の各行について、その行に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
- A の各列について、その列に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
- A の行を上から 3 行ずつ 3 つに分け、同様に列も左から 3 列ずつ 3 つに分ける。 これによって A を 9 つの 3\times 3 のマス目に分けたとき、それぞれの 3\times 3 のマス目には 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
制約
- 1\leq A_{i,j}\leq 9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A_{1,1} A_{1,2} \ldots A_{1,9} A_{2,1} A_{2,2} \ldots A_{2,9} \vdots A_{9,1} A_{9,2} \ldots A_{9,9}
出力
マス目 A が問題文の条件をすべてみたすならば Yes
を、
そうでないならば No
を出力せよ。
入力例 1
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 4 5 6 7 8 9 1 5 6 7 8 9 1 2 3 4 8 9 1 2 3 4 5 6 7 3 4 5 6 7 8 9 1 2 6 7 8 9 1 2 3 4 5 9 1 2 3 4 5 6 7 8
出力例 1
Yes
マス目 A は次のようになっています。
マス目 A は 3 つの条件をすべてみたしているため、Yes
を出力します。
入力例 2
1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
出力例 2
No
マス目 A は次のようになっています。
例えば左上の 3\times 3 のマス目に注目すると 3 つめの条件をみたしていないことが分かるため、No
を出力します。
入力例 3
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6
出力例 3
No
マス目 A は次のようになっています。
例えば一番左の列に注目すると 2 つめの条件をみたしていないことが分かるため、No
を出力します。
Score : 250 points
Problem Statement
There is a 9\times 9 grid A, where each cell contains an integer between 1 and 9, inclusive.
Specifically, the cell at the i-th row from the top and j-th column from the left contains A_{i,j}.
If A satisfies all of the following conditions, print Yes
. Otherwise, print No
.
- For each row of A, the nine cells in that row contain each integer from 1 to 9 exactly once.
- For each column of A, the nine cells in that column contain each integer from 1 to 9 exactly once.
- Divide the rows of A into three groups, each of three rows, from top to bottom, and similarly divide the columns into three groups, each of three columns, from left to right. Each 3\times 3 grid obtained from A in this way contains each integer from 1 to 9 exactly once.
Constraints
- 1\leq A_{i,j}\leq 9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A_{1,1} A_{1,2} \ldots A_{1,9} A_{2,1} A_{2,2} \ldots A_{2,9} \vdots A_{9,1} A_{9,2} \ldots A_{9,9}
Output
If the grid A satisfies all the conditions in the problem statement, print Yes
; otherwise, print No
.
Sample Input 1
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 4 5 6 7 8 9 1 5 6 7 8 9 1 2 3 4 8 9 1 2 3 4 5 6 7 3 4 5 6 7 8 9 1 2 6 7 8 9 1 2 3 4 5 9 1 2 3 4 5 6 7 8
Sample Output 1
Yes
The grid A is shown below.
The grid A satisfies all three conditions, so print Yes
.
Sample Input 2
1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
Sample Output 2
No
The grid A is shown below.
For example, if you look at the top left 3\times 3 grid, you can see that the third condition is unsatisfied, so print No
.
Sample Input 3
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6
Sample Output 3
No
The grid A is shown below.
For example, if you look at the leftmost column, you can see that the second condition is unsatisfied, so print No
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
1, 2, \ldots, N の番号のついた N 人の候補者から当選者を 1 人選ぶ選挙において、M 票の投票がありました。
各票ではそれぞれちょうど 1 人が投票先として選ばれており、i 票目の投票先は候補者 A_i です。
これから 1 票目から順に開票を行い、 1 票ごとにその時点で開票が終了した場合の当選者を更新して表示します。
開票された票において最も得票数が多かった候補者が当選となります。ただし、最も得票数が多かった候補者が複数いる場合は、その中で最も番号の小さい候補者が当選となります。
各 i = 1, 2, \ldots, M について、1, 2, \ldots, i 票目のみを開票した場合の当選者を求めてください。
制約
- 1 \leq N,M \leq 200000
- 1 \leq A_i \leq N
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_M
出力
M 行出力せよ。
i 行目には 1, 2, \ldots, i 票目のみを開票した場合の当選者の番号を出力せよ。
入力例 1
3 7 1 2 2 3 1 3 3
出力例 1
1 1 2 2 1 1 3
候補者 i の得票数を C_i で表すこととします。
- 1 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 0, 0) なので当選者は 1 です。
- 2 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 1, 0) なので当選者は 1 です。
- 3 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 2, 0) なので当選者は 2 です。
- 4 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 2, 1) なので当選者は 2 です。
- 5 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 1) なので当選者は 1 です。
- 6 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 2) なので当選者は 1 です。
- 7 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 3) なので当選者は 3 です。
入力例 2
100 5 100 90 80 70 60
出力例 2
100 90 80 70 60
入力例 3
9 8 8 8 2 2 8 8 2 2
出力例 3
8 8 8 2 8 8 8 2
Score : 350 points
Problem Statement
There is an election to choose one winner from N candidates with candidate numbers 1, 2, \ldots, N, and there have been M votes cast.
Each vote is for exactly one candidate, with the i-th vote being for candidate A_i.
The votes will be counted in order from first to last, and after each vote is counted, the current winner will be updated and displayed.
The candidate with the most votes among those counted is the winner. If there are multiple candidates with the most votes, the one with the smallest candidate number is the winner.
For each i = 1, 2, \ldots, M, determine the winner when counting only the first i votes.
Constraints
- 1 \leq N, M \leq 200000
- 1 \leq A_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_M
Output
Print M lines.
The i-th line should contain the winner's candidate number when counting only the first i votes.
Sample Input 1
3 7 1 2 2 3 1 3 3
Sample Output 1
1 1 2 2 1 1 3
Let C_i denote the number of votes for candidate i.
- After the first vote is counted, (C_1, C_2, C_3) = (1, 0, 0), so the winner is 1.
- After the second vote is counted, (C_1, C_2, C_3) = (1, 1, 0), so the winner is 1.
- After the third vote is counted, (C_1, C_2, C_3) = (1, 2, 0), so the winner is 2.
- After the fourth vote is counted, (C_1, C_2, C_3) = (1, 2, 1), so the winner is 2.
- After the fifth vote is counted, (C_1, C_2, C_3) = (2, 2, 1), so the winner is 1.
- After the sixth vote is counted, (C_1, C_2, C_3) = (2, 2, 2), so the winner is 1.
- After the seventh vote is counted, (C_1, C_2, C_3) = (2, 2, 3), so the winner is 3.
Sample Input 2
100 5 100 90 80 70 60
Sample Output 2
100 90 80 70 60
Sample Input 3
9 8 8 8 2 2 8 8 2 2
Sample Output 3
8 8 8 2 8 8 8 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。
以下の Q 個のクエリに、与えられる順番で対応してください。
k 番目のクエリは以下の形式で与えられます。
i_k x_k
- まず、 A_{i_k} = x_k と変更する。この変更は以降のクエリにも引き継がれる。
- その後、 A の \rm{mex} を出力する。
- A の \rm{mex} とは、 A に含まれない最小の非負整数を指す。
制約
- 入力は全て整数
- 1 \le N,Q \le 2 \times 10^5
- 0 \le A_i \le 10^9
- 1 \le i_k \le N
- 0 \le x_k \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \dots A_N i_1 x_1 i_2 x_2 \vdots i_Q x_Q
出力
全体で Q 行出力せよ。
そのうち k 行目には k 番目のクエリに対する答えを整数として出力せよ。
入力例 1
8 5 2 0 2 2 1 1 2 5 4 3 4 4 6 3 8 1000000000 2 1
出力例 1
4 3 6 5 0
最初、数列 A は (2,0,2,2,1,1,2,5) です。
この入力では、 5 つのクエリを処理します。
- 1 番目のクエリで A_4 = 3 と変更し、 A=(2,0,2,3,1,1,2,5) となりました。
- この時点で、 A の \rm{mex} は 4 です。
- 2 番目のクエリで A_4 = 4 と変更し、 A=(2,0,2,4,1,1,2,5) となりました。
- この時点で、 A の \rm{mex} は 3 です。
- 3 番目のクエリで A_6 = 3 と変更し、 A=(2,0,2,4,1,3,2,5) となりました。
- この時点で、 A の \rm{mex} は 6 です。
- 4 番目のクエリで A_8 = 1000000000 と変更し、 A=(2,0,2,4,1,3,2,1000000000) となりました。
- この時点で、 A の \rm{mex} は 5 です。
- 5 番目のクエリで A_2 = 1 と変更し、 A=(2,1,2,4,1,3,2,1000000000) となりました。
- この時点で、 A の \rm{mex} は 0 です。
Score : 475 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.
Respond to the following Q queries in the order they are given.
The k-th query is given in the following format:
i_k x_k
- First, change A_{i_k} to x_k. This change will carry over to subsequent queries.
- Then, print the \rm{mex} of A.
- The \rm{mex} of A is the smallest non-negative integer not contained in A.
Constraints
- All input values are integers.
- 1 \le N,Q \le 2 \times 10^5
- 0 \le A_i \le 10^9
- 1 \le i_k \le N
- 0 \le x_k \le 10^9
Input
Input is given from Standard Input in the following format:
N Q A_1 A_2 \dots A_N i_1 x_1 i_2 x_2 \vdots i_Q x_Q
Output
Print Q lines in total.
The k-th line should contain the answer to the k-th query as an integer.
Sample Input 1
8 5 2 0 2 2 1 1 2 5 4 3 4 4 6 3 8 1000000000 2 1
Sample Output 1
4 3 6 5 0
Initially, the sequence A is (2,0,2,2,1,1,2,5).
This input gives you five queries.
- The first query changes A_4 to 3, making A=(2,0,2,3,1,1,2,5).
- At this point, the \rm{mex} of A is 4.
- The second query changes A_4 to 4, making A=(2,0,2,4,1,1,2,5).
- At this point, the \rm{mex} of A is 3.
- The third query changes A_6 to 3, making A=(2,0,2,4,1,3,2,5).
- At this point, the \rm{mex} of A is 6.
- The fourth query changes A_8 to 1000000000, making A=(2,0,2,4,1,3,2,1000000000).
- At this point, the \rm{mex} of A is 5.
- The fifth query changes A_2 to 1, making A=(2,1,2,4,1,3,2,1000000000).
- At this point, the \rm{mex} of A is 0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) が与えられます。\{1,2,\ldots,N\} の空でない部分集合 S であって、以下の条件を満たすものの個数を数えてください。
- \max_{i \in S} A_i \geq \sum_{i \in S} B_i
なお、答えは非常に大きくなることがあるため、998244353 で割ったあまりを出力してください。
制約
- 1 \leq N \leq 5000
- 1 \leq A_i,B_i \leq 5000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
問題文中の条件を満たす S の個数を 998244353 で割ったあまりを出力せよ。
入力例 1
2 3 1 1 2
出力例 1
2
\{1,2,\ldots,N\} の空でない部分集合としてあり得るものは、\{1\}, \{2\}, \{1,2\} の 3 通りです。
- S=\{1\} のとき \max_{i \in S} A_i=3, \sum_{i \in S} B_i=1
- S=\{2\} のとき \max_{i \in S} A_i=1, \sum_{i \in S} B_i=2
- S=\{1,2\} のとき \max_{i \in S} A_i=3, \sum_{i \in S} B_i=3
であるため、問題文中の条件、即ち \max_{i \in S} A_i \geq \sum_{i \in S} B_i を満たす S は \{1\} と \{1,2\} の 2 通りです。
入力例 2
2 1 1 2 2
出力例 2
0
条件を満たす S が存在しない場合もあります。
入力例 3
20 1937 3980 2689 1208 3640 1979 581 2271 4229 3948 3708 1522 4161 4661 3797 96 3388 3395 2920 2247 4485 2580 174 1156 3770 3396 3558 3500 3494 479 269 3383 1230 1711 3545 3919 134 475 3796 1017
出力例 3
476
Score : 500 points
Problem Statement
Given are sequences of N integers each: A = (A_1, \dots, A_N) and B = (B_1, \dots, B_N). Find the number of non-empty subsets S of \{1,2,\ldots,N\} that satisfy the following condition:
- \max_{i \in S} A_i \geq \sum_{i \in S} B_i.
Since the count can be enormous, print it modulo 998244353.
Constraints
- 1 \leq N \leq 5000
- 1 \leq A_i,B_i \leq 5000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the number of subsets S that satisfy the condition in the Problem Statement, modulo 998244353.
Sample Input 1
2 3 1 1 2
Sample Output 1
2
\{1,2,\ldots,N\} has three subsets: \{1\}, \{2\}, and \{1,2\}.
- For S=\{1\}, we have \max_{i \in S} A_i=3 and \sum_{i \in S} B_i=1.
- For S=\{2\}, we have \max_{i \in S} A_i=1 and \sum_{i \in S} B_i=2.
- For S=\{1,2\}, we have \max_{i \in S} A_i=3 and \sum_{i \in S} B_i=3.
Thus, the condition \max_{i \in S} A_i \geq \sum_{i \in S} B_i is satisfied by two subsets: \{1\} and \{1,2\}.
Sample Input 2
2 1 1 2 2
Sample Output 2
0
There may be no subsets that satisfy the condition.
Sample Input 3
20 1937 3980 2689 1208 3640 1979 581 2271 4229 3948 3708 1522 4161 4661 3797 96 3388 3395 2920 2247 4485 2580 174 1156 3770 3396 3558 3500 3494 479 269 3383 1230 1711 3545 3919 134 475 3796 1017
Sample Output 3
476