「NOIP2011」聪明的质监员

传送门
Luogu

解题思路

第一眼肯定是没什么思路的 dalao勿喷,但我们仔细看一看式子就会发现 \(Y\) 是随着 \(W\) 的变大而变小的。
所以 \(Y\)\(W\) 的变化是单调的,然后就可以考虑二分了,尽可能让 \(Y\) 靠近 \(S\) 即可。
至于计算,只需要开两个前缀和,分别记录 \(\sum 1\)\(\sum v\) ,但是只算 \(w_j \geq mid\) 的位置。

细节注意事项

  • long long

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while (!isdigit(c)) f |= (c == '-'), c = getchar();
	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
	s = f ? -s : s;
}

typedef long long LL;
const int _ = 200002;

int n, m; LL S, _ans;
int w[_], v[_], L[_], R[_];
LL sum[_], num[_];

inline bool check(int mid) {
	for (rg int i = 1; i <= n; ++i) {
		num[i] = num[i - 1] + 1ll * (w[i] >= mid);
		sum[i] = sum[i - 1] + 1ll * (w[i] >= mid) * v[i];
	}
	LL Y = 0;
	for (rg int i = 1; i <= m; ++i)
		Y += 1ll * (sum[R[i]] - sum[L[i] - 1]) * (num[R[i]] - num[L[i] - 1]);
	return _ans = labs(S - Y), Y >= S;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	read(n), read(m), read(S);
	int mx = -1e9, mn = 1e9;
	for (rg int i = 1; i <= n; ++i) read(w[i]), read(v[i]);
	for (rg int i = 1; i <= m; ++i) read(L[i]), read(R[i]);
	int l = 0, r = 1e6; LL ans = 1e18;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (check(mid)) l = mid + 1;
		else r = mid;
		ans = min(ans, _ans);
	}
	printf("%lld\n", ans);
	return 0;
}

完结撒花 \(qwq\)

posted @ 2019-11-05 09:25  Sangber  阅读(104)  评论(0编辑  收藏  举报