跳到正文

题解

题解:P13717 [GCPC 2024] Bookshelf Bottleneck

__CuSO4__约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月18日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
2
题目
P13717

版本

  1. v1当前

题意简述

给你 nn 个立方体,具有三个边长 ll、ww 和 hh,可以任意旋转每个立方体的三维方向。只有当立方体的高不超过 HH 时,才能将立方体竖直放置,求摆放所有立方体所需的宽度和是多少?

思路

对于每个立方体,我们要在能放的情况下使得:

  • 把尽可能大的一条边当作高。
  • 把尽可能小的一条边当作宽。

根据这个贪心策略,用几个判断就能过了。

注意

要开 long long,109×109=101810^9\times 10^9=10^{18},会爆 int。

代码

C++
#include <iostream>
#define int long long //注意
using namespace std;

int n, h, ans;

signed main() {
    cin >> n >> h;
    while (n--) {
        int x, y, z;
        cin >> x >> y >> z;
        //相当于排序
        int a = min(x,min(y,z)), c = max(x,max(y,z)), b = x+y+z-a-c;
        //根据贪心策略进行选择
        if (c <= h) ans += a;
        else if (b <= h) ans += a;
        else if (a <= h) ans += b;
        else { //如果三条边都放不了,说明不合法
            puts("impossible");
            return 0;
        }
    }
    cout << ans;
    return 0;
}
原文