codeforces 589B(两次排序 + 暴力 ) (重点在于思路)

本文解决了一个关于如何切割堆叠的矩形蛋糕以获得最大体积的问题。通过确保每个矩形的宽度大于长度,并采用两次排序和暴力计算的方法找到了最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/589/B

题意:题意有点说不清楚,大致意思是从上到下叠放了很多矩形蛋糕,求怎么切长度和宽度才能使切后的蛋糕体积最大。每块蛋糕的长宽可以颠换。

思路:既然每个举行的长宽可以颠换,那就使宽>长,这些矩形肯定是长边与长边对齐,短边与短边在一起时切才会保留最多的蛋糕,使切后蛋糕体积最大。第一次根据宽排序,排好序后遍历每一个宽w,将宽度大于等于w的宽所对应的高再次进行排序,对于每一个高h求出所有蛋糕在宽为w高为h时的总体积取最大值即可。总体思路是两次排序加暴力。看了学长学姐们的代码才看懂,不得不说,我 还差得远呢。未来,我会继续努力的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 4000 + 10;
const int maxt = 1e6 + 10;
const double eps = 0.00000000000001;
const int inf = 0x3f3f3f3f;
struct reg
{
    int w,h;
    bool operator<(const reg& r) const
    {
        return w < r.w;
    }
}r[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&r[i].w, &r[i].h);
            if(r[i].w > r[i].h) swap(r[i].w, r[i].h);//保证每个矩形w > h
        }
        sort(r ,r + n);
        vector<int> hh;
        ll ans = 0, mxw, mxh;
        for(int i = 0; i < n; i++)//枚举所有的宽w
        {
            hh.clear();
            for(int j = i; j < n; j++)//将第i至n个矩形的长储存在集合中
                hh.push_back(r[j].h);
            sort(hh.begin(), hh.end());//将长排序
            int len = hh.size();
            for(int j = 0; j < hh.size(); ++j,--len)
            {
                ll tmp = (ll)r[i].w * hh[j] * len;//暴力所有宽,高对应的体积求最大值
                if(tmp > ans)
                {
                    ans = tmp;
                    mxw = r[i].w;
                    mxh = hh[j];
                }
            }
        }
        printf("%I64d\n%I64d %I64d\n",ans, mxw, mxh);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值