結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-20 14:51:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 942 ms / 1,000 ms
コード長 13,135 bytes
コンパイル時間 2,602 ms
コンパイル使用メモリ 140,812 KB
実行使用メモリ 88,636 KB
スコア 83,343,838
最終ジャッジ日時 2024-02-25 12:44:28
合計ジャッジ時間 51,889 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 934 ms
86,588 KB
testcase_01 AC 932 ms
87,996 KB
testcase_02 AC 933 ms
86,460 KB
testcase_03 AC 932 ms
88,508 KB
testcase_04 AC 929 ms
88,252 KB
testcase_05 AC 930 ms
87,100 KB
testcase_06 AC 933 ms
86,844 KB
testcase_07 AC 931 ms
87,356 KB
testcase_08 AC 942 ms
86,460 KB
testcase_09 AC 932 ms
87,484 KB
testcase_10 AC 935 ms
86,076 KB
testcase_11 AC 936 ms
84,668 KB
testcase_12 AC 930 ms
87,484 KB
testcase_13 AC 935 ms
88,508 KB
testcase_14 AC 931 ms
88,380 KB
testcase_15 AC 933 ms
87,100 KB
testcase_16 AC 941 ms
87,868 KB
testcase_17 AC 936 ms
86,076 KB
testcase_18 AC 940 ms
88,380 KB
testcase_19 AC 938 ms
88,252 KB
testcase_20 AC 925 ms
85,948 KB
testcase_21 AC 940 ms
86,844 KB
testcase_22 AC 932 ms
87,100 KB
testcase_23 AC 934 ms
88,252 KB
testcase_24 AC 934 ms
87,740 KB
testcase_25 AC 938 ms
86,972 KB
testcase_26 AC 934 ms
86,716 KB
testcase_27 AC 935 ms
87,356 KB
testcase_28 AC 934 ms
87,356 KB
testcase_29 AC 933 ms
85,820 KB
testcase_30 AC 928 ms
86,972 KB
testcase_31 AC 932 ms
88,252 KB
testcase_32 AC 927 ms
87,868 KB
testcase_33 AC 933 ms
86,460 KB
testcase_34 AC 933 ms
87,868 KB
testcase_35 AC 921 ms
87,996 KB
testcase_36 AC 940 ms
88,380 KB
testcase_37 AC 934 ms
86,204 KB
testcase_38 AC 933 ms
88,636 KB
testcase_39 AC 933 ms
86,588 KB
testcase_40 AC 936 ms
86,716 KB
testcase_41 AC 930 ms
88,380 KB
testcase_42 AC 939 ms
88,124 KB
testcase_43 AC 931 ms
87,868 KB
testcase_44 AC 924 ms
85,820 KB
testcase_45 AC 930 ms
88,252 KB
testcase_46 AC 937 ms
85,564 KB
testcase_47 AC 931 ms
85,692 KB
testcase_48 AC 937 ms
87,996 KB
testcase_49 AC 932 ms
87,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// [想定解答 9]
//    - 想定解答 8 を高速化し,Order[36] からではなく (部分的に) Order[35] から全探索できるようにする.
//    - 88% 前後の得点が得られる.

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

const long long MaxVal = 500000000000000000LL * 2LL;
const long long Target = 500000000000000000LL;
vector<pair<long long, long long>> ValList[11][11][11][11][100];
vector<pair<int, int>> backtrack, backtrack_valid;
int backtrack_finish;
vector<vector<pair<int, int>>> Possibility;
map<long long, bool> Possibility_Record;

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

void init_dfs(int depth, vector<long long> ArrayA) {
	if (Possibility_Record[ArrayA[0]] == false) {
		Possibility_Record[ArrayA[0]] = true;
		Possibility.push_back(backtrack);
	}
	if (depth == 8) 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(make_pair(i, j));
			init_dfs(depth + 1, ArrayC);
			backtrack.pop_back();
		}
	}
}

void brute_force(int depth, vector<int> idx, vector<long long> ArrayA, vector<long long> ArrayB) {
	for (int i = 0; i < 4; i++) {
		for (vector<pair<int, int>> track : Possibility) {
			vector<long long> ArrayC = ArrayA;
			vector<long long> ArrayD = ArrayB;
			for (pair<int, int> place : track) {
				int idx1 = (place.first + i) % 4;
				int idx2 = (place.second + i) % 4;
				long long avg1 = (ArrayC[idx1] + ArrayC[idx2]) / 2LL;
				long long avg2 = (ArrayD[idx1] + ArrayD[idx2]) / 2LL;
				ArrayC[idx1] = avg1; ArrayD[idx1] = avg2;
				ArrayC[idx2] = avg1; ArrayD[idx2] = avg2;
			}
			ValList[idx[0]][idx[1]][idx[2]][idx[3]][ArrayC[i] / (MaxVal / 100LL)].push_back(make_pair(ArrayD[i], ArrayC[i]));
		}
	}
}

