CF1498-B.Box Fitting

CF1498-B.Box Fitting

题意

给出 n n n个长条,每个长条保证可以表示为 2 x 2^x 2x的形式,问你如果一个宽度为 w w w的盒子最少要多高才能装下这些长条。

思路

贪心。将长条按照长度从大到小排序,对于每一层我们尽量将它装满再装下一层。

可以用 m u l t i s e t multiset multiset维护每一层剩余的空间。对于当前要放入盒子的长条,在集合中 l o w e r _ b o u n d lower\_bound lower_bound得到一个合适的位置,如果没有那就新开一层,答案加一;如果有就将该层的空间减少,还有剩余就再次加入到集合中。

代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <stack>
#include <cstdlib>
#include <map>
#include <queue>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <set>
#include <ctime>
 
#define rep(i, s, t) for (int i = (s); i < (t); i++)
#define wa std::cerr << "----WARN----" << std::endl;
#define wl std::cerr << "********" << std::endl;
#define wr std::cerr << "~~~~~~~~" << std::endl;
#define PII pair<int, int>
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
 
typedef long long LL;
 
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const LL LINF = 1LL * 0x3f3f3f3f3f3f3f3f;
 
inline int read();
 
const int N = 100005;
 
int a[N];
 
void solve() {
    int n, w;
    std::cin >> n >> w;
    rep (i, 0, n) {
        std::cin >> a[i];
    }
    std::sort(a, a + n, std::greater<int>());
    std::multiset<int> s;
    int ans = 0;
    rep (i, 0, n) {
        if (s.empty()) {
            if (a[i] != w) s.insert(w - a[i]);
            ans++;
        } else {
            auto it = s.lower_bound(a[i]);
            if (it == s.end()) {
                if (w != a[i]) s.insert(w - a[i]);
                ans++;
                continue;
            } else {
                int t = *it;
                s.erase(it);
                if (t != a[i]) s.insert(t - a[i]);
            }
        }
    }
    std::cout << ans << std::endl;
}
 
inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    return s * w;
}
 
signed main() {
    // freopen("in.txt", "r", stdin);
    int T = 1;
    T = read();
    while (T--) solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值