結果

問題 No.5020 Averaging
ユーザー square1001square1001
提出日時 2024-02-25 17:33:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 812 ms / 1,000 ms
コード長 2,713 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 219,288 KB
実行使用メモリ 458,904 KB
スコア 86,102,336
最終ジャッジ日時 2024-02-25 17:34:22
合計ジャッジ時間 41,500 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 812 ms
458,904 KB
testcase_01 AC 670 ms
458,904 KB
testcase_02 AC 699 ms
458,904 KB
testcase_03 AC 665 ms
458,904 KB
testcase_04 AC 698 ms
458,904 KB
testcase_05 AC 689 ms
458,904 KB
testcase_06 AC 672 ms
458,904 KB
testcase_07 AC 682 ms
458,904 KB
testcase_08 AC 687 ms
458,904 KB
testcase_09 AC 697 ms
458,904 KB
testcase_10 AC 668 ms
458,904 KB
testcase_11 AC 688 ms
458,904 KB
testcase_12 AC 671 ms
458,904 KB
testcase_13 AC 712 ms
458,904 KB
testcase_14 AC 672 ms
458,904 KB
testcase_15 AC 700 ms
458,904 KB
testcase_16 AC 677 ms
458,904 KB
testcase_17 AC 666 ms
458,904 KB
testcase_18 AC 712 ms
458,904 KB
testcase_19 AC 688 ms
458,904 KB
testcase_20 AC 678 ms
458,904 KB
testcase_21 AC 714 ms
458,904 KB
testcase_22 AC 683 ms
458,904 KB
testcase_23 AC 717 ms
458,904 KB
testcase_24 AC 652 ms
458,904 KB
testcase_25 AC 697 ms
458,904 KB
testcase_26 AC 690 ms
458,904 KB
testcase_27 AC 701 ms
458,904 KB
testcase_28 AC 646 ms
458,904 KB
testcase_29 AC 688 ms
458,904 KB
testcase_30 AC 676 ms
458,904 KB
testcase_31 AC 673 ms
458,904 KB
testcase_32 AC 709 ms
458,904 KB
testcase_33 AC 686 ms
458,904 KB
testcase_34 AC 691 ms
458,904 KB
testcase_35 AC 673 ms
458,904 KB
testcase_36 AC 706 ms
458,904 KB
testcase_37 AC 712 ms
458,904 KB
testcase_38 AC 624 ms
458,904 KB
testcase_39 AC 679 ms
458,904 KB
testcase_40 AC 754 ms
458,904 KB
testcase_41 AC 721 ms
458,904 KB
testcase_42 AC 731 ms
458,904 KB
testcase_43 AC 687 ms
458,904 KB
testcase_44 AC 697 ms
458,904 KB
testcase_45 AC 716 ms
458,904 KB
testcase_46 AC 679 ms
458,904 KB
testcase_47 AC 692 ms
458,904 KB
testcase_48 AC 695 ms
458,904 KB
testcase_49 AC 723 ms
458,904 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
	const int BEAMWIDTH = 16000;
	vector<vector<state> > beam(N);
	for (int i = 1; i < N; i++) {
		uint64_t bit = ((1ULL << N) - 1) ^ (1ULL << i);
		beam[0].push_back(state(A[i] * 0.5, B[i] * 0.5, 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 - 1; level++) {
		double mul = pow(0.5, min(level + 1, N - 1));
		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 = " << (*min_element(beam[level].begin(), beam[level].end())).score() << ", back = " << beam[level].back().score() << endl;
	}
	
	// step #3. construct depth
	vector<int> depth(N, -1);
	int idx = min_element(beam[N - 1].begin(), beam[N - 1].end()) - beam[N - 1].begin();
	uint64_t prebit = beam[N - 1][idx].bit;
	for (int i = N - 1; i >= 0; i--) {
		uint64_t nxtbit = (i != 0 ? beam[i - 1][beam[i][idx].pre].bit : (1ULL << N) - 1);
		uint64_t bitdiff = prebit ^ nxtbit;
		depth[__builtin_ctzll(bitdiff)] = min(i + 1, N - 1);
		prebit = nxtbit;
		if (i != 0) {
			idx = beam[i][idx].pre;
		}
	}
	
	// step #4. construct answer
	vector<array<int, 2> > answer;
	vector<int> curdepth = depth;
	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 #5. output
	cout << answer.size() << endl;
	for (array<int, 2> v : answer) {
		cout << v[0] + 1 << ' ' << v[1] + 1 << endl;
	}
	
	return 0;
}
0