実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 以上 3 以下の整数 X が与えられます。
1 以上 3 以下の整数であって X と異なる整数を 1 つ出力してください。
制約
- X は 1 以上 3 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
1 以上 3 以下の整数であって X と異なる整数を 1 つ出力せよ。
条件を満たす整数が複数存在する場合、どれを出力しても正答となる。
入力例 1
1
出力例 1
2
1 以上 3 以下の整数で X(=1) と異なる整数は 2,3 です。
したがって、2 または 3 のいずれかを出力すると正答となります。
入力例 2
2
出力例 2
3
入力例 3
3
出力例 3
2
Score : 100 points
Problem Statement
You are given an integer X between 1 and 3, inclusive.
Output an integer between 1 and 3, inclusive, that is different from X.
Constraints
- X is an integer between 1 and 3, inclusive.
Input
The input is given from Standard Input in the following format:
X
Output
Output an integer between 1 and 3, inclusive, that is different from X.
If there are multiple integers satisfying the condition, any of them will be accepted.
Sample Input 1
1
Sample Output 1
2
The integers between 1 and 3, inclusive, that are different from X(=1) are 2 and 3.
Thus, outputing either 2 or 3 will be accepted.
Sample Input 2
2
Sample Output 2
3
Sample Input 3
3
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
ある映画館には 1 から N までの番号が付いた N 個の座席があり、各座席には客が一人ずつ座っています。
この映画館では混雑対策として以下のルールに従って客が退場することになっています。
- 座っている座席の番号が小さい順に、客を 10 人ずつのグループに分ける。
- 座席番号が小さい客からなるグループから順に退場する。ただし、同じグループ内の客の退場順は自由である。
最後のグループの客の人数は 10 人未満にもなり得ることに注意してください。
i 番目に退場したのは座席 P_i に座っていた客でした。
N 人の客がルールに従って退場したかどうかを判定してください。
制約
- 10 \leq N \leq 100
- (P_1,P_2,\dots,P_N) は (1,2,\dots,N) の順列
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N
出力
N 人の客がルールに従って退場したならば Yes を、そうでないならば No を 1 行で出力せよ。
入力例 1
25 1 6 5 7 8 10 2 4 3 9 15 17 12 11 19 20 18 13 14 16 21 23 24 22 25
出力例 1
Yes
25 人の客は座席番号が 1 \sim 10,\; 11 \sim 20,\;21 \sim 25 の 3 つのグループに分けられます。
まず、1 つ目のグループに属する座席番号 1 \sim 10 の客 10 人が退場します。
次に、2 つ目のグループに属する座席番号 11 \sim 20 の客 10 人が退場します。
最後に、3 つ目のグループに属する座席番号 21 \sim 25 の客 5 人が退場します。
したがって、25 人の客はルールに従って退場しています。
入力例 2
11 11 10 7 2 1 5 6 4 8 9 3
出力例 2
No
Score : 200 points
Problem Statement
There is a movie theater with N seats numbered 1 through N, and one customer is sitting in each seat.
As a measure against congestion, this theater has customers leave according to the following rule.
- Divide the customers into groups of 10 people each, in increasing order of the seat number they are sitting in.
- The groups leave in order, starting from the group consisting of customers with the smallest seat numbers. Customers within the same group may leave in any order.
Here, the last group may have fewer than 10 customers.
The customer who left i-th was the one sitting in seat P_i.
Determine whether the N customers left according to the rule.
Constraints
- 10 \leq N \leq 100
- (P_1,P_2,\dots,P_N) is a permutation of (1,2,\dots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N
Output
Output Yes if the N customers left according to the rule, and No otherwise, in one line.
Sample Input 1
25 1 6 5 7 8 10 2 4 3 9 15 17 12 11 19 20 18 13 14 16 21 23 24 22 25
Sample Output 1
Yes
The 25 customers were divided into three groups, with seat numbers 1 to 10, 11 to 20, and 21 to 25.
First, the 10 customers in the first group, with seat numbers 1 to 10, left.
Next, the 10 customers in the second group, with seat numbers 11 to 20, left.
Finally, the 5 customers in the third group, with seat numbers 21 to 25, left.
Thus, the 25 customers left according to the rule.
Sample Input 2
11 11 10 7 2 1 5 6 4 8 9 3
Sample Output 2
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。
q=1,2,\dots,Q の順に、以下の操作を行います。
- P から値が a_q である要素を削除し、それを P の末尾に追加する。
Q 個の操作を行った後の P の各要素の値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- (P_1,P_2,\dots,P_N) は (1,2,\dots,N) の順列
- 1 \leq a_q \leq N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q P_1 P_2 \ldots P_N a_1 a_2 \vdots a_Q
出力
Q 個の操作を行った後の P_1,P_2,\dots,P_N をこの順に空白区切りで 1 行で出力せよ。
入力例 1
4 2 2 4 3 1 3 2
出力例 1
4 1 3 2
最初、P=(2,4,3,1) です。
1 回目の操作を行うと、P=(2,4,1,3) となります。
2 回目の操作を行うと、P=(4,1,3,2) となります。
入力例 2
3 3 1 2 3 1 1 1
出力例 2
2 3 1
入力例 3
2 5 2 1 1 1 2 2 1
出力例 3
2 1
Score : 300 points
Problem Statement
You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).
For q=1,2,\dots,Q in this order, perform the following operation.
- Remove the element with value a_q from P, and append it to the end of P.
Find the value of each element of P after performing the Q operations.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- (P_1,P_2,\dots,P_N) is a permutation of (1,2,\dots,N).
- 1 \leq a_q \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q P_1 P_2 \ldots P_N a_1 a_2 \vdots a_Q
Output
Output P_1,P_2,\dots,P_N after performing the Q operations, in this order, separated by spaces, in one line.
Sample Input 1
4 2 2 4 3 1 3 2
Sample Output 1
4 1 3 2
Initially, P=(2,4,3,1).
After the first operation, P=(2,4,1,3).
After the second operation, P=(4,1,3,2).
Sample Input 2
3 3 1 2 3 1 1 1
Sample Output 2
2 3 1
Sample Input 3
2 5 2 1 1 1 2 2 1
Sample Output 3
2 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 種類の石 1,2,\dots,N があり、同じ種類の石の重みはすべて同じです。
高橋君と青木君は石 i をそれぞれ A_i,B_i 個ずつ持っています。
以下の条件を満たすような正整数列 W=(W_1,W_2,\dots,W_N) が存在するかどうかを判定し、存在するならば一つ構成してください。
- 1 \leq W_i \leq 10^{18}
- 石 i の重みが W_i のとき、高橋君が持っている石の重みの総和が青木君が持っている石の重みの総和よりも真に大きい。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
条件を満たすような W=(W_1,W_2,\dots,W_N) が存在するならば以下の形式で出力せよ。
Yes W_1 W_2 \ldots W_N
条件を満たすような W=(W_1,W_2,\dots,W_N) が存在しないならば No を 1 行で出力せよ。
入力例 1
3 4 7 4 5 5 5
出力例 1
Yes 4 7 4
W=(4,7,4) のとき、
高橋君が持っている石の重みの総和は 4 \times 4 + 7 \times 7 + 4 \times 4=81 です。
青木君が持っている石の重みの総和は 4 \times 5 + 7 \times 5 + 4 \times 5=75 です。
入力例 2
3 3 3 3 4 7 4
出力例 2
No
入力例 3
1 2 2
出力例 3
No
Score : 400 points
Problem Statement
There are N types of stones, 1,2,\dots,N. Stones of the same type all have the same weight.
Takahashi and Aoki have A_i and B_i stones of type i, respectively.
Determine whether there exists a sequence of positive integers W=(W_1,W_2,\dots,W_N) satisfying the following conditions, and if it exists, construct one such sequence.
- 1 \leq W_i \leq 10^{18}
- If the weight of a type-i stone is W_i, the total weight of the stones Takahashi has is strictly greater than the total weight of the stones Aoki has.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 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 B_1 B_2 \ldots B_N
Output
If there exists W=(W_1,W_2,\dots,W_N) satisfying the conditions, output it in the following format:
Yes W_1 W_2 \ldots W_N
If there is no W=(W_1,W_2,\dots,W_N) satisfying the conditions, output No in one line.
Sample Input 1
3 4 7 4 5 5 5
Sample Output 1
Yes 4 7 4
If W=(4,7,4),
the total weight of the stones Takahashi has is 4 \times 4 + 7 \times 7 + 4 \times 4=81.
The total weight of the stones Aoki has is 4 \times 5 + 7 \times 5 + 4 \times 5=75.
Sample Input 2
3 3 3 3 4 7 4
Sample Output 2
No
Sample Input 3
1 2 2
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
ある店では N 種類の商品が売られています。各商品は何回でも購入することができます。
i 種類目 (1\le i\le N) の商品は以下の 2 通りの方法で買うことができます:
- クーポンを使わずに A_i 円で購入し、クーポンを 1 枚もらう。
- クーポンを 1 枚使い、B_i 円で購入する。
最初、あなたはクーポンを 1 枚も持っていません。
全ての商品を 1 回以上購入するために必要な金額の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 2\times 10^5
- 1\le N\le 2\times 10^5
- 1\le B_i < A_i \le 10^9
- 全てのテストケースにおける N の総和は 2\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
3 5 11 6 6 5 2 1 8 3 7 4 4 5 1 5 2 5 3 5 4 6 24 13 24 2 50 12 35 25 28 26 10 1
出力例 1
23 13 100
1 番目のテストケースについて考えます。
例えば以下のように行動することで 23 円で全ての商品を 1 回以上購入することができます:
- 2 種類目の商品をクーポンを使わず 6 円で購入する。持っているクーポンは 1 枚となる。
- 3 種類目の商品をクーポンを使わず 2 円で購入する。持っているクーポンは 2 枚となる。
- 3 種類目の商品をクーポンを使わず 2 円で購入する。持っているクーポンは 3 枚となる。
- 1 種類目の商品をクーポンを使って 6 円で購入する。持っているクーポンは 2 枚となる。
- 4 種類目の商品をクーポンを使って 3 円で購入する。持っているクーポンは 1 枚となる。
- 5 種類目の商品をクーポンを使って 4 円で購入する。持っているクーポンは 0 枚となる。
23 円未満で全ての商品を 1 回以上購入することはできないので、1 行目には 23 を出力してください。
Score : 450 points
Problem Statement
A certain shop sells N types of products. Each product can be purchased any number of times.
The i-th type of product (1\le i\le N) can be bought in the following two ways:
- Buy it for A_i yen without using a coupon, and receive one coupon.
- Use one coupon and buy it for B_i yen.
Initially, you have no coupons.
Find the minimum amount of money required to buy every product at least once.
You are given T test cases; solve each of them.
Constraints
- 1\le T\le 2\times 10^5
- 1\le N\le 2\times 10^5
- 1\le B_i < A_i \le 10^9
- The sum of N over all test cases is at most 2\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
3 5 11 6 6 5 2 1 8 3 7 4 4 5 1 5 2 5 3 5 4 6 24 13 24 2 50 12 35 25 28 26 10 1
Sample Output 1
23 13 100
Consider the first test case.
For example, by acting as follows, you can buy every product at least once for 23 yen:
- Buy the second type of product for 6 yen without using a coupon. You now have 1 coupon.
- Buy the third type of product for 2 yen without using a coupon. You now have 2 coupons.
- Buy the third type of product for 2 yen without using a coupon. You now have 3 coupons.
- Use a coupon to buy the first type of product for 6 yen. You now have 2 coupons.
- Use a coupon to buy the fourth type of product for 3 yen. You now have 1 coupon.
- Use a coupon to buy the fifth type of product for 4 yen. You now have 0 coupons.
It is impossible to buy every product at least once for less than 23 yen, so output 23 on the first line.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
A に対して、以下の操作を好きな回数行うことができます。
- 1 以上 N 以下の整数 i を一つ選び、i の正の約数であるようなすべての整数 j について A_j に 1 を足す。
A の要素をすべて等しくすることが可能かを判定し、可能ならばそのために必要な操作回数の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
A の要素をすべて等しくすることが可能ならばそのために必要な操作回数の最小値を、そうでないならば -1 を 1 行で出力せよ。
入力例 1
3 4 7 4
出力例 1
3
1 回目の操作で i=3 を選べば、A=(5,7,5) となります。
2 回目の操作で i=3 を選べば、A=(6,7,6) となります。
3 回目の操作で i=3 を選べば、A=(7,7,7) となります。
3 回未満の操作で A の要素をすべて等しくすることはできないため、答えは 3 です。
入力例 2
5 1 3 4 5 6
出力例 2
5
入力例 3
2 5 2
出力例 3
-1
Score : 500 points
Problem Statement
You are given a length-N integer sequence A=(A_1,A_2,\dots,A_N).
You can perform the following operation on A any number of times.
- Choose an integer i with 1 \leq i \leq N. For every integer j that is a positive divisor of i, add 1 to A_j.
Determine whether it is possible to make all elements of A equal, and if it is possible, find the minimum number of operations required to do so.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 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
If it is possible to make all elements of A equal, output, in one line, the minimum number of operations required to do so; otherwise, output -1.
Sample Input 1
3 4 7 4
Sample Output 1
3
If you choose i=3 for the first operation, you get A=(5,7,5).
If you choose i=3 for the second operation, you get A=(6,7,6).
If you choose i=3 for the third operation, you get A=(7,7,7).
It is impossible to make all elements of A equal with fewer than three operations, so the answer is 3.
Sample Input 2
5 1 3 4 5 6
Sample Output 2
5
Sample Input 3
2 5 2
Sample Output 3
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 575 点
問題文
正整数 N,K が与えられます。
N\times N のマス目があり、上から r 行目、左から c 列目のマスをマス (r,c) と表します。
はじめ、コマがマス (1,1) に置かれています。
あなたは以下の操作をちょうど N^2-1 回行い、最終的にコマをマス (N,N) に移動させます:
- 現在いるマスから、上下左右に隣接するマスへコマを 1 マス移動させる。
ただし、移動中に N^2 個のすべてのマスをそれぞれちょうど 1 回ずつ訪れなければなりません。 ここで、最初にコマが置かれているマス (1,1) も訪れたマスに含めます。
右へ 1 マス移動する操作の回数がちょうど K 回となるような操作列が存在するか判定し、存在する場合はそのような操作列を 1 つ求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 5\times 10^3
- 2\le N\le 10^3
- 0\le K\le N^2-1
- 全てのテストケースにおける N^2 の総和は 10^6 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、条件を満たす操作列が存在しない場合は No を出力せよ。
条件を満たす操作列が存在する場合、以下の形式で操作列を出力せよ。
Yes
S_1S_2\dots S_{N^2-1}
ただし、S_k は k 回目の移動を表し、以下のいずれかとする。
- 左に 1 マス移動する場合は S_k=
L - 右に 1 マス移動する場合は S_k=
R - 上に 1 マス移動する場合は S_k=
U - 下に 1 マス移動する場合は S_k=
D
条件を満たす操作列が複数存在する場合、どれを出力しても正答となる。
入力例 1
3 3 4 2 1 5 10
出力例 1
Yes RRDLLDRR No Yes RRRRDDDLLLURRULLLDDDRRRR
1 番目のテストケースについて考えます。
マス (1,1) から順にマス (1,2),(1,3),(2,3),(2,2),(2,1),(3,1),(3,2),(3,3) と移動させることで右に 1 マス進む操作を 4 回行いマス (3,3) に到達することができます。
Score : 575 points
Problem Statement
You are given positive integers N and K.
There is an N\times N grid. The cell at the r-th row from the top and the c-th column from the left is denoted as cell (r,c).
Initially, a piece is placed on cell (1,1).
You will perform the following operation exactly N^2-1 times so that the piece ends up in cell (N,N):
- Move the piece one cell to a cell vertically or horizontally adjacent to the cell it is currently on.
Here, over the course of the movement, each of the N^2 cells must be visited exactly once. Cell (1,1), where the piece is initially placed, is considered visited.
Determine whether there exists a sequence of operations where the piece moves one cell to the right exactly K times, and if it exists, find one such sequence.
You are given T test cases; solve each of them.
Constraints
- 1\le T\le 5\times 10^3
- 2\le N\le 10^3
- 0\le K\le N^2-1
- The sum of N^2 over all test cases is at most 10^6.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, if there is no sequence of operations satisfying the condition, output No.
If there exists a sequence of operations satisfying the condition, output it in the following format:
Yes
S_1S_2\dots S_{N^2-1}
Here, S_k represents the k-th move, and is one of the following:
- S_k=
Lif the piece moves one cell to the left - S_k=
Rif the piece moves one cell to the right - S_k=
Uif the piece moves one cell up - S_k=
Dif the piece moves one cell down
If there are multiple sequences of operations satisfying the condition, any of them will be accepted.
Sample Input 1
3 3 4 2 1 5 10
Sample Output 1
Yes RRDLLDRR No Yes RRRRDDDLLLURRULLLDDDRRRR
Consider the first test case.
By moving from cell (1,1) in order to cells (1,2),(1,3),(2,3),(2,2),(2,1),(3,1),(3,2),(3,3), you can move one cell to the right four times and reach cell (3,3).