跳到正文

题解

题解:P10260 [COCI 2023/2024 #5] Rolete

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

文章信息

发布
2025年7月7日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟
获赞
5
题目
P10260

版本

  1. v1当前

下文中记

#{P}=∑i=1n[P]\#\{P\} = \sum _ {i = 1} ^ n [P]

不妨设我们先用机器拉 xx 厘米,那么我们可以把 xx 厘米的拉升看作 xx 次对耗时的增加,则第 jj 次增加的耗时为

s+k×#{ai<j}s + k \times \#\{a _ i < j\}

累加下来 xx 次,一共就是

∑j=1x(s+k×#{ai<j})=s×x+k×∑j=1x#{ai<j}=s×x+k×∑ai<x(x−ai)\begin{aligned} \sum _ {j = 1} ^ x (s + k \times \#\{a _ i < j\}) &= s \times x + k \times \sum _ {j = 1} ^ x \#\{a _ i < j\} \\ &= s \times x + k \times \sum _ {a _ i < x}(x - a _ i) \end{aligned}

然后我们还要再手动把窗帘拉上去,我们显然只需要拉 ai>h+xa _ i > h + x 的那些窗帘,所以一共需要

t×∑ai>h+x(ai−h−x)t \times \sum _ {a _ i > h + x}(a _ i - h - x)

综合下来,我们得到函数

f(x)=s×x+k×∑ai<x(x−ai)+t×∑ai>h+x(ai−h−x)f(x) = s \times x + k \times \sum _ {a _ i < x}(x - a _ i) + t \times \sum _ {a _ i > h + x}(a _ i - h - x)

显然这个函数是凹的,可以三分,但是我不会。考虑到这个 xx 的取值是离散的(因为只能取整数),所以我们不妨设

Δ(x)=f(x+1)−f(x)=s+k×#{ai<x+1}−t×#{ai>h+x}\begin{aligned} \Delta(x) &= f(x + 1) - f(x) \\ &= s + k \times \#\{a _ i < x + 1\} - t \times \#\{a _ i > h + x\}\end{aligned}

然后在 [0,max⁡{ai}−h][0, \max \{ a _ i\} - h] 上二分第一个使得 Δ(x)≥0\Delta(x) \ge 0 的点即可,然后答案就是 f(x)f(x)。这里再推一下 f(x)f(x) 的更好算的式子:

f(x)=s×x+k×∑ai<x(x−ai)+t×∑ai>h+x(ai−h−x)=s×x+k×(#{ai<x}×x−∑ai<xai)+t×[∑ai>h+xai−#{ai>h+x}×(h+x)]\begin{aligned} f(x) &= s \times x + k \times \sum _ {a _ i < x}(x - a _ i) + t \times \sum _ {a _ i > h + x}(a _ i - h - x) \\ &= s \times x + k \times \left(\#\{a _ i < x\} \times x - \sum _ {a _ i < x} a _ i\right) + t \times \left[ \sum _ {a _ i > h + x} a _ i - \#\{a _ i > h + x\} \times (h + x)\right] \end{aligned}

然后你就会发现现在 Δ\Delta 和 ff 中的所有 #{⋯ ⁣ }\#\{\cdots\!\,\} 都可以用二分求出,所有的 ∑\sum 都可以用前缀和求出。

于是每次二分再求 Δ\Delta 的时间复杂度为 O(log⁡nlog⁡V)O(\log n \log V),求 ff 即为 O(log⁡n)O(\log n)。最初的前缀和 + 排序是 O(nlog⁡n)O(n \log n) 的,所以总时间复杂度为 O(nlog⁡n+qlog⁡nlog⁡V)O(n \log n + q \log n \log V)。

C++
#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;
}
原文