跳到正文

题解

题解:P13717 [GCPC 2024] Bookshelf Bottleneck

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P13717

版本

  1. v1当前

这是一道运用了贪心算法的题目。

题目大意
  1. 给定一个盒子,要我们放入 nn 本书,盒子限定高度为 HH,
  2. 每本书可的长宽高分别为 ll,ww 和 hh 的长方体,
  3. 让我们求出最小的宽度值。

因为不能超过限定数值 HH,所以当 ll,ww 和 HH 均大于 HH 的时候输出 "impossible\texttt{impossible}"。因此我们可以推出以下代码:

C++
if(l > H && w > H && h > H)
{
    cout << "impossible";
    return 0;
}

在开始的时候,我们先定义一个 min_totalmin\_total 和 xx 来分别存储最小值和标记书籍是否已经找到一种可行的摆放方式,然后再比较可行范围内的最小值,之后用 ansans 加起来,最后输出。

最后为本蒟蒻的代码:

C++
#include<bits/stdc++.h>

using namespace std;

#define ll long long

ll n, H;

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> H;
	ll min_total = 0, ans = 0;
	for(int i = 1; i <= n; i++)
	{
		ll l, w, h;
		cin >> l >> w >> h;
		if(l > H && w > H && h > H)
		{
			cout << "impossible";
			return 0;
		}
		bool x = false;
		if(l <= H)
		{
			min_total = min(h, w);
			x = true;
		}
		if(w <= H)
		{
			ll tmp = min(l, h);
			if(x == false || tmp < min_total)
			{
				min_total = tmp;
				x = true;
			}
		}
		if(h <= H)
		{
			ll tmp = min(l, w);
			if(x == false || tmp < min_total)
			{
				min_total = tmp;
				x = true;
			}
		}
		ans += min_total;
	}
	cout << ans;
	return 0;
}
原文