結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-19 21:04:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,537 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 99,504 KB
実行使用メモリ 6,548 KB
スコア 12,719,802
最終ジャッジ日時 2024-02-25 12:33:18
合計ジャッジ時間 35,655 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <ctime>
#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;
}

double GetScore(vector<long long> P, long long Target, vector<int> Depth) {
	vector<long long> Vec(P.size(), 0); bool flag = false;
	for (int i = 0; i < Vec.size(); i++) {
		int idx = (Depth[i] >= 1 ? Depth[i] + 1 : (flag == true ? 1 : 0));
		Vec[idx] = P[i];
		if (idx == 0) flag = true;
	}
	double Sum = Target * pow(2.0, (int)P.size() - 1);
	for (int i = Vec.size() - 1; i >= 0; i--) {
		Sum -= (double)Vec[i] * pow(2.0, max(0, i - 1));
	}
	return abs(Sum);
}

int main() {
	srand((unsigned)time(NULL));

	// 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 2. 最初のステップ
	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 3. ランダム貪欲
	vector<int> MinOrder;
	int StartTime = clock();
	long long RecordScore = Target;
	while (clock() - StartTime < 5 * CLOCKS_PER_SEC / 10) {
		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 4. 焼きなまし法
	vector<int> Depth(N, 0);
	for (int i = 0; i < MinOrder.size(); i++) {
		Depth[MinOrder[i]] = max(0, (int)MinOrder.size() - 2 - i);
	}
	vector<int> BstDepth = Depth;
	double BstScore = max(GetScore(A, Target, Depth), GetScore(B, Target, Depth));
	double CurScore = max(GetScore(A, Target, Depth), GetScore(B, Target, Depth));
	double IniScore = CurScore;
	int StartTime2 = clock();
	while (clock() - StartTime2 < 4 * CLOCKS_PER_SEC / 10) {
		int idx1 = rand() % Depth.size();
		int idx2 = rand() % Depth.size();
		if (Depth[idx1] >= 10 || Depth[idx2] >= 10 || abs(Depth[idx1] - Depth[idx2]) >= 3) continue;
		swap(Depth[idx1], Depth[idx2]);
		double CandScore = max(GetScore(A, Target, Depth), GetScore(B, Target, Depth));
		if (CurScore >= CandScore || CandScore < 4.0e19) {
			CurScore = CandScore;
			if (BstScore > CandScore) {
				BstScore = CandScore;
				BstDepth = Depth;
			}
		}
		else {
			swap(Depth[idx1], Depth[idx2]);
		}
	}

	// Step 5. 答えを求める
	vector<int> AnsList(N, 0); bool flag = false;
	vector<pair<int, int>> Answer;
	for (int i = 0; i < N; i++) {
		if (flag == false && BstDepth[i] == 0) { flag = true; AnsList[0] = i; }
		else AnsList[BstDepth[i] + 1] = i;
	}
	Answer.push_back(make_pair(0, MinID));
	for (int i = 0; i < N - 1; i++) Answer.push_back(make_pair(AnsList[i], AnsList[i + 1]));
	for (int i = 0; i < N - 1; i++) Operation(A, B, AnsList[i], AnsList[i + 1]);

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