結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-22 16:10:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 886 ms / 1,000 ms
コード長 9,281 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 114,440 KB
実行使用メモリ 188,648 KB
スコア 93,699,458
最終ジャッジ日時 2024-02-25 12:56:03
合計ジャッジ時間 45,714 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 540 ms
187,044 KB
testcase_01 AC 884 ms
187,712 KB
testcase_02 AC 884 ms
187,864 KB
testcase_03 AC 885 ms
188,384 KB
testcase_04 AC 883 ms
187,472 KB
testcase_05 AC 882 ms
187,472 KB
testcase_06 AC 883 ms
187,888 KB
testcase_07 AC 882 ms
187,844 KB
testcase_08 AC 796 ms
185,880 KB
testcase_09 AC 457 ms
186,968 KB
testcase_10 AC 878 ms
186,224 KB
testcase_11 AC 880 ms
186,404 KB
testcase_12 AC 782 ms
187,056 KB
testcase_13 AC 806 ms
187,092 KB
testcase_14 AC 884 ms
188,220 KB
testcase_15 AC 882 ms
187,724 KB
testcase_16 AC 880 ms
186,704 KB
testcase_17 AC 880 ms
186,904 KB
testcase_18 AC 844 ms
187,404 KB
testcase_19 AC 886 ms
188,648 KB
testcase_20 AC 882 ms
187,556 KB
testcase_21 AC 881 ms
186,912 KB
testcase_22 AC 597 ms
187,460 KB
testcase_23 AC 860 ms
187,536 KB
testcase_24 AC 882 ms
187,416 KB
testcase_25 AC 880 ms
186,932 KB
testcase_26 AC 883 ms
187,400 KB
testcase_27 AC 881 ms
186,900 KB
testcase_28 AC 558 ms
188,044 KB
testcase_29 AC 883 ms
187,504 KB
testcase_30 AC 682 ms
187,088 KB
testcase_31 AC 882 ms
187,684 KB
testcase_32 AC 880 ms
186,816 KB
testcase_33 AC 609 ms
186,900 KB
testcase_34 AC 535 ms
187,680 KB
testcase_35 AC 883 ms
187,872 KB
testcase_36 AC 882 ms
187,388 KB
testcase_37 AC 657 ms
188,188 KB
testcase_38 AC 883 ms
187,808 KB
testcase_39 AC 882 ms
187,272 KB
testcase_40 AC 882 ms
187,404 KB
testcase_41 AC 883 ms
187,388 KB
testcase_42 AC 881 ms
186,968 KB
testcase_43 AC 642 ms
187,548 KB
testcase_44 AC 885 ms
187,584 KB
testcase_45 AC 882 ms
187,472 KB
testcase_46 AC 878 ms
185,840 KB
testcase_47 AC 884 ms
187,772 KB
testcase_48 AC 884 ms
187,896 KB
testcase_49 AC 882 ms
187,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const long long MaxVal = 1000000000000000000LL;
const long long MinVal = 100000000000000000LL;
const long long Target = 500000000000000000LL;
long long N;
long long A[59], LA[59];
long long B[59], LB[59];
long long FinalScore = MaxVal;
vector<pair<int, int>> FinalAns;

// 終盤探索用
vector<int> backtrack;
vector<pair<vector<int>, int>> Possibility;
map<long long, bool> Possibility_Record;
int IndexCount = 0;
int Masks[1 << 20];
long long MaximumA[1 << 18], MaximumB[1 << 18];
long long MinimumA[1 << 18], MinimumB[1 << 18];
vector<tuple<int, int, int>> List[3060][2000];

// ========================================================================================================== 初期解探索
void init_dfs(int depth, vector<long long> ArrayA) {
	for (int i = 0; i < 4; i++) {
		if (Possibility_Record[ArrayA[i]] == false) {
			Possibility_Record[ArrayA[i]] = true;
			backtrack.push_back(i + 1);
			int hash_val = 0;
			for (int j = 0; j < backtrack.size(); j++) hash_val += (backtrack[j] << (4 * j));
			Possibility.push_back(make_pair(backtrack, hash_val));
			backtrack.pop_back();
		}
	}
	if (depth == 6) return;
	for (int i = 0; i < 4; i++) {
		for (int j = i + 1; j < 4; j++) {
			if (ArrayA[i] == ArrayA[j]) continue;
			vector<long long> ArrayC = ArrayA;
			long long avg1 = (ArrayC[i] + ArrayC[j]) / 2LL;
			ArrayC[i] = avg1;
			ArrayC[j] = avg1;
			backtrack.push_back(i + (j << 2));
			init_dfs(depth + 1, ArrayC);
			backtrack.pop_back();
		}
	}
}

