結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-20 13:44:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 3,571 bytes
コンパイル時間 1,140 ms
コンパイル使用メモリ 92,332 KB
実行使用メモリ 6,548 KB
スコア 46,753,581
最終ジャッジ日時 2024-02-25 12:41:46
合計ジャッジ時間 3,352 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 3 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 2 ms
6,548 KB
testcase_20 AC 2 ms
6,548 KB
testcase_21 AC 2 ms
6,548 KB
testcase_22 AC 2 ms
6,548 KB
testcase_23 AC 2 ms
6,548 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
testcase_27 AC 2 ms
6,548 KB
testcase_28 AC 2 ms
6,548 KB
testcase_29 AC 2 ms
6,548 KB
testcase_30 AC 2 ms
6,548 KB
testcase_31 AC 2 ms
6,548 KB
testcase_32 AC 2 ms
6,548 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 2 ms
6,548 KB
testcase_35 AC 2 ms
6,548 KB
testcase_36 AC 2 ms
6,548 KB
testcase_37 AC 2 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
testcase_39 AC 2 ms
6,548 KB
testcase_40 AC 2 ms
6,548 KB
testcase_41 AC 2 ms
6,548 KB
testcase_42 AC 2 ms
6,548 KB
testcase_43 AC 2 ms
6,548 KB
testcase_44 AC 2 ms
6,548 KB
testcase_45 AC 2 ms
6,548 KB
testcase_46 AC 2 ms
6,548 KB
testcase_47 AC 2 ms
6,548 KB
testcase_48 AC 2 ms
6,548 KB
testcase_49 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// [想定解答 5]
//    - Order[1], Order[2], ..., Order[N] を決め,以下のようにカードを選ぶことを考える.
//        * 1 手目: カード Order[N-1] と Order[N] を選んで操作する
//        * 2 手目: カード Order[N-2] と Order[N-1] を選んで操作する
//        * 3 手目: カード Order[N-3] と Order[N-2] を選んで操作する
//        *   :
//        * N-1 手目: カード Order[1] と Order[2] を選んで操作する
//
//    - このとき,最終的なカード 1 の値における Order[i] の寄与は 0.5 の min(N-1, i) 乗.
//        * つまり最終結果は 0.5 * Order[1] + 0.25 * Order[2] + 0.125 * Order[3] + ...
//
//    - そこで Order[i] を i = 1, 2, 3, ..., N の順に貪欲に決めることを考える.
//        * カード 1 の表面を TargetA にしたいとして,寄与分が残り RemainA * 2^{i-1} 足りないとする.
//        * カード 1 の裏面を TargetB にしたいとして,寄与分が残り RemainB * 2^{i-1} 足りないとする.
//        * このとき,できるだけ max(A[x] - RemainA / 2, B[x] - RemainB / 2) が小さいカードを選ぶ.
//
//    - ただし,最初のカード 1 の値が 9.9*10^{17} など非常に大きい場合は貪欲が成立しづらくなる
//    - そこで,最初の 1 手でカード 1 をできるだけ 5*10^{17} に近づけるような手を打つ.
//    - 48% 程度の得点が得られる.

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const long long Target = 500000000000000000LL;

void Operation(vector<long long>& A, vector<long long>& B, int idx1, int idx2) {
	long long avg1 = (A[idx1] + A[idx2]) / 2LL;
	long long avg2 = (B[idx1] + B[idx2]) / 2LL;
	A[idx1] = avg1; B[idx1] = avg2;
	A[idx2] = avg1; B[idx2] = avg2;
}

int main() {
	// Step 1. 入力
	vector<long long> A(45, 0);
	vector<long long> B(45, 0);
	long long N; cin >> N;
	for (int i = 0; i < N; i++) cin >> A[i] >> B[i];

	// Step 2. 最初のステップ
	vector<pair<int, int>> Answer;
	long long MinV = (1LL << 60);
	long long MinID = -1;
	for (int i = 1; i < N; i++) {
		long long v1 = (A[0] + A[i]) / 2LL;
		long long v2 = (B[0] + B[i]) / 2LL;
		long long err = max(abs(v1 - Target), abs(v2 - Target));
		if (MinV > err) {
			MinV = err;
			MinID = i;
		}
	}
	Operation(A, B, 0, MinID);
	Answer.push_back(make_pair(0, MinID));

	// Step 3. 貪欲法
	long long RemainA = Target * 2 - A[0];
	long long RemainB = Target * 2 - B[0];
	vector<bool> Used(N, false);
	vector<int> Order; Order.push_back(0);
	while (true) {
		vector<pair<long long, int>> Vec;
		for (int i = 1; i < N; i++) {
			if (Used[i] == true) continue;
			long long v1 = abs(RemainA - A[i]); if (v1 >= RemainA) continue;
			long long v2 = abs(RemainB - B[i]); if (v2 >= RemainB) continue;
			Vec.push_back(make_pair(max(v1, v2), i));
		}
		sort(Vec.begin(), Vec.end()); if (Vec.size() == 0) break;
		int idx = Vec[0].second;
		RemainA = min(6LL * Target, 2LL * RemainA - A[idx]);
		RemainB = min(6LL * Target, 2LL * RemainB - B[idx]);
		Order.push_back(idx);
		Used[idx] = true;
	}
	for (int i = 1; i < N; i++) {
		if (Used[i] == false) Order.push_back(i);
	}

	// Step 4. 答えを出す
	for (int i = N - 2; i >= 0; i--) {
		Answer.push_back(make_pair(Order[i], Order[i + 1]));
		Operation(A, B, Order[i], Order[i + 1]);
	}

	// Step 5. 出力
	cout << Answer.size() << endl;
	for (int i = 0; i < Answer.size(); i++) cout << Answer[i].first + 1 << " " << Answer[i].second + 1 << endl;
	return 0;
}
0