zoukankan      html  css  js  c++  java
  • 叠罗汉

    传送门
    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)

  • 相关阅读:
    剑指offer二十九---最小的k个数
    Select2插件 点击、选中事件 解读
    Datatable插件的简单的使用方式 和 学习方式
    java map获取值方式
    mysql delete语句使用别名报错
    springmvc 添加@ResponseBody
    maven 创建后报错
    nodejs
    gulp
    Nodejs-express 4.0框架 简单介绍
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/11751257.html
Copyright © 2011-2022 走看看