結果

問題 No.5020 Averaging
ユーザー risujirohrisujiroh
提出日時 2024-02-26 04:36:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 982 ms / 1,000 ms
コード長 6,754 bytes
コンパイル時間 3,933 ms
コンパイル使用メモリ 280,704 KB
実行使用メモリ 6,676 KB
スコア 92,177,573
最終ジャッジ日時 2024-02-26 04:37:31
合計ジャッジ時間 55,083 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 961 ms
6,676 KB
testcase_01 AC 967 ms
6,676 KB
testcase_02 AC 938 ms
6,676 KB
testcase_03 AC 964 ms
6,676 KB
testcase_04 AC 948 ms
6,676 KB
testcase_05 AC 944 ms
6,676 KB
testcase_06 AC 970 ms
6,676 KB
testcase_07 AC 940 ms
6,676 KB
testcase_08 AC 965 ms
6,676 KB
testcase_09 AC 959 ms
6,676 KB
testcase_10 AC 942 ms
6,676 KB
testcase_11 AC 965 ms
6,676 KB
testcase_12 AC 938 ms
6,676 KB
testcase_13 AC 966 ms
6,676 KB
testcase_14 AC 965 ms
6,676 KB
testcase_15 AC 940 ms
6,676 KB
testcase_16 AC 968 ms
6,676 KB
testcase_17 AC 941 ms
6,676 KB
testcase_18 AC 968 ms
6,676 KB
testcase_19 AC 966 ms
6,676 KB
testcase_20 AC 943 ms
6,676 KB
testcase_21 AC 965 ms
6,676 KB
testcase_22 AC 962 ms
6,676 KB
testcase_23 AC 969 ms
6,676 KB
testcase_24 AC 935 ms
6,676 KB
testcase_25 AC 953 ms
6,676 KB
testcase_26 AC 968 ms
6,676 KB
testcase_27 AC 967 ms
6,676 KB
testcase_28 AC 965 ms
6,676 KB
testcase_29 AC 944 ms
6,676 KB
testcase_30 AC 946 ms
6,676 KB
testcase_31 AC 982 ms
6,676 KB
testcase_32 AC 969 ms
6,676 KB
testcase_33 AC 967 ms
6,676 KB
testcase_34 AC 936 ms
6,676 KB
testcase_35 AC 939 ms
6,676 KB
testcase_36 AC 968 ms
6,676 KB
testcase_37 AC 966 ms
6,676 KB
testcase_38 AC 968 ms
6,676 KB
testcase_39 AC 943 ms
6,676 KB
testcase_40 AC 941 ms
6,676 KB
testcase_41 AC 967 ms
6,676 KB
testcase_42 AC 966 ms
6,676 KB
testcase_43 AC 959 ms
6,676 KB
testcase_44 AC 950 ms
6,676 KB
testcase_45 AC 941 ms
6,676 KB
testcase_46 AC 968 ms
6,676 KB
testcase_47 AC 969 ms
6,676 KB
testcase_48 AC 967 ms
6,676 KB
testcase_49 AC 946 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

namespace {

constexpr i64 H = 500'000'000'000'000'000;

struct Input {
  int n;
  int m;
  std::vector<i64> a;
  std::vector<i64> b;
};

Input read_input() {
  Input in;
  scan(in.n);
  in.m = 50;
  in.a.resize(in.n);
  in.b.resize(in.n);
  for (const int i : rep(in.n)) {
    scan(in.a[i], in.b[i]);
  }
  return in;
}

double evaluate(const Input& in, const std::vector<int>& p,
                const std::vector<std::pair<int, int>>& pre) {
  auto a = in.a;
  auto b = in.b;
  for (auto [i, j] : pre) {
    i = p[i];
    j = p[j];
    a[i] = a[j] = (a[i] + a[j]) >> 1;
    b[i] = b[j] = (b[i] + b[j]) >> 1;
  }
  i64 x = a[p.back()];
  i64 y = b[p.back()];
  for (const int i : p | views::reverse | views::drop(1)) {
    x = (x + a[i]) >> 1;
    y = (y + b[i]) >> 1;
  }
  return std::log1p(std::max(std::abs(x - H), std::abs(y - H)));
}

void answer(const std::vector<int>& p, const std::vector<std::pair<int, int>>& pre) {
  print(len(pre) + len(p) - 1);
  for (const auto& [i, j] : pre) {
    print(p[i] + 1, p[j] + 1);
  }
  int min_i = p.back();
  for (const int i : p | views::reverse | views::drop(1)) {
    print(min_i + 1, i + 1);
    chmin(min_i, i);
  }
}

void solve() {
  const Input in = read_input();

  constexpr int S = 40;

  std::vector<int> p(in.n);
  std::iota(p.begin(), p.end(), 0);
  std::vector<std::pair<int, int>> pre(in.m - in.n + 1);
  ranges::generate(pre, std::bind(randint2, S, in.n - 1));

  double cur = evaluate(in, p, pre);
  double best = cur;
  auto best_p = p;
  auto best_pre = pre;
  for (const auto& sa : Sa<256>(5 << 20, 0.5, 0.4)) {
    if (randint(0, 9)) {
      const int i = randint(0, in.n - 2);
      std::swap(p[i], p[i + 1]);
      const double nxt = evaluate(in, p, pre);
      if (cur + sa.tol < nxt) {
        std::swap(p[i], p[i + 1]);
      } else {
        cur = nxt;
      }

    } else {
      const int i = randint(0, in.m - in.n);
      const auto old = std::exchange(pre[i], randint2(S, in.n - 1));
      const double nxt = evaluate(in, p, pre);
      if (cur + sa.tol < nxt) {
        pre[i] = old;
      } else {
        cur = nxt;
      }
    }

    if (chmin(best, cur)) {
      best_p = p;
      best_pre = pre;
    }
  }

  answer(best_p, best_pre);
}

}  // namespace

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout << std::setprecision(DBL_DECIMAL_DIG);

  solve();
}

#else  // __INCLUDE_LEVEL__

#include <bits/stdc++.h>

namespace xorshift {

using u64 = std::uint64_t;
using u128 = ::__uint128_t;

inline u64 x = std::chrono::steady_clock::now().time_since_epoch().count();

inline u64 next() {
  x ^= x << 7;
  x ^= x >> 9;
  return x;
}

int randint(int a, int b) {
  const u64 m = static_cast<u64>(b) - static_cast<u64>(a) + 1;
  return a + static_cast<int>(u128{next()} * m >> 64);
}

std::pair<int, int> randint2(int a, int b) {
  int x = randint(a, b - 1);
  int y = randint(a, b);
  if (y == x) {
    y = b;
  } else if (y < x) {
    std::swap(x, y);
  }
  return {x, y};
}

template <std::ranges::random_access_range R>
void shuffle(R&& r) {
  const int n = static_cast<int>(std::ranges::size(r));
  if (n < 2) {
    return;
  }
  for (const int i : std::views::iota(1, n)) {
    if (const int j = randint(0, i); j < i) {
      std::ranges::swap(r[i], r[j]);
    }
  }
}

double uniform() { return static_cast<double>(next()) / static_cast<double>(UINT64_MAX); }
double uniform(double a, double b) { return a + (b - a) * uniform(); }

}  // namespace xorshift

using xorshift::randint;
using xorshift::randint2;
using xorshift::shuffle;
using xorshift::uniform;

template <int UpdateInterval = 1>
class Sa {
 public:
  explicit Sa(std::int64_t n_iters, double start_temp = 0, double end_temp = 0)
      : n_iters_(n_iters),
        start_temp_(start_temp),
        cooling_rate_(start_temp ? end_temp / start_temp : 1),
        iter_(0),
        elapsed_(0),
        temp_(start_temp) {
    assert(0 <= end_temp && end_temp <= start_temp);
  }

  Sa begin() const { return *this; }
  int end() const { return 0; }

  bool operator!=(int) const { return iter_ < n_iters_; }

  void operator++() {
    if (++iter_ % UpdateInterval == 0) {
      elapsed_ = static_cast<double>(iter_) / static_cast<double>(n_iters_);
      temp_ = start_temp_ * std::pow(cooling_rate_, elapsed_);
    }
  }

  auto operator*() const {
    struct {
      std::int64_t iter;
      double elapsed;
      double temp;
      double tol;
    } sa{iter_, elapsed_, temp_, temp_ * -std::log(uniform())};
    return sa;
  }

 private:
  std::int64_t n_iters_;
  double start_temp_;
  double cooling_rate_;
  std::int64_t iter_;
  double elapsed_;
  double temp_;
};

template <class T, class U = T>
bool chmin(T& x, U&& y) {
  return y < x && (x = std::forward<U>(y), true);
}

template <class T, class U = T>
bool chmax(T& x, U&& y) {
  return x < y && (x = std::forward<U>(y), true);
}

template <std::signed_integral T = int>
T inf() {
  T ret;
  std::memset(&ret, 0x3f, sizeof(ret));
  return ret;
}

template <std::floating_point T>
T inf() {
  return std::numeric_limits<T>::infinity();
}

template <class T>
concept Range = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;

template <class T>
concept Tuple = std::__is_tuple_like<T>::value && !Range<T>;

namespace std {

istream& operator>>(istream& is, Range auto&& r) {
  for (auto&& e : r) {
    is >> e;
  }
  return is;
}

istream& operator>>(istream& is, Tuple auto&& t) {
  return apply([&](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

ostream& operator<<(ostream& os, Range auto&& r) {
  for (string_view sep = ""; auto&& e : r) {
    os << exchange(sep, " ") << e;
  }
  return os;
}

ostream& operator<<(ostream& os, Tuple auto&& t) {
  const auto f = [&](auto&... xs) -> ostream& {
    [[maybe_unused]] string_view sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

}  // namespace std

void scan(auto&&... xs) { std::cin >> std::tie(xs...); }
void print(auto&&... xs) { std::cout << std::tie(xs...) << '\n'; }

template <class F>
class fix {
 public:
  explicit fix(F f) : f_(std::move(f)) {}

  decltype(auto) operator()(auto&&... xs) const {
    return f_(std::ref(*this), std::forward<decltype(xs)>(xs)...);
  }

 private:
  F f_;
};

inline auto rep(int l, int r) { return std::views::iota(std::min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }

namespace ranges = std::ranges;
namespace views = std::views;

using i64 = std::int64_t;

#define len(...) static_cast<int>(ranges::size(__VA_ARGS__))

#endif  // __INCLUDE_LEVEL__
0