結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-20 14:02:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 835 ms / 1,000 ms
コード長 5,014 bytes
コンパイル時間 1,491 ms
コンパイル使用メモリ 105,872 KB
実行使用メモリ 6,676 KB
スコア 82,145,377
最終ジャッジ日時 2024-02-25 12:43:21
合計ジャッジ時間 45,790 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// [想定解答 7]
//    - 想定解答 6 のプログラムを 0.4 秒回し,一番良かった順番を Order[1], Order[2], ..., Order[45] とする.
//    - その後,Order[36], Order[37], ..., Order[45] の順番を 10! 通り全探索する.(Order[35] 以前の順番は変わらず)
//        * 上手く処理すれば計算回数は 10! * 10 のオーダーになる.
//    
//    - 10! 通りの中で一番良いものを答えとして出力する.
//    - 82% 程度の得点が得られる.

#include <iostream>
#include <map>
#include <cmath>
#include <tuple>
#include <vector>
#include <algorithm>
using namespace std;

const long long MaxVal = 500000000000000000LL * 2LL;
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;
}

long long BinarySearch(int N, vector<long long> X, vector<int> Order, int border) {
	long long cl = 0;
	long long cr = Target * 2LL;
	long long cm;
	for (int i = 0; i < 65; i++) {
		cm = (cl + cr) / 2LL;
		vector<long long> Y = X; Y[Order[border]] = cm;
		vector<long long> Z = X; Z[Order[border]] = cm;
		for (int j = border - 1; j >= 0; j--) Operation(Y, Z, Order[j], Order[j + 1]);
		if (Y[0] >= Target) cr = cm;
		else cl = cm;
	}
	return cm;
}

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

	// Step 3. 最初のステップ
	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);

	// Step 4. ランダム貪欲
	vector<int> MinOrder;
	int StartTime = clock();
	long long RecordScore = Target;
	while (clock() - StartTime < 40 * CLOCKS_PER_SEC / 100) {
		vector<long long> C = A;
		vector<long long> D = B;
		long long RemainC = Target * 2 - C[0];
		long long RemainD = Target * 2 - D[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(RemainC - C[i]); if (v1 >= RemainC) continue;
				long long v2 = abs(RemainD - D[i]); if (v2 >= RemainD) continue;
				Vec.push_back(make_pair(max(v1, v2), i));
			}
			sort(Vec.begin(), Vec.end()); if (Vec.size() == 0) break;
			int idx = Vec[rand() % max(1, min(3, (int)Vec.size() / 3))].second;
			RemainC = min(6LL * Target, 2LL * RemainC - C[idx]);
			RemainD = min(6LL * Target, 2LL * RemainD - D[idx]);
			Order.push_back(idx);
			Used[idx] = true;
		}
		for (int i = 1; i < N; i++) {
			if (Used[i] == false) Order.push_back(i);
		}

		// スコアを出す
		vector<pair<int, int>> CurrentAns;
		CurrentAns.push_back(make_pair(0, MinID));
		for (int i = N - 2; i >= 0; i--) {
			CurrentAns.push_back(make_pair(Order[i], Order[i + 1]));
			Operation(C, D, Order[i], Order[i + 1]);
		}

		// スコアを比較
		long long CandScore = max(abs(Target - C[0]), abs(Target - D[0]));
		if (RecordScore > CandScore) {
			RecordScore = CandScore;
			MinOrder = Order;
		}
	}

	// Step 5. 二分探索
	long long BaseA = BinarySearch(N, A, MinOrder, N - 10);
	long long BaseB = BinarySearch(N, B, MinOrder, N - 10);

	// Step 6. 指数探索
	long long BestScore = (1LL << 60);
	vector<int> LastOrder;
	vector<int> BestOrder;
	for (int i = N - 10; i < N; i++) LastOrder.push_back(MinOrder[i]);
	sort(LastOrder.begin(), LastOrder.end());
	do {
		vector<long long> C = A;
		vector<long long> D = B;
		for (int i = LastOrder.size() - 2; i >= 0; i--) Operation(C, D, LastOrder[i], LastOrder[i + 1]);
		long long err_c = abs(BaseA - C[LastOrder[0]]);
		long long err_d = abs(BaseB - D[LastOrder[0]]);
		long long score = max(err_c, err_d);
		if (BestScore > score) {
			BestScore = score;
			BestOrder = LastOrder;
		}
	} while (next_permutation(LastOrder.begin(), LastOrder.end()));

	// Step 7. 答えを得る
	vector<pair<int, int>> Answer;
	vector<int> FinalOrder;
	for (int i = 0; i < N - 10; i++) FinalOrder.push_back(MinOrder[i]);
	for (int idx : BestOrder) FinalOrder.push_back(idx);
	Answer.push_back(make_pair(0, MinID));
	for (int i = FinalOrder.size() - 2; i >= 0; i--) {
		Answer.push_back(make_pair(FinalOrder[i], FinalOrder[i + 1]));
	}

	// Step 8. シミュレーション
	vector<long long> SimA = A;
	vector<long long> SimB = B;
	for (int i = 1; i < Answer.size(); i++) Operation(SimA, SimB, Answer[i].first, Answer[i].second);

	// Step 9. 出力
	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