P9755 [CSP-S 2023] 种树 题解

· · 题解

考场上这道题想了半天,最后一分都没骗到。。。下了考场立马想到二分,但是来不及了。。。我太蒻了 qwq 回归正题,此题要求一个最小的日期,使得在这一天 n 棵树的高度都超过 a_i,这时就会自然地想到二分。

1. 二分什么

这个很好想,就是二分结束的日期,使其尽可能小。

2. 如何判断是否满足条件

众所周知,二分时要有一个判断函数 check,以判断当前的 mid 是否满足条件。咱们可以考虑在每次判断时分别计算出 s_i,表示第 i 棵树如果能在 mid 天时的高度超过 a_i ,最晚需要在哪天种下,然后可以容易想到贪心策略:先种 s_i 小的树。大家可以感性地理解一下,就不证明了。最后用 O(n) 的时间判断一下是否能在这种策略下满足条件就可以了。

3. 如何处理出 s_i

这个问题有两种解决方法,一是解一个二次不等式,时间复杂度更优秀,二是再套一个二分,时间复杂度更高一些,但是更好写。这里主要对第二种方法做讲解主要是第一种蒟蒻不会。首先是对二分正确性的证明,因为 max(b_i +x \times c_i, 1) 是一定大于等于 1 的这不是废话吗?,所以树的高度满足单调性,可以二分。而这个二分的判断直接用等差数列求和来做就行了,具体实现见代码部分。

4. 其他

(1) 时间复杂度:外层二分是 O(log10^9),内层判断需要 O(nlogn),正常常数是能过的。

(2) 数据范围:内层二分判断时可能会爆 long long 要开 int128,所以要小心常数太大。

5. 代码实现

code:
#include<bits/stdc++.h>
#define int long long //不能全部用__int128,否则会爆,算的时候转换就行
#define int128 __int128
using namespace std;
struct node {
    int a, b, c, id, t, s;
}a[100005];
bool flag[100005];
int fa[100005], head[100005], tail[200005], nxt[200005], tot, L = 1, R = 1e9, dis[200005];
int n;
int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-')   f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}
void add_edge(int u, int v) {
    tail[++tot] = v;
    nxt[tot] = head[u];
    head[u] = tot;
    tail[++tot] = u;
    nxt[tot] = head[v];
    head[v] = tot;
    return;
}
void dfs(int x, int f) {
    fa[x] = f;
    dis[x] = dis[f] + 1;
    for(int i = head[x]; i ; i = nxt[i]) {
        int ed = tail[i];
        if(ed == f) continue;
        dfs(ed, x);
    }
    return;
}
int calc(int i, int x) {
    return max(a[i].c + a[i].b * x, 1ll);
}
bool Check(int i, int s, int D) { //分类讨论判断内层二分
    if(a[i].b < 0) {
        if(a[i].t > D) {
            int128 tmp = (int128)(calc(i, s) + calc(i, D)) * (D - s + 1) / 2;
            return tmp >= a[i].a;
        }
        else if(a[i].t <= s) {
            return (D - s + 1) >= a[i].a;
        }
        else {
            int128 tmp = (int128)(calc(i, s) + calc(i, a[i].t - 1)) * (a[i].t - s) / 2;
            return tmp + (D - a[i].t + 1) >= a[i].a;
        }
    }
    else if(a[i].b > 0){
        if(a[i].t <= s) {
            int128 tmp = (int128)(calc(i, s) + calc(i, D)) * (D - s + 1) / 2;
            return tmp >= a[i].a;
        }
        else if(a[i].t > s) {
            return (D - s + 1) >= a[i].a;
        }
        else {
            int128 tmp = (int128)(calc(i, a[i].t) + calc(i, D)) * (D - a[i].t + 1) / 2;
            return tmp + (a[i].t - s) >= a[i].a;
        }
    }
    else {
        int tmp = max(a[i].c, 1ll);
        return tmp * (D - s + 1) >= a[i].a;
    }
}
bool cmp(node x, node y) {
    if(x.s != y.s)  return x.s < y.s;
    return dis[x.id] > dis[y.id];
}
bool check(int D) {
    for(int i = 1; i <= n; ++i) {
        int l = 1, r = D;
        while(l <= r) {//内层二分
            int mid = l + r >> 1;
            if(Check(i, mid, D)) {
                l = mid + 1;
            }
            else {
                r = mid - 1;
            }
        }
        if(r == 0)  return false;
        a[i].s = r;
    }
    sort(a + 1, a + n + 1, cmp);
    memset(flag, 0, sizeof flag);
    flag[0] = true;
    int cnt = 0;//天数
    for(int i = 1; i <= n; ++i) {//判断是否能实现
        if(!flag[a[i].id]) {
            int now = a[i].id;
            while(!flag[now]) {
                flag[now] = true;
                now = fa[now];
                cnt++;
            }
        }
        if(cnt > a[i].s)    return false;
    }
    return true;
}
signed main() {
    n = read();
    for(int i = 1; i <= n; ++i) {
        a[i].a = read(); a[i].c = read(); a[i].b = read();
        int b = a[i].b, c = a[i].c;//a[i].t表示b[i]*x+c[i]的值的小于1和大于等于1的交界点
        if(a[i].b < 0) {
            a[i].t = (1 - c) / b + 1;
        }
        else if(a[i].b > 0) {
            a[i].t = ceil((1 - c) / (double)b) - 1;
        }
        a[i].id = i;
    }
    for(int i = 1; i < n; ++i) {
        int x, y;
        x = read(); y = read();
        add_edge(x, y); 
    }
    dfs(1, 0);
    L = n;
    while(L <= R) {//外层二分
        int mid = L + R >> 1;
        if(check(mid)) {
            R = mid - 1;
        }
        else {
            L = mid + 1;
        }
    }
    cout<<L;
    return 0;
}