题解:P13138 [GCJ 2018 #1A] Edgy Baking
非背包做法。
思路
切一块 map 维护区间,具体地,键记录左端点,值记录右端点,时复
::::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;
}
::::
跑得飞快。