void Initialize(int mask, vector<int> idx) {
	Masks[mask] = IndexCount;
	vector<long long> ArrayA(4, 0);
	vector<long long> ArrayB(4, 0);
	for (int i = 0; i < Possibility.size(); i++) {
		ArrayA[0] = A[idx[0]]; ArrayB[0] = B[idx[0]];
		ArrayA[1] = A[idx[1]]; ArrayB[1] = B[idx[1]];
		ArrayA[2] = A[idx[2]]; ArrayB[2] = B[idx[2]];
		ArrayA[3] = A[idx[3]]; ArrayB[3] = B[idx[3]];
		for (int j = 0; j < Possibility[i].first.size(); j++) {
			if (j + 1 == Possibility[i].first.size()) {
				int num = Possibility[i].first[j] - 1;
				List[IndexCount][(ArrayA[num] >> 41) / 256LL].push_back(make_tuple(ArrayB[num] >> 41, ArrayA[num] >> 41, Possibility[i].second));
			}
			else {
				int idxA = (Possibility[i].first[j] & 3);
				int idxB = (Possibility[i].first[j] >> 2);
				long long avgA = (ArrayA[idxA] + ArrayA[idxB]) >> 1;
				long long avgB = (ArrayB[idxA] + ArrayB[idxB]) >> 1;
				ArrayA[idxA] = avgA; ArrayB[idxA] = avgB;
				ArrayA[idxB] = avgA; ArrayB[idxB] = avgB;
			}
		}

	}
}

// ========================================================================================================== 基本関数
long long GetScore(vector<int>& Order) {
	long long TotalScore1 = (A[1] >> (N - 1));
	long long TotalScore2 = (B[1] >> (N - 1));
	for (int i = 0; i < N - 1; i++) TotalScore1 += (A[Order[i]] >> (N - 1 - i));
	for (int i = 0; i < N - 1; i++) TotalScore2 += (B[Order[i]] >> (N - 1 - i));
	return max(abs(Target - TotalScore1), abs(Target - TotalScore2));
}

long long GetActual(vector<pair<int, int>>& Operation) {
	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 = 0; i < Operation.size(); i++) {
		int idx1 = Operation[i].first;
		int idx2 = Operation[i].second;
		long long avgA = (LA[idx1] + LA[idx2]) / 2LL;
		long long avgB = (LB[idx1] + LB[idx2]) / 2LL;
		LA[idx1] = avgA; LA[idx2] = avgA;
		LB[idx1] = avgB; LB[idx2] = avgB;
	}
	long long gosa = max(abs(Target - LA[1]), abs(Target - LB[1]));
	return gosa;
}

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;
}

// ========================================================================================================== 序盤・中盤探索
vector<int> GetBestOrder() {
	vector<int> Order(N - 1, 0);
	vector<int> BestOrder(N - 1, 0);
	for (int i = 0; i < N - 1; i++) Order[i] = i + 2;

	// 準備
	long long CurrentScore = GetScore(Order);
	long long BestScore = CurrentScore;
	int StartTime = clock();

	// 焼きなまし法開始
	while (clock() - StartTime <= 10 * CLOCKS_PER_SEC / 100) {
		int idx1 = rand() % (N - 1);
		int idx2 = rand() % (N - 1);
		swap(Order[idx1], Order[idx2]);
		long long CandScore = GetScore(Order);
		double Diff = log10(CurrentScore) - log10(CandScore);
		if (Randouble() < exp(Diff / 0.17)) {
			if (BestScore > CandScore) {
				BestScore = CandScore;
				BestOrder = Order;
			}
			CurrentScore = CandScore;
		}
		else {
			swap(Order[idx1], Order[idx2]);
		}
	}
	return BestOrder;
}

// ========================================================================================================== 終盤探索
vector<int> CurrentOrder;
int StartTime;
int NumLoops;

