叠罗汉

传送门
Luogu团队题链接

解题思路

考虑交换两个相邻罗汉答案会如何变化。
假设我们应该这样摆:(cdots i, i+1, cdots)
其中 (i) 号罗汉在 (i+1) 号上面,那么此时应满足:(设(s=sum_{k=1}^{i-1}a_k)

[s + a_i < b_{i+1} ]

要是换了之后不符合要求,则会有:

[s+a_{i+1}>b_i ]

经过一些变换得到:(a_i+b_i < a_{i+1} + b_{i + 1})
那么我们就按照这样排序,然后能叠就叠即可。

细节注意事项

  • 注意一些地方的 (<)(le)

参考代码

#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;
}

const int _ = 50010;

int n; struct node{ int a, b; }t[_];
inline bool cmp(const node& x, const node& y)
{ return x.a + x.b < y.a + y.b; }

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	read(n);
	for (rg int i = 1; i <= n; ++i)
		read(t[i].a), read(t[i].b);
	sort(t + 1, t + n + 1, cmp);
	int s = 0, ans = 0;
	for (rg int i = 1; i <= n; ++i)
		if (s <= t[i].b) ++ans, s += t[i].a;
	printf("%d
", ans);
	return 0;
}

完结撒花 (qwq)

原文地址:https://www.cnblogs.com/zsbzsb/p/11751257.html