c语言叠罗汉问题解决思路,叠罗汉(示例代码)

解题思路

考虑交换两个相邻罗汉答案会如何变化。

假设我们应该这样摆:(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})

那么我们就按照这样排序,然后能叠就叠即可。

细节注意事项

注意一些地方的 (

参考代码

#include

#include

#include

#include

#include

#include

#include

#include

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值