题目描述
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;
}