結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-18 19:05:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 914 ms / 1,000 ms
コード長 6,036 bytes
コンパイル時間 1,637 ms
コンパイル使用メモリ 103,260 KB
実行使用メモリ 6,676 KB
スコア 14,730,780
最終ジャッジ日時 2024-02-25 12:31:50
合計ジャッジ時間 49,824 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

// ======================================================================================= 最大 135 桁の多倍長整数
struct BigInteger {
	vector<int> val;
	BigInteger() {
		val = vector<int>(15, 0);
	}
	BigInteger(string str) {
		val = vector<int>(15, 0);
		vector<int> keisuu(15, 1);
		reverse(str.begin(), str.end());
		for (int i = 0; i < str.size(); i++) {
			val[i / 9] += keisuu[i / 9] * (int)(str[i] - '0');
			keisuu[i / 9] *= 10;
		}
	}
};

bool operator>= (const BigInteger& a1, const BigInteger& a2) {
	for (int i = 14; i >= 0; i--) {
		if (a1.val[i] > a2.val[i]) return true;
		if (a1.val[i] < a2.val[i]) return false;
	}
	return true;
}

bool operator> (const BigInteger& a1, const BigInteger& a2) {
	for (int i = 14; i >= 0; i--) {
		if (a1.val[i] > a2.val[i]) return true;
		if (a1.val[i] < a2.val[i]) return false;
	}
	return false;
}

bool operator< (const BigInteger& a1, const BigInteger& a2) {
	for (int i = 14; i >= 0; i--) {
		if (a1.val[i] < a2.val[i]) return true;
		if (a1.val[i] > a2.val[i]) return false;
	}
	return false;
}

BigInteger operator+(const BigInteger& a1, const BigInteger& a2) {
	BigInteger a3;
	for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i] + a2.val[i];
	for (int i = 0; i < 14; i++) {
		if (a3.val[i] >= 1000000000) {
			a3.val[i] -= 1000000000;
			a3.val[i + 1] += 1;
		}
	}
	return a3;
}

BigInteger operator-(const BigInteger& a1, const BigInteger& a2) {
	BigInteger a3;
	for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i] - a2.val[i];
	for (int i = 0; i < 14; i++) {
		if (a3.val[i] < 0) {
			a3.val[i] += 1000000000;
			a3.val[i + 1] -= 1;
		}
	}
	return a3;
}

BigInteger operator<<(const BigInteger& a1, const int& a2) {
	BigInteger a3;
	for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i];
	for (int i = 0; i < a2; i++) {
		for (int j = 0; j < 15; j++) a3.val[j] <<= 1;
		for (int j = 0; j < 14; j++) {
			if (a3.val[j] >= 1000000000) {
				a3.val[j] -= 1000000000;
				a3.val[j + 1] += 1;
			}
		}
	}
	return a3;
}

BigInteger operator>>(const BigInteger& a1, const int& a2) {
	BigInteger a3;
	for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i];
	for (int i = 0; i < a2; i++) {
		for (int j = 0; j < 14; j++) {
			if ((a3.val[j + 1] & 1) == 1) {
				a3.val[j + 1] -= 1;
				a3.val[j] += 1000000000;
			}
			a3.val[j] >>= 1;
		}
	}
	return a3;
}

BigInteger Abs(BigInteger a, BigInteger b) {
	if (a >= b) return a - b;
	return b - a;
}

// ======================================================================================= メイン部分
void Operation(vector<BigInteger>& A, int p1, int p2) {
	BigInteger Avg = (A[p1] + A[p2]) >> 1;
	A[p1] = Avg;
	A[p2] = Avg;
}

BigInteger GetScore(vector<BigInteger> P, BigInteger Target, vector<int> Depth) {
	BigInteger Sum;
	for (int i = 0; i < P.size(); i++) {
		Sum = Sum + (P[i] << Depth[i]);
	}
	return Abs(Sum, (Target << (P.size() - 1)));
}

int main() {
	srand((unsigned)time(NULL));
	BigInteger Target; Target.val[11] = 5;
	vector<pair<int, int>> Answer;

	// Step 1. 入力
	int N; cin >> N;
	vector<BigInteger> P(N);
	for (int i = 0; i < N; i++) {
		string str; cin >> str;
		P[i] = BigInteger(str);
	}

	// Step 2. シミュレーション準備
	int StartTime1 = clock();
	BigInteger MinScore = Target;
	vector<int> MinOrder;
	pair<BigInteger, int> Minimum1 = make_pair(Target, -1);
	for (int i = 1; i < N; i++) {
		BigInteger Avg = (P[0] + P[i]) >> 1;
		Minimum1 = min(Minimum1, make_pair(Abs(Avg, Target), i));
	}
	Operation(P, 0, Minimum1.second);
	Answer.push_back(make_pair(0, Minimum1.second));

	// Step 3. シミュレーション
	while (clock() - StartTime1 < 4 * CLOCKS_PER_SEC / 10) {
		vector<BigInteger> A = P;

		// 貪欲法
		BigInteger Remain = (Target << (N - 1)) - (A[0] << (N - 2));
		vector<int> Order;
		vector<bool> Used(N, false);
		Used[0] = true; Order.push_back(0);
		for (int i = N - 3; i >= 1; i--) {
			vector<pair<BigInteger, int>> Vec;
			for (int j = 0; j < N; j++) {
				if (Used[j] == true) continue;
				if (Remain < (A[j] << i)) continue;
				BigInteger Score = Abs(Remain, A[j] << (i + 1));
				Vec.push_back(make_pair(Score, j));
			}
			sort(Vec.begin(), Vec.end());
			if (Vec.size() == 0) break;
			int idx = Vec[rand() % max(1, (int)Vec.size() / 5)].second;
			Order.push_back(idx);
			Used[idx] = true;
			Remain = Remain - (A[idx] << i);
		}
		for (int i = 0; i < N; i++) {
			if (Used[i] == false) Order.push_back(i);
		}

		// 現在のスコアと比較
		for (int i = N - 2; i >= 0; i--) Operation(A, Order[i], Order[i + 1]);
		if (MinScore > Abs(Target, A[0])) {
			MinScore = Abs(Target, A[0]);
			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;
	BigInteger BstScore = GetScore(P, Target, Depth);
	BigInteger CurScore = GetScore(P, Target, Depth);
	int StartTime2 = clock();
	while (clock() - StartTime2 < 5 * CLOCKS_PER_SEC / 10) {
		int idx1 = rand() % Depth.size();
		int idx2 = rand() % Depth.size();
		if (Depth[idx1] >= 13 || Depth[idx2] >= 13 || abs(Depth[idx1] - Depth[idx2]) >= 3) continue;
		swap(Depth[idx1], Depth[idx2]);
		BigInteger CandScore = GetScore(P, Target, Depth);
		if (CurScore >= CandScore || CandScore < Target) {
			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;
	for (int i = 0; i < N; i++) {
		if (flag == false && BstDepth[i] == 0) { flag = true; AnsList[0] = i; }
		else AnsList[BstDepth[i] + 1] = i;
	}
	for (int i = 0; i < N - 1; i++) Answer.push_back(make_pair(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