From 96ccc02052c9b950b749c406388b850051f769f5 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 13 Sep 2022 16:26:05 +0800 Subject: [PATCH] =?UTF-8?q?P4322=20[JSOI2016]=E6=9C=80=E4=BD=B3=E5=9B=A2?= =?UTF-8?q?=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/86535205 --- Luogu/P4322/P4322.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Luogu/P4322/P4322.cpp diff --git a/Luogu/P4322/P4322.cpp b/Luogu/P4322/P4322.cpp new file mode 100644 index 00000000..3804d53c --- /dev/null +++ b/Luogu/P4322/P4322.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2505; +const double eps = 1e-4; + +int k, n; +std::array siz; +std::array d; +std::array, N> f; +std::array, N> g; +std::array, N> a; + +void dfs(int u) { + f[u][1] = d[u]; + siz[u] = 1; + + for (int v : g[u]) { + dfs(v); + + siz[u] += siz[v]; + + for (int i = std::min(siz[u], k + 1); i; i--) { + for (int j = 0; j <= std::min(siz[v], i - 1); j++) { + f[u][i] = std::max(f[u][i], f[u][i - j] + f[v][j]); + } + } + } +} + +bool check(double mid) { + for (int i = 1; i <= n; i++) { + d[i] = static_cast(a[i].second) - mid * a[i].first; + } + + std::for_each(f.begin(), f.end(), [&](auto &x) { + std::fill(x.begin(), x.end(), std::numeric_limits::min()); + }); + + dfs(0); + + return f[0][k + 1] >= 0; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> k >> n; + + for (int i = 1, r; i <= n; i++) { + cin >> a[i].first >> a[i].second >> r; + + g[r].emplace_back(i); + } + + double l = 0, + r = 10000; + + while (r - l > eps) { + double mid = (l + r) / 2; + + if (check(mid)) l = mid; + else r = mid; + } + + cout << std::fixed << std::setprecision(3) << l << endl; + + return 0; +}