void search(int depth, vector<int> idx, vector<long long> ArrayA, vector<long long> ArrayB, long long tgt_a, long long tgt_b) {
	for (vector<pair<int, int>> track : Possibility) {
		vector<long long> ArrayC = ArrayA;
		vector<long long> ArrayD = ArrayB;
		for (pair<int, int> idx : track) {
			long long avg1 = (ArrayC[idx.first] + ArrayC[idx.second]) / 2LL;
			long long avg2 = (ArrayD[idx.first] + ArrayD[idx.second]) / 2LL;
			ArrayC[idx.first] = avg1; ArrayD[idx.first] = avg2;
			ArrayC[idx.second] = avg1; ArrayD[idx.second] = avg2;
		}
		if (ArrayC[0] == tgt_a && ArrayD[0] == tgt_b) { backtrack_valid = track; backtrack_finish = 0; return; }
		if (ArrayC[1] == tgt_a && ArrayD[1] == tgt_b) { backtrack_valid = track; backtrack_finish = 1; return; }
		if (ArrayC[2] == tgt_a && ArrayD[2] == tgt_b) { backtrack_valid = track; backtrack_finish = 2; return; }
		if (ArrayC[3] == tgt_a && ArrayD[3] == tgt_b) { backtrack_valid = track; backtrack_finish = 3; return; }
	}
}

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. 初期化
	vector<long long> Random(4, 0);
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 18; j++) Random[i] += 10LL * Random[i] + 1LL * (rand() % 10LL);
	}
	init_dfs(0, Random);

	// 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;
	int Loops = 0;
	while (true) {
		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);
		Loops += 1;

		// 貪欲法スタート
		int param1 = rand() % 3 + 2;
		int param2 = rand() % 3 + 2;
		int param3 = rand() % 7;
		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;
				double keisuu = 1.0 - 0.1 * param3 * max(abs(C[i] - Target), abs(D[i] - Target)) / Target;
				Vec.push_back(make_pair(max(v1, v2) * keisuu, i));
			}
			sort(Vec.begin(), Vec.end()); if (Vec.size() == 0) break;
			int idx = Vec[rand() % max(1, min(param1, (int)Vec.size() / param2))].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;
		}
		if (Loops % 100 == 0 && clock() - StartTime > 35 * CLOCKS_PER_SEC / 100) break; // clock の呼び出しは時間がかかるので 100 回に 1 回呼び出す
	}

	// ============================================================================================================================== シナリオ 1
	long long FinalScore = (1LL << 60);
	vector<pair<int, int>> FinalAns;

	if (true) {
		// Step 5. 二分探索
		long long BaseA = BinarySearch(N, A, MinOrder, N - 11);
		long long BaseB = BinarySearch(N, B, MinOrder, N - 11);
		long long BestScore = (1LL << 60);
		vector<int> LastOrder;
		vector<int> Ord(11, 0);
		for (int i = N - 11; i < N; i++) LastOrder.push_back(MinOrder[i]);
		for (int i = 0; i < 11; i++) Ord[i] = i;

		// Step 6. 指数探索の前計算
		for (int i = 0; i < LastOrder.size(); i++) {
			for (int j = i + 1; j < LastOrder.size(); j++) {
				for (int k = j + 1; k < LastOrder.size(); k++) {
					for (int l = k + 1; l < LastOrder.size(); l++) {
						brute_force(0, vector<int>{i, j, k, l}, vector<long long>{A[LastOrder[i]], A[LastOrder[j]], A[LastOrder[k]], A[LastOrder[l]]}, vector<long long>{B[LastOrder[i]], B[LastOrder[j]], B[LastOrder[k]], B[LastOrder[l]]});
						for (int m = 0; m < 100; m++) sort(ValList[i][j][k][l][m].begin(), ValList[i][j][k][l][m].end());
						for (int m = 0; m < 100; m++) ValList[i][j][k][l][m].erase(unique(ValList[i][j][k][l][m].begin(), ValList[i][j][k][l][m].end()), ValList[i][j][k][l][m].end());
					}
				}
			}
		}

		// Step 7. 指数探索
		long long CurrentBest = (1LL << 60);
		while (true) {
			// Ord の更新
			swap(Ord[7], Ord[10]);
			swap(Ord[8], Ord[9]);
			int c1 = 6;
			int c2 = 0;
			while (c1 >= 0 && Ord[c1] > Ord[c1 + 1]) c1 -= 1;
			if (c1 == -1) break;
			c2 = c1 + 1;
			while (c2 <= 9 && Ord[c1] < Ord[c2 + 1]) c2 += 1;
			swap(Ord[c1], Ord[c2]);
			sort(Ord.begin() + (c1 + 1), Ord.end());
			if (c1 <= 3 && clock() - StartTime > 72 * CLOCKS_PER_SEC / 100) break;

			// 誤差の計算
			vector<long long> C = A; C[LastOrder[Ord[7]]] = 0;
			vector<long long> D = B; D[LastOrder[Ord[7]]] = 0;
			for (int i = 6; i >= 0; i--) Operation(C, D, LastOrder[Ord[i]], LastOrder[Ord[i + 1]]);
			long long err_c = BaseA - C[LastOrder[Ord[0]]];
			long long err_d = BaseB - D[LastOrder[Ord[0]]];
			long long goal_c = (err_c < 0LL ? 0LL : (err_c >= MaxVal / 128LL ? MaxVal - 1LL : err_c * 128LL));
			long long goal_d = (err_d < 0LL ? 0LL : (err_d >= MaxVal / 128LL ? MaxVal - 1LL : err_d * 128LL));
			if (goal_c <= 0LL || goal_c >= MaxVal - 1LL) continue;
			if (goal_d <= 0LL || goal_d >= MaxVal - 1LL) continue;

			// 探索準備
			vector<int> tmp = { Ord[7], Ord[8], Ord[9], Ord[10] };
			int pos1 = goal_c / (MaxVal / 100LL);
			tuple<long long, int, int> Record = make_tuple(1LL << 60, -1, -1);

			// 探索
			for (int i = pos1 - 10; i <= pos1 + 9; i++) {
				if (i < 0 || i >= 100) continue;
				int pos2 = lower_bound(ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][i].begin(), ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][i].end(), make_pair(goal_d, 0LL)) - ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][i].begin();
				for (int j = pos2 - 5; j <= pos2 + 4; j++) {
					if (j < 0 || j >= ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][i].size()) continue;
					long long diff_a = abs(goal_c - ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][i][j].second);
					long long diff_b = abs(goal_d - ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][i][j].first);
					Record = min(Record, make_tuple(max(diff_a, diff_b), i, j));
				}
			}

			// 記録更新の場合
			if (CurrentBest > get<0>(Record)) {
				CurrentBest = get<0>(Record);
				long long tgt_a = ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][get<1>(Record)][get<2>(Record)].second;
				long long tgt_b = ValList[tmp[0]][tmp[1]][tmp[2]][tmp[3]][get<1>(Record)][get<2>(Record)].first;
				search(0, tmp, vector<long long>{A[LastOrder[tmp[0]]], A[LastOrder[tmp[1]]], A[LastOrder[tmp[2]]], A[LastOrder[tmp[3]]]}, vector<long long>{B[LastOrder[tmp[0]]], B[LastOrder[tmp[1]]], B[LastOrder[tmp[2]]], B[LastOrder[tmp[3]]]}, tgt_a, tgt_b);
				vector<pair<int, int>> Answer;
				Answer.push_back(make_pair(0, MinID));
				for (int i = 0; i < backtrack_valid.size(); i++) Answer.push_back(make_pair(LastOrder[tmp[backtrack_valid[i].first]], LastOrder[tmp[backtrack_valid[i].second]]));
				vector<int> NewOrder;
				for (int i = 0; i < N - 11; i++) NewOrder.push_back(MinOrder[i]);
				for (int i = 0; i < 7; i++) NewOrder.push_back(LastOrder[Ord[i]]);
				NewOrder.push_back(LastOrder[tmp[backtrack_finish]]);
				for (int i = NewOrder.size() - 2; i >= 0; i--) Answer.push_back(make_pair(NewOrder[i], NewOrder[i + 1]));
				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);
				long long diff_a = abs(Target - SimA[0]);
				long long diff_b = abs(Target - SimB[0]);
				if (FinalScore > max(diff_a, diff_b)) {
					FinalScore = max(diff_a, diff_b);
					FinalAns = Answer;
				}
			}
		}
	}

	// ============================================================================================================================== シナリオ 2
	if (true) {
		// Step 8. 二分探索
		long long BaseA = BinarySearch(N, A, MinOrder, N - 9);
		long long BaseB = BinarySearch(N, B, MinOrder, N - 9);

		// Step 9. 指数探索
		long long BestScore = (1LL << 60);
		vector<int> LastOrder;
		vector<int> BestOrder;
		for (int i = N - 9; 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 10. 答えを得る
		vector<pair<int, int>> Answer;
		vector<int> FinalOrder;
		for (int i = 0; i < N - 9; 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 11. シミュレーション
		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);
		long long diff_a = abs(Target - SimA[0]);
		long long diff_b = abs(Target - SimB[0]);
		if (FinalScore > max(diff_a, diff_b)) {
			FinalScore = max(diff_a, diff_b);
			FinalAns = Answer;
		}
	}

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