結果

問題 No.5020 Averaging
ユーザー square1001square1001
提出日時 2024-02-25 16:29:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 709 ms / 1,000 ms
コード長 4,665 bytes
コンパイル時間 2,900 ms
コンパイル使用メモリ 224,428 KB
実行使用メモリ 428,360 KB
スコア 86,152,842
最終ジャッジ日時 2024-02-25 16:30:09
合計ジャッジ時間 40,203 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 642 ms
428,360 KB
testcase_01 AC 636 ms
428,360 KB
testcase_02 AC 667 ms
428,360 KB
testcase_03 AC 649 ms
428,360 KB
testcase_04 AC 678 ms
428,360 KB
testcase_05 AC 628 ms
428,360 KB
testcase_06 AC 694 ms
428,360 KB
testcase_07 AC 659 ms
428,360 KB
testcase_08 AC 688 ms
428,360 KB
testcase_09 AC 679 ms
428,360 KB
testcase_10 AC 663 ms
428,360 KB
testcase_11 AC 637 ms
428,360 KB
testcase_12 AC 671 ms
428,360 KB
testcase_13 AC 675 ms
428,360 KB
testcase_14 AC 709 ms
428,360 KB
testcase_15 AC 687 ms
428,360 KB
testcase_16 AC 659 ms
428,360 KB
testcase_17 AC 660 ms
428,360 KB
testcase_18 AC 685 ms
428,360 KB
testcase_19 AC 699 ms
428,360 KB
testcase_20 AC 677 ms
428,360 KB
testcase_21 AC 697 ms
428,360 KB
testcase_22 AC 651 ms
428,360 KB
testcase_23 AC 697 ms
428,360 KB
testcase_24 AC 658 ms
428,360 KB
testcase_25 AC 704 ms
428,360 KB
testcase_26 AC 678 ms
428,360 KB
testcase_27 AC 689 ms
428,360 KB
testcase_28 AC 665 ms
428,360 KB
testcase_29 AC 683 ms
428,360 KB
testcase_30 AC 665 ms
428,360 KB
testcase_31 AC 666 ms
428,360 KB
testcase_32 AC 672 ms
428,360 KB
testcase_33 AC 694 ms
428,360 KB
testcase_34 AC 701 ms
428,360 KB
testcase_35 AC 657 ms
428,360 KB
testcase_36 AC 688 ms
428,360 KB
testcase_37 AC 661 ms
428,360 KB
testcase_38 AC 638 ms
428,360 KB
testcase_39 AC 663 ms
428,360 KB
testcase_40 AC 688 ms
428,360 KB
testcase_41 AC 679 ms
428,360 KB
testcase_42 AC 687 ms
428,360 KB
testcase_43 AC 658 ms
428,360 KB
testcase_44 AC 669 ms
428,360 KB
testcase_45 AC 644 ms
428,360 KB
testcase_46 AC 700 ms
428,360 KB
testcase_47 AC 650 ms
428,360 KB
testcase_48 AC 707 ms
428,360 KB
testcase_49 AC 672 ms
428,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

class state {
public:
	double x, y; uint64_t bit; int pre;
	state() : x(0.0), y(0.0), bit(0), pre(-1) {}
	state(double x_, double y_, uint64_t bit_, int pre_) : x(x_), y(y_), bit(bit_), pre(pre_) {}
	double score() const {
		return max(abs(x), abs(y));
	}
	bool operator<(const state& s) const {
		return score() < s.score();
	}
};

