Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数 1, 2, \dots, N が書かれたカードが 4 枚ずつ、合計 4N 枚あります。
高橋君は、これらのカードをシャッフルしたのち 1 枚のカードを選んで抜き取り、残りの 4N - 1 枚を束にしてあなたに渡しました。渡された束の i \, (1 \leq i \leq 4N - 1) 枚目のカードには、整数 A_i が書かれています。
高橋君が抜き取ったカードに書かれていた整数を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
- 各 k \, (1 \leq k \leq N) に対し、A_i = k となる i は 4 個以下である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{4N - 1}
出力
答えを出力せよ。
入力例 1
3 1 3 2 3 3 2 2 1 1 1 2
出力例 1
3
高橋君が抜き取ったカードには 3 が書かれています。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 3 2 1 1 2 4 4 4 4 3 1 3 2 1 3
出力例 3
2
Score : 200 points
Problem Statement
We have 4 cards with an integer 1 written on it, 4 cards with 2, \ldots, 4 cards with N, for a total of 4N cards.
Takahashi shuffled these cards, removed one of them, and gave you a pile of the remaining 4N-1 cards. The i-th card (1 \leq i \leq 4N - 1) of the pile has an integer A_i written on it.
Find the integer written on the card removed by Takahashi.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
- For each k \, (1 \leq k \leq N), there are at most 4 indices i such that A_i = k.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{4N - 1}
Output
Print the answer.
Sample Input 1
3 1 3 2 3 3 2 2 1 1 1 2
Sample Output 1
3
Takahashi removed a card with 3 written on it.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 3 2 1 1 2 4 4 4 4 3 1 3 2 1 3
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 匹のヘビがいます。
はじめ、i 匹目のヘビの太さは T_i、長さは L_i です。
ヘビの重さは太さと長さの積となります。
1 \leq k \leq D を満たす各整数 k について、すべてのヘビの長さが k 伸びたときの最も重いヘビの重さを求めてください。
制約
- 1 \leq N, D \leq 100
- 1 \leq T_i, L_i \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D T_1 L_1 T_2 L_2 \vdots T_N L_N
出力
D 行出力せよ。k 行目には、すべてのヘビの長さが k 伸びたときの最も重いヘビの重さを出力せよ。
入力例 1
4 3 3 3 5 1 2 4 1 10
出力例 1
12 15 20
すべてのヘビの長さが 1 伸びたとき、ヘビの重さはそれぞれ 12, 10, 10, 11 となるので 1 行目には 12 を出力します。
すべてのヘビの長さが 2 伸びたとき、ヘビの重さはそれぞれ 15, 15, 12, 12 となるので 2 行目には 15 を出力します。
すべてのヘビの長さが 3 伸びたとき、ヘビの重さはそれぞれ 18, 20, 14, 13 となるので 3 行目には 20 を出力します。
入力例 2
1 4 100 100
出力例 2
10100 10200 10300 10400
Score : 200 points
Problem Statement
There are N snakes.
Initially, the thickness of the i-th snake is T_i, and its length is L_i.
The weight of a snake is defined as the product of its thickness and length.
For each integer k satisfying 1 \leq k \leq D, find the weight of the heaviest snake when every snake's length has increased by k.
Constraints
- 1 \leq N, D \leq 100
- 1 \leq T_i, L_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D T_1 L_1 T_2 L_2 \vdots T_N L_N
Output
Print D lines. The k-th line should contain the weight of the heaviest snake when every snake's length has increased by k.
Sample Input 1
4 3 3 3 5 1 2 4 1 10
Sample Output 1
12 15 20
When every snake’s length has increased by 1, the snakes' weights become 12, 10, 10, 11, so print 12 on the first line.
When every snake’s length has increased by 2, the snakes' weights become 15, 15, 12, 12, so print 15 on the second line.
When every snake’s length has increased by 3, the snakes' weights become 18, 20, 14, 13, so print 20 on the third line.
Sample Input 2
1 4 100 100
Sample Output 2
10100 10200 10300 10400
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
配点 : 350 点
問題文
高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さは |i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。
残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。
制約
- 1\leq K\leq N \leq 2\times 10^5
- 1\leq A_1 < A_2 < \dots < A_K \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_K
出力
奇妙さの総和の最小値を整数として出力せよ。
入力例 1
4 2 1 3
出力例 1
2
以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。
色 1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4) の 3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。
入力例 2
5 1 2
出力例 2
0
(1,1),(3,3),(4,4),(5,5) の 4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。
入力例 3
8 5 1 2 4 7 8
出力例 3
2
Score : 350 points
Problem Statement
Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.
Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.
Constraints
- 1\leq K\leq N \leq 2\times 10^5
- 1\leq A_1 < A_2 < \dots < A_K \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_K
Output
Print the minimum total weirdness as an integer.
Sample Input 1
4 2 1 3
Sample Output 1
2
Below, let (i,j) denote a pair of a sock of color i and a sock of color j.
There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.
Sample Input 2
5 1 2
Sample Output 2
0
The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).
Sample Input 3
8 5 1 2 4 7 8
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。 ここで、どの i\ (1\le i\le N) についても A _ i が 0 でないことが保証されます。
A を適切に並べ替えた数列 B=(B _ 1,B _ 2,\ldots,B _ N) が等比数列になることがあるか判定してください。
ただし、数列 S=(S _ 1,S _ 2,\ldots,S _ N) が等比数列であるとは、ある実数 r が存在してすべての整数 1\le i\lt N に対して S _ {i+1}=rS _ i が成り立つことをいいます。
1 つの入力ファイルにつき、T 個のテストケースを解いてください。
制約
- 1\le T\le10 ^ 5
- 2\le N\le2\times10 ^ 5
- -10 ^ 9\le A _ i\le10 ^ 9\ (1\le i\le N)
- A _ i\ne0\ (1\le i\le N)
- 1 つの入力ファイルにおける N の総和は 2\times10 ^ 5 以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T
ここで、\mathrm{testcase} _ i は i 番目 (1\le i\le T) のテストケースであり、各テストケースは以下の形式で与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
T 行にわたって出力せよ。
i 行目 (1\le i\le T) には、i 番目のテストケースにおいて A を並べ替えて等比数列にできる場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
3 5 1 8 2 4 16 5 -16 24 54 81 -36 7 90000 8100 -27000 729 -300000 -2430 1000000
出力例 1
Yes No Yes
1 つめのテストケースでは、A を並べ替えた (16,8,4,2,1) は、公比 r=\dfrac12 の等比数列になります。
よって、1 行目には Yes と出力してください。
2 つめのテストケースでは、A をどのように並べ替えても条件を満たしません。
よって、2 行目には No と出力してください。
Score : 425 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N. It is guaranteed that for any i\ (1\le i\le N), A_i is not 0.
Determine whether there exists a permutation B=(B_1,B_2,\ldots,B_N) of A such that B forms a geometric sequence.
A sequence S=(S_1,S_2,\ldots,S_N) is a geometric sequence if there exists a real number r such that S_{i+1}=rS_i for all integers 1\le i\lt N.
Solve T test cases per input file.
Constraints
- 1\le T\le10^5
- 2\le N\le2\times10^5
- -10^9\le A_i\le10^9\ (1\le i\le N)
- A_i\ne0\ (1\le i\le N)
- The sum of N over all test cases in a single input file is at most 2\times10^5.
- All input values are integers.
Input
The input is given from standard input in the following format:
T
\mathrm{testcase}_1
\mathrm{testcase}_2
\vdots
\mathrm{testcase}_T
where \mathrm{testcase}_i is the i-th test case (1\le i\le T), and each test case is given in the following format:
N A_1 A_2 \ldots A_N
Output
Output T lines.
The i-th line (1\le i\le T) should contain Yes if A can be rearranged to form a geometric sequence in the i-th test case, and No otherwise.
Sample Input 1
3 5 1 8 2 4 16 5 -16 24 54 81 -36 7 90000 8100 -27000 729 -300000 -2430 1000000
Sample Output 1
Yes No Yes
In the first test case, the rearrangement (16,8,4,2,1) of A forms a geometric sequence with common ratio r=\frac{1}{2}.
Thus, print Yes on the first line.
In the second test case, no rearrangement of A satisfies the condition.
Thus, print No on the second line.