我们考虑维护 3 个变量:
cnt: 当前睡了多少时间,初始值为 0.
ans: 当前的天数,初始值为 1.
lft:当前还能睡多少时间,初始值为 x。
每件任务的时间为 t。
以下是我的代码。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void fastio() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int t[100010];
int main() {
fastio();
int n, x, p, q;
cin >> n >> x >> p >> q;
ll cnt = 0;
int lft = x;
ll ans = 1;
while (n--) {
int t;
cin >> t;
if (lft > t && (cnt + lft - t) * q >= ans * x * p) {
lft -= t;
} else {
int y = (q * (cnt + lft + x - t) - x * p * (ans + 1)) / (x * (p - q));
cnt += lft;
cnt += x * y;
lft = x - t;
ans += y + 1;
// cout << "t = " << t << endl;
// cout << "ans = " << ans << endl;
// cout << "lft = " << lft << endl;
// cout << "cnt = " << cnt << endl;
}
}
cout << ans << endl;
return 0;
}
那个 y 的算法本来是一个不等式,即:
cnt+lft+x×y+x−t≥(ans+y+1)∗x∗p/q
后来你可以自行化简,最后结果就是 x∗(p−q)q∗(cnt+lft+x−t)−x∗p∗(ans+1)。我确定我没有算错。
但是莫名奇妙的在第二个样例上输出 2?我发现在 t = 1 的时候 y 会变成一个负数,但是我不知道怎么改成正确的。