int main() {
	// step #1. input
	int N;
	cin >> N;
	vector<double> A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i] >> B[i];
		A[i] -= 5.0e+17;
		B[i] -= 5.0e+17;
	}
	
	// step #2. beam search
	constexpr int BEAMWIDTH = 15000;
	constexpr int REMAINS = 6;
	vector<vector<state> > beam(N - REMAINS - 2);
	for (int v1 = 1; v1 < N; v1++) {
		for (int v2 = v1 + 1; v2 < N; v2++) {
			double x = (A[0] + A[v1] + A[v2]) / 4;
			double y = (B[0] + B[v1] + B[v2]) / 4;
			uint64_t bit = ((1ULL << N) - 1) ^ (1ULL << 0) ^ (1ULL << v1) ^ (1ULL << v2);
			beam[0].push_back(state(x, y, bit, -1));
		}
	}
	sort(beam[0].begin(), beam[0].end());
	if (int(beam[0].size()) > BEAMWIDTH) {
		beam[0].resize(BEAMWIDTH);
	}
	for (int level = 1; level <= N - REMAINS - 3; level++) {
		double mul = pow(0.5, level + 2);
		for (int i = 0; i < int(beam[level - 1].size()); i++) {
			state s = beam[level - 1][i];
			uint64_t b = s.bit;
			while (b != 0) {
				int pos = __builtin_ctzll(b);
				b ^= 1ULL << pos;
				double x = s.x + A[pos] * mul;
				double y = s.y + B[pos] * mul;
				uint64_t bit = s.bit ^ (1ULL << pos);
				beam[level].push_back(state(x, y, bit, i));
			}
		}
		if (int(beam[level].size()) > BEAMWIDTH) {
			nth_element(beam[level].begin(), beam[level].begin() + BEAMWIDTH, beam[level].end());
			beam[level].resize(BEAMWIDTH);
		}
		// cerr << "level = " << level << ": size = " << beam[level].size() << ", score = " << beam[level][0].score() << ", back = " << beam[level].back().score() << endl;
	}
	
	// step #3. enumerate all remaining combinations
	vector<array<double, REMAINS> > weights;
	vector<int> path;
	auto dfs = [&](auto& self, int depth, int rem, int val) -> void {
		if (rem < val) {
			return;
		}
		if (val == 0) {
			array<double, REMAINS> v;
			for (int i = 0; i < REMAINS; i++) {
				v[i] = (i < int(path.size()) ? pow(2.0, -((N - REMAINS - 1) + path[i])) : 0.0);
			}
			reverse(v.begin(), v.end());
			do {
				weights.push_back(v);
			} while (next_permutation(v.begin(), v.end()));
			return;
		}
		for (int i = 0; i <= rem && i <= val; i++) {
			self(self, depth + 1, rem - i, (val - i) * 2);
			path.push_back(depth);
		}
		while (!path.empty() && path.back() == depth) {
			path.pop_back();
		}
	};
	dfs(dfs, 0, REMAINS, 1);
	const int SUBSTATES = weights.size();
	cerr << "substates = " << SUBSTATES << endl;
	
	// step #4. brute force
	double bestscore = 1.0e+99;
	vector<int> bestdepth;
	for (int i = 0; i < int(beam[N - REMAINS - 3].size()); i++) {
		state s = beam[N - REMAINS - 3][i];
		uint64_t rembit = s.bit;
		array<int, REMAINS> subidx;
		array<double, REMAINS> suba, subb;
		for (int j = 0; j < REMAINS; j++) {
			subidx[j] = __builtin_ctzll(rembit);
			suba[j] = A[subidx[j]];
			subb[j] = B[subidx[j]];
			rembit ^= 1ULL << subidx[j];
		}
		for (int j = 0; j < SUBSTATES; j++) {
			double x = s.x, y = s.y;
			for (int k = 0; k < REMAINS; k++) {
				x += suba[k] * weights[j][k];
				y += subb[k] * weights[j][k];
			}
			double score = max(abs(x), abs(y));
			if (bestscore > score) {
				// reconstruction
				vector<int> depth(N, -1);
				for (int k = 0; k < REMAINS; k++) {
					if (weights[j][k] != 0.0) {
						depth[subidx[k]] = int(-log2(weights[j][k]) + 0.5);
					}
				}
				int idx = i;
				uint64_t prebit = s.bit;
				for (int k = N - REMAINS - 3; k >= 0; k--) {
					uint64_t nxtbit = (k != 0 ? beam[k - 1][beam[k][idx].pre].bit : (1ULL << N) - 1);
					uint64_t bitdiff = prebit ^ nxtbit;
					while (bitdiff != 0) {
						int pos = __builtin_ctzll(bitdiff);
						depth[pos] = k + 2;
						bitdiff ^= 1ULL << pos;
					}
					prebit = nxtbit;
					if (k != 0) {
						idx = beam[k][idx].pre;
					}
				}
				bestscore = score;
				bestdepth = depth;
			}
		}
	}
	cerr << "bestscore = " << bestscore << endl;
	
	// step #5. construction
	vector<array<int, 2> > answer;
	vector<int> curdepth = bestdepth;
	while (curdepth[0] != 0) {
		int p1 = max_element(curdepth.begin(), curdepth.end()) - curdepth.begin();
		int p2 = max_element(curdepth.begin() + p1 + 1, curdepth.end()) - curdepth.begin();
		answer.push_back({p1, p2});
		curdepth[p1] -= 1;
		curdepth[p2] = -1;
	}
	
	// step #6. output
	cout << answer.size() << endl;
	for (array<int, 2> v : answer) {
		cout << v[0] + 1 << ' ' << v[1] + 1 << endl;
	}
	
	return 0;
}
0