void dfs(int depth, int mask, long long CurrentA, long long CurrentB, vector<int>& Cand, vector<int>& PostOrder) {
	if ((NumLoops & 1023) == 0) {
		if (clock() - StartTime > 80 * CLOCKS_PER_SEC / 100) return;
	}
	NumLoops += 1;
	if (depth == 4) {
		int idx = Masks[(1 << Cand.size()) - 1 - mask];
		int Remain = max(0LL, min(1999LL, (Target - CurrentA) / 256LL));
		for (tuple<int, int, int> tup : List[idx][Remain]) {
			long long gosaA = abs(Target - (CurrentA + 1LL * get<1>(tup)));
			long long gosaB = abs(Target - (CurrentB + 1LL * get<0>(tup)));
			if (max(gosaA, gosaB) > FinalScore + 100LL) continue;

			// 手順の計算
			vector<int> MaskOrd;
			vector<pair<int, int>> Final;
			for (int i = 0; i < Cand.size(); i++) {
				if (!((mask >> i) & 1)) MaskOrd.push_back(Cand[i]);
			}
			for (int i = 0; i < 8; i++) {
				int val1 = (get<2>(tup) >> (4 * i)) & 15;
				int val2 = (get<2>(tup) >> (4 * i + 4)) & 15;
				if (val2 == 0) {
					Final.push_back(make_pair(1, MaskOrd[val1 - 1]));
					for (int j = CurrentOrder.size() - 1; j >= 0; j--) Final.push_back(make_pair(1, CurrentOrder[j]));
					for (int j = 0; j < PostOrder.size(); j++) Final.push_back(make_pair(1, PostOrder[j]));
					break;
				}
				else Final.push_back(make_pair(MaskOrd[val1 & 3], MaskOrd[val1 >> 2]));
			}

			// スコアの計算
			long long Score = GetActual(Final);
			if (FinalScore > Score) {
				FinalScore = Score;
				FinalAns = Final;
			}
		}
		return;
	}

	// 枝刈り探索
	int dep = PostOrder.size() + CurrentOrder.size() + 1;
	long long Border1 = Target - MaximumA[(1 << Cand.size()) - 1 - mask] * (1LL << (41 - dep) - 1LL) - 10000LL;
	long long Border2 = Target - MinimumA[(1 << Cand.size()) - 1 - mask] * (1LL << (41 - dep) - 1LL) + 10000LL;
	long long Border3 = Target - MaximumB[(1 << Cand.size()) - 1 - mask] * (1LL << (41 - dep) - 1LL) - 10000LL;
	long long Border4 = Target - MinimumB[(1 << Cand.size()) - 1 - mask] * (1LL << (41 - dep) - 1LL) + 10000LL;
	for (int i = 0; i < Cand.size(); i++) {
		if ((mask >> i) & 1) continue;
		long long NextA = CurrentA + (A[Cand[i]] >> dep);
		long long NextB = CurrentB + (B[Cand[i]] >> dep);
		if (NextA < Border1 || NextA > Border2) continue;
		if (NextB < Border3 || NextB > Border4) continue;
		CurrentOrder.push_back(Cand[i]);
		dfs(depth - 1, mask + (1 << i), NextA, NextB, Cand, PostOrder);
		CurrentOrder.pop_back();
	}
}

int main() {
	// Step 1. 入力
	cin >> N;
	for (int i = 1; i <= N; i++) cin >> A[i] >> B[i];
	
	// Step 2. 初期化
	vector<long long> Random(4, 0);
	Random[0] = 123456789123456789;
	Random[1] = 314159265358979323;
	Random[2] = 869120869120869120;
	Random[3] = 999999999999999999;
	init_dfs(0, Random);

	// Step 3. 焼きなまし法
	StartTime = clock();
	vector<int> BestOrder = GetBestOrder();

	// Step 4. 終盤探索 (初期化)
	for (int i = 0; i < 18; i++) {
		for (int j = i + 1; j < 18; j++) {
			for (int k = j + 1; k < 18; k++) {
				for (int l = k + 1; l < 18; l++) {
					Initialize((1 << i) + (1 << j) + (1 << k) + (1 << l), vector<int>{BestOrder[i], BestOrder[j], BestOrder[k], BestOrder[l]});
					IndexCount += 1;
				}
			}
		}
	}
	for (int i = 0; i < (1 << 18); i++) {
		MaximumA[i] = -(1LL << 60);
		MaximumB[i] = -(1LL << 60);
		MinimumA[i] = +(1LL << 60);
		MinimumB[i] = +(1LL << 60);
		for (int j = 0; j < 18; j++) {
			if ((i >> j) & 1) {
				MaximumA[i] = max(MaximumA[i], A[BestOrder[j]] >> 40);
				MinimumA[i] = min(MinimumA[i], A[BestOrder[j]] >> 40);
				MaximumB[i] = max(MaximumB[i], B[BestOrder[j]] >> 40);
				MinimumB[i] = min(MinimumB[i], B[BestOrder[j]] >> 40);
			}
		}
	}

	// Step 5. 終盤探索
	for (int depth = 6; depth <= 18; depth++) {
		long long SumA = (A[1] >> (N - 4));
		long long SumB = (B[1] >> (N - 4));
		for (int i = depth; i < N - 1; i++) SumA += (A[BestOrder[i]] >> (N - 1 - i));
		for (int i = depth; i < N - 1; i++) SumB += (B[BestOrder[i]] >> (N - 1 - i));
		vector<int> Vec1;
		vector<int> Vec2;
		for (int i = 0; i < depth; i++) Vec1.push_back(BestOrder[i]);
		for (int i = depth; i < N - 1; i++) Vec2.push_back(BestOrder[i]);
		dfs(depth, 0, SumA, SumB, Vec1, Vec2);
		// printf("% 3d% 9lld% 9d% 7d\n", depth, min(99999999LL, FinalScore), NumLoops, clock() - StartTime);
		if (clock() - StartTime > 80 * CLOCKS_PER_SEC / 100) break;
	}

	// Step 6. 出力
	cout << FinalAns.size() << endl;
	for (pair<int, int> ans : FinalAns) cout << ans.first << " " << ans.second << endl;
	return 0;
}
0