結果

問題 No.5020 Averaging
ユーザー e869120e869120
提出日時 2024-02-18 18:04:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 911 ms / 1,000 ms
コード長 4,698 bytes
コンパイル時間 1,476 ms
コンパイル使用メモリ 102,720 KB
実行使用メモリ 6,676 KB
スコア 14,655,578
最終ジャッジ日時 2024-02-25 12:30:59
合計ジャッジ時間 49,477 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#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;
}


int main() {
	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);
	}
	int StartTime = clock();
	BigInteger MinScore = Target;

	// Step 2. シミュレーション
	while (clock() - StartTime < 9 * CLOCKS_PER_SEC / 10) {
		vector<BigInteger> A = P;
		vector<pair<int, int>> CurrentAns;

		// 最初に 1 回操作
		pair<BigInteger, int> Minimum1 = make_pair(Target, -1);
		for (int i = 1; i < N; i++) {
			BigInteger Avg = (A[0] + A[i]) >> 1;
			Minimum1 = min(Minimum1, make_pair(Abs(Avg, Target), i));
		}
		Operation(A, 0, Minimum1.second);
		CurrentAns.push_back(make_pair(0, Minimum1.second));

		// 貪欲法
		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() / 3)].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--) {
			CurrentAns.push_back(make_pair(Order[i], Order[i + 1]));
			Operation(A, Order[i], Order[i + 1]);
		}

		// 現在のスコアと比較
		if (MinScore > Abs(Target, A[0])) {
			MinScore = Abs(Target, A[0]);
			Answer = CurrentAns;
		}
	}

	// Step 5. 出力
	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