题解:P10260 [COCI 2023/2024 #5] Rolete
下文中记
不妨设我们先用机器拉
累加下来
然后我们还要再手动把窗帘拉上去,我们显然只需要拉
综合下来,我们得到函数
显然这个函数是凹的,可以三分,但是我不会。考虑到这个
然后在
然后你就会发现现在
于是每次二分再求
#include<iostream>
#include<algorithm>
typedef long long ll;
typedef unsigned long long ull;
constexpr int N = 1e5 + 1;
int n; ll s, k, t; int q, a[N]; ll S[N];
inline ll D(int x, int h) {
ll c1 = std::lower_bound(a + 1, a + n + 1, x + 1) - a - 1;
ll c2 = n - (std::upper_bound(a + 1, a + n + 1, h + x) - a) + 1;
return s + k * c1 - t * c2;
}
inline ll f(int x, int h) {
ll c1 = std::lower_bound(a + 1, a + n + 1, x + 1) - a - 1;
ll c2 = n - (std::upper_bound(a + 1, a + n + 1, h + x) - a) + 1;
ll s1 = S[c1], s2 = S[n] - S[n - c2];
return s * x + k * (c1 * x - s1) + t * (s2 - c2 * (h + x));
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0), std::cout.tie(0);
std::cin >> n >> t >> s >> k;
for(int i = 1; i <= n; ++i) std::cin >> a[i];
std::sort(a + 1, a + n + 1);
for(int i = 1; i <= n; ++i) S[i] = S[i - 1] + a[i];
std::cin >> q; while(q--) {
int h; std::cin >> h;
int l = 0, r = a[n] - h;
while(l < r) {
int mid = (l + r) >> 1;
D(mid, h) >= 0 ? r = mid : l = mid + 1;
}
std::cout << f(l, h) << " ";
}
return 0;
}