AT2565 Chocolate Bar 洛谷

题目描述
There is a bar of chocolate with a height of H blocks and a width of W blocks. Snuke is dividing this bar into exactly three pieces. He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.
Snuke is trying to divide the bar as evenly as possible. More specifically, he is trying to minimize Smax - Smin, where Smax is the area (the number of blocks contained) of the largest piece, and Smin is the area of the smallest piece. Find the minimum possible value of Smax−Smin.

Constraints
2≤H,W≤105
输入
Input is given from Standard Input in the following format:
H W
输出
Print the minimum possible value of Smax−Smin.
样例输入 Copy
3 5
样例输出 Copy
0

题意翻译
给定一个大小为 H \times WH×W 的矩形,由 H \times WH×W 个小矩形组成。

你需要将这个大矩形切成三个部分,要求只能沿着小矩形的边切且且出来的三个部分必须均为矩形。

请最小化切出来的三个矩形中最大矩形与最小矩形的差,并输出这个差值。

暴力解法,一共就两种情况,一条边上平行两刀和一边一刀,讨论即可

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long h,w,i;//h长,w宽
    long long cut1,cut2,s1,s2,s3,max1,min1;
    cin>>h>>w;;
    long long ans=10000000000;
    //case1 长上平行切两刀
    for(i=1;i<h;i++)
    {
        cut1=i;
        cut2=(h-i)/2;
        s1=cut1*w;
        s2=cut2*w;
        s3=(h-i-cut2)*w;
        max1=max(s1,max(s2,s3));
        min1=min(s1,min(s2,s3));
        ans=min(ans,max1-min1);

    }
    //case2 宽上平行切两刀
    for(i=1;i<w;i++)
    {
        cut1=i;
        cut2=(w-i)/2;
        s1=cut1*h;
        s2=cut2*h;
        s3=(w-cut1-cut2)*h;
        max1=max(s1,max(s2,s3));
        min1=min(s1,min(s2,s3));
        ans=min(ans,max1-min1);

    }
    //case3长一刀,宽两刀
    for(i=1;i<h;i++)
    {
        cut1=i;
        cut2=w/2;
        s1=cut1*w;
        s2=(h-i)*cut2;
        s3=(h-i)*(w-cut2);
        max1=max(s1,max(s2,s3));
        min1=min(s1,min(s2,s3));
        ans=min(ans,max1-min1);

    }
    //case4宽一刀,长两刀
    for(i=1;i<w;i++)
    {
        cut1=i;
        cut2=h/2;
        s1=cut1*h;
        s2=cut2*(w-i);
        s3=(h-cut2)*(w-i);
        max1=max(s1,max(s2,s3));
        min1=min(s1,min(s2,s3));
        ans=min(ans,max1-min1);

    }
    cout<<ans;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值