题解:P13138 [GCJ 2018 #1A] Edgy Baking

· · 题解

非背包做法。

思路

切一块 \mathbf{W}_{\mathbf{i}} \times \mathbf{H}_{\mathbf{i}} 的饼干后周长增加 \left[2 \min \left\{ \mathbf{W}_{\mathbf{i}}, \mathbf{H}_{\mathbf{i}} \right\}, 2 \sqrt{\mathbf{W}_{\mathbf{i}}^2 + \mathbf{H}_{\mathbf{i}}^2} \right] ,记为 \left[ l_i, r_i \right] ,显然 r_i \ge \sqrt 2 \ l_i ,称一 S \sub \left\{ 1, 2, \dots, \mathbf{N} \right\} 对应的 \left[ \sum\limits_{i \in S} l_i, \sum\limits_{i \in S} r_i \right] 为一可行区间,合并重叠的可行区间,仍满足前面的性质,于是区间只有 \mathcal{O} \left( \log V \right) 个。用 map 维护区间,具体地,键记录左端点,值记录右端点,时复 \mathcal{O} \left( \mathbf{N} \log V \log \log V \right) 。详细看代码。

::::info[Code]

#include <bits/stdc++.h>
#include <bits/extc++.h>

namespace io
{
  char buf[1 << 20], *p1(buf), *p2(buf), c;
#define gc() ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
  template <typename _Tp> inline void read(_Tp &x)
  { x = 0; while (!isdigit(c = gc())) { } while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = gc())) { } }
  template <typename _Tp, typename... Args> inline void read(_Tp &x, Args&... args) { read(x), read(args...); }
} // namespace io

using u32 = unsigned; using f64 = double;

constexpr u32 N{100}; constexpr f64 eps{1e-9};

u32 t, n, S, T; struct pair { u32 a; f64 b; } x[N]; std::map<u32, f64> s;

inline void insert(u32 a, f64 b)
{
  while (true)
  {
    auto p{s.upper_bound(a)}; if (p == s.begin()) { break; } p = std::prev(p);
    if (p -> second + eps < a) { break; } a = p -> first; b = std::max(b, p -> second); s.erase(p);
  }
  while (true) { auto p{s.lower_bound(a)}; if (p == s.end() || b + eps < p -> first) { break; } b = std::max(b, p -> second); s.erase(p); }
  s.emplace(a, b);
}

signed main(void)
{
  io::read(t);
  for (u32 I{1}; I <= t; I++)
  {
    io::read(n, S); T = 0; s.clear(); s.emplace(0, 0);
    for (u32 i{0}, h, w; i < n; i++) { io::read(h, w); T += h + w; if (w < h) { std::swap(h, w); } x[i] = {h << 1, 2 * sqrt(h * h + w * w)}; }
    for (u32 i{0}; i < n; i++) { auto cp{s}; for (auto &p : cp) { insert(p.first + x[i].a, p.second + x[i].b); } }
    T <<= 1; auto p{std::prev(s.upper_bound(S - T))}; printf("Case #%d: %.15lf\n", I, std::min((f64)S, p -> second + T));
  }
  return 0;
}

::::

跑得飞快。