結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-24 11:45:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 922 ms / 1,000 ms
コード長 2,406 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 75,828 KB
実行使用メモリ 6,676 KB
スコア 33,135,481
最終ジャッジ日時 2024-02-25 12:58:34
合計ジャッジ時間 10,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 856 ms
6,676 KB
testcase_01 AC 857 ms
6,676 KB
testcase_02 AC 856 ms
6,676 KB
testcase_03 AC 856 ms
6,676 KB
testcase_04 AC 855 ms
6,676 KB
testcase_05 AC 922 ms
6,676 KB
testcase_06 AC 858 ms
6,676 KB
testcase_07 AC 904 ms
6,676 KB
testcase_08 AC 856 ms
6,676 KB
testcase_09 AC 856 ms
6,676 KB
testcase_10 AC 857 ms
6,676 KB
testcase_11 AC 856 ms
6,676 KB
testcase_12 AC 856 ms
6,676 KB
testcase_13 AC 856 ms
6,676 KB
testcase_14 AC 856 ms
6,676 KB
testcase_15 AC 856 ms
6,676 KB
testcase_16 AC 856 ms
6,676 KB
testcase_17 AC 856 ms
6,676 KB
testcase_18 AC 856 ms
6,676 KB
testcase_19 AC 856 ms
6,676 KB
testcase_20 AC 857 ms
6,676 KB
testcase_21 AC 855 ms
6,676 KB
testcase_22 AC 857 ms
6,676 KB
testcase_23 AC 857 ms
6,676 KB
testcase_24 AC 856 ms
6,676 KB
testcase_25 AC 856 ms
6,676 KB
testcase_26 AC 855 ms
6,676 KB
testcase_27 AC 856 ms
6,676 KB
testcase_28 AC 856 ms
6,676 KB
testcase_29 AC 858 ms
6,676 KB
testcase_30 AC 855 ms
6,676 KB
testcase_31 AC 858 ms
6,676 KB
testcase_32 AC 856 ms
6,676 KB
testcase_33 AC 854 ms
6,676 KB
testcase_34 AC 856 ms
6,676 KB
testcase_35 AC 856 ms
6,676 KB
testcase_36 AC 856 ms
6,676 KB
testcase_37 AC 856 ms
6,676 KB
testcase_38 AC 856 ms
6,676 KB
testcase_39 AC 881 ms
6,676 KB
testcase_40 AC 881 ms
6,676 KB
testcase_41 AC 856 ms
6,676 KB
testcase_42 AC 857 ms
6,676 KB
testcase_43 AC 856 ms
6,676 KB
testcase_44 AC 894 ms
6,676 KB
testcase_45 AC 857 ms
6,676 KB
testcase_46 AC 856 ms
6,676 KB
testcase_47 AC 856 ms
6,676 KB
testcase_48 AC 856 ms
6,676 KB
testcase_49 AC 856 ms
6,676 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 U[59], BestU[59];
long long V[59], BestV[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 <= 50; i++) {
		long long avgA = (LA[U[i]] + LA[V[i]]) / 2LL;
		long long avgB = (LB[U[i]] + LB[V[i]]) / 2LL;
		LA[U[i]] = avgA; LA[V[i]] = avgA;
		LB[U[i]] = avgB; LB[V[i]] = avgB;
	}
	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. 焼きなまし法の初期値を決める (50 手で固定)
	for (int i = 1; i <= 50; i++) {
		while (true) {
			U[i] = rand() % N + 1;
			V[i] = rand() % N + 1;
			if (U[i] < V[i]) break;
		}
	}

	// Step 3. 焼きなまし法 (0.85 秒)
	long long CurrentScore = GetScore();
	long long BestScore = CurrentScore;
	int StartTime = clock();
	while (clock() - StartTime <= 85 * CLOCKS_PER_SEC / 100) {
		int idx = rand() % 50 + 1;
		int u, v;
		while (true) { u = rand() % N + 1; v = rand() % N + 1; if (u < v) break; }
		int moto_u = U[idx]; // 不採用のときのために元々の U[idx] も記録
		int moto_v = V[idx]; // 不採用のときのために元々の V[idx] も記録

		// 変更をする
		U[idx] = u;
		V[idx] = v;

		// スコアを計算
		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 <= 50; i++) BestU[i] = U[i];
				for (int i = 1; i <= 50; i++) BestV[i] = V[i];
			}
			CurrentScore = CandScore;
		}
		else {
			U[idx] = moto_u;
			V[idx] = moto_v;
		}
	}

	// Step 4. 出力
	cout << 50 << endl;
	for (int i = 1; i <= 50; i++) cout << BestU[i] << " " << BestV[i] << endl;
	return 0;
}
0