結果
問題 | No.5020 Averaging |
ユーザー |
![]() |
提出日時 | 2024-02-25 17:33:31 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.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 |
純コード判定しない問題か言語 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 50 |
ソースコード
#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. inputint 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 searchconst 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 depthvector<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 answervector<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. outputcout << answer.size() << endl;for (array<int, 2> v : answer) {cout << v[0] + 1 << ' ' << v[1] + 1 << endl;}return 0;}