実行時間制限: 2 sec / メモリ制限: 1024 MiB
ストーリー
あなたはある飲料工場で働いています。この工場では、ベースとなる飲料水が大量に用意されており、それにシロップと炭酸を加えることで製品である炭酸飲料を作ります。加えるシロップと炭酸の量を変えることで複数の炭酸飲料を作ることができます。さらに、すでに作られた炭酸飲料にシロップや炭酸を追加して別の炭酸飲料を作ることも可能です。
作りたい炭酸飲料の甘さと炭酸の強さの組合わせが複数与えられたとき、なるべく効率的にそれらを作る手順を見つけてください。
問題文
炭酸飲料の甘さと炭酸の強さは、それぞれ非負整数で表されます。甘さ x、炭酸の強さ y の炭酸飲料を (x, y) と表記します。
あなたは最初、炭酸飲料 (0, 0) だけを持っています。以下、すでに作られた炭酸飲料の集合を S と書きます。最初は S = \{(0, 0)\} です。
あなたは以下の操作を繰り返し行うことで複数の炭酸飲料を作ることができます。
- すでに持っている炭酸飲料 (x, y) \in S を一つ選ぶ
- x' \ge x かつ y' \ge y を満たす整数 x', y' を選ぶ
- (x, y) を二つの容器に分ける。片方はそのまま取っておき、もう一方にシロップと炭酸を加えることで炭酸飲料 (x', y') を作る。S に (x', y') が追加される。
- このとき (x, y) が S から削除されないことに注意してください
- コストが (x' - x) + (y' - y) かかる
入力として N 種類の炭酸飲料 (A_1, B_1), \dots, (A_N, B_N) が指定されます。操作を 5N 回まで行えるとき、指定された炭酸飲料をすべて作るような操作列(つまり、操作をすべて終えたときにどの i \in \{1, 2, \dots, N\} に対しても (A_i, B_i) \in S が成り立つ操作列)のうち、コストの総和がなるべく小さいものを見つけてください。途中で入力に含まれない炭酸飲料が作られても構いません。
得点
コストの総和を C とし、L = \max\{A_1, B_1, A_2, B_2, \dots, A_N, B_N\} とする。このとき得点は \mathrm{round}\left(10^6 \times \frac{NL}{1 + C}\right) で与えられる。
テストケースは全部で 150 個あり、各テストケースの得点の合計が提出の得点となる。 一つ以上のテストケースで不正な出力や制限時間超過をした場合、提出全体の判定が WA や TLE となる。 コンテスト時間中に得た最高得点で最終順位が決定され、コンテスト終了後のシステムテストは行われない。 同じ得点を複数の参加者が得た場合、提出時刻に関わらず同じ順位となる。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 \vdots A_N B_N
入力はすべて整数であり、以下の制約を満たす。
- N = 1000
- 0 \le A_i < 10^9
- 0 \le B_i < 10^9
- A_1, A_2, \dots, A_N は相異なる
- B_1, B_2, \dots, B_N は相異なる
- A_i = 0 を満たす i が存在する
- B_i = 0 を満たす i が存在する
出力
操作の回数を M とする。m 回目の操作で (x_m, y_m) から (x'_m, y'_m) を作るとして、以下のフォーマットで標準出力に出力せよ。
M x_1 y_1 x'_1 y'_1 \vdots x_M y_M x'_M y'_M
出力はすべて整数で、以下の条件を満たさなければならない。
- 0 \le M \le 5N
- 0 \le x_m \le x'_m < 10^9 かつ 0 \le y_m \le y'_m < 10^9
- (x_m, y_m) \ne (0, 0) ならば、ある k < m に対して (x_m, y_m) = (x'_k, y'_k)
- (A_i, B_i) \ne (0, 0) ならば、ある m に対して (A_i, B_i) = (x'_m, y'_m)
なお、M \le 5N 以外の条件をすべて満たす操作列でコスト C のものがあるとき、コストが C 以下の操作列で M \le 5N を含むすべての条件を満たすものが存在することが証明できる。
入力生成方法
N = 1000 とする。
A_1, \dots, A_N を以下のように生成する。
- a_1 = 0 とし、a_2, \dots, a_N を 1 以上 10^9 未満の範囲から重複しないよう一様ランダムに選ぶ
- a_1, \dots, a_N をランダムに並べ替えたものを A_1, \dots, A_N とする
B_1, \dots, B_N も同様の方法で生成する。
ツール(入力ジェネレータ・ビジュアライザ)
- Web版: ローカル版より高性能でアニメーション表示が可能です。
- ローカル版: 使用するにはRust言語のコンパイル環境をご用意下さい。
- Windows用のコンパイル済みバイナリ: Rust言語の環境構築が面倒な方は代わりにこちらをご利用下さい。
コンテスト期間中に、ビジュアライズ結果の共有や、解法・考察に関する言及は禁止されています。ご注意下さい。
入力例 1
4 0 6 2 5 3 2 4 0
出力例 1
6 0 0 2 0 0 0 0 6 2 0 4 0 2 0 2 2 2 2 3 2 2 2 2 5
これは説明のための例であり、入力の制約は満たしていません。
作りたい炭酸飲料は (0, 6), (2, 5), (3, 2), (4, 0) です。問題文と同様に、すでに作られた炭酸飲料の集合を S で表します。
- 最初の操作では (0, 0) から (2, 0) を作る。コストが 2 かかる。
- この時点で S = \{(0, 0), (2, 0)\} である。
- 2番目の操作では (0, 0) から (0, 6) を作る。コストが 6 かかる。
- この時点で S = \{(0, 0), (2, 0), (0, 6)\} である。
- 3番目の操作では (2, 0) から (4, 0) を作る。コストが 2 かかる。
- この時点で S = \{(0, 0), (2, 0), (0, 6), (4, 0)\} である。
- 4番目の操作では (2, 0) から (2, 2) を作る。コストが 2 かかる。
- この時点で S = \{(0, 0), (2, 0), (0, 6), (4, 0), (2, 2)\} である。
- 5番目の操作では (2, 2) から (3, 2) を作る。コストが 1 かかる。
- この時点で S = \{(0, 0), (2, 0), (0, 6), (4, 0), (2, 2), (3, 2)\} である。
- 6番目の操作では (2, 2) から (2, 5) を作る。コストが 3 かかる。
- この時点で S = \{(0, 0), (2, 0), (0, 6), (4, 0), (2, 2), (3, 2), (2, 5)\} である。
最終的に S に (0, 6), (2, 5), (3, 2), (4, 0) がすべて含まれているので、この出力は AC と判定されます。
コストの総和は 16 です。C = 16, L = 6, N = 4 なので、スコアは \mathrm{round}\left(10^6 \times \frac{6 \times 4}{1 + 16}\right) = 1411765 となります。
Story
You work in a beverage factory. In this factory, a large quantity of drinking water is prepared, and carbonated beverages, the final products, are made by adding syrup and carbon dioxide. By varying the amounts of syrup and carbon dioxide added, multiple variants of carbonated beverages can be produced. Furthermore, it is possible to add syrup or carbon dioxide to already made beverages to make other variants.
Given multiple combinations of desired sweetness and carbonation levels of the beverages you want to make, find an efficient way to produce them.
Problem Statement
The sweetness and carbonation level of beverages are represented by non-negative integers. A beverage with sweetness x and carbonation level y is denoted by (x, y).
Initially you only have beverage (0, 0). Below we write S for the set of beverages already made. Initially S = \{(0, 0)\}.
You can repeat the following operation to make multiple beverages.
- Choose an already made beverage (x, y) \in S.
- Choose integers x', y' such that x' \ge x and y' \ge y.
- Divide beverage (x, y) into two containers, and keep one of them. Make a new beverage (x', y') by adding the appropriate amount of syrup and carbon dioxide to the other. Add (x', y') to S.
- Notice that (x, y) will not be removed from S.
- This operation costs (x' - x) + (y' - y).
Given N beverages (A_1, B_1), \dots, (A_N, B_N) in the input, you can perform the above operation up to 5N times. Find a sequence of operations that makes all of the specified beverages (that is, a sequence of operations such that (A_i, B_i) \in S holds for all i \in \{1, 2, \dots, N\} after all operations) with the total cost as small as possible. It is allowed to make beverages not present in the input during the process.
Scoring
Let C be the total cost, and L = \max\{A_1, B_1, A_2, B_2, \dots, A_N, B_N\}. Then the score is given by \mathrm{round}\left(10^6 \times \frac{NL}{1 + C}\right).
There are 150 test cases, and the score of a submission is the total score for each test case. If your submission produces an illegal output or exceeds the time limit for some test cases, the submission itself will be judged as WA or TLE. The highest score obtained during the contest will determine the final ranking, and there will be no system test after the contest. If more than one participant gets the same score, they will be ranked in the same place regardless of the submission time.
Input
Input is given from standard input in the following format:
N A_1 B_1 \vdots A_N B_N
All values are integers, and the input satisfies the following constraints:
- N = 1000
- 0 \le A_i < 10^9
- 0 \le B_i < 10^9
- A_1, A_2, \dots, A_N are pairwise distinct
- B_1, B_2, \dots, B_N are pairwise distinct
- there exists i such that A_i = 0
- there exists i such that B_i = 0
Output
Let M be the number of operations, and suppose that m-th operation makes (x'_m, y'_m) from (x_m, y_m). Then, output to standard output in the following format:
M x_1 y_1 x'_1 y'_1 \vdots x_M y_M x'_M y'_M
All values should be integers, and satisfy the following conditions:
- 0 \le M \le 5N
- 0 \le x_m \le x'_m < 10^9 and 0 \le y_m \le y'_m < 10^9
- if (x_m, y_m) \ne (0, 0), then there exists k < m such that (x_m, y_m) = (x'_k, y'_k)
- if (A_i, B_i) \ne (0, 0), then there exists m such that (A_i, B_i) = (x'_m, y'_m)
It is possible to prove that if there is a sequence of operations with cost C that satisfies all the conditions except M \le 5N, then there is a sequence of operations with cost at most C that satisfies all the conditions including M \le 5N.
Input Generation
Let N = 1000.
A_1, \dots, A_N are generated as follows:
- Let a_1 = 0, and generate pairwise distinct integers a_2, \dots, a_N between 1 (inclusive) and 10^9 (exclusive) uniformly at random.
- Let A_1, \dots, A_N be a random permutation of a_1, \dots, a_N.
B_1, \dots, B_N are generated in the same manner.
Tools (Input generator and visualizer)
- Web version: This is more powerful than the local version providing animations.
- Local version: You need a compilation environment of Rust language.
- Pre-compiled binary for Windows: If you are not familiar with the Rust language environment, please use this instead.
Please be aware that sharing visualization results or discussing solutions/ideas during the contest is prohibited.
Sample Input 1
4 0 6 2 5 3 2 4 0
Sample Output 1
6 0 0 2 0 0 0 0 6 2 0 4 0 2 0 2 2 2 2 3 2 2 2 2 5
This is an example for illustrative purposes, and does not satisfy the constraints on input.
The beverages you want to make are (0, 6), (2, 5), (3, 2), (4, 0). Let S be the set of beverages already made, as in the problem statement.
- The first operation makes (2, 0) from (0, 0), and costs 2.
- After this operation, S = \{(0, 0), (2, 0)\}.
- The second operation makes (0, 6) from (0, 0), and costs 6.
- After this operation, S = \{(0, 0), (2, 0), (0, 6)\}.
- The third operation makes (4, 0) from (2, 0), and costs 2.
- After this operation, S = \{(0, 0), (2, 0), (0, 6), (4, 0)\}.
- The fourth operation makes (2, 2) from (2, 0), and costs 2.
- After this operation, S = \{(0, 0), (2, 0), (0, 6), (4, 0), (2, 2)\}.
- The fifth operation makes (3, 2) from (2, 2), and costs 1.
- After this operation, S = \{(0, 0), (2, 0), (0, 6), (4, 0), (2, 2), (3, 2)\}.
- The sixth operation makes (2, 5) from (2, 2), and costs 3.
- After this operation, S = \{(0, 0), (2, 0), (0, 6), (4, 0), (2, 2), (3, 2), (2, 5)\}.
Finally, S contains all of (0, 6), (2, 5), (3, 2), (4, 0), so this output will be judged as AC.
The total cost is 16. Since C = 16, L = 6, N = 4, the score is \mathrm{round}\left(10^6 \times \frac{6 \times 4}{1 + 16}\right) = 1411765.