結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-24 13:00:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,779 bytes
コンパイル時間 878 ms
コンパイル使用メモリ 82,744 KB
実行使用メモリ 6,676 KB
スコア 39,837,923
最終ジャッジ日時 2024-02-25 12:58:57
合計ジャッジ時間 11,519 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

const long long Target = 500000000000000000LL;
long long N;
long long A[59], LA[59];
long long B[59], LB[59];
long long Order[59], BestOrder[59];

// 現在のスコアを計算する関数 (操作は 50 手, i 回目の操作は U[i], V[i])
long long GetScore() {
	for (int i = 1; i <= N; i++) LA[i] = A[i];
	for (int i = 1; i <= N; i++) LB[i] = B[i];
	for (int i = 1; i <= N - 1; i++) {
		long long avgC = (LA[Order[1]] + LA[Order[i + 1]]) / 2LL;
		long long avgD = (LB[Order[1]] + LB[Order[i + 1]]) / 2LL;
		LA[Order[1]] = avgC; LA[Order[i + 1]] = avgC;
		LB[Order[1]] = avgD; LB[Order[i + 1]] = avgD;
	}
	return max(abs(Target - LA[1]), abs(Target - LB[1]));
}

// ランダムな 0 以上 1 未満の乱数
long double Randouble() {
	double s = 0.0, t = 1.0;
	for (int i = 0; i < 3; i++) {
		t /= 1024.0;
		s += 1.0 * (rand() % 1024) * t;
	}
	return s;
}

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

	// Step 2. 焼きなまし法の初期値を決める
	for (int i = 1; i <= N; i++) Order[i] = i;
	long long CurrentScore = GetScore();
	long long BestScore = CurrentScore;
	int StartTime = clock();

	// Step 3. 焼きなまし法 (0.10 秒)
	while (clock() - StartTime <= 10 * CLOCKS_PER_SEC / 100) {
		int idx1 = rand() % (N - 1) + 2;
		int idx2 = rand() % (N - 1) + 2;

		// 変更をする
		swap(Order[idx1], Order[idx2]);

		// スコアを計算
		long long CandScore = GetScore();
		double Diff = log10(CurrentScore) - log10(CandScore);

		// 採用するか決定 [確率は exp(log10(スコアの悪化分)/0.2)]
		if (Randouble() < exp(Diff / 0.2)) {
			if (BestScore > CandScore) {
				BestScore = CandScore;
				for (int i = 1; i <= N; i++) BestOrder[i] = Order[i];
			}
			CurrentScore = CandScore;
		}
		else {
			swap(Order[idx1], Order[idx2]);
		}
	}

	// Step 4. Order[1], ..., Order[9] を全探索
	long long FinalScore = BestScore;
	vector<int> Last9;
	for (int i = 1; i <= 9; i++) Last9.push_back(BestOrder[i]);
	do {
		for (int i = 1; i <= N - 1; i++) {
			if (i <= 9) Order[i] = Last9[i];
			else Order[i] = BestOrder[i];
		}

		// スコアを計算
		long long CandScore = GetScore();
		if (BestScore > CandScore) {
			BestScore = CandScore;
			for (int i = 1; i <= N; i++) BestOrder[i] = Order[i];
		}
	} while (next_permutation(Last9.begin(), Last9.end()));

	// Step 5. 答えを得る
	vector<pair<int, int>> Answer;
	for (int i = 1; i <= N - 1; i++) Answer.push_back(make_pair(BestOrder[1], BestOrder[i + 1]));

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