跳到正文

题解

题解:AT_abc374_e [ABC374E] Sensor Optimization Dilemma 2

RainPPR约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月28日
篇幅
约 5 分钟
获赞
3

版本

  1. v1当前

思路来自こたつがめ(kotatsugame)大佬的视频。


首先,容易想到我们可以二分答案 WW(最大生产力),对于每一个工程令生产力总和 ≥W\ge W,然后计算出来所需花费,判断是否 ≤X\le X 即可。

对于一个工程 ⟨A,P,B,Q⟩\langle A,P,B,Q\rangle,我们需要找到两个机器的生产力 x,y≥0x,y\ge0 满足 Ax+By≥WAx+By\ge W 的条件下,最小化 Px+QyPx+Qy 的值。

Ax+By≥W⟹y≥⌈W−AxB⌉⟹y=max⁡(0,⌈W−AxB⌉)\begin{aligned} &Ax+By\ge W\\ \Longrightarrow\quad&y\ge\left\lceil\dfrac{W-Ax}{B}\right\rceil\\ \Longrightarrow\quad&y=\max\left(0,\left\lceil\dfrac{W-Ax}{B}\right\rceil\right) \end{aligned}

不妨使用带余除法分解 xx:

x=x1B+x0(0≤x0<B)x=x_1B+x_0\quad(0\le x_0<B)

那么,使用顶函数的性质:

y=max⁡(0,⌈W−AxB⌉)=max⁡(0,⌈W−Ax0B−Ax1⌉)=max⁡(0,⌈W−Ax0B⌉−Ax1)\begin{aligned} y&=\max\left(0,\left\lceil\dfrac{W-Ax}{B}\right\rceil\right)\\ &=\max\left(0,\left\lceil\dfrac{W-Ax_0}{B}-Ax_1\right\rceil\right)\\ &=\max\left(0,\left\lceil\dfrac{W-Ax_0}{B}\right\rceil-Ax_1\right)\\ \end{aligned}

不妨记:

w=⌈W−Ax0B⌉w=\left\lceil\dfrac{W-Ax_0}{B}\right\rceil

则:

y=max⁡(0,w−Ax1)y=\max\left(0,w-Ax_1\right)

我们考虑直接枚举 x0x_0 为常数并钦定后面的式子 ≥0\ge0,

w−Ax1≥0  ⟺  x1≤⌊wA⌋w-Ax_1\ge0 \iff x_1\le\left\lfloor\dfrac{w}{A}\right\rfloor

此时:

Px+Qy=Px0+Qw+PBx1−QAx1=Px0+Qw+(PB−QA)x1\begin{aligned} Px+Qy&=Px_0+Qw+PBx_1-QAx_1\\ &=Px_0+Qw+(PB-QA)x_1 \end{aligned}

是关于 x1x_1 的一次函数,不妨两个端点都直接记入贡献。


然后考虑后面的式子 ≤0\le0 即:

x1>⌊wA⌋=⌊wA⌋+1x_1>\left\lfloor\dfrac{w}{A}\right\rfloor=\left\lfloor\dfrac{w}{A}\right\rfloor+1

那么:

Px+Qy=P(x0+Bx1)Px+Qy=P(x_0+Bx_1)

代码:

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

using namespace std;

#define endl "\n"

using ll = long long;

#define int ll

int n, x;

struct pt {
    int a, p;
    int b, q;
};

vector<pt> process;

bool check(int W) {
    int rest = x;
    for (auto [a, p, b, q] : process) {
        int cost = rest + 1;
        for (int x0 = 0; x0 < b; ++x0) {
            int w = (max(0ll, W - a * x0) + b - 1) / b;
            int base = p * x0 + q * w;
            int add = p * b - q * a;
            int lx = 0, rx = w / a;
            cost = min({
                cost,
                p * (x0 + b * (rx + 1)),
                base + add * lx,
                base + add * rx
            });
        }
        if ((rest -= cost) < 0)
            return false;
    }
    return true;
}

void Main() {
    cin >> n >> x;
    process.resize(n);
    for (auto &[a, p, b, q] : process)
        cin >> a >> p >> b >> q;
    int l = 0, r = 1e9;
    int ans = -1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check(mid))
            ans = mid, l = mid + 1;
        else
            r = mid - 1;
    }
    cout << ans << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    return Main(), 0;
}
原文
存档评论1 条抓取于