結果

問題 No.180 美しいWhitespace (2)
ユーザー rsk0315rsk0315
提出日時 2019-01-06 17:50:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,167 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 72,052 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 16:11:03
合計ジャッジ時間 7,511 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
4,376 KB
testcase_01 AC 7 ms
4,376 KB
testcase_02 AC 44 ms
4,376 KB
testcase_03 AC 113 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 507 ms
4,380 KB
testcase_10 AC 513 ms
4,380 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 243 ms
4,380 KB
testcase_15 AC 299 ms
4,376 KB
testcase_16 AC 384 ms
4,380 KB
testcase_17 AC 432 ms
4,380 KB
testcase_18 AC 502 ms
4,380 KB
testcase_19 AC 516 ms
4,380 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 WA -
testcase_22 AC 412 ms
4,376 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 7 ms
4,376 KB
testcase_25 AC 24 ms
4,376 KB
testcase_26 AC 23 ms
4,376 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 68 ms
4,380 KB
testcase_30 AC 146 ms
4,380 KB
testcase_31 AC 141 ms
4,380 KB
testcase_32 AC 7 ms
4,376 KB
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cassert>
#include <vector>
#include <algorithm>
#include <map>

template <class Tp>
class lines {
  std::map<Tp, Tp> ll;

  bool is_redundant(const typename std::map<Tp, Tp>::iterator& it) const {
    if (it == ll.begin() || it == ll.end()) return false;
    auto next = std::next(it);
    if (next == ll.end()) return false;
    auto prev = std::prev(it);

    Tp a1, b1, a2, b2, a3, b3;
    std::tie(a1, b1) = *prev;
    std::tie(a2, b2) = *it;
    std::tie(a3, b3) = *next;

    // fprintf(stderr, "[%d, %d] [%d, %d] [%d, %d]\n", a1, b1, a2, b2, a3, b3);
    return (b1-b2) * (a3-a2) <= (b2-b3) * (a2-a1);
  }

public:
  void append(Tp a, Tp b) {
    if (ll.empty()) {
      ll.emplace(a, b);
      // fprintf(stderr, "(+) %dx%+d: empty set\n", a, b);
      return;
    }

    auto it = ll.lower_bound(a);
    if (it != ll.end() && it->first == a) {
      if (it->second <= b) {
        // fprintf(stderr, "(x) %dx%+d: one with less constant exists\n", a, b);
        return;
      }
      it->second = b;
      // fprintf(stderr, "(+) %dx%+d: update constant\n", a, b);
    } else if (it == ll.begin() || it == ll.end()) {
      it = ll.emplace_hint(it, a, b);
      // fprintf(stderr, "(+) %dx%+d: either ends\n", a, b);
    } else {
      it = ll.emplace_hint(it, a, b);
      if (is_redundant(it)) {
        ll.erase(it);
        // fprintf(stderr, "(x) %dx%+d: redundant\n", a, b);
        return;
      }
      // fprintf(stderr, "(+) %dx%+d: normally\n", a, b);
    }

    auto prev = std::prev(it);
    while (is_redundant(prev)) {
      // fprintf(stderr, "(-) %dx%+d: prev\n", prev->first, prev->second);
      ll.erase(prev);
      prev = std::prev(it);
    }

    auto next = std::next(it);
    while (is_redundant(next)) {
      // fprintf(stderr, "(-) %dx%+d: next\n", next->first, next->second);
      next = ll.erase(next);
    }
  }

  Tp min_at(Tp x) const {
    assert(!ll.empty());
    if (ll.size() == 1) {
      Tp a = ll.begin()->first;
      Tp b = ll.begin()->second;
      return a*x + b;
    }

    Tp lb = ll.begin()->first;
    auto last = std::prev(ll.end());
    Tp ub = last->first;
    while (ub-lb > 1) {
      Tp mid = (lb+ub) / 2;
      auto it1 = ll.upper_bound(mid);
      auto it0 = std::prev(it1);
      Tp a0, b0, a1, b1;
      std::tie(a0, b0) = *it0;
      std::tie(a1, b1) = *it1;
      ((a0*x+b0 < a1*x+b1)? ub:lb) = mid;
    }

    auto it1 = ll.upper_bound(lb);
    auto it0 = std::prev(it1);

    Tp a0, b0, a1, b1;
    std::tie(a0, b0) = *it0;
    std::tie(a1, b1) = *it1;
    return std::min(a0*x+b0, a1*x+b1);
  }

  void inspect() const {
    // for (const auto& p: ll)
    //   fprintf(stderr, "%dx%+d\n", p.first, p.second);
  }
};

int main() {
  size_t N;
  scanf("%zu", &N);

  lines<intmax_t> lmin, lmax;
  for (size_t i = 0; i < N; ++i) {
    intmax_t a, b;
    scanf("%jd %jd", &a, &b);
    lmin.append(b, a);
    lmax.append(-b, -a);
  }

  intmax_t res = 1e18;
  intmax_t x = 0;
  for (intmax_t i = 1; i <= 1000000; ++i) {
    intmax_t y = -lmax.min_at(i) - lmin.min_at(i);
    if (res > y) {
      res = y;
      x = i;
    }
  }
  printf("%jd\n", x);